perforce.py 6.7 KB

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