Exemplo n.º 1
0
def test_add_git_source():
    metadata.reset()

    metadata.add_git_source(sha="sha", name="name", remote="remote")

    current = metadata.get()

    assert current.sources[0].git.sha == "sha"
    assert current.sources[0].git.name == "name"
    assert current.sources[0].git.remote == "remote"
Exemplo n.º 2
0
def test_write(tmpdir):
    metadata.reset()

    metadata.add_git_source(sha="sha", name="name", remote="remote")

    output_file = tmpdir / "synth.metadata"

    metadata.write(str(output_file))

    data = output_file.read()

    # Ensure the file was written, that *some* metadata is in it, and that it
    # is valid JSON.
    assert data
    assert "sha" in data
    assert json.loads(data)
Exemplo n.º 3
0
def test_used_to_append_git_log_to_metadata(source_tree):
    """Synthtool used to append the git log for each git source.  But nothing
    consumes the log, and there's no design for anything to consume the log.
    Plus, it cluttered up synth.metadata.
    """
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir /
                                           "synth.metadata"):
        # Create one commit that will be recorded in the metadata.
        source_tree.write("a")
        source_tree.git_add("a")
        source_tree.git_commit("a")

        hash = subprocess.run(
            [source_tree.git, "log", "-1", "--pretty=format:%H"],
            stdout=subprocess.PIPE,
            universal_newlines=True,
        ).stdout.strip()
        metadata.add_git_source(name="tmp", local_path=os.getcwd(), sha=hash)

    metadata.reset()
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir /
                                           "synth.metadata"):
        # Create two more commits that should appear in metadata git log.
        source_tree.write("code/b")
        source_tree.git_add("code/b")
        source_tree.git_commit("code/b")

        source_tree.write("code/c")
        source_tree.git_add("code/c")
        source_tree.git_commit("code/c")

        hash = subprocess.run(
            [source_tree.git, "log", "-1", "--pretty=format:%H"],
            stdout=subprocess.PIPE,
            universal_newlines=True,
        ).stdout.strip()
        metadata.add_git_source(name="tmp", local_path=os.getcwd(), sha=hash)

    # Read the metadata that we just wrote.
    mdata = metadata._read_or_empty(source_tree.tmpdir / "synth.metadata")
    # The log should be empty.
    assert "" == mdata.sources[1].git.log
    # Make sure the local path field is not recorded.
    assert not mdata.sources[0].git.local_path is None
Exemplo n.º 4
0
def test_append_git_log_to_metadata(source_tree):
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        # Create one commit that will be recorded in the metadata.
        source_tree.write("a")
        source_tree.git_add("a")
        source_tree.git_commit("a")

        hash = subprocess.run(
            [source_tree.git, "log", "-1", "--pretty=format:%H"],
            stdout=subprocess.PIPE,
            universal_newlines=True,
        ).stdout.strip()
        metadata.add_git_source(name="tmp", local_path=os.getcwd(), sha=hash)

    metadata.reset()
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        # Create two more commits that should appear in metadata git log.
        source_tree.write("code/b")
        source_tree.git_add("code/b")
        source_tree.git_commit("code/b")

        source_tree.write("code/c")
        source_tree.git_add("code/c")
        source_tree.git_commit("code/c")

        hash = subprocess.run(
            [source_tree.git, "log", "-1", "--pretty=format:%H"],
            stdout=subprocess.PIPE,
            universal_newlines=True,
        ).stdout.strip()
        metadata.add_git_source(name="tmp", local_path=os.getcwd(), sha=hash)

    # Read the metadata that we just wrote.
    mdata = metadata._read_or_empty(source_tree.tmpdir / "synth.metadata")
    # Match 2 log lines.
    assert re.match(
        r"[0-9A-Fa-f]+\ncode/c\n+[0-9A-Fa-f]+\ncode/b\n+",
        mdata.sources[0].git.log,
        re.MULTILINE,
    )
    # Make sure the local path field is not recorded.
    assert not mdata.sources[0].git.local_path is None
