Example #1
0
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] = string_types(v)

    command     = " ".join([string_types(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 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 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
Example #2
0
def test_get_revision(tmpdir):
    directory = tmpdir.mkdir("tmp")
    path = string_types(directory)

    with pytest.raises(subprocess.CalledProcessError):
        get_revision(path)

    assert get_revision(path, raise_err=False) == None

    # Initialize the git repository
    call("git", "init", path)
    call("git", "config", "user.email", "*****@*****.**", cwd=path)
    call("git", "config", "user.name", "Foo Bar", cwd=path)

    with pytest.raises(subprocess.CalledProcessError):
        get_revision(path)

    tempfile = directory.join("foobar.txt")
    tempfile.write("foobar")

    call("git", "add", ".", cwd=path)
    call("git", "commit", "-m", "'Test Commit'", cwd=path)

    assert len(get_revision(path)) == 40
    assert len(get_revision(path, short=True)) == 7
Example #3
0
def test_popen(tmpdir):
    directory = tmpdir.mkdir("tmp")
    dirpath = string_types(directory)

    string = "Hello, World!"

    code, out, err = popen("echo %s" % string, output=True)
    assert code == 0
    assert out == string
    assert not err

    env = dict({"FOOBAR": "foobar"})
    code, out, err = popen("echo $FOOBAR; echo $PATH", output=True, env=env)
    assert code == 0
    assert out == "%s\n%s" % (env["FOOBAR"], os.environ["PATH"])
    assert not err

    with pytest.raises(sp.CalledProcessError):
        code = popen("exit 42")

    errstr = "foobar"
    code, out, err = popen('python -c "raise Exception("%s")"' % errstr,
                           output=True,
                           raise_err=False)
    assert code == 1
    assert not out
    assert errstr in err

    filename = "foobar.txt"
    popen("touch %s" % filename, cwd=dirpath)
    assert osp.exists(osp.join(dirpath, filename))

    code = popen("echo $FOOBAR; echo $PATH", quiet=True)
    assert code == 0
Example #4
0
def test_read(tmpdir):
    directory = tmpdir.mkdir("tmp")
    tempfile = directory.join("foobar.txt")
    tempfile.write("foobar")

    assert tempfile.read() == read(string_types(tempfile))

    tempfile = directory.join("barfoo.txt")
    tempfile.write(\
        """
        foobar
        \n
        barfoo
        """
    )

    assert tempfile.read() == read(string_types(tempfile))
Example #5
0
def test_touch(tmpdir):
    directory = tmpdir.mkdir("tmp")
    path = osp.join(string_types(directory), "foo")

    assert not osp.exists(path)

    touch(path)
    assert osp.exists(path)
Example #6
0
    def to_dict(self, repr_=None):
        key = repr_(self.obj) if repr_ else string_types(self.obj)
        dict_ = dict({
            key: [d.to_dict(repr_ = repr_) \
                for d in self.children]
        })

        return dict_
Example #7
0
def test_makedirs(tmpdir):
    directory = tmpdir.mkdir("tmp")
    path = osp.join(string_types(directory), "foo", "bar")

    makedirs(path)
    assert osp.exists(path)

    makedirs(path, exist_ok=True)
    assert osp.exists(path)

    with pytest.raises(OSError):
        makedirs(path)
Example #8
0
def tabulate(rows):
    # Shamelessly taken from: https://git.io/fARTL (pip)
    # Also: https://git.io/fARTY (yarn)
    # Yo, WTF is wrong with git.io and its "fart" filled short urls?

    sizes = [0] * max(len(x) for x in rows)
    for row in rows:
        sizes = [
            max(s, len(string_types(_sanitize_string(c if c else ""))))
            for s, c in zip_longest(sizes, row)
        ]

    result = []
    for row in rows:
        display = " ".join([
            string_types(c) + " " * (s - len(_sanitize_string(c if c else "")))
            if c is not None else "" for s, c in zip_longest(sizes, row)
        ])
        result.append(display)

    return result, sizes
Example #9
0
def test_node():
    tree1 = Node("foo")
    assert tree1.empty == True
    assert tree1 == Node("foo")

    assert string_types(tree1) == "<Node 'foo'>"

    assert tree1.render() == \
"""\
foo
"""

    tree2 = Node("foo", children=["bar", "baz"])
    assert tree2.parent == None
    assert tree2 != Node("foo", children=["bar", "baz", "boo"])
    assert Node("foo", children = ["bar", "baz"]) \
        == Node("foo", children = ["bar", "baz"])
    assert not Node("foo", children = ["bar", "baz"]) \
            == Node("foo", children = ["baz", "boo"])

    assert tree2.render() == \
"""\
foo
  bar
  baz
"""

    assert tree2.render(indent = 4) == \
"""\
foo
    bar
    baz
"""

    assert tree2.render(indent = 4, formatter = lambda x: "* %s" % x) == \
"""\
* foo
    * bar
    * baz
"""

    assert tree2.find(lambda x: x.obj == "foo")
    assert tree2.find(lambda x: x.obj == "bar")
    assert not tree2.find(lambda x: x.obj == "foobaz")

    tree3 = Node("foo")
    tree3.add_children(["bar", "baz"])

    tree4 = Node("foo")
    tree4.children = ["bar", "baz"]

    with pytest.raises(TypeError):
        tree4.children = "foo"
Example #10
0
def test_write(tmpdir):
    directory = tmpdir.mkdir("tmp")
    tempfile = directory.join("foobar.txt")

    path = string_types(tempfile)

    prev, next_ = "foobar", "barfoo"

    write(path, prev)
    assert tempfile.read() == prev

    write(path, next_)
    assert tempfile.read() == prev

    write(path, next_, force=True)
    assert tempfile.read() == next_
Example #11
0
    def set(self, section, key, value, force = False):
        config = self.config
        value  = string_types(value)

        if not section in config:
            config[section] = dict({ key: value })
        else:
            if not key in config[section]:
                config[section][key] = value
            else:
                if force:
                    config[section][key] = value

        path = osp.join(self.location, self.name)
        with open(path, "w") as f:
            config.write(f)
Example #12
0
def value_to_envval(value):
    """
	Convert python types to environment values
	"""

    if not isinstance(value, string_types):
        if value == True:
            value = "true"
        elif value == False:
            value = "false"
        elif isinstance(value, int):
            value = string_types(value)
        else:
            raise TypeError("Unknown parameter type %s with value %r" %
                            (value, type(value)))

    return value
def test_post():
    res = req.post("https://httpbin.org/post")
    assert res.ok
    assert res.status_code == 200

    json = res.json()
    assert all(k in json for k in ("url", "origin", "headers", "args"))

    res.raise_for_status()

    res = req.post("http://httpbin.org/status/404")
    assert not res.ok
    assert res.status_code == 404

    with pytest.raises(HTTPError):
        res.raise_for_status()

    assert string_types(res) == "<Response [{code}]>".format(code=404)
Example #14
0
    def __init__(self, package, sync=False, pip_exec=None):
        logger.info("Initializing Package %s of type %s..." %
                    (package, type(package)))

        self.current_version = None

        if isinstance(package, (_pip.Distribution, _pip.DistInfoDistribution,
                                _pip.EggInfoDistribution)):
            self.name = package.project_name
            self.current_version = package.version
        elif isinstance(package, _pip.InstallRequirement):
            self.name = package.name

            if hasattr(package, "req"):
                if hasattr(package.req, "specifier"):
                    self.current_version = string_types(package.req.specifier)
            else:
                self.current_version = package.installed_version
        elif isinstance(package, dict):
            self.name = package["name"]
            self.current_version = package["version"]
            self.latest_version = package.get("latest_version")
        elif isinstance(package, str):
            self.name = package
            if pip_exec:
                self.current_version = _get_package_version(package,
                                                            pip_exec=pip_exec)

        res = None

        try:
            logger.info("Fetching package %s information from DB..." %
                        self.name)

            res = _db.query("""
				SELECT *
				FROM `tabPackage`
				WHERE name = '%s'
			""" % self.name)
        except db.OperationalError as e:
            logger.warn("Unable to fetch package name. %s" % e)

        if res:
            cache_timeout = settings.get("cache_timeout")
            time_difference = res["_updated_at"] + timedelta(
                seconds=cache_timeout)

            if datetime.now() > time_difference:
                sync = True

        if not res or sync:
            logger.info("Fetching PyPI info for package %s..." % self)
            _pypi_info = _get_pypi_info(self.name, raise_err=False) or {}

            if not getattr(self, "latest_version", None) or sync:
                self.latest_version = _pypi_info.get("version")

            self.home_page = _pypi_info.get("home_page")

        if not res:
            try:
                values = (self.name, self.latest_version
                          or "NULL", self.home_page, datetime.now(),
                          datetime.now())
                logger.info(
                    "Attempting to INSERT package %s into database with values: %s."
                    % (self, values))

                _db.query("""
					INSERT INTO `tabPackage`
						(name, latest_version, home_page, _created_at, _updated_at)
					VALUES
						('%s', '%s', '%s', '%s', '%s')
				""" % values)
            except (db.IntegrityError, db.OperationalError) as e:
                logger.warn("Unable to save package name. %s" % e)
        else:
            if sync:
                logger.info(
                    "Attempting to UPDATE package %s within database." % self)

                try:
                    _db.query("""
						UPDATE `tabPackage`
							SET latest_version = '%s', home_page = '%s', _updated_at = '%s'
						WHERE
							name = '%s'
					""" % (self.latest_version, self.home_page, datetime.now(), self.name))
                except db.OperationalError as e:
                    logger.warn("Unable to update package name. %s" % e)
            else:
                logger.info("Using cached info for package %s." % self)

                self.latest_version = res["latest_version"]
                self.home_page = res["home_page"]

        self.dependency_tree = TreeNode(self)
Example #15
0
 def __repr__(self):
     repr_ = "<Node '%s'%s>" % (string_types(
         self.obj), "parent='%s'" % self.parent if self.parent else "")
     return repr_