Beispiel #1
0
def _update_requirements(path, package):
    path = osp.realpath(path)

    content = read(path)

    try:
        pattern = r"{package}(=={version})*".format(
            package=re.escape(package.name),
            version=re.escape(package.current_version))
        lines = content.splitlines()
        nlines = len(lines)

        with open(path, "w") as f:
            for i, line in enumerate(lines):
                if re.search(pattern, line, flags=re.IGNORECASE):
                    line = line.replace("==%s" % package.current_version,
                                        "==%s" % package.latest_version)

                f.write(line)

                if i < nlines - 1:
                    f.write("\n")
    except Exception:
        # In case we f****d up!
        write(path, content, force=True)
Beispiel #2
0
def echo(string="", file=None, nl=True):
    nl = "\n" if nl else ""

    print(string, end=nl)

    if file:
        string = strip_ansi(string)
        write(file, string + nl, append=True)
def test_write(tmpdir):
    directory = tmpdir.mkdir("tmp")
    tempfile = directory.join("foobar.txt")

    path = str(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_