Python delegator.run() Examples
The following are 29
code examples of delegator.run().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
delegator
, or try the search function
.
Example #1
Source File: convert_to_rst.py From deepchem with MIT License | 6 votes |
def convert_to_rst(fname): cmd = "jupyter-nbconvert --to rst %s" % fname c = delegator.run(cmd) base_name = os.path.splitext(fname)[0] image_files = "%s_files" % base_name new_path = os.path.join(target_dir, image_files) if os.path.isdir(new_path): shutil.rmtree(new_path) if os.path.isdir(image_files): shutil.move(image_files, target_dir) rst_file = '%s.rst' % base_name new_path = os.path.join(target_dir, rst_file) if os.path.isfile(new_path): os.remove(new_path) shutil.move(rst_file, target_dir)
Example #2
Source File: core.py From pipenv with MIT License | 6 votes |
def get_downloads_info(names_map, section): from .vendor.requirementslib.models.requirements import Requirement info = [] p = project.parsed_pipfile for fname in os.listdir(project.download_location): # Get name from filename mapping. name = Requirement.from_line(names_map[fname]).name # Get the version info from the filenames. version = parse_download_fname(fname, name) # Get the hash of each file. cmd = '{0} hash "{1}"'.format( escape_grouped_arguments(which_pip()), os.sep.join([project.download_location, fname]), ) c = delegator.run(cmd) hash = c.out.split("--hash=")[1].strip() # Verify we're adding the correct version from Pipfile # and not one from a dependency. specified_version = p[section].get(name, "") if is_required_version(version, specified_version): info.append(dict(name=name, version=version, hash=hash)) return info
Example #3
Source File: core.py From pipenv with MIT License | 6 votes |
def pip_download(package_name): cache_dir = vistir.compat.Path(PIPENV_CACHE_DIR) pip_config = { "PIP_CACHE_DIR": vistir.misc.fs_str(cache_dir.as_posix()), "PIP_WHEEL_DIR": vistir.misc.fs_str(cache_dir.joinpath("wheels").as_posix()), "PIP_DESTINATION_DIR": vistir.misc.fs_str( cache_dir.joinpath("pkgs").as_posix() ), } for source in project.sources: cmd = '{0} download "{1}" -i {2} -d {3}'.format( escape_grouped_arguments(which_pip()), package_name, source["url"], project.download_location, ) c = delegator.run(cmd, env=pip_config) if c.return_code == 0: break return c
Example #4
Source File: bash.py From bash.py with Apache License 2.0 | 6 votes |
def __init__(self, args, parent: "Bash", blocking: bool = True) -> None: """constructor""" # Environ inherents from parent. # Remember passed-in arguments. self.parent = parent self.args = args # Run the subprocess. args = " ".join(args) self.start_time = time.time() self.sub = delegator.run( f"{self.parent.path} {args}", env=self.parent.environ, block=blocking ) if blocking: self.elapsed_time = time.time() - self.start_time
Example #5
Source File: test_general.py From dump-env with MIT License | 5 votes |
def test_simple_usage_file_output(monkeypatch, tmpdir): """Check that CLI puts prefixed variables into file correctly.""" monkeypatch.setenv('SOM_TT_VALUE', '1') filename = tmpdir.mkdir('tests').join('.env').strpath delegator.run('dump-env -p SOM_TT_ > {0}'.format(filename)) with open(filename) as env_file: assert env_file.read() == 'VALUE=1\n'
Example #6
Source File: test_general.py From dump-env with MIT License | 5 votes |
def test_multiple_prefixes(monkeypatch): """ Check that CLI with multiple prefixes. CLI must show all prefixed variables correctly. """ monkeypatch.setenv('SOM_TT_VALUE', '1') monkeypatch.setenv('ANOTHER_TT_VALUE', '2') variables = delegator.run('dump-env -p SOM_TT_ -p ANOTHER_TT_') assert variables.out == 'VALUE=2\n'
Example #7
Source File: test_general.py From dump-env with MIT License | 5 votes |
def test_both_options(monkeypatch, env_file): """ Check with template and prefix. CLI must show all prefixed variables by template. """ monkeypatch.setenv('SOM_TT_VALUE', '1') variables = delegator.run('dump-env -p SOM_TT_ -t {0}'.format(env_file)) assert variables.out == 'NORMAL_KEY=SOMEVALUE\nVALUE=1\n'
Example #8
Source File: test_help.py From dump-env with MIT License | 5 votes |
def test_help_option(): """Check that cli shows help.""" variables = delegator.run('dump-env --help') assert 'show this help message and exit' in variables.out assert '--template TEMPLATE' in variables.out assert '--prefix PREFIX' in variables.out assert '--strict' in variables.out assert variables.subprocess.returncode == 0
Example #9
Source File: test_strict.py From dump-env with MIT License | 5 votes |
def test_strict_missing_vars2(monkeypatch): """Check that cli raises errors for missing strict keys.""" variables = delegator.run( 'dump-env -p SOM_TT_ --strict=SOM_TT_KEY --strict=SOM_TT_VALUE', ) assert variables.out == '' assert 'SOM_TT_VALUE' in variables.err assert 'SOM_TT_KEY' in variables.err assert variables.subprocess.returncode == 1
Example #10
Source File: test_strict.py From dump-env with MIT License | 5 votes |
def test_strict_missing_vars1(monkeypatch): """Check that cli raises errors for missing strict keys.""" variables = delegator.run('dump-env -p SOM_TT_ --strict=SOM_TT_KEY') assert variables.out == '' assert variables.err == 'Missing env vars: SOM_TT_KEY\n' assert variables.subprocess.returncode == 1
Example #11
Source File: test_strict.py From dump-env with MIT License | 5 votes |
def test_strict_vars2(monkeypatch): """Check that cli works correctly with strict vars.""" monkeypatch.setenv('SOM_TT_VALUE', '1') monkeypatch.setenv('SOM_TT_KEY', '2') variables = delegator.run( 'dump-env -p SOM_TT_ --strict=SOM_TT_KEY --strict=SOM_TT_VALUE', ) assert variables.out == 'KEY=2\nVALUE=1\n' assert variables.subprocess.returncode == 0
Example #12
Source File: test_strict.py From dump-env with MIT License | 5 votes |
def test_strict_vars1(monkeypatch): """Check that cli works correctly with strict vars.""" monkeypatch.setenv('SOM_TT_VALUE', '1') monkeypatch.setenv('SOM_TT_KEY', '2') variables = delegator.run('dump-env -p SOM_TT_ --strict=SOM_TT_KEY') assert variables.out == 'KEY=2\nVALUE=1\n' assert variables.subprocess.returncode == 0
Example #13
Source File: cache.py From bake with ISC License | 5 votes |
def __setitem__(self, k, v): key = self._key_for_hashes(k) cmd = f"git config --local {key} {v}" if self.debug: click.echo(f" {click.style('$', fg='green')} {cmd}", err=True) c = delegator.run(cmd) return c.ok
Example #14
Source File: cache.py From bake with ISC License | 5 votes |
def __getitem__(self, k): key = self._key_for_hashes(k) cmd = f"git config --local --get {key}" if self.debug: click.echo(f" {click.style('$', fg='green')} {cmd}", err=True) c = delegator.run(cmd) if c.ok: return c.out.strip() else: return None
Example #15
Source File: cache.py From bake with ISC License | 5 votes |
def __iter__(self): # TODO: Print these. cmd = "git config --local --list" if self.debug: click.echo(f" {click.style('$', fg='green')} {cmd}", err=True) c = delegator.run(cmd) for result in c.out.split("\n"): if result.startswith(self.prefix): yield result.split("=", -1)[0][ len(self.prefix + "." + self.namespace + ".") : ]
Example #16
Source File: cache.py From bake with ISC License | 5 votes |
def __init__(self, *, bf, namespace="hashes", debug=False, enabled=True): self.bf = bf self.enabled = enabled self.debug = debug self.namespace = namespace try: # Assert git exists, and appears functioning. c = delegator.run("git --version") assert c.ok if self.debug: click.echo(" + cache.git.ok: true", err=True) # Record the top-level directory of the git repository. c = delegator.run("git rev-parse --show-toplevel") self.git_root = c.out.strip() if self.debug: click.echo(f" + cache.git.root: {self.git_root!r}", err=True) # Assert Bakefile exists within it. assert self.bf.path.startswith(self.git_root) if self.debug: click.echo(f" + cache.git.contains_bakefile: true", err=True) except AssertionError: # Cache is disabled. self.enabled = False if debug: click.echo(f" + cache.enabled: true", err=True)
Example #17
Source File: bash.py From bake with ISC License | 5 votes |
def system_which(command, mult=False): """Emulates the system's which. Returns None if not found.""" _which = "which -a" if not os.name == "nt" else "where" # os.environ = { # vistir.compat.fs_str(k): vistir.compat.fs_str(val) # for k, val in os.environ.items() # } result = None try: c = delegator.run("{0} {1}".format(_which, command)) try: # Which Not found… if c.return_code == 127: click.echo( "{}: the {} system utility is required for bake to find bash properly." "\n Please install it.".format( click.style("Warning", bold=True), click.style(_which, fg="red") ), err=True, ) assert c.return_code == 0 except AssertionError: result = None except TypeError: if not result: result = None else: if not result: result = next(iter([c.out, c.err]), "").split("\n") result = next(iter(result)) if not mult else result return result if not result: result = None result = [result] if mult else result return result
Example #18
Source File: core.py From pipenv with MIT License | 5 votes |
def system_which(command, mult=False): """Emulates the system's which. Returns None if not found.""" _which = "which -a" if not os.name == "nt" else "where" os.environ.update({ vistir.compat.fs_str(k): vistir.compat.fs_str(val) for k, val in os.environ.items() }) result = None try: c = delegator.run("{0} {1}".format(_which, command)) try: # Which Not found… if c.return_code == 127: click.echo( "{}: the {} system utility is required for Pipenv to find Python installations properly." "\n Please install it.".format( crayons.red("Warning", bold=True), crayons.red(_which) ), err=True, ) assert c.return_code == 0 except AssertionError: result = fallback_which(command, allow_global=True) except TypeError: if not result: result = fallback_which(command, allow_global=True) else: if not result: result = next(iter([c.out, c.err]), "").split("\n") result = next(iter(result)) if not mult else result return result if not result: result = fallback_which(command, allow_global=True) result = [result] if mult else result return result
Example #19
Source File: bash.py From bash.py with Apache License 2.0 | 5 votes |
def run(script=None, **kwargs): """Runs the given bash script.""" return Bash(**kwargs).command(script)
Example #20
Source File: utils.py From pipenv with MIT License | 5 votes |
def load_path(python): from ._compat import Path import delegator import json python = Path(python).as_posix() json_dump_commmand = '"import json, sys; print(json.dumps(sys.path));"' c = delegator.run('"{0}" -c {1}'.format(python, json_dump_commmand)) if c.return_code == 0: return json.loads(c.out.strip()) else: return []
Example #21
Source File: utils.py From pipenv with MIT License | 5 votes |
def run_command(cmd, *args, **kwargs): """ Take an input command and run it, handling exceptions and error codes and returning its stdout and stderr. :param cmd: The list of command and arguments. :type cmd: list :returns: A 2-tuple of the output and error from the command :rtype: Tuple[str, str] :raises: exceptions.PipenvCmdError """ from pipenv.vendor import delegator from ._compat import decode_for_output from .cmdparse import Script catch_exceptions = kwargs.pop("catch_exceptions", True) if isinstance(cmd, (six.string_types, list, tuple)): cmd = Script.parse(cmd) if not isinstance(cmd, Script): raise TypeError("Command input must be a string, list or tuple") if "env" not in kwargs: kwargs["env"] = os.environ.copy() kwargs["env"]["PYTHONIOENCODING"] = "UTF-8" try: cmd_string = cmd.cmdify() except TypeError: click_echo("Error turning command into string: {0}".format(cmd), err=True) sys.exit(1) if environments.is_verbose(): click_echo("Running command: $ {0}".format(cmd_string, err=True)) c = delegator.run(cmd_string, *args, **kwargs) return_code = c.return_code if environments.is_verbose(): click_echo("Command output: {0}".format( crayons.blue(decode_for_output(c.out)) ), err=True) if not c.ok and catch_exceptions: raise PipenvCmdError(cmd_string, c.out, c.err, return_code) return c
Example #22
Source File: test_install_uri.py From pipenv with MIT License | 5 votes |
def test_install_local_vcs_not_in_lockfile(PipenvInstance): with PipenvInstance(chdir=True) as p: # six_path = os.path.join(p.path, "six") six_path = p._pipfile.get_fixture_path("git/six/").as_posix() c = delegator.run("git clone {0} ./six".format(six_path)) assert c.return_code == 0 c = p.pipenv("install -e ./six".format(six_path)) assert c.return_code == 0 six_key = list(p.pipfile["packages"].keys())[0] # we don't need the rest of the test anymore, this just works on its own assert six_key == "six"
Example #23
Source File: test_install_uri.py From pipenv with MIT License | 5 votes |
def test_local_vcs_urls_work(PipenvInstance, tmpdir): six_dir = tmpdir.join("six") six_path = Path(six_dir.strpath) with PipenvInstance(chdir=True) as p: c = delegator.run( "git clone https://github.com/benjaminp/six.git {0}".format(six_dir.strpath) ) assert c.return_code == 0 c = p.pipenv("install git+{0}#egg=six".format(six_path.as_uri())) assert c.return_code == 0 assert "six" in p.pipfile["packages"]
Example #24
Source File: comparison.py From speed-comparison with MIT License | 4 votes |
def run(self): """Run a measurement. Returns: list -- List of results. """ print(f"> {self.name}") if self.compile_cmd: cmd_compile = delegator.run(self.compile_cmd) if self.debug or cmd_compile.return_code is not 0: logging.info(cmd_compile.out) print(f"Version: {delegator.run(self.version_cmd).out.splitlines()[0]}") times = [] count = 0 while count < 10: count += 1 # Measure execution time watch = StopWatch() watch.start() cmd_run = delegator.run(self.run_cmd) watch.stop() # Convert the measurement into milliseconds times.append(int(watch.elapsed_time * 1000)) print(f"Speed (all): {'ms, '.join(map(str, times))}ms") print(f"Speed (best): {min(times)}ms") print(f"Speed (worst): {max(times)}ms") print(f"Speed (median): {statistics.median(times)}ms") # Strip output from new line result = cmd_run.out.strip() print(f"Result: {result}") # Calculate accuracy if not self.debug: accuracy = self.diff_letters(f"{math.pi:.{len(result)-2}f}", result) print(f"Accuracy: {accuracy:.2%}") else: accuracy = 0.0000 print() # new line self.results["median"] = statistics.median(times) self.results["best"] = min(times) self.results["worst"] = max(times) self.results["accuracy"] = f"{accuracy*100:.4}" return self.results
Example #25
Source File: comparison.py From speed-comparison with MIT License | 4 votes |
def measurement(): """Management function for the measurement. """ # Change the working directory to the `src` folder cwd = os.getcwd() os.chdir(os.path.join(cwd, "src")) with open("rounds.txt") as file: iterations = file.read().strip() print("\n======= Comparison =======") print(f"Iterations: {iterations}\n") languages = [ # language name / version command / execute command / compile command ["Julia", "julia --version", "julia leibniz.jl"], ["Python 3 (CPython)", "python --version", "python leibniz.py"], ["R", "R --version", "Rscript leibniz.r"], ["Ruby", "ruby --version", "ruby leibniz.rb"], ["PHP 5.6", "php56 --version", "php56 leibniz.php"], ["Java", "pacman -Qi jdk8-openjdk | grep 'Version' | cut -d: -f2- | cut -d ' ' -f2", "java leibniz", "javac leibniz.java"], ["Lua", "lua -v", "lua leibniz.lua"], ["Rust", "rustc --version", "./leibniz", "export RUST_BACKTRACE=1; rustc leibniz.rs"], ["JS (node)", "node --version", "node leibniz.js"], ["PHP 7", "php --version", "php leibniz.php"], ["Python 3 (pypy)", "pacman -Qi pypy | grep 'Version' | cut -d: -f2- | cut -d ' ' -f2", "pypy leibniz.py"], ["Nim", "pacman -Qi nim | grep 'Version' | cut -d: -f2 | cut -d ' ' -f2", "./leibniz", "nim c --verbosity:0 leibniz.nim"], ["C++", "g++ --version", "./leibniz", "g++ leibniz.cpp -o leibniz"], ["Crystal", "crystal --version", "./leibniz", "crystal build leibniz.cr"], ["C", "gcc --version", "./leibniz", "gcc leibniz.c -o leibniz"], ["Go", "go version", "./leibniz", "go build leibniz.go"], # ["Swift", "swift --version", "swift leibniz.swift"], ] complete_results = [] for language in languages: complete_results.append(Measure(*language).run()) if ask_yes_no("Do you want to save the results?"): # Write csv file with results os.chdir(os.path.join(cwd, "results")) keys = complete_results[0].keys() with open("data.csv", "w", newline="", encoding="utf8") as file: dict_writer = csv.DictWriter(file, keys) dict_writer.writeheader() dict_writer.writerows(complete_results) print(f"Saved the results in: {colored('results/data.csv', 'red', attrs=['bold'])}")
Example #26
Source File: core.py From pipenv with MIT License | 4 votes |
def format_help(help): """Formats the help string.""" help = help.replace("Options:", str(crayons.normal("Options:", bold=True))) help = help.replace( "Usage: pipenv", str("Usage: {0}".format(crayons.normal("pipenv", bold=True))) ) help = help.replace(" check", str(crayons.red(" check", bold=True))) help = help.replace(" clean", str(crayons.red(" clean", bold=True))) help = help.replace(" graph", str(crayons.red(" graph", bold=True))) help = help.replace(" install", str(crayons.magenta(" install", bold=True))) help = help.replace(" lock", str(crayons.green(" lock", bold=True))) help = help.replace(" open", str(crayons.red(" open", bold=True))) help = help.replace(" run", str(crayons.yellow(" run", bold=True))) help = help.replace(" shell", str(crayons.yellow(" shell", bold=True))) help = help.replace(" sync", str(crayons.green(" sync", bold=True))) help = help.replace(" uninstall", str(crayons.magenta(" uninstall", bold=True))) help = help.replace(" update", str(crayons.green(" update", bold=True))) additional_help = """ Usage Examples: Create a new project using Python 3.7, specifically: $ {1} Remove project virtualenv (inferred from current directory): $ {9} Install all dependencies for a project (including dev): $ {2} Create a lockfile containing pre-releases: $ {6} Show a graph of your installed dependencies: $ {4} Check your installed dependencies for security vulnerabilities: $ {7} Install a local setup.py into your virtual environment/Pipfile: $ {5} Use a lower-level pip command: $ {8} Commands:""".format( crayons.red("pipenv --three"), crayons.red("pipenv --python 3.7"), crayons.red("pipenv install --dev"), crayons.red("pipenv lock"), crayons.red("pipenv graph"), crayons.red("pipenv install -e ."), crayons.red("pipenv lock --pre"), crayons.red("pipenv check"), crayons.red("pipenv run pip freeze"), crayons.red("pipenv --rm"), ) help = help.replace("Commands:", additional_help) return help
Example #27
Source File: core.py From pipenv with MIT License | 4 votes |
def do_purge(bare=False, downloads=False, allow_global=False): """Executes the purge functionality.""" if downloads: if not bare: click.echo(crayons.normal(fix_utf8("Clearing out downloads directory…"), bold=True)) vistir.path.rmtree(project.download_location) return # Remove comments from the output, if any. installed = set([ pep423_name(pkg.project_name) for pkg in project.environment.get_installed_packages() ]) bad_pkgs = set([pep423_name(pkg) for pkg in BAD_PACKAGES]) # Remove setuptools, pip, etc from targets for removal to_remove = installed - bad_pkgs # Skip purging if there is no packages which needs to be removed if not to_remove: if not bare: click.echo("Found 0 installed package, skip purging.") click.echo(crayons.green("Environment now purged and fresh!")) return installed if not bare: click.echo( fix_utf8("Found {0} installed package(s), purging…".format(len(to_remove))) ) command = "{0} uninstall {1} -y".format( escape_grouped_arguments(which_pip(allow_global=allow_global)), " ".join(to_remove), ) if environments.is_verbose(): click.echo("$ {0}".format(command)) c = delegator.run(command) if c.return_code != 0: raise exceptions.UninstallError(installed, command, c.out + c.err, c.return_code) if not bare: click.echo(crayons.blue(c.out)) click.echo(crayons.green("Environment now purged and fresh!")) return installed
Example #28
Source File: utils.py From pipenv with MIT License | 4 votes |
def resolve(cmd, sp): import delegator from .cmdparse import Script from .vendor.pexpect.exceptions import EOF, TIMEOUT from .vendor.vistir.compat import to_native_string from .vendor.vistir.misc import echo EOF.__module__ = "pexpect.exceptions" from ._compat import decode_output c = delegator.run(Script.parse(cmd).cmdify(), block=False, env=os.environ.copy()) if environments.is_verbose(): c.subprocess.logfile = sys.stderr _out = decode_output("") result = None out = to_native_string("") while True: result = None try: result = c.expect(u"\n", timeout=environments.PIPENV_INSTALL_TIMEOUT) except TIMEOUT: pass except EOF: break except KeyboardInterrupt: c.kill() break if result: _out = c.subprocess.before _out = decode_output("{0}".format(_out)) out += _out # sp.text = to_native_string("{0}".format(_out[:100])) if environments.is_verbose(): sp.hide_and_write(out.splitlines()[-1].rstrip()) else: break c.block() if c.return_code != 0: sp.red.fail(environments.PIPENV_SPINNER_FAIL_TEXT.format( "Locking Failed!" )) echo(c.out.strip(), err=True) if not environments.is_verbose(): echo(out, err=True) sys.exit(c.return_code) if environments.is_verbose(): echo(c.err.strip(), err=True) return c
Example #29
Source File: views.py From website with MIT License | 4 votes |
def post(self, name, upload_id): if not current_app.config["RELEASE_ENABLED"]: message = "Releasing is currently out of service" flash(message) logging.info(message) if self.upload.released_at: flash( f"The upload {self.upload.filename} has already been released " f"and can't be released again." ) return self.redirect_to_project() release_form = ReleaseForm(project_name=self.project.name) context = { "release_form": release_form, "project": self.project, "upload": self.upload, } if release_form.validate_on_submit(): # copy path to new tmp directory with tempfile.TemporaryDirectory() as tmpdir: upload_path = os.path.join(tmpdir, self.upload.filename) shutil.copy(self.upload.full_path, upload_path) # run twine upload against copied upload file twine_run = delegator.run(f"twine upload {upload_path}") if twine_run.return_code == 0: errors = self.validate_upload() release_form.add_global_error(*errors) if not errors: # create ProjectRelease object with reference to project self.upload.released_at = datetime.utcnow() # write to database self.upload.save() message = f"You've successfully released {self.upload} to PyPI." flash(message) logger.info(message) return self.redirect_to_project() else: error = f"Release of {self.upload} failed." release_form.add_global_error(error) logger.error( error, extra={"data": {"out": twine_run.out, "err": twine_run.err}} ) context.update({"twine_run": twine_run, "upload": self.upload}) return context