Convert fetchers to use bb.msg
[vuplus_bitbake] / lib / bb / fetch / svn.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' implementations
6
7 Classes for obtaining upstream sources for the
8 BitBake build tools.
9
10 Copyright (C) 2003, 2004  Chris Larson
11
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free Software
14 Foundation; either version 2 of the License, or (at your option) any later
15 version.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 USA. 
24
25 Based on functions from the base bb module, Copyright 2003 Holger Schurig
26 """
27
28 import os, re
29 import sys
30 import bb
31 from   bb import data
32 from   bb.fetch import Fetch
33 from   bb.fetch import FetchError
34 from   bb.fetch import MissingParameterError
35
36 class Svn(Fetch):
37     """Class to fetch a module or modules from svn repositories"""
38     def supports(url, d):
39         """Check to see if a given url can be fetched with svn.
40            Expects supplied url in list form, as outputted by bb.decodeurl().
41         """
42         (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
43         return type in ['svn']
44     supports = staticmethod(supports)
45
46     def localpath(url, d):
47         (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
48         if "localpath" in parm:
49 #           if user overrides local path, use it.
50             return parm["localpath"]
51
52         if not "module" in parm:
53             raise MissingParameterError("svn method needs a 'module' parameter")
54         else:
55             module = parm["module"]
56         if 'rev' in parm:
57             revision = parm['rev']
58         else:
59             revision = ""
60
61         date = Fetch.getSRCDate(d)
62
63         return os.path.join(data.getVar("DL_DIR", d, 1),data.expand('%s_%s_%s_%s_%s.tar.gz' % ( module.replace('/', '.'), host, path.replace('/', '.'), revision, date), d))
64     localpath = staticmethod(localpath)
65
66     def go(self, d, urls = []):
67         """Fetch urls"""
68         if not urls:
69             urls = self.urls
70
71         localdata = data.createCopy(d)
72         data.setVar('OVERRIDES', "svn:%s" % data.getVar('OVERRIDES', localdata), localdata)
73         data.update_data(localdata)
74
75         for loc in urls:
76             (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(loc, localdata))
77             if not "module" in parm:
78                 raise MissingParameterError("svn method needs a 'module' parameter")
79             else:
80                 module = parm["module"]
81
82             dlfile = self.localpath(loc, localdata)
83             dldir = data.getVar('DL_DIR', localdata, 1)
84 #           if local path contains the svn
85 #           module, consider the dir above it to be the
86 #           download directory
87 #           pos = dlfile.find(module)
88 #           if pos:
89 #               dldir = dlfile[:pos]
90 #           else:
91 #               dldir = os.path.dirname(dlfile)
92
93 #           setup svn options
94             options = []
95             if 'rev' in parm:
96                 revision = parm['rev']
97             else:
98                 revision = ""
99
100             date = Fetch.getSRCDate(d)
101
102             if "proto" in parm:
103                 proto = parm["proto"]
104             else:
105                 proto = "svn"
106
107             svn_rsh = None
108             if proto == "svn+ssh" and "rsh" in parm:
109                 svn_rsh = parm["rsh"]
110
111             tarfn = data.expand('%s_%s_%s_%s_%s.tar.gz' % (module.replace('/', '.'), host, path.replace('/', '.'), revision, date), localdata)
112             data.setVar('TARFILES', dlfile, localdata)
113             data.setVar('TARFN', tarfn, localdata)
114
115             # try to use the tarball stash
116             if Fetch.check_for_tarball(d, tarfn, dldir, date):
117                 bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists or was mirrored, skipping svn checkout." % tarfn)
118                 continue
119
120             olddir = os.path.abspath(os.getcwd())
121             os.chdir(data.expand(dldir, localdata))
122
123             svnroot = host + path
124
125             # either use the revision, or SRCDATE in braces, or nothing for SRCDATE = "now"
126             if revision:
127                 options.append("-r %s" % revision)
128             elif date != "now":
129                 options.append("-r {%s}" % date)
130
131             data.setVar('SVNROOT', "%s://%s/%s" % (proto, svnroot, module), localdata)
132             data.setVar('SVNCOOPTS', " ".join(options), localdata)
133             data.setVar('SVNMODULE', module, localdata)
134             svncmd = data.getVar('FETCHCOMMAND', localdata, 1)
135             svnupcmd = data.getVar('UPDATECOMMAND', localdata, 1)
136
137             if svn_rsh:
138                 svncmd = "svn_RSH=\"%s\" %s" % (svn_rsh, svncmd)
139                 svnupcmd = "svn_RSH=\"%s\" %s" % (svn_rsh, svnupcmd)
140
141             pkg=data.expand('${PN}', d)
142             pkgdir=os.path.join(data.expand('${SVNDIR}', localdata), pkg)
143             moddir=os.path.join(pkgdir, module)
144             bb.msg.debug(2, bb.msg.domain.Fetcher, "Fetch: checking for module directory '" + moddir + "'")
145
146             if os.access(os.path.join(moddir,'.svn'), os.R_OK):
147                 bb.msg.note(1, bb.msg.domain.Fetcher, "Update " + loc)
148                 # update sources there
149                 os.chdir(moddir)
150                 bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s" % svnupcmd)
151                 myret = os.system(svnupcmd)
152             else:
153                 bb.msg.note(1, bb.msg.domain.Fetcher, "Fetch " + loc)
154                 # check out sources there
155                 bb.mkdirhier(pkgdir)
156                 os.chdir(pkgdir)
157                 bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s" % svncmd)
158                 myret = os.system(svncmd)
159
160             if myret != 0:
161                 raise FetchError(module)
162
163             os.chdir(pkgdir)
164             # tar them up to a defined filename
165             myret = os.system("tar -czf %s %s" % (os.path.join(dldir,tarfn), os.path.basename(module)))
166             if myret != 0:
167                 try:
168                     os.unlink(tarfn)
169                 except OSError:
170                     pass
171             os.chdir(olddir)
172
173         del localdata