perforce.py 6.7 KB

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