Exemplo n.º 5
0
def clone(
    url: str,
    dest: pathlib.Path = None,
    committish: str = "master",
    force: bool = False,
    depth: int = None,
) -> pathlib.Path:
    if dest is None:
        dest = cache.get_cache_dir()

    dest = dest / pathlib.Path(url).stem

    if force and dest.exists():
        shutil.rmtree(dest)

    if not dest.exists():
        cmd = ["git", "clone", url, dest]
        if depth is not None:
            cmd.extend(["--depth", str(depth)])
        shell.run(cmd)
    else:
        shell.run(["git", "pull"], cwd=str(dest))

    shell.run(["git", "reset", "--hard", committish], cwd=str(dest))

    # track all git repositories
    _tracked_paths.add(dest)

    # add repo to metadata
    sha, message = get_latest_commit(dest)
    commit_metadata = extract_commit_message_metadata(message)

    metadata.add_git_source(
        name=dest.name,
        remote=url,
        sha=sha,
        internal_ref=commit_metadata.get("PiperOrigin-RevId"),
    )

    return dest
Exemplo n.º 6
0
def test_git_sources_are_sorted(source_tree: SourceTree):
    metadata_path = source_tree.tmpdir / "synth.metadata"
    with metadata.MetadataTrackerAndWriter(metadata_path):
        metadata.add_generator_source(name="a-generator",
                                      version="1",
                                      docker_image="x")
        metadata.add_generator_source(name="b-generator",
                                      version="2",
                                      docker_image="y")
        metadata.add_template_source(name="a-template",
                                     origin="andromeda",
                                     version="3")
        metadata.add_template_source(name="b-template",
                                     origin="milky way",
                                     version="4")
        metadata.add_git_source(name="a-git", sha="1a")
        metadata.add_git_source(name="b-git", sha="1b")
    m1 = metadata._read_or_empty(metadata_path)
    # Add the same sources in reverse order.
    metadata.reset()
    with metadata.MetadataTrackerAndWriter(metadata_path):
        metadata.add_git_source(name="b-git", sha="1b")
        metadata.add_git_source(name="a-git", sha="1a")
        metadata.add_template_source(name="b-template",
                                     origin="milky way",
                                     version="4")
        metadata.add_template_source(name="a-template",
                                     origin="andromeda",
                                     version="3")
        metadata.add_generator_source(name="b-generator",
                                      version="2",
                                      docker_image="y")
        metadata.add_generator_source(name="a-generator",
                                      version="1",
                                      docker_image="x")
    m2 = metadata._read_or_empty(metadata_path)
    assert m1.sources == m2.sources
Exemplo n.º 7
0
def clone(
    url: str,
    dest: pathlib.Path = None,
    committish: str = None,
    force: bool = False,
) -> pathlib.Path:
    """Clones a remote git repo.

    Will not actually clone the repo if it's already local via two ways:
      1. It's in the cache (the default destitination).
      2. It was supplied via the preconfig file.

    Arguments:
        url {str} -- Url pointing to remote git repo.

    Keyword Arguments:
        dest {pathlib.Path} -- Local folder where repo should be cloned. (default: {None})
        committish {str} -- The commit hash to check out. (default: {None})
        force {bool} -- Wipe out and reclone if it already exists it the cache. (default: {False})

    Returns:
        pathlib.Path -- Local directory where the repo was cloned.
    """
    preclone = get_preclone(url)

    if preclone:
        logger.debug(f"Using precloned repo {preclone}")
        dest = pathlib.Path(preclone)
    else:
        if dest is None:
            dest = cache.get_cache_dir()

        dest = dest / pathlib.Path(url).stem

        if force and dest.exists():
            shutil.rmtree(dest)

        if not dest.exists():
            cmd = [
                "git", "clone", "--recurse-submodules", "--single-branch", url,
                dest
            ]
            shell.run(cmd, check=True)
        else:
            shell.run(["git", "checkout", "master"], cwd=str(dest), check=True)
            shell.run(["git", "pull"], cwd=str(dest), check=True)
        committish = committish or "master"

    if committish:
        shell.run(["git", "reset", "--hard", committish], cwd=str(dest))

    # track all git repositories
    _tracked_paths.add(dest)

    # add repo to metadata
    sha, message = get_latest_commit(dest)
    commit_metadata = extract_commit_message_metadata(message)

    metadata.add_git_source(
        name=dest.name,
        remote=url,
        sha=sha,
        internal_ref=commit_metadata.get("PiperOrigin-RevId"),
        local_path=str(dest),
    )

    return dest