summaryrefslogtreecommitdiffstats
path: root/testing/mozharness/mozharness/base/vcs/gittool.py
blob: e9d0c0e2c9f6bfa90a740af09dfca18745049d45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import re

try:
    import urlparse
except ImportError:
    import urllib.parse as urlparse

from mozharness.base.errors import GitErrorList, VCSException
from mozharness.base.log import LogMixin, OutputParser
from mozharness.base.script import ScriptMixin


class GittoolParser(OutputParser):
    """
    A class that extends OutputParser such that it can find the "Got revision"
    string from gittool.py output
    """

    got_revision_exp = re.compile(r"Got revision (\w+)")
    got_revision = None

    def parse_single_line(self, line):
        m = self.got_revision_exp.match(line)
        if m:
            self.got_revision = m.group(1)
        super(GittoolParser, self).parse_single_line(line)


class GittoolVCS(ScriptMixin, LogMixin):
    def __init__(self, log_obj=None, config=None, vcs_config=None, script_obj=None):
        super(GittoolVCS, self).__init__()

        self.log_obj = log_obj
        self.script_obj = script_obj
        if config:
            self.config = config
        else:
            self.config = {}
        # vcs_config = {
        #  repo: repository,
        #  branch: branch,
        #  revision: revision,
        #  ssh_username: ssh_username,
        #  ssh_key: ssh_key,
        # }
        self.vcs_config = vcs_config
        self.gittool = self.query_exe("gittool.py", return_type="list")

    def ensure_repo_and_revision(self):
        """Makes sure that `dest` is has `revision` or `branch` checked out
        from `repo`.

        Do what it takes to make that happen, including possibly clobbering
        dest.
        """
        c = self.vcs_config
        for conf_item in ("dest", "repo"):
            assert self.vcs_config[conf_item]
        dest = os.path.abspath(c["dest"])
        repo = c["repo"]
        revision = c.get("revision")
        branch = c.get("branch")
        clean = c.get("clean")
        share_base = c.get("vcs_share_base", os.environ.get("GIT_SHARE_BASE_DIR", None))
        env = {"PATH": os.environ.get("PATH")}
        env.update(c.get("env", {}))
        if self._is_windows():
            # git.exe is not in the PATH by default
            env["PATH"] = "%s;C:/mozilla-build/Git/bin" % env["PATH"]
            # SYSTEMROOT is needed for 'import random'
            if "SYSTEMROOT" not in env:
                env["SYSTEMROOT"] = os.environ.get("SYSTEMROOT")
        if share_base is not None:
            env["GIT_SHARE_BASE_DIR"] = share_base

        cmd = self.gittool[:]
        if branch:
            cmd.extend(["-b", branch])
        if revision:
            cmd.extend(["-r", revision])
        if clean:
            cmd.append("--clean")

        for base_mirror_url in self.config.get(
            "gittool_base_mirror_urls", self.config.get("vcs_base_mirror_urls", [])
        ):
            bits = urlparse.urlparse(repo)
            mirror_url = urlparse.urljoin(base_mirror_url, bits.path)
            cmd.extend(["--mirror", mirror_url])

        cmd.extend([repo, dest])
        parser = GittoolParser(
            config=self.config, log_obj=self.log_obj, error_list=GitErrorList
        )
        retval = self.run_command(
            cmd, error_list=GitErrorList, env=env, output_parser=parser
        )

        if retval != 0:
            raise VCSException("Unable to checkout")

        return parser.got_revision