perforce.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. from future_builtins import zip
  25. import os
  26. import subprocess
  27. import logging
  28. import bb
  29. from bb import data
  30. from bb.fetch2 import FetchMethod
  31. from bb.fetch2 import FetchError
  32. from bb.fetch2 import logger
  33. from bb.fetch2 import runfetchcmd
  34. class Perforce(FetchMethod):
  35. def supports(self, ud, d):
  36. return ud.type in ['p4']
  37. def doparse(url, d):
  38. parm = {}
  39. path = url.split("://")[1]
  40. delim = path.find("@");
  41. if delim != -1:
  42. (user, pswd, host, port) = path.split('@')[0].split(":")
  43. path = path.split('@')[1]
  44. else:
  45. (host, port) = data.getVar('P4PORT', d).split(':')
  46. user = ""
  47. pswd = ""
  48. if path.find(";") != -1:
  49. keys=[]
  50. values=[]
  51. plist = path.split(';')
  52. for item in plist:
  53. if item.count('='):
  54. (key, value) = item.split('=')
  55. keys.append(key)
  56. values.append(value)
  57. parm = dict(zip(keys, values))
  58. path = "//" + path.split(';')[0]
  59. host += ":%s" % (port)
  60. parm["cset"] = Perforce.getcset(d, path, host, user, pswd, parm)
  61. return host, path, user, pswd, parm
  62. doparse = staticmethod(doparse)
  63. def getcset(d, depot, host, user, pswd, parm):
  64. p4opt = ""
  65. if "cset" in parm:
  66. return parm["cset"];
  67. if user:
  68. p4opt += " -u %s" % (user)
  69. if pswd:
  70. p4opt += " -P %s" % (pswd)
  71. if host:
  72. p4opt += " -p %s" % (host)
  73. p4date = data.getVar("P4DATE", d, True)
  74. if "revision" in parm:
  75. depot += "#%s" % (parm["revision"])
  76. elif "label" in parm:
  77. depot += "@%s" % (parm["label"])
  78. elif p4date:
  79. depot += "@%s" % (p4date)
  80. p4cmd = data.getVar('FETCHCOMMAND_p4', d, True)
  81. logger.debug(1, "Running %s%s changes -m 1 %s", p4cmd, p4opt, depot)
  82. p4file, errors = bb.process.run("%s%s changes -m 1 %s" % (p4cmd, p4opt, depot))
  83. cset = p4file.strip()
  84. logger.debug(1, "READ %s", cset)
  85. if not cset:
  86. return -1
  87. return cset.split(' ')[1]
  88. getcset = staticmethod(getcset)
  89. def urldata_init(self, ud, d):
  90. (host, path, user, pswd, parm) = Perforce.doparse(ud.url, d)
  91. # If a label is specified, we use that as our filename
  92. if "label" in parm:
  93. ud.localfile = "%s.tar.gz" % (parm["label"])
  94. return
  95. base = path
  96. which = path.find('/...')
  97. if which != -1:
  98. base = path[:which]
  99. base = self._strip_leading_slashes(base)
  100. cset = Perforce.getcset(d, path, host, user, pswd, parm)
  101. ud.localfile = data.expand('%s+%s+%s.tar.gz' % (host, base.replace('/', '.'), cset), d)
  102. def download(self, ud, d):
  103. """
  104. Fetch urls
  105. """
  106. (host, depot, user, pswd, parm) = Perforce.doparse(ud.url, d)
  107. if depot.find('/...') != -1:
  108. path = depot[:depot.find('/...')]
  109. else:
  110. path = depot
  111. module = parm.get('module', os.path.basename(path))
  112. localdata = data.createCopy(d)
  113. data.setVar('OVERRIDES', "p4:%s" % data.getVar('OVERRIDES', localdata), localdata)
  114. data.update_data(localdata)
  115. # Get the p4 command
  116. p4opt = ""
  117. if user:
  118. p4opt += " -u %s" % (user)
  119. if pswd:
  120. p4opt += " -P %s" % (pswd)
  121. if host:
  122. p4opt += " -p %s" % (host)
  123. p4cmd = data.getVar('FETCHCOMMAND', localdata, True)
  124. # create temp directory
  125. logger.debug(2, "Fetch: creating temporary directory")
  126. bb.utils.mkdirhier(data.expand('${WORKDIR}', localdata))
  127. data.setVar('TMPBASE', data.expand('${WORKDIR}/oep4.XXXXXX', localdata), localdata)
  128. tmpfile, errors = bb.process.run(data.getVar('MKTEMPDIRCMD', localdata, True) or "false")
  129. tmpfile = tmpfile.strip()
  130. if not tmpfile:
  131. raise FetchError("Fetch: unable to create temporary directory.. make sure 'mktemp' is in the PATH.", ud.url)
  132. if "label" in parm:
  133. depot = "%s@%s" % (depot, parm["label"])
  134. else:
  135. cset = Perforce.getcset(d, depot, host, user, pswd, parm)
  136. depot = "%s@%s" % (depot, cset)
  137. os.chdir(tmpfile)
  138. logger.info("Fetch " + ud.url)
  139. logger.info("%s%s files %s", p4cmd, p4opt, depot)
  140. p4file, errors = bb.process.run("%s%s files %s" % (p4cmd, p4opt, depot))
  141. p4file = [f.rstrip() for f in p4file.splitlines()]
  142. if not p4file:
  143. raise FetchError("Fetch: unable to get the P4 files from %s" % depot, ud.url)
  144. count = 0
  145. for file in p4file:
  146. list = file.split()
  147. if list[2] == "delete":
  148. continue
  149. dest = list[0][len(path)+1:]
  150. where = dest.find("#")
  151. subprocess.call("%s%s print -o %s/%s %s" % (p4cmd, p4opt, module, dest[:where], list[0]), shell=True)
  152. count = count + 1
  153. if count == 0:
  154. logger.error()
  155. raise FetchError("Fetch: No files gathered from the P4 fetch", ud.url)
  156. runfetchcmd("tar -czf %s %s" % (ud.localpath, module), d, cleanup = [ud.localpath])
  157. # cleanup
  158. bb.utils.prunedir(tmpfile)