perforce.py 6.4 KB

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