def dependency_check(repopath, path=".", maven_config=None, **kwargs): import invoke import tempfile c = invoke.Context() if maven_config is not None: maven_config_path = tempfile.mkstemp() with open(maven_config_path[1], mode="w") as config: config.write(maven_config) project_path = os.path.join(repopath, path) scan = c.run((f"/usr/share/dependency-check/bin/dependency-check.sh " f"-d /usr/share/dependency-check/notavolume " f"--project DEEPTRACY_SCAN " f"--noupdate " f"--scan {project_path} " f"-f JSON -o /tmp ")) yield AppendStdout(scan.stdout) yield AppendStderr(scan.stderr) with open("/tmp/dependency-check-report.json", "r") as f: report = json.load(f) yield SetProperty("dependencies", list(odc_dependencies(report))) yield SetProperty("vulnerabilities", list(odc_vulnerabilities(report))) return True
def mvn_dependencytree(repopath, path=".", maven_config=None, **kwargs): import invoke import tempfile c = invoke.Context() with c.cd(repopath): with c.cd(path): if maven_config is not None: maven_config_path = tempfile.mkstemp() with open(maven_config_path[1], mode="w") as config: config.write(maven_config) deps = c.run(("mvn dependency:tree" " -DoutputFile=/tmp/mvndeps.tgf" " -DoutputType=tgf" " -s ") + maven_config_path[1], warn=True) else: deps = c.run(("mvn dependency:tree" " -DoutputFile=/tmp/mvndeps.tgf" " -DoutputType=tgf"), warn=True) for line in deps.stdout.splitlines(): yield AppendStdout(line) for line in deps.stderr.splitlines(): yield AppendStderr(line) with open("/tmp/mvndeps.tgf", "r") as f: yield SetProperty("dependencies", list(mvntgf2deps(f.read()))) return True
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._adb_params = { "port": 22350, "ip": "127.0.0.1", "stdout": "adb-server.out", "stderr": "adb-server.err", "key": ".adb_key", } # the local connection self._invoke_config = invoke.Config( {"run": { "warn": True, "hide": True }}) self._fabric_config = fabric.Config( {"run": { "hide": True, "warn": True }}) self._connection = invoke.Context(self._invoke_config) self._spawned = False self._local_adb_version = None self._remote_adb_version = None
def manual_context(): c = invoke.Context() c.run("cmd1; cmd2") # $getCommand="cmd1; cmd2" c.sudo("cmd1; cmd2") # $getCommand="cmd1; cmd2" # invoke.Context is just an alias for invoke.context.Context c2 = invoke.context.Context() c2.run("cmd1; cmd2") # $getCommand="cmd1; cmd2"
def __init__(self, host=None, user=None, ssh_config=None, sudo_ssh_config=None, wrapped_context=None, sudo_wrapped_context=None): """Create context that will be used to run command with. Args: host: a host name. user: username of the host. ssh_config: ssh_util.SshConfig for login to the host. sudo_ssh_config: ssh_util.SshConfig for login to the host as sudo. wrapped_context: invoke context, used for test. sudo_wrapped_context: invoke context, used for sudo test. Raises: FabricNotFoundError: fabric is not installed. """ self._host = host or socket.gethostname() self._user = user or getpass.getuser() self._is_local = False self._tried_gcloud = False self._gcloud = None if socket.gethostname() == self._host or socket.getfqdn() == self._host: self._is_local = True self._ssh_config = ssh_config or ssh_util.SshConfig( user=user, hostname=host) self._sudo_ssh_config = sudo_ssh_config self._closed = False # TODO: We should make the subprocess's output configurable by # parent process. self._out_stream = HostCommandOutStream( None if self._is_local else host, 'stdout') # We also output stderr to DEBUG level, if the command failed and # raise on failure, the raised exception will have the information. self._err_stream = HostCommandOutStream( None if self._is_local else host, 'stderr') self._wrapped_context = wrapped_context self._sudo_wrapped_context = sudo_wrapped_context if self._wrapped_context: return if self._is_local: if invoke: # Use invoke context to running locally. self._wrapped_context = invoke.Context() # Else use _RunDirectly instead of using invoke.context. return if self._ssh_config.use_native_ssh or not fabric: if not self._ssh_config.use_native_ssh and not fabric: logger.debug('No fabric package installed, using native ssh.') self._CreateNativeSshContexts() return self._CreateFabricContexts()
def pip_install(repopath, path=".", **kwargs): import invoke c = invoke.Context() with c.cd(repopath): with c.cd(path): res = c.run("pipenv install .") deps = c.run("pipenv graph --json") yield AppendStdout(res.stdout) yield AppendStderr(res.stderr) yield SetProperty("dependencies", list(pipenv_graph2deps(deps.stdout))) return True
def main(argv=None): argv = sys.argv if argv is None else argv _, *args = argv if not args: raise SystemExit(MESSAGE) elif args[0] == 'bash': tasks.bash(inv.Context()) elif args[0] == 'tasks': get_program(True).run(['jarbas_tasks', '-l']) elif args[0] == 'run': get_program(False).run(['jarbas_tasks'] + args) else: get_program(False).run(['jarbas_tasks'] + args)
def requirement_file(repopath, requirement="requirements.txt", path=".", **kwargs): import invoke c = invoke.Context() with c.cd(repopath): with c.cd(path): res = c.run("pipenv install -r %s" % requirement) deps = c.run("pipenv graph --json") yield AppendStdout(res.stdout) yield AppendStderr(res.stderr) yield SetProperty("dependencies", list(pipenv_graph2deps(deps.stdout))) return True
def npm_install(repopath, path=".", **kwargs): import invoke c = invoke.Context() with c.cd(repopath): with c.cd(path): res = c.run("npm install", warn=True) # Dump the output to a file to avoid # https://github.com/npm/npm/issues/17331 deps = c.run("npm ls --json > /tmp/npmdeps.json", warn=True) yield AppendStdout(res.stdout) yield AppendStderr(res.stderr) with open("/tmp/npmdeps.json", "r", encoding='utf-8') as f: yield SetProperty("dependencies", list(npmjson2deps(f.read()))) return True
def execute(cmd, **command_ctx): """ :param str shell: Which shell binary to use. Default: ``/bin/bash`` (on Unix; ``COMSPEC`` or ``cmd.exe`` on Windows.) :param bool warn: Whether to warn and continue, instead of raising `.UnexpectedExit`, when the executed command exits with a nonzero status. Default: ``False``. .. note:: This setting has no effect on exceptions, which will still be raised, typically bundled in `.ThreadException` objects if they were raised by the IO worker threads. Similarly, `.WatcherError` exceptions raised by `.StreamWatcher` instances will also ignore this setting, and will usually be bundled inside `.Failure` objects (in order to preserve the execution context). Ditto `.CommandTimedOut` - basically, anything that prevents a command from actually getting to "exited with an exit code" ignores this flag. :param hide: Allows the caller to disable ``run``'s default behavior of copying the subprocess' stdout and stderr to the controlling terminal. Specify ``hide='out'`` (or ``'stdout'``) to hide only the stdout stream, ``hide='err'`` (or ``'stderr'``) to hide only stderr, or ``hide='both'`` (or ``True``) to hide both streams. The default value is ``None``, meaning to print everything; ``False`` will also disable hiding. .. note:: Stdout and stderr are always captured and stored in the ``Result`` object, regardless of ``hide``'s value. .. note:: ``hide=True`` will also override ``echo=True`` if both are given (either as kwargs or via config/CLI). :param bool pty: By default, ``run`` connects directly to the invoked process and reads its stdout/stderr streams. Some programs will buffer (or even behave) differently in this situation compared to using an actual terminal or pseudoterminal (pty). To use a pty instead of the default behavior, specify ``pty=True``. .. warning:: Due to their nature, ptys have a single output stream, so the ability to tell stdout apart from stderr is **not possible** when ``pty=True``. As such, all output will appear on ``out_stream`` (see below) and be captured into the ``stdout`` result attribute. ``err_stream`` and ``stderr`` will always be empty when ``pty=True``. :param bool fallback: Controls auto-fallback behavior re: problems offering a pty when ``pty=True``. Whether this has any effect depends on the specific `Runner` subclass being invoked. Default: ``True``. :param bool echo: Controls whether `.run` prints the command string to local stdout prior to executing it. Default: ``False``. .. note:: ``hide=True`` will override ``echo=True`` if both are given. :param dict env: By default, subprocesses receive a copy of Invoke's own environment (i.e. ``os.environ``). Supply a dict here to update that child environment. For example, ``run('command', env={'PYTHONPATH': '/some/virtual/env/maybe'})`` would modify the ``PYTHONPATH`` env var, with the rest of the child's env looking identical to the parent. .. seealso:: ``replace_env`` for changing 'update' to 'replace'. :param bool replace_env: When ``True``, causes the subprocess to receive the dictionary given to ``env`` as its entire shell environment, instead of updating a copy of ``os.environ`` (which is the default behavior). Default: ``False``. :param str encoding: Override auto-detection of which encoding the subprocess is using for its stdout/stderr streams (which defaults to the return value of `default_encoding`). :param out_stream: A file-like stream object to which the subprocess' standard output should be written. If ``None`` (the default), ``sys.stdout`` will be used. :param err_stream: Same as ``out_stream``, except for standard error, and defaulting to ``sys.stderr``. :param in_stream: A file-like stream object to used as the subprocess' standard input. If ``None`` (the default), ``sys.stdin`` will be used. If ``False``, will disable stdin mirroring entirely (though other functionality which writes to the subprocess' stdin, such as autoresponding, will still function.) Disabling stdin mirroring can help when ``sys.stdin`` is a misbehaving non-stream object, such as under test harnesses or headless command runners. :param watchers: A list of `.StreamWatcher` instances which will be used to scan the program's ``stdout`` or ``stderr`` and may write into its ``stdin`` (typically ``str`` or ``bytes`` objects depending on Python version) in response to patterns or other heuristics. See :doc:`/concepts/watchers` for details on this functionality. Default: ``[]``. :param bool echo_stdin: Whether to write data from ``in_stream`` back to ``out_stream``. In other words, in normal interactive usage, this parameter controls whether Invoke mirrors what you type back to your terminal. By default (when ``None``), this behavior is triggered by the following: * Not using a pty to run the subcommand (i.e. ``pty=False``), as ptys natively echo stdin to stdout on their own; * And when the controlling terminal of Invoke itself (as per ``in_stream``) appears to be a valid terminal device or TTY. (Specifically, when `~invoke.util.isatty` yields a ``True`` result when given ``in_stream``.) .. note:: This property tends to be ``False`` when piping another program's output into an Invoke session, or when running Invoke within another program (e.g. running Invoke from itself). If both of those properties are true, echoing will occur; if either is false, no echoing will be performed. When not ``None``, this parameter will override that auto-detection and force, or disable, echoing. :param timeout: """ ## use formatter to format command command_ctx = command_ctx or {} if "cwd" in command_ctx: if command_ctx["cwd"]: cwd = command_ctx["cwd"] del command_ctx["cwd"] c = invoke.Context() with c.cd(cwd): res = c.run(cmd, **command_ctx) return res.return_code, res.stdout, res.stderr del command_ctx["cwd"] res = invoke.run(cmd, **command_ctx) return res.return_code, res.stdout, res.stderr
def manual_context(): c = invoke.Context() c.run('echo run from manual context')
import os import zipfile import invoke import shutil import filecmp local = invoke.Context() base_dir = os.path.dirname(__file__) def git_copy(src, dst, temp_dir=base_dir): tmp_fl_path = os.path.join(base_dir, 'git_copy_tmp.zip') with local.cd(src): local.run(r'git archive -o %(tmp_fl_path)s HEAD' % locals()) with zipfile.ZipFile(tmp_fl_path, 'r') as zip_ref: zip_ref.extractall(dst) def copy(src_root, target_root, filters=[], overwrite=True): #src_root = r'D:\work\H5\dist' #target_root = r'D:\work\Release\Live-Binary\H5\dist' for root, dirs, files in os.walk(src_root): rel_path = os.path.relpath(root, src_root) for fl in files: check_passed = True for item in filters: if not item.check_file(root, fl): check_passed = False break
import prompt_toolkit from .lib import HOSTNAME, run, silent_run from .xonsh_builtin import x_env, x_aliases, x_events, x_completers from prompt_toolkit.keys import Keys from prompt_toolkit.filters import ViInsertMode from .commands import load_commands from .path import get_paths from .aliases import get_aliases from .env import apply_envs from . import ctrl_r # from .detect_user_docker import detect_user_docker_for_xonsh x_env["XONSH_SHOW_TRACEBACK"] = True c = invoke.Context({"run": {"echo": True, "warn": True, "hide": False}}) paths = x_env["PATH"] def _add_path_if_exists(path: str, if_path: str = None): if if_path: exist = os.path.exists(if_path) else: exist = os.path.exists(path) if exist: paths.insert(0, path) HOME = x_env["HOME"]
else: d.getElementsByTagName("title")[0].firstChild.data = "" with open(f"{chap[0]}.xml.html", "w") as f: d.writexml(f) return title, sub_tiles @task def open_vivliostyle(c): ver = "2019.8.101" url = "https://github.com/vivliostyle/vivliostyle/releases/download/2019.8.101/vivliostyle-js-2019.8.101.zip" if not os.path.exists("vivliostyle-viewer"): c.run(f"curl -OL {url}") c.run(f"unzip vivliostyle-js-{ver}.zip") c.run(f"mv vivliostyle-js-{ver}/viewer vivliostyle-viewer") c.run(f"rm -rf vivliostyle-js-{ver} vivliostyle-js-{ver}.zip") page = "http://127.0.0.1:8000/vivliostyle-viewer/vivliostyle-viewer.html#b=http://127.0.0.1:8000/contents.html&renderAllPages=true&userStyle=data:,/*%3Cviewer%3E*/%0A@page%20%7B%20size:%20A5;%20%7D%0A/*%3C/viewer%3E*/" print(page) # c.run( # 'open /Applications/Google\\ Chrome.app "http://127.0.0.1:8000/vivliostyle-viewer/vivliostyle-viewer.html#b=http://127.0.0.1:8000/contents.html&renderAllPages=true&userStyle=data:,/*%3Cviewer%3E*/%0A@page%20%7B%20size:%20A5;%20%7D%0A/*%3C/viewer%3E*/"', # warn=True, # hide=True, # ) c.run("npx http-server -p 8000 -c-1") if __name__ == "__main__": build(invoke.Context())