aboutsummaryrefslogtreecommitdiff
blob: 486b4fdd699d8bac6dcde076ae9cf30e9512d451 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright 2018-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import logging
import subprocess

import portage
from portage import os
from portage.util import writemsg_level, shlex_split

from portage.sync.syncbase import NewBase


class MercurialSync(NewBase):
    """Mercurial sync class"""

    short_desc = "Perform sync operations on mercurial based repositories"

    @staticmethod
    def name():
        return "MercurialSync"

    def __init__(self):
        NewBase.__init__(self, "hg", portage.const.HG_PACKAGE_ATOM)

    def exists(self, **kwargs):
        """Tests whether the repo actually exists"""
        return os.path.exists(os.path.join(self.repo.location, ".hg"))

    def new(self, **kwargs):
        """Do the initial clone of the repository"""
        if kwargs:
            self._kwargs(kwargs)
        try:
            if not os.path.exists(self.repo.location):
                os.makedirs(self.repo.location)
                self.logger(
                    self.xterm_titles, "Created new directory %s" % self.repo.location
                )
        except IOError:
            return (1, False)

        sync_uri = self.repo.sync_uri
        if sync_uri.startswith("file://"):
            sync_uri = sync_uri[7:]

        hg_cmd_opts = ""
        if self.repo.module_specific_options.get("sync-mercurial-env"):
            shlexed_env = shlex_split(
                self.repo.module_specific_options["sync-mercurial-env"]
            )
            env = dict(
                (k, v)
                for k, _, v in (assignment.partition("=") for assignment in shlexed_env)
                if k
            )
            self.spawn_kwargs["env"].update(env)

        if self.repo.module_specific_options.get("sync-mercurial-clone-env"):
            shlexed_env = shlex_split(
                self.repo.module_specific_options["sync-mercurial-clone-env"]
            )
            clone_env = dict(
                (k, v)
                for k, _, v in (assignment.partition("=") for assignment in shlexed_env)
                if k
            )
            self.spawn_kwargs["env"].update(clone_env)

        if self.settings.get("PORTAGE_QUIET") == "1":
            hg_cmd_opts += " --quiet"
        if self.repo.module_specific_options.get("sync-mercurial-clone-extra-opts"):
            hg_cmd_opts += (
                " %s"
                % self.repo.module_specific_options["sync-mercurial-clone-extra-opts"]
            )
        hg_cmd = "%s clone%s %s ." % (
            self.bin_command,
            hg_cmd_opts,
            portage._shell_quote(sync_uri),
        )
        writemsg_level(hg_cmd + "\n")

        exitcode = portage.process.spawn(
            shlex_split(hg_cmd),
            cwd=portage._unicode_encode(self.repo.location),
            **self.spawn_kwargs
        )
        if exitcode != os.EX_OK:
            msg = "!!! hg clone error in %s" % self.repo.location
            self.logger(self.xterm_titles, msg)
            writemsg_level(msg + "\n", level=logging.ERROR, noiselevel=-1)
            return (exitcode, False)
        return (os.EX_OK, True)

    def update(self):
        """Update existing mercurial repository, and ignore the syncuri. We are
        going to trust the user and assume that the user is in the branch
        that he/she wants updated. We'll let the user manage branches with
        hg directly.
        """

        hg_cmd_opts = ""
        if self.repo.module_specific_options.get("sync-mercurial-env"):
            shlexed_env = shlex_split(
                self.repo.module_specific_options["sync-mercurial-env"]
            )
            env = dict(
                (k, v)
                for k, _, v in (assignment.partition("=") for assignment in shlexed_env)
                if k
            )
            self.spawn_kwargs["env"].update(env)

        if self.repo.module_specific_options.get("sync-mercurial-pull-env"):
            shlexed_env = shlex_split(
                self.repo.module_specific_options["sync-mercurial-pull-env"]
            )
            pull_env = dict(
                (k, v)
                for k, _, v in (assignment.partition("=") for assignment in shlexed_env)
                if k
            )
            self.spawn_kwargs["env"].update(pull_env)

        if self.settings.get("PORTAGE_QUIET") == "1":
            hg_cmd_opts += " --quiet"
        if self.repo.module_specific_options.get("sync-mercurial-pull-extra-opts"):
            hg_cmd_opts += (
                " %s"
                % self.repo.module_specific_options["sync-mercurial-pull-extra-opts"]
            )
        hg_cmd = "%s pull -u%s" % (self.bin_command, hg_cmd_opts)
        writemsg_level(hg_cmd + "\n")

        rev_cmd = [self.bin_command, "id", "--id", "--rev", "tip"]
        previous_rev = subprocess.check_output(
            rev_cmd, cwd=portage._unicode_encode(self.repo.location)
        )

        exitcode = portage.process.spawn(
            shlex_split(hg_cmd),
            cwd=portage._unicode_encode(self.repo.location),
            **self.spawn_kwargs
        )
        if exitcode != os.EX_OK:
            msg = "!!! hg pull error in %s" % self.repo.location
            self.logger(self.xterm_titles, msg)
            writemsg_level(msg + "\n", level=logging.ERROR, noiselevel=-1)
            return (exitcode, False)

        current_rev = subprocess.check_output(
            rev_cmd, cwd=portage._unicode_encode(self.repo.location)
        )

        return (os.EX_OK, current_rev != previous_rev)

    def retrieve_head(self, **kwargs):
        """Get information about the head commit"""
        if kwargs:
            self._kwargs(kwargs)
        rev_cmd = [self.bin_command, "id", "--id", "--rev", "tip"]
        try:
            ret = (
                os.EX_OK,
                portage._unicode_decode(
                    subprocess.check_output(
                        rev_cmd, cwd=portage._unicode_encode(self.repo.location)
                    )
                ),
            )
        except subprocess.CalledProcessError:
            ret = (1, False)
        return ret