diff options
author | Yu Ke <ke.yu@intel.com> | 2011-01-10 14:23:36 +0000 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2011-01-10 14:23:36 +0000 |
commit | 4dccd92439f3b21056c53f2317249228067defe6 (patch) | |
tree | b5507598213381b870f53b3f816102b0b2899b86 /bitbake/lib/bb/fetch2/git.py | |
parent | f46cdcbbae0ac3cbce423d262358e670add6065e (diff) | |
download | poky-4dccd92439f3b21056c53f2317249228067defe6.tar.gz |
bitbake: copy bb.fetch to bb.fetch2 as initial code base for fetcher overhaul
Signed-off-by: Yu Ke <ke.yu@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/fetch2/git.py')
-rw-r--r-- | bitbake/lib/bb/fetch2/git.py | 261 |
1 files changed, 261 insertions, 0 deletions
diff --git a/bitbake/lib/bb/fetch2/git.py b/bitbake/lib/bb/fetch2/git.py new file mode 100644 index 0000000000..de415ec309 --- /dev/null +++ b/bitbake/lib/bb/fetch2/git.py | |||
@@ -0,0 +1,261 @@ | |||
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' git implementation | ||
5 | |||
6 | """ | ||
7 | |||
8 | #Copyright (C) 2005 Richard Purdie | ||
9 | # | ||
10 | # This program is free software; you can redistribute it and/or modify | ||
11 | # it under the terms of the GNU General Public License version 2 as | ||
12 | # published by the Free Software Foundation. | ||
13 | # | ||
14 | # This program is distributed in the hope that it will be useful, | ||
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | # GNU General Public License for more details. | ||
18 | # | ||
19 | # You should have received a copy of the GNU General Public License along | ||
20 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
21 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
22 | |||
23 | import os | ||
24 | import bb | ||
25 | from bb import data | ||
26 | from bb.fetch import Fetch | ||
27 | from bb.fetch import runfetchcmd | ||
28 | from bb.fetch import logger | ||
29 | |||
30 | class Git(Fetch): | ||
31 | """Class to fetch a module or modules from git repositories""" | ||
32 | def init(self, d): | ||
33 | # | ||
34 | # Only enable _sortable revision if the key is set | ||
35 | # | ||
36 | if bb.data.getVar("BB_GIT_CLONE_FOR_SRCREV", d, True): | ||
37 | self._sortable_buildindex = self._sortable_buildindex_disabled | ||
38 | def supports(self, url, ud, d): | ||
39 | """ | ||
40 | Check to see if a given url can be fetched with git. | ||
41 | """ | ||
42 | return ud.type in ['git'] | ||
43 | |||
44 | def localpath(self, url, ud, d): | ||
45 | |||
46 | if 'protocol' in ud.parm: | ||
47 | ud.proto = ud.parm['protocol'] | ||
48 | elif not ud.host: | ||
49 | ud.proto = 'file' | ||
50 | else: | ||
51 | ud.proto = "rsync" | ||
52 | |||
53 | ud.branch = ud.parm.get("branch", "master") | ||
54 | |||
55 | gitsrcname = '%s%s' % (ud.host, ud.path.replace('/', '.')) | ||
56 | ud.mirrortarball = 'git_%s.tar.gz' % (gitsrcname) | ||
57 | ud.clonedir = os.path.join(data.expand('${GITDIR}', d), gitsrcname) | ||
58 | |||
59 | tag = Fetch.srcrev_internal_helper(ud, d) | ||
60 | if tag is True: | ||
61 | ud.tag = self.latest_revision(url, ud, d) | ||
62 | elif tag: | ||
63 | ud.tag = tag | ||
64 | |||
65 | if not ud.tag or ud.tag == "master": | ||
66 | ud.tag = self.latest_revision(url, ud, d) | ||
67 | |||
68 | subdir = ud.parm.get("subpath", "") | ||
69 | if subdir != "": | ||
70 | if subdir.endswith("/"): | ||
71 | subdir = subdir[:-1] | ||
72 | subdirpath = os.path.join(ud.path, subdir); | ||
73 | else: | ||
74 | subdirpath = ud.path; | ||
75 | |||
76 | if 'fullclone' in ud.parm: | ||
77 | ud.localfile = ud.mirrortarball | ||
78 | else: | ||
79 | ud.localfile = data.expand('git_%s%s_%s.tar.gz' % (ud.host, subdirpath.replace('/', '.'), ud.tag), d) | ||
80 | |||
81 | ud.basecmd = data.getVar("FETCHCMD_git", d, True) or "git" | ||
82 | |||
83 | if 'noclone' in ud.parm: | ||
84 | ud.localfile = None | ||
85 | return None | ||
86 | |||
87 | return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile) | ||
88 | |||
89 | def forcefetch(self, url, ud, d): | ||
90 | if 'fullclone' in ud.parm: | ||
91 | return True | ||
92 | if 'noclone' in ud.parm: | ||
93 | return False | ||
94 | if os.path.exists(ud.localpath): | ||
95 | return False | ||
96 | if not self._contains_ref(ud.tag, d): | ||
97 | return True | ||
98 | return False | ||
99 | |||
100 | def try_premirror(self, u, ud, d): | ||
101 | if 'noclone' in ud.parm: | ||
102 | return False | ||
103 | if os.path.exists(ud.clonedir): | ||
104 | return False | ||
105 | if os.path.exists(ud.localpath): | ||
106 | return False | ||
107 | |||
108 | return True | ||
109 | |||
110 | def go(self, loc, ud, d): | ||
111 | """Fetch url""" | ||
112 | |||
113 | if ud.user: | ||
114 | username = ud.user + '@' | ||
115 | else: | ||
116 | username = "" | ||
117 | |||
118 | repofile = os.path.join(data.getVar("DL_DIR", d, 1), ud.mirrortarball) | ||
119 | |||
120 | coname = '%s' % (ud.tag) | ||
121 | codir = os.path.join(ud.clonedir, coname) | ||
122 | |||
123 | # If we have no existing clone and no mirror tarball, try and obtain one | ||
124 | if not os.path.exists(ud.clonedir) and not os.path.exists(repofile): | ||
125 | try: | ||
126 | Fetch.try_mirrors(ud.mirrortarball) | ||
127 | except: | ||
128 | pass | ||
129 | |||
130 | # If the checkout doesn't exist and the mirror tarball does, extract it | ||
131 | if not os.path.exists(ud.clonedir) and os.path.exists(repofile): | ||
132 | bb.mkdirhier(ud.clonedir) | ||
133 | os.chdir(ud.clonedir) | ||
134 | runfetchcmd("tar -xzf %s" % (repofile), d) | ||
135 | |||
136 | # If the repo still doesn't exist, fallback to cloning it | ||
137 | if not os.path.exists(ud.clonedir): | ||
138 | runfetchcmd("%s clone -n %s://%s%s%s %s" % (ud.basecmd, ud.proto, username, ud.host, ud.path, ud.clonedir), d) | ||
139 | |||
140 | os.chdir(ud.clonedir) | ||
141 | # Update the checkout if needed | ||
142 | if not self._contains_ref(ud.tag, d) or 'fullclone' in ud.parm: | ||
143 | # Remove all but the .git directory | ||
144 | runfetchcmd("rm * -Rf", d) | ||
145 | if 'fullclone' in ud.parm: | ||
146 | runfetchcmd("%s fetch --all" % (ud.basecmd), d) | ||
147 | else: | ||
148 | runfetchcmd("%s fetch %s://%s%s%s %s" % (ud.basecmd, ud.proto, username, ud.host, ud.path, ud.branch), d) | ||
149 | runfetchcmd("%s fetch --tags %s://%s%s%s" % (ud.basecmd, ud.proto, username, ud.host, ud.path), d) | ||
150 | runfetchcmd("%s prune-packed" % ud.basecmd, d) | ||
151 | runfetchcmd("%s pack-redundant --all | xargs -r rm" % ud.basecmd, d) | ||
152 | |||
153 | # Generate a mirror tarball if needed | ||
154 | os.chdir(ud.clonedir) | ||
155 | mirror_tarballs = data.getVar("BB_GENERATE_MIRROR_TARBALLS", d, True) | ||
156 | if mirror_tarballs != "0" or 'fullclone' in ud.parm: | ||
157 | logger.info("Creating tarball of git repository") | ||
158 | runfetchcmd("tar -czf %s %s" % (repofile, os.path.join(".", ".git", "*") ), d) | ||
159 | |||
160 | if 'fullclone' in ud.parm: | ||
161 | return | ||
162 | |||
163 | if os.path.exists(codir): | ||
164 | bb.utils.prunedir(codir) | ||
165 | |||
166 | subdir = ud.parm.get("subpath", "") | ||
167 | if subdir != "": | ||
168 | if subdir.endswith("/"): | ||
169 | subdirbase = os.path.basename(subdir[:-1]) | ||
170 | else: | ||
171 | subdirbase = os.path.basename(subdir) | ||
172 | else: | ||
173 | subdirbase = "" | ||
174 | |||
175 | if subdir != "": | ||
176 | readpathspec = ":%s" % (subdir) | ||
177 | codir = os.path.join(codir, "git") | ||
178 | coprefix = os.path.join(codir, subdirbase, "") | ||
179 | else: | ||
180 | readpathspec = "" | ||
181 | coprefix = os.path.join(codir, "git", "") | ||
182 | |||
183 | scmdata = ud.parm.get("scmdata", "") | ||
184 | if scmdata == "keep": | ||
185 | runfetchcmd("%s clone -n %s %s" % (ud.basecmd, ud.clonedir, coprefix), d) | ||
186 | os.chdir(coprefix) | ||
187 | runfetchcmd("%s checkout -q -f %s%s" % (ud.basecmd, ud.tag, readpathspec), d) | ||
188 | else: | ||
189 | bb.mkdirhier(codir) | ||
190 | os.chdir(ud.clonedir) | ||
191 | runfetchcmd("%s read-tree %s%s" % (ud.basecmd, ud.tag, readpathspec), d) | ||
192 | runfetchcmd("%s checkout-index -q -f --prefix=%s -a" % (ud.basecmd, coprefix), d) | ||
193 | |||
194 | os.chdir(codir) | ||
195 | logger.info("Creating tarball of git checkout") | ||
196 | runfetchcmd("tar -czf %s %s" % (ud.localpath, os.path.join(".", "*") ), d) | ||
197 | |||
198 | os.chdir(ud.clonedir) | ||
199 | bb.utils.prunedir(codir) | ||
200 | |||
201 | def supports_srcrev(self): | ||
202 | return True | ||
203 | |||
204 | def _contains_ref(self, tag, d): | ||
205 | basecmd = data.getVar("FETCHCMD_git", d, True) or "git" | ||
206 | output = runfetchcmd("%s log --pretty=oneline -n 1 %s -- 2> /dev/null | wc -l" % (basecmd, tag), d, quiet=True) | ||
207 | return output.split()[0] != "0" | ||
208 | |||
209 | def _revision_key(self, url, ud, d): | ||
210 | """ | ||
211 | Return a unique key for the url | ||
212 | """ | ||
213 | return "git:" + ud.host + ud.path.replace('/', '.') + ud.branch | ||
214 | |||
215 | def _latest_revision(self, url, ud, d): | ||
216 | """ | ||
217 | Compute the HEAD revision for the url | ||
218 | """ | ||
219 | if ud.user: | ||
220 | username = ud.user + '@' | ||
221 | else: | ||
222 | username = "" | ||
223 | |||
224 | basecmd = data.getVar("FETCHCMD_git", d, True) or "git" | ||
225 | cmd = "%s ls-remote %s://%s%s%s %s" % (basecmd, ud.proto, username, ud.host, ud.path, ud.branch) | ||
226 | output = runfetchcmd(cmd, d, True) | ||
227 | if not output: | ||
228 | raise bb.fetch.FetchError("Fetch command %s gave empty output\n" % (cmd)) | ||
229 | return output.split()[0] | ||
230 | |||
231 | def _build_revision(self, url, ud, d): | ||
232 | return ud.tag | ||
233 | |||
234 | def _sortable_buildindex_disabled(self, url, ud, d, rev): | ||
235 | """ | ||
236 | Return a suitable buildindex for the revision specified. This is done by counting revisions | ||
237 | using "git rev-list" which may or may not work in different circumstances. | ||
238 | """ | ||
239 | |||
240 | cwd = os.getcwd() | ||
241 | |||
242 | # Check if we have the rev already | ||
243 | |||
244 | if not os.path.exists(ud.clonedir): | ||
245 | print("no repo") | ||
246 | self.go(None, ud, d) | ||
247 | if not os.path.exists(ud.clonedir): | ||
248 | logger.error("GIT repository for %s doesn't exist in %s, cannot get sortable buildnumber, using old value", url, ud.clonedir) | ||
249 | return None | ||
250 | |||
251 | |||
252 | os.chdir(ud.clonedir) | ||
253 | if not self._contains_ref(rev, d): | ||
254 | self.go(None, ud, d) | ||
255 | |||
256 | output = runfetchcmd("%s rev-list %s -- 2> /dev/null | wc -l" % (ud.basecmd, rev), d, quiet=True) | ||
257 | os.chdir(cwd) | ||
258 | |||
259 | buildindex = "%s" % output.split()[0] | ||
260 | logger.debug(1, "GIT repository for %s in %s is returning %s revisions in rev-list before %s", url, ud.clonedir, buildindex, rev) | ||
261 | return buildindex | ||