def test_strip(): string = "foobar" assert strip(string) == string string = "\n foobar\nfoobar \n " assert strip(string) == "foobar\nfoobar" string = "\n\n\n" assert strip(string) == ""
def popen(*args, **kwargs): output = kwargs.get("output", False) quiet = kwargs.get("quiet", False) directory = kwargs.get("cwd") environment = kwargs.get("env") shell = kwargs.get("shell", True) raise_err = kwargs.get("raise_err", True) environ = os.environ.copy() if environment: environ.update(environment) for k, v in iteritems(environ): environ[k] = str(v) command = " ".join([str(arg) for arg in args]) logger.info("Executing command: %s" % command) if quiet: output = True proc = sp.Popen(command, bufsize=-1, stdin=sp.PIPE if output else kwargs.get("stdin"), stdout=sp.PIPE if output else None, stderr=sp.PIPE if output else None, env=environ, cwd=directory, shell=shell) code = proc.wait() if code and raise_err: print("An error occurred while updating package:", PopenError(code, command)) #raise PopenError(code, command) if output: output, error = proc.communicate() if output: output = safe_decode(output) output = strip(output) if error: error = safe_decode(error) error = strip(error) if quiet: return code else: return code, output, error else: return code
def popen(*args, **kwargs): output = kwargs.get("output", False) quiet = kwargs.get("quiet" , False) directory = kwargs.get("cwd") environment = kwargs.get("env") shell = kwargs.get("shell", True) raise_err = kwargs.get("raise_err", True) environ = os.environ.copy() if environment: environ.update(environment) for k, v in iteritems(environ): environ[k] = str(v) command = " ".join([str(arg) for arg in args]) if quiet: output = True proc = sp.Popen(command, stdin = sp.PIPE if output else None, stdout = sp.PIPE if output else None, stderr = sp.PIPE if output else None, env = environ, cwd = directory, shell = shell ) code = proc.wait() if code and raise_err: raise sp.CalledProcessError(code, command) if output: output, error = proc.communicate() if output: output = output.decode("utf-8") output = strip(output) if error: error = error.decode("utf-8") error = strip(error) if quiet: return code else: return code, output, error else: return code
def _render_dependency_tree(packages): rendered = [] for package in packages: dependencies = package.dependency_tree string = dependencies.render( indent=4, formatter=lambda package: _format_package(package)) sanitized = strip(string) rendered.append(sanitized) string = strip("\n".join(rendered)) return string
def _get_pip_info(*args, **kwargs): packages = args pip_exec = kwargs.get("pip_exec", None) _, out, _ = _pip.call("show", *packages, pip_exec=pip_exec, output=True) results = [strip(o) for o in out.split("---")] info = dict() for i, package in enumerate(packages): result = results[i] detail = dict((kebab_case(k), v) \ for k, v in \ iteritems( dict([(s + [""]) if len(s) == 1 else s \ for s in [re.split(r":\s?", o, maxsplit = 1) \ for o in result.split("\n")]] ) ) ) info[package] = detail return info
def _get_queries(buffer): queries = [ ] lines = buffer.split(";") for line in lines: line = strip(line) queries.append(line) return queries
def _render_yaml(packages): try: import yaml content = _render_json(packages) dict_ = dict() for details in content: name = details.pop("name") dict_[name] = details string = strip(yaml.safe_dump(dict_)) return string except ImportError: raise ValueError( ("Unable to import yaml. " "Please install pyyaml. https://github.com/yaml/pyyaml."))
def get_included_requirements(filename): path = osp.realpath(filename) basepath = osp.dirname(path) requirements = [] with open(path) as f: content = f.readlines() for line in content: line = strip(line) if line.startswith("-r "): filename = line.split()[1] realpath = osp.abspath(osp.join(basepath, filename)) requirements.append(realpath) requirements += get_included_requirements(realpath) return requirements