fetchers: Cleanup more variables
[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, True), 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         # tag=="master" must always update
86         if (ud.tag != "master") and Fetch.try_mirror(d, ud.localfile):
87             bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists (or was stashed). Skipping git checkout." % ud.localpath)
88             return
89
90         if not os.path.exists(repodir):
91             if Fetch.try_mirror(d, repofilename):    
92                 bb.mkdirhier(repodir)
93                 os.chdir(repodir)
94                 rungitcmd("tar -xzf %s" % (repofile),d)
95             else:
96                 rungitcmd("git clone -n %s://%s%s %s" % (ud.proto, ud.host, ud.path, repodir),d)
97
98         os.chdir(repodir)
99         rungitcmd("git pull %s://%s%s" % (ud.proto, ud.host, ud.path),d)
100         rungitcmd("git pull --tags %s://%s%s" % (ud.proto, ud.host, ud.path),d)
101         rungitcmd("git prune-packed", d)
102         rungitcmd("git pack-redundant --all | xargs -r rm", d)
103         # Remove all but the .git directory
104         rungitcmd("rm * -Rf", d)
105         # old method of downloading tags
106         #rungitcmd("rsync -a --verbose --stats --progress rsync://%s%s/ %s" % (ud.host, ud.path, os.path.join(repodir, ".git", "")),d)
107
108         os.chdir(repodir)
109         bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git repository")
110         rungitcmd("tar -czf %s %s" % (repofile, os.path.join(".", ".git", "*") ),d)
111
112         if os.path.exists(codir):
113             prunedir(codir)
114
115         bb.mkdirhier(codir)
116         os.chdir(repodir)
117         rungitcmd("git read-tree %s" % (ud.tag),d)
118         rungitcmd("git checkout-index -q -f --prefix=%s -a" % (os.path.join(codir, "git", "")),d)
119
120         os.chdir(codir)
121         bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git checkout")
122         rungitcmd("tar -czf %s %s" % (ud.localpath, os.path.join(".", "*") ),d)