def venv_package_status_site(venv: Venv, site: Path, req_spec: str) -> Do: ws = yield IO.delay(pkg_resources.WorkingSet, [site]) req = yield IO.delay(pkg_resources.Requirement, req_spec) return Maybe.check(ws.by_key.get(req.key)).cata_strict( lambda a: VenvPackageExistent(venv, a), VenvPackageAbsent(venv), )
def find_interpreter(spec: str) -> Do: path = Path(spec) exists = yield IO.delay(path.exists) venv = env.get('VIRTUAL_ENV').get_or_strict('[no venv]') yield ( IO.pure(path) if exists else virtualenv_interpreter(venv, spec) )
def build_venv(global_interpreter: Maybe[str], dir: Path, rplugin_interpreter: Maybe[str], name: str) -> Do: interpreter = yield python_interpreter(global_interpreter, rplugin_interpreter) retval, out, err = yield Subprocess.popen(str(interpreter), '-m', 'venv', str(dir), '--upgrade', timeout=30) success = retval == 0 yield ( IO.pure(cons_venv(dir, name)) if success else IO.failed(f'creating venv for `{name}`: {err.join_lines}') )
def purge(self, cmd): days = cmd.args.head | 30 self.log.info('Deleting releases older than {} days'.format(days)) data = Map(self.client.put('release/purge', dict(days=days))) return IO.pure( purge_msg.format(data['monitors'], data['links'], data['releases']) )
def _lookup(self, data): def check(resp): return (IO.failed('no release found') if resp == -1 else IO.pure( (resp, self.rest))) return IO.delay(self.client.get, '{}_id'.format(self._type), body=data) // check
def help(self, cmd): if self.doc: maxlen = len(max(self.doc.keys(), key=len)) pad = lambda s: s.ljust(maxlen) def output(): yield 'Available commands:' data = List.wrap(self.doc.items()).sort_by(_[0]) for name, (param_desc, desc) in data: yield '' yield '{} {}'.format(pad(name), param_desc) yield ' {}'.format(desc) return IO.delay(output) / '\n'.join else: return IO.failed('no doc for client')
def check_venv(base_dir: Path, plugin: Rplugin) -> Do: dir = base_dir / plugin.name pip = dir / 'bin' / 'pip' exists = yield IO.delay(pip.exists) return ( venv_present(dir, plugin) if exists else VenvAbsent(plugin) )
def list_shows(self, cmd): regex = cmd.args.head | '' def output(matches): if matches: for id, description in matches: i = ColorString('#{}'.format(id), term.green) d = ColorString(description, term.yellow) yield '{}: {}'.format(i, d) else: yield 'No matching show found.' return (IO.delay(self.client.get, 'show', body=dict(regex=regex)) / output / List.wrap)
def search(monitor) -> None: r = monitor['release'] date = Try(datetime.strptime, r['airdate'], '%Y-%m-%d') | None release = Release(name=r['series'], season=r['season'], episode=r['episode'], search_name=r['search_name'], airdate=date) query = cmd.rest.filter_not(_.empty) / _.join_tokens | release.search_query_no_res query1 = release.search_query_date if query == 'date' else query self.log.info(f'Searching torrent for "{release}" with query "{query1}"...') dl = TorrentDownloader([query1]) res = Boolean(dl._search()).m(dl.search_results) / Lists.wrap // _.head return ( res / (lambda l: cmd.put(data=(lambda i: Just(dict(url=l))), path=Just('link'))) | IO.failed('no search results') )
def hackage( self, a: HsHackageRplugin ) -> NS[CrmRibosome, GatherItem[Tuple[str, bool]]]: exe = cabal_rplugin_executable(self.rplugin) return NS.pure(GatherIO(IO.pure((self.rplugin.name, exe.is_file()))))
def check_error(response): if isinstance(response, dict) and 'error' in response: return IO.failed(response['error']) else: return IO.pure(response)
def data(self): return (self.spec // __.lmap(lambda a: IO.pure( (a, self.args.tail | List()))).left_or_map(self._lookup))
def io(i, body): return IO.delay(meth(self.client), '{}/{}{}'.format(self._type, i, sub), body=body)
def create_episode(self, cmd): series, season, episode = cmd.args data = dict(episode=episode) path = 'series/{}/seasons/{}/episodes'.format(series, season) return IO.delay(self.client.post, path, body=data)
def which(name: str) -> Do: exe = yield IO.delay(shutil.which, name) return Path(exe)
def pane_dir(pane: P) -> Do: ui_pane = yield TmuxIO.from_either(UiPane.e_for(pane)) yield (ui_pane.cwd(pane) / TmuxIO.pure).get_or(lambda: TmuxIO.from_io(IO.delay(Path.cwd)))
def venv_absent(self, status: VenvAbsent) -> IO[Boolean]: return IO.pure(false)
def venv_site(venv: Venv) -> Do: lib_dir = venv.meta.dir / 'lib' libs = yield IO.delay(lib_dir.glob, 'python3.?') lib = Lists.wrap(libs).head.to_either(f'no python dirs in {lib_dir}') return lib.map(lambda a: a / 'site-packages')
def codegen(task: CodegenTask) -> Do: template_path = yield IO.delay(task.template.absolute) template_code = yield IO.delay(template_path.read_text) replaced = task.subs.fold_left(template_code)( lambda z, a: re.sub(a[0], a[1], z)) return task.imports.cat(replaced).join_lines
def virtualenv_interpreter(venv: str, executable: str) -> Do: path_s = yield IO.from_either(env.get('PATH')) path = Lists.split(path_s, ':') clean_path = path.filter_not(lambda a: a.startswith(venv)) candidate = yield IO.delay(shutil.which, executable, path=clean_path.mk_string(':')) return Path(candidate)
def create_dir(dir: Path) -> IO[None]: return IO.delay(dir.mkdir, parents=True, exist_ok=True)
def remove_dir(dir: Path) -> Do: exists = yield IO.delay(dir.exists) yield IO.delay(shutil.rmtree, dir) if exists else IO.pure(None)
def dir(self, a: HsStackDirRplugin ) -> NS[CrmRibosome, GatherItem[Tuple[str, bool]]]: return NS.pure(GatherIO(IO.pure((self.rplugin.name, False))))
def clear_cache() -> Do: yield IO.delay(shutil.rmtree, str(venvs_path), ignore_errors=True) yield create_dir(venvs_path)
def to_io(fa: 'TmuxIO[A]', tmux: Tmux) -> Do: result = yield IO.delay(fa.run, tmux) yield IO.from_either(result.to_either)
def check(resp): return (IO.failed('no release found') if resp == -1 else IO.pure( (resp, self.rest)))
def subfps(self, cmd): series, season, episode, subfps = cmd.args data = dict(subfps=subfps) path = 'series/{}/seasons/{}/episodes/{}'.format( series, season, episode) return IO.delay(self.client.put, path, body=data)
def mkdir(path: Path) -> IO[None]: return IO.delay(path.mkdir, parents=True, exist_ok=True)
def codegen_write(task: CodegenTask, outpath: Path) -> Do: code = yield codegen(task) yield IO.delay(outpath.write_text, code) return code
def venv_package_status(venv: Venv, req: str) -> Do: site_e = yield venv_site(venv) yield site_e.cata(lambda a: IO.pure(VenvPackageAbsent(venv)), lambda a: venv_package_status_site(venv, a, req))