blob: 3b743ff51d4b5b58a244f6548e21880cbf8c7949 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3"""
4BitBake 'Fetch' implementation for mercurial DRCS (hg).
5
6"""
7
8# Copyright (C) 2003, 2004 Chris Larson
9# Copyright (C) 2004 Marcin Juszkiewicz
10# Copyright (C) 2007 Robert Schuster
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License version 2 as
14# published by the Free Software Foundation.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License along
22# with this program; if not, write to the Free Software Foundation, Inc.,
23# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24#
25# Based on functions from the base bb module, Copyright 2003 Holger Schurig
26
27import os
28import sys
29import logging
30import bb
Patrick Williamsf1e5d692016-03-30 15:21:19 -050031import errno
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032from bb import data
33from bb.fetch2 import FetchMethod
34from bb.fetch2 import FetchError
35from bb.fetch2 import MissingParameterError
36from bb.fetch2 import runfetchcmd
37from bb.fetch2 import logger
38
39class Hg(FetchMethod):
40 """Class to fetch from mercurial repositories"""
41 def supports(self, ud, d):
42 """
43 Check to see if a given url can be fetched with mercurial.
44 """
45 return ud.type in ['hg']
46
47 def supports_checksum(self, urldata):
48 """
49 Don't require checksums for local archives created from
50 repository checkouts.
51 """
52 return False
53
54 def urldata_init(self, ud, d):
55 """
56 init hg specific variable within url data
57 """
58 if not "module" in ud.parm:
59 raise MissingParameterError('module', ud.url)
60
61 ud.module = ud.parm["module"]
62
63 if 'protocol' in ud.parm:
64 ud.proto = ud.parm['protocol']
65 elif not ud.host:
66 ud.proto = 'file'
67 else:
68 ud.proto = "hg"
69
70 ud.setup_revisons(d)
71
72 if 'rev' in ud.parm:
73 ud.revision = ud.parm['rev']
74 elif not ud.revision:
75 ud.revision = self.latest_revision(ud, d)
76
77 # Create paths to mercurial checkouts
78 hgsrcname = '%s_%s_%s' % (ud.module.replace('/', '.'), \
79 ud.host, ud.path.replace('/', '.'))
80 ud.mirrortarball = 'hg_%s.tar.gz' % hgsrcname
81 ud.fullmirror = os.path.join(d.getVar("DL_DIR", True), ud.mirrortarball)
82
83 hgdir = d.getVar("HGDIR", True) or (d.getVar("DL_DIR", True) + "/hg/")
84 ud.pkgdir = os.path.join(hgdir, hgsrcname)
85 ud.moddir = os.path.join(ud.pkgdir, ud.module)
86 ud.localfile = ud.moddir
87 ud.basecmd = data.getVar("FETCHCMD_hg", d, True) or "/usr/bin/env hg"
88
89 ud.write_tarballs = d.getVar("BB_GENERATE_MIRROR_TARBALLS", True)
90
91 def need_update(self, ud, d):
92 revTag = ud.parm.get('rev', 'tip')
93 if revTag == "tip":
94 return True
95 if not os.path.exists(ud.localpath):
96 return True
97 return False
98
99 def try_premirror(self, ud, d):
100 # If we don't do this, updating an existing checkout with only premirrors
101 # is not possible
102 if d.getVar("BB_FETCH_PREMIRRORONLY", True) is not None:
103 return True
104 if os.path.exists(ud.moddir):
105 return False
106 return True
107
108 def _buildhgcommand(self, ud, d, command):
109 """
110 Build up an hg commandline based on ud
111 command is "fetch", "update", "info"
112 """
113
114 proto = ud.parm.get('protocol', 'http')
115
116 host = ud.host
117 if proto == "file":
118 host = "/"
119 ud.host = "localhost"
120
121 if not ud.user:
122 hgroot = host + ud.path
123 else:
124 if ud.pswd:
125 hgroot = ud.user + ":" + ud.pswd + "@" + host + ud.path
126 else:
127 hgroot = ud.user + "@" + host + ud.path
128
129 if command == "info":
130 return "%s identify -i %s://%s/%s" % (ud.basecmd, proto, hgroot, ud.module)
131
132 options = [];
133
134 # Don't specify revision for the fetch; clone the entire repo.
135 # This avoids an issue if the specified revision is a tag, because
136 # the tag actually exists in the specified revision + 1, so it won't
137 # be available when used in any successive commands.
138 if ud.revision and command != "fetch":
139 options.append("-r %s" % ud.revision)
140
141 if command == "fetch":
142 if ud.user and ud.pswd:
143 cmd = "%s --config auth.default.prefix=* --config auth.default.username=%s --config auth.default.password=%s --config \"auth.default.schemes=%s\" clone %s %s://%s/%s %s" % (ud.basecmd, ud.user, ud.pswd, proto, " ".join(options), proto, hgroot, ud.module, ud.module)
144 else:
145 cmd = "%s clone %s %s://%s/%s %s" % (ud.basecmd, " ".join(options), proto, hgroot, ud.module, ud.module)
146 elif command == "pull":
147 # do not pass options list; limiting pull to rev causes the local
148 # repo not to contain it and immediately following "update" command
149 # will crash
150 if ud.user and ud.pswd:
151 cmd = "%s --config auth.default.prefix=* --config auth.default.username=%s --config auth.default.password=%s --config \"auth.default.schemes=%s\" pull" % (ud.basecmd, ud.user, ud.pswd, proto)
152 else:
153 cmd = "%s pull" % (ud.basecmd)
154 elif command == "update":
155 if ud.user and ud.pswd:
156 cmd = "%s --config auth.default.prefix=* --config auth.default.username=%s --config auth.default.password=%s --config \"auth.default.schemes=%s\" update -C %s" % (ud.basecmd, ud.user, ud.pswd, proto, " ".join(options))
157 else:
158 cmd = "%s update -C %s" % (ud.basecmd, " ".join(options))
159 else:
160 raise FetchError("Invalid hg command %s" % command, ud.url)
161
162 return cmd
163
164 def download(self, ud, d):
165 """Fetch url"""
166
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167 logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
168
169 # If the checkout doesn't exist and the mirror tarball does, extract it
170 if not os.path.exists(ud.pkgdir) and os.path.exists(ud.fullmirror):
171 bb.utils.mkdirhier(ud.pkgdir)
172 os.chdir(ud.pkgdir)
173 runfetchcmd("tar -xzf %s" % (ud.fullmirror), d)
174
175 if os.access(os.path.join(ud.moddir, '.hg'), os.R_OK):
176 # Found the source, check whether need pull
177 updatecmd = self._buildhgcommand(ud, d, "update")
178 os.chdir(ud.moddir)
179 logger.debug(1, "Running %s", updatecmd)
180 try:
181 runfetchcmd(updatecmd, d)
182 except bb.fetch2.FetchError:
183 # Runnning pull in the repo
184 pullcmd = self._buildhgcommand(ud, d, "pull")
185 logger.info("Pulling " + ud.url)
186 # update sources there
187 os.chdir(ud.moddir)
188 logger.debug(1, "Running %s", pullcmd)
189 bb.fetch2.check_network_access(d, pullcmd, ud.url)
190 runfetchcmd(pullcmd, d)
Patrick Williamsd7e96312015-09-22 08:09:05 -0500191 try:
192 os.unlink(ud.fullmirror)
193 except OSError as exc:
194 if exc.errno != errno.ENOENT:
195 raise
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196
197 # No source found, clone it.
198 if not os.path.exists(ud.moddir):
199 fetchcmd = self._buildhgcommand(ud, d, "fetch")
200 logger.info("Fetch " + ud.url)
201 # check out sources there
202 bb.utils.mkdirhier(ud.pkgdir)
203 os.chdir(ud.pkgdir)
204 logger.debug(1, "Running %s", fetchcmd)
205 bb.fetch2.check_network_access(d, fetchcmd, ud.url)
206 runfetchcmd(fetchcmd, d)
207
208 # Even when we clone (fetch), we still need to update as hg's clone
209 # won't checkout the specified revision if its on a branch
210 updatecmd = self._buildhgcommand(ud, d, "update")
211 os.chdir(ud.moddir)
212 logger.debug(1, "Running %s", updatecmd)
213 runfetchcmd(updatecmd, d)
214
215 def clean(self, ud, d):
216 """ Clean the hg dir """
217
218 bb.utils.remove(ud.localpath, True)
219 bb.utils.remove(ud.fullmirror)
220 bb.utils.remove(ud.fullmirror + ".done")
221
222 def supports_srcrev(self):
223 return True
224
225 def _latest_revision(self, ud, d, name):
226 """
227 Compute tip revision for the url
228 """
229 bb.fetch2.check_network_access(d, self._buildhgcommand(ud, d, "info"))
230 output = runfetchcmd(self._buildhgcommand(ud, d, "info"), d)
231 return output.strip()
232
233 def _build_revision(self, ud, d, name):
234 return ud.revision
235
236 def _revision_key(self, ud, d, name):
237 """
238 Return a unique key for the url
239 """
240 return "hg:" + ud.moddir
241
242 def build_mirror_data(self, ud, d):
243 # Generate a mirror tarball if needed
Patrick Williamsd7e96312015-09-22 08:09:05 -0500244 if ud.write_tarballs == "1" and not os.path.exists(ud.fullmirror):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500245 # it's possible that this symlink points to read-only filesystem with PREMIRROR
246 if os.path.islink(ud.fullmirror):
247 os.unlink(ud.fullmirror)
248
249 os.chdir(ud.pkgdir)
250 logger.info("Creating tarball of hg repository")
251 runfetchcmd("tar -czf %s %s" % (ud.fullmirror, ud.module), d)
252 runfetchcmd("touch %s.done" % (ud.fullmirror), d)
253
254 def localpath(self, ud, d):
255 return ud.pkgdir
256
257 def unpack(self, ud, destdir, d):
258 """
259 Make a local clone or export for the url
260 """
261
262 revflag = "-r %s" % ud.revision
263 subdir = ud.parm.get("destsuffix", ud.module)
264 codir = "%s/%s" % (destdir, subdir)
265
266 scmdata = ud.parm.get("scmdata", "")
267 if scmdata != "nokeep":
268 if not os.access(os.path.join(codir, '.hg'), os.R_OK):
269 logger.debug(2, "Unpack: creating new hg repository in '" + codir + "'")
270 runfetchcmd("%s init %s" % (ud.basecmd, codir), d)
271 logger.debug(2, "Unpack: updating source in '" + codir + "'")
272 os.chdir(codir)
273 runfetchcmd("%s pull %s" % (ud.basecmd, ud.moddir), d)
274 runfetchcmd("%s up -C %s" % (ud.basecmd, revflag), d)
275 else:
276 logger.debug(2, "Unpack: extracting source to '" + codir + "'")
277 os.chdir(ud.moddir)
278 runfetchcmd("%s archive -t files %s %s" % (ud.basecmd, revflag, codir), d)