fetch/perforce.py: Fix to use commandline options instead of being overriden by the...
[vuplus_bitbake] / lib / bb / fetch / perforce.py
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' implementations
5
6 Classes for obtaining upstream sources for the
7 BitBake build tools.
8
9 """
10
11 # Copyright (C) 2003, 2004  Chris Larson
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License version 2 as
15 # published by the Free Software Foundation.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along
23 # with this program; if not, write to the Free Software Foundation, Inc.,
24 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #
26 # Based on functions from the base bb module, Copyright 2003 Holger Schurig
27
28 import os, re
29 import bb
30 from   bb import data
31 from   bb.fetch import Fetch
32 from   bb.fetch import FetchError
33 from   bb.fetch import MissingParameterError
34
35 class Perforce(Fetch):
36     def supports(self, url, ud, d):
37         return ud.type in ['p4']
38
39     def doparse(url,d):
40         parm = {}
41         path = url.split("://")[1]
42         delim = path.find("@");
43         if delim != -1:
44             (user,pswd,host,port) = path.split('@')[0].split(":")
45             path = path.split('@')[1]
46         else:
47             (host,port) = data.getVar('P4PORT', d).split(':')
48             user = ""
49             pswd = ""
50
51         if path.find(";") != -1:
52             keys=[]
53             values=[]
54             plist = path.split(';')
55             for item in plist:
56                 if item.count('='):
57                     (key,value) = item.split('=')
58                     keys.append(key)
59                     values.append(value)
60
61             parm = dict(zip(keys,values))
62         path = "//" + path.split(';')[0]
63         host += ":%s" % (port)
64         parm["cset"] = Perforce.getcset(d, path, host, user, pswd, parm)
65
66         return host,path,user,pswd,parm
67     doparse = staticmethod(doparse)
68
69     def getcset(d, depot,host,user,pswd,parm):
70         p4opt = ""
71         if "cset" in parm:
72             return parm["cset"];
73         if user:
74             p4opt += " -u %s" % (user)
75         if pswd:
76             p4opt += " -P %s" % (pswd)
77         if host:
78             p4opt += " -p %s" % (host)
79
80         p4date = data.getVar("P4DATE", d, 1)
81         if "revision" in parm:
82             depot += "#%s" % (parm["revision"])
83         elif "label" in parm:
84             depot += "@%s" % (parm["label"])
85         elif p4date:
86             depot += "@%s" % (p4date)
87
88         p4cmd = data.getVar('FETCHCOMMAND_p4', d, 1)
89         bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s%s changes -m 1 %s" % (p4cmd, p4opt, depot))
90         p4file = os.popen("%s%s changes -m 1 %s" % (p4cmd, p4opt, depot))
91         cset = p4file.readline().strip()
92         bb.msg.debug(1, bb.msg.domain.Fetcher, "READ %s" % (cset))
93         if not cset:
94             return -1
95
96         return cset.split(' ')[1]
97     getcset = staticmethod(getcset)
98
99     def localpath(self, url, ud, d):        
100
101         (host,path,user,pswd,parm) = Perforce.doparse(url,d)
102
103         # If a label is specified, we use that as our filename
104
105         if "label" in parm:
106             ud.localfile = "%s.tar.gz" % (parm["label"])
107             return os.path.join(data.getVar("DL_DIR", d, 1), ud.localfile)
108
109         base = path
110         which = path.find('/...')
111         if which != -1:
112             base = path[:which]
113
114         if base[0] == "/":
115             base = base[1:]
116
117         cset = Perforce.getcset(d, path, host, user, pswd, parm)
118
119         ud.localfile = data.expand('%s+%s+%s.tar.gz' % (host,base.replace('/', '.'), cset), d)
120
121         return os.path.join(data.getVar("DL_DIR", d, 1), ud.localfile)
122
123     def go(self, loc, ud, d):
124         """
125         Fetch urls
126         """
127
128         # try to use the tarball stash
129         if Fetch.try_mirror(d, ud.localfile):
130             bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists or was mirrored, skipping perforce checkout." % ud.localpath)
131             return
132
133         (host,depot,user,pswd,parm) = Perforce.doparse(loc, d)
134
135         if depot.find('/...') != -1:
136             path = depot[:depot.find('/...')]
137         else:
138             path = depot
139
140         if "module" in parm:
141             module = parm["module"]
142         else:
143             module = os.path.basename(path)
144
145         localdata = data.createCopy(d)
146         data.setVar('OVERRIDES', "p4:%s" % data.getVar('OVERRIDES', localdata), localdata)
147         data.update_data(localdata)
148
149         # Get the p4 command
150         p4opt = ""
151         if user:
152             p4opt += " -u %s" % (user)
153
154         if pswd:
155             p4opt += " -P %s" % (pswd)
156
157         if host:
158             p4opt += " -p %s" % (host)
159
160         p4cmd = data.getVar('FETCHCOMMAND', localdata, 1)
161
162         # create temp directory
163         bb.msg.debug(2, bb.msg.domain.Fetcher, "Fetch: creating temporary directory")
164         bb.mkdirhier(data.expand('${WORKDIR}', localdata))
165         data.setVar('TMPBASE', data.expand('${WORKDIR}/oep4.XXXXXX', localdata), localdata)
166         tmppipe = os.popen(data.getVar('MKTEMPDIRCMD', localdata, 1) or "false")
167         tmpfile = tmppipe.readline().strip()
168         if not tmpfile:
169             bb.error("Fetch: unable to create temporary directory.. make sure 'mktemp' is in the PATH.")
170             raise FetchError(module)
171
172         if "label" in parm:
173             depot = "%s@%s" % (depot,parm["label"])
174         else:
175             cset = Perforce.getcset(d, depot, host, user, pswd, parm)
176             depot = "%s@%s" % (depot,cset)
177
178         os.chdir(tmpfile)
179         bb.msg.note(1, bb.msg.domain.Fetcher, "Fetch " + loc)
180         bb.msg.note(1, bb.msg.domain.Fetcher, "%s%s files %s" % (p4cmd, p4opt, depot))
181         p4file = os.popen("%s%s files %s" % (p4cmd, p4opt, depot))
182
183         if not p4file:
184             bb.error("Fetch: unable to get the P4 files from %s" % (depot))
185             raise FetchError(module)
186
187         count = 0
188
189         for file in p4file: 
190             list = file.split()
191
192             if list[2] == "delete":
193                 continue
194
195             dest = list[0][len(path)+1:]
196             where = dest.find("#")
197
198             os.system("%s%s print -o %s/%s %s" % (p4cmd, p4opt, module,dest[:where],list[0]))
199             count = count + 1
200             
201         if count == 0:
202             bb.error("Fetch:  No files gathered from the P4 fetch")
203             raise FetchError(module)
204
205         myret = os.system("tar -czf %s %s" % (ud.localpath, module))
206         if myret != 0:
207             try:
208                 os.unlink(ud.localpath)
209             except OSError:
210                 pass
211             raise FetchError(module)
212         # cleanup
213         os.system('rm -rf %s' % tmpfile)
214
215