4987e87333a8f6b992121f66bdd14a6c02d2a18f
[vuplus_bitbake] / lib / bb / fetch / git.py
1 #!/usr/bin/env python
2 # ex:ts=4:sw=4:sts=4:et
3 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4 """
5 BitBake 'Fetch' git implementation
6
7 Copyright (C) 2005 Richard Purdie
8
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
12 version.
13
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 Place, Suite 330, Boston, MA 02111-1307 USA. 
21 """
22
23 import os, re
24 import bb
25 from   bb    import data
26 from   bb.fetch import Fetch
27 from   bb.fetch import FetchError
28
29 def prunedir(topdir):
30     # Delete everything reachable from the directory named in 'topdir'.
31     # CAUTION:  This is dangerous!
32     for root, dirs, files in os.walk(topdir, topdown=False):
33         for name in files:
34             os.remove(os.path.join(root, name))
35         for name in dirs:
36             os.rmdir(os.path.join(root, name))
37
38 def rungitcmd(cmd,d):
39
40     bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s" % cmd)
41
42     # Need to export PATH as git is likely to be in metadata paths 
43     # rather than host provided
44     pathcmd = 'export PATH=%s; %s' % (data.expand('${PATH}', d), cmd)
45
46     myret = os.system(pathcmd)
47
48     if myret != 0:
49         raise FetchError("Git: %s failed" % pathcmd)
50
51 class Git(Fetch):
52     """Class to fetch a module or modules from git repositories"""
53     def supports(self, url, ud, d):
54         """
55         Check to see if a given url can be fetched with cvs.
56         """
57         return ud.type in ['git']
58
59     def localpath(self, url, ud, d):
60
61         ud.proto = "rsync"
62         if 'protocol' in ud.parm:
63             ud.proto = ud.parm['protocol']
64
65         ud.tag = "master"
66         if 'tag' in ud.parm:
67             ud.tag = ud.parm['tag']
68
69         ud.localfile = data.expand('git_%s%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.tag), d)
70
71         return os.path.join(data.getVar("DL_DIR", d, 1), ud.localfile)
72
73     def go(self, loc, ud, d):
74         """Fetch url"""
75
76         gitsrcname = '%s%s' % (ud.host, path.replace('/', '.'))
77
78         repofilename = 'git_%s.tar.gz' % (gitsrcname)
79         repofile = os.path.join(data.getVar("DL_DIR", d, 1), repofilename)
80         repodir = os.path.join(data.expand('${GITDIR}', d), gitsrcname)
81
82         coname = '%s' % (ud.tag)
83         codir = os.path.join(repodir, coname)
84
85         cofile = ud.localpath
86
87         # tag=="master" must always update
88         if (ud.tag != "master") and Fetch.try_mirror(d, ud.localfile):
89             bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists (or was stashed). Skipping git checkout." % cofile)
90             return
91
92         if not os.path.exists(repodir):
93             if Fetch.try_mirror(d, repofilename):    
94                 bb.mkdirhier(repodir)
95                 os.chdir(repodir)
96                 rungitcmd("tar -xzf %s" % (repofile),d)
97             else:
98                 rungitcmd("git clone -n %s://%s%s %s" % (ud.proto, ud.host, ud.path, repodir),d)
99
100         os.chdir(repodir)
101         rungitcmd("git pull %s://%s%s" % (ud.proto, ud.host, ud.path),d)
102         rungitcmd("git pull --tags %s://%s%s" % (ud.proto, ud.host, ud.path),d)
103         rungitcmd("git prune-packed", d)
104         rungitcmd("git pack-redundant --all | xargs -r rm", d)
105         # Remove all but the .git directory
106         rungitcmd("rm * -Rf", d)
107         # old method of downloading tags
108         #rungitcmd("rsync -a --verbose --stats --progress rsync://%s%s/ %s" % (ud.host, ud.path, os.path.join(repodir, ".git", "")),d)
109
110         os.chdir(repodir)
111         bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git repository")
112         rungitcmd("tar -czf %s %s" % (repofile, os.path.join(".", ".git", "*") ),d)
113
114         if os.path.exists(codir):
115             prunedir(codir)
116
117         bb.mkdirhier(codir)
118         os.chdir(repodir)
119         rungitcmd("git read-tree %s" % (ud.tag),d)
120         rungitcmd("git checkout-index -q -f --prefix=%s -a" % (os.path.join(codir, "git", "")),d)
121
122         os.chdir(codir)
123         bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git checkout")
124         rungitcmd("tar -czf %s %s" % (cofile, os.path.join(".", "*") ),d)