a068fa1441456e546fb9a9ea38b6cfbdeb40a85c
[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(self, url, ud, d):
39         """
40         Check to see if a given url can be fetched with svn.
41         """
42         return ud.type in ['svn']
43
44     def localpath(self, url, ud, d):
45         if not "module" in ud.parm:
46             raise MissingParameterError("svn method needs a 'module' parameter")
47         else:
48             module = ud.parm["module"]
49
50         ud.revision = ""
51         if 'rev' in parm:
52             ud.revision = ud.parm['rev']
53
54         ud.localfile = data.expand('%s_%s_%s_%s_%s.tar.gz' % (module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision, ud.date), d)
55
56         return os.path.join(data.getVar("DL_DIR", d, 1), )
57
58     def go(self, loc, ud, d):
59         """Fetch url"""
60
61         localdata = data.createCopy(d)
62         data.setVar('OVERRIDES', "svn:%s" % data.getVar('OVERRIDES', localdata), localdata)
63         data.update_data(localdata)
64
65         if not "module" in ud.parm:
66             raise MissingParameterError("svn method needs a 'module' parameter")
67         else:
68             module = ud.parm["module"]
69
70         dlfile = self.localpath(loc, localdata)
71         dldir = data.getVar('DL_DIR', localdata, 1)
72
73         # setup svn options
74         options = []
75
76         if "proto" in ud.parm:
77             proto = ud.parm["proto"]
78         else:
79             proto = "svn"
80
81         svn_rsh = None
82         if proto == "svn+ssh" and "rsh" in ud.parm:
83             svn_rsh = ud.parm["rsh"]
84
85         tarfn = ud.localfile
86
87         # try to use the tarball stash
88         if (date != "now") and Fetch.try_mirror(d, ud.localfile):
89             bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists or was mirrored, skipping svn checkout." % tarfn)
90             return
91
92         olddir = os.path.abspath(os.getcwd())
93         os.chdir(data.expand(dldir, localdata))
94
95         svnroot = ud.host + ud.path
96
97         # either use the revision, or SRCDATE in braces, or nothing for SRCDATE = "now"
98         if ud.revision:
99             options.append("-r %s" % ud.revision)
100         elif ud.date != "now":
101             options.append("-r {%s}" % ud.date)
102
103         data.setVar('SVNROOT', "%s://%s/%s" % (proto, svnroot, module), localdata)
104         data.setVar('SVNCOOPTS', " ".join(options), localdata)
105         data.setVar('SVNMODULE', module, localdata)
106         svncmd = data.getVar('FETCHCOMMAND', localdata, 1)
107         svnupcmd = data.getVar('UPDATECOMMAND', localdata, 1)
108
109         if svn_rsh:
110             svncmd = "svn_RSH=\"%s\" %s" % (svn_rsh, svncmd)
111             svnupcmd = "svn_RSH=\"%s\" %s" % (svn_rsh, svnupcmd)
112
113         pkg=data.expand('${PN}', d)
114         pkgdir=os.path.join(data.expand('${SVNDIR}', localdata), pkg)
115         moddir=os.path.join(pkgdir, module)
116         bb.msg.debug(2, bb.msg.domain.Fetcher, "Fetch: checking for module directory '" + moddir + "'")
117
118         if os.access(os.path.join(moddir,'.svn'), os.R_OK):
119             bb.msg.note(1, bb.msg.domain.Fetcher, "Update " + loc)
120             # update sources there
121             os.chdir(moddir)
122             bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s" % svnupcmd)
123             myret = os.system(svnupcmd)
124         else:
125             bb.msg.note(1, bb.msg.domain.Fetcher, "Fetch " + loc)
126             # check out sources there
127             bb.mkdirhier(pkgdir)
128             os.chdir(pkgdir)
129             bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s" % svncmd)
130             myret = os.system(svncmd)
131
132         if myret != 0:
133             raise FetchError(module)
134
135         os.chdir(pkgdir)
136         # tar them up to a defined filename
137         myret = os.system("tar -czf %s %s" % (os.path.join(dldir,tarfn), os.path.basename(module)))
138         if myret != 0:
139             try:
140                 os.unlink(tarfn)
141             except OSError:
142                 pass
143         os.chdir(olddir)