diff options
Diffstat (limited to 'bitbake/lib/bb/fetch2/hg.py')
-rw-r--r-- | bitbake/lib/bb/fetch2/hg.py | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/bitbake/lib/bb/fetch2/hg.py b/bitbake/lib/bb/fetch2/hg.py new file mode 100644 index 0000000000..81592f6e04 --- /dev/null +++ b/bitbake/lib/bb/fetch2/hg.py | |||
@@ -0,0 +1,193 @@ | |||
1 | # ex:ts=4:sw=4:sts=4:et | ||
2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- | ||
3 | """ | ||
4 | BitBake '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 | |||
27 | import os | ||
28 | import sys | ||
29 | import logging | ||
30 | import bb | ||
31 | from bb import data | ||
32 | from bb.fetch2 import FetchMethod | ||
33 | from bb.fetch2 import FetchError | ||
34 | from bb.fetch2 import MissingParameterError | ||
35 | from bb.fetch2 import runfetchcmd | ||
36 | from bb.fetch2 import logger | ||
37 | |||
38 | class Hg(FetchMethod): | ||
39 | """Class to fetch from mercurial repositories""" | ||
40 | def supports(self, ud, d): | ||
41 | """ | ||
42 | Check to see if a given url can be fetched with mercurial. | ||
43 | """ | ||
44 | return ud.type in ['hg'] | ||
45 | |||
46 | def urldata_init(self, ud, d): | ||
47 | """ | ||
48 | init hg specific variable within url data | ||
49 | """ | ||
50 | if not "module" in ud.parm: | ||
51 | raise MissingParameterError('module', ud.url) | ||
52 | |||
53 | ud.module = ud.parm["module"] | ||
54 | |||
55 | # Create paths to mercurial checkouts | ||
56 | relpath = self._strip_leading_slashes(ud.path) | ||
57 | ud.pkgdir = os.path.join(data.expand('${HGDIR}', d), ud.host, relpath) | ||
58 | ud.moddir = os.path.join(ud.pkgdir, ud.module) | ||
59 | |||
60 | ud.setup_revisons(d) | ||
61 | |||
62 | if 'rev' in ud.parm: | ||
63 | ud.revision = ud.parm['rev'] | ||
64 | elif not ud.revision: | ||
65 | ud.revision = self.latest_revision(ud, d) | ||
66 | |||
67 | ud.localfile = data.expand('%s_%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision), d) | ||
68 | |||
69 | def need_update(self, ud, d): | ||
70 | revTag = ud.parm.get('rev', 'tip') | ||
71 | if revTag == "tip": | ||
72 | return True | ||
73 | if not os.path.exists(ud.localpath): | ||
74 | return True | ||
75 | return False | ||
76 | |||
77 | def _buildhgcommand(self, ud, d, command): | ||
78 | """ | ||
79 | Build up an hg commandline based on ud | ||
80 | command is "fetch", "update", "info" | ||
81 | """ | ||
82 | |||
83 | basecmd = data.expand('${FETCHCMD_hg}', d) | ||
84 | |||
85 | proto = ud.parm.get('protocol', 'http') | ||
86 | |||
87 | host = ud.host | ||
88 | if proto == "file": | ||
89 | host = "/" | ||
90 | ud.host = "localhost" | ||
91 | |||
92 | if not ud.user: | ||
93 | hgroot = host + ud.path | ||
94 | else: | ||
95 | if ud.pswd: | ||
96 | hgroot = ud.user + ":" + ud.pswd + "@" + host + ud.path | ||
97 | else: | ||
98 | hgroot = ud.user + "@" + host + ud.path | ||
99 | |||
100 | if command == "info": | ||
101 | return "%s identify -i %s://%s/%s" % (basecmd, proto, hgroot, ud.module) | ||
102 | |||
103 | options = []; | ||
104 | |||
105 | # Don't specify revision for the fetch; clone the entire repo. | ||
106 | # This avoids an issue if the specified revision is a tag, because | ||
107 | # the tag actually exists in the specified revision + 1, so it won't | ||
108 | # be available when used in any successive commands. | ||
109 | if ud.revision and command != "fetch": | ||
110 | options.append("-r %s" % ud.revision) | ||
111 | |||
112 | if command == "fetch": | ||
113 | if ud.user and ud.pswd: | ||
114 | 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" % (basecmd, ud.user, ud.pswd, proto, " ".join(options), proto, hgroot, ud.module, ud.module) | ||
115 | else: | ||
116 | cmd = "%s clone %s %s://%s/%s %s" % (basecmd, " ".join(options), proto, hgroot, ud.module, ud.module) | ||
117 | elif command == "pull": | ||
118 | # do not pass options list; limiting pull to rev causes the local | ||
119 | # repo not to contain it and immediately following "update" command | ||
120 | # will crash | ||
121 | if ud.user and ud.pswd: | ||
122 | cmd = "%s --config auth.default.prefix=* --config auth.default.username=%s --config auth.default.password=%s --config \"auth.default.schemes=%s\" pull" % (basecmd, ud.user, ud.pswd, proto) | ||
123 | else: | ||
124 | cmd = "%s pull" % (basecmd) | ||
125 | elif command == "update": | ||
126 | if ud.user and ud.pswd: | ||
127 | cmd = "%s --config auth.default.prefix=* --config auth.default.username=%s --config auth.default.password=%s --config \"auth.default.schemes=%s\" update -C %s" % (basecmd, ud.user, ud.pswd, proto, " ".join(options)) | ||
128 | else: | ||
129 | cmd = "%s update -C %s" % (basecmd, " ".join(options)) | ||
130 | else: | ||
131 | raise FetchError("Invalid hg command %s" % command, ud.url) | ||
132 | |||
133 | return cmd | ||
134 | |||
135 | def download(self, ud, d): | ||
136 | """Fetch url""" | ||
137 | |||
138 | logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'") | ||
139 | |||
140 | if os.access(os.path.join(ud.moddir, '.hg'), os.R_OK): | ||
141 | updatecmd = self._buildhgcommand(ud, d, "pull") | ||
142 | logger.info("Update " + ud.url) | ||
143 | # update sources there | ||
144 | os.chdir(ud.moddir) | ||
145 | logger.debug(1, "Running %s", updatecmd) | ||
146 | bb.fetch2.check_network_access(d, updatecmd, ud.url) | ||
147 | runfetchcmd(updatecmd, d) | ||
148 | |||
149 | else: | ||
150 | fetchcmd = self._buildhgcommand(ud, d, "fetch") | ||
151 | logger.info("Fetch " + ud.url) | ||
152 | # check out sources there | ||
153 | bb.utils.mkdirhier(ud.pkgdir) | ||
154 | os.chdir(ud.pkgdir) | ||
155 | logger.debug(1, "Running %s", fetchcmd) | ||
156 | bb.fetch2.check_network_access(d, fetchcmd, ud.url) | ||
157 | runfetchcmd(fetchcmd, d) | ||
158 | |||
159 | # Even when we clone (fetch), we still need to update as hg's clone | ||
160 | # won't checkout the specified revision if its on a branch | ||
161 | updatecmd = self._buildhgcommand(ud, d, "update") | ||
162 | os.chdir(ud.moddir) | ||
163 | logger.debug(1, "Running %s", updatecmd) | ||
164 | runfetchcmd(updatecmd, d) | ||
165 | |||
166 | scmdata = ud.parm.get("scmdata", "") | ||
167 | if scmdata == "keep": | ||
168 | tar_flags = "" | ||
169 | else: | ||
170 | tar_flags = "--exclude '.hg' --exclude '.hgrags'" | ||
171 | |||
172 | os.chdir(ud.pkgdir) | ||
173 | runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, ud.module), d, cleanup = [ud.localpath]) | ||
174 | |||
175 | def supports_srcrev(self): | ||
176 | return True | ||
177 | |||
178 | def _latest_revision(self, ud, d, name): | ||
179 | """ | ||
180 | Compute tip revision for the url | ||
181 | """ | ||
182 | bb.fetch2.check_network_access(d, self._buildhgcommand(ud, d, "info")) | ||
183 | output = runfetchcmd(self._buildhgcommand(ud, d, "info"), d) | ||
184 | return output.strip() | ||
185 | |||
186 | def _build_revision(self, ud, d, name): | ||
187 | return ud.revision | ||
188 | |||
189 | def _revision_key(self, ud, d, name): | ||
190 | """ | ||
191 | Return a unique key for the url | ||
192 | """ | ||
193 | return "hg:" + ud.moddir | ||