summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/fetch2/hg.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/bb/fetch2/hg.py')
-rw-r--r--bitbake/lib/bb/fetch2/hg.py181
1 files changed, 181 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..b1c8675dd4
--- /dev/null
+++ b/bitbake/lib/bb/fetch2/hg.py
@@ -0,0 +1,181 @@
1# 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
31from bb import data
32from bb.fetch2 import FetchMethod
33from bb.fetch2 import FetchError
34from bb.fetch2 import MissingParameterError
35from bb.fetch2 import runfetchcmd
36from bb.fetch2 import logger
37
38class Hg(FetchMethod):
39 """Class to fetch from mercurial repositories"""
40 def supports(self, url, 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.url, 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, url, 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 hgroot = ud.user + "@" + host + ud.path
96
97 if command == "info":
98 return "%s identify -i %s://%s/%s" % (basecmd, proto, hgroot, ud.module)
99
100 options = [];
101
102 # Don't specify revision for the fetch; clone the entire repo.
103 # This avoids an issue if the specified revision is a tag, because
104 # the tag actually exists in the specified revision + 1, so it won't
105 # be available when used in any successive commands.
106 if ud.revision and command != "fetch":
107 options.append("-r %s" % ud.revision)
108
109 if command == "fetch":
110 cmd = "%s clone %s %s://%s/%s %s" % (basecmd, " ".join(options), proto, hgroot, ud.module, ud.module)
111 elif command == "pull":
112 # do not pass options list; limiting pull to rev causes the local
113 # repo not to contain it and immediately following "update" command
114 # will crash
115 cmd = "%s pull" % (basecmd)
116 elif command == "update":
117 cmd = "%s update -C %s" % (basecmd, " ".join(options))
118 else:
119 raise FetchError("Invalid hg command %s" % command, ud.url)
120
121 return cmd
122
123 def download(self, loc, ud, d):
124 """Fetch url"""
125
126 logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
127
128 if os.access(os.path.join(ud.moddir, '.hg'), os.R_OK):
129 updatecmd = self._buildhgcommand(ud, d, "pull")
130 logger.info("Update " + loc)
131 # update sources there
132 os.chdir(ud.moddir)
133 logger.debug(1, "Running %s", updatecmd)
134 bb.fetch2.check_network_access(d, updatecmd, ud.url)
135 runfetchcmd(updatecmd, d)
136
137 else:
138 fetchcmd = self._buildhgcommand(ud, d, "fetch")
139 logger.info("Fetch " + loc)
140 # check out sources there
141 bb.utils.mkdirhier(ud.pkgdir)
142 os.chdir(ud.pkgdir)
143 logger.debug(1, "Running %s", fetchcmd)
144 bb.fetch2.check_network_access(d, fetchcmd, ud.url)
145 runfetchcmd(fetchcmd, d)
146
147 # Even when we clone (fetch), we still need to update as hg's clone
148 # won't checkout the specified revision if its on a branch
149 updatecmd = self._buildhgcommand(ud, d, "update")
150 os.chdir(ud.moddir)
151 logger.debug(1, "Running %s", updatecmd)
152 runfetchcmd(updatecmd, d)
153
154 scmdata = ud.parm.get("scmdata", "")
155 if scmdata == "keep":
156 tar_flags = ""
157 else:
158 tar_flags = "--exclude '.hg' --exclude '.hgrags'"
159
160 os.chdir(ud.pkgdir)
161 runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, ud.module), d, cleanup = [ud.localpath])
162
163 def supports_srcrev(self):
164 return True
165
166 def _latest_revision(self, url, ud, d, name):
167 """
168 Compute tip revision for the url
169 """
170 bb.fetch2.check_network_access(d, self._buildhgcommand(ud, d, "info"))
171 output = runfetchcmd(self._buildhgcommand(ud, d, "info"), d)
172 return output.strip()
173
174 def _build_revision(self, url, ud, d, name):
175 return ud.revision
176
177 def _revision_key(self, url, ud, d, name):
178 """
179 Return a unique key for the url
180 """
181 return "hg:" + ud.moddir