perforce.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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' implementation for perforce
  5. """
  6. # Copyright (C) 2003, 2004 Chris Larson
  7. # Copyright (C) 2016 Kodak Alaris, Inc.
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License version 2 as
  11. # published by the Free Software Foundation.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. # Based on functions from the base bb module, Copyright 2003 Holger Schurig
  23. import os
  24. import logging
  25. import bb
  26. from bb import data
  27. from bb.fetch2 import FetchMethod
  28. from bb.fetch2 import FetchError
  29. from bb.fetch2 import logger
  30. from bb.fetch2 import runfetchcmd
  31. class Perforce(FetchMethod):
  32. """ Class to fetch from perforce repositories """
  33. def supports(self, ud, d):
  34. """ Check to see if a given url can be fetched with perforce. """
  35. return ud.type in ['p4']
  36. def urldata_init(self, ud, d):
  37. """
  38. Initialize perforce specific variables within url data. If P4CONFIG is
  39. provided by the env, use it. If P4PORT is specified by the recipe, use
  40. its values, which may override the settings in P4CONFIG.
  41. """
  42. ud.basecmd = d.getVar('FETCHCMD_p4')
  43. if not ud.basecmd:
  44. ud.basecmd = "/usr/bin/env p4"
  45. ud.dldir = d.getVar('P4DIR')
  46. if not ud.dldir:
  47. ud.dldir = '%s/%s' % (d.getVar('DL_DIR'), 'p4')
  48. path = ud.url.split('://')[1]
  49. path = path.split(';')[0]
  50. delim = path.find('@');
  51. if delim != -1:
  52. (ud.user, ud.pswd) = path.split('@')[0].split(':')
  53. ud.path = path.split('@')[1]
  54. else:
  55. ud.path = path
  56. ud.usingp4config = False
  57. p4port = d.getVar('P4PORT')
  58. if p4port:
  59. logger.debug(1, 'Using recipe provided P4PORT: %s' % p4port)
  60. ud.host = p4port
  61. else:
  62. logger.debug(1, 'Trying to use P4CONFIG to automatically set P4PORT...')
  63. ud.usingp4config = True
  64. p4cmd = '%s info | grep "Server address"' % ud.basecmd
  65. bb.fetch2.check_network_access(d, p4cmd)
  66. ud.host = runfetchcmd(p4cmd, d, True)
  67. ud.host = ud.host.split(': ')[1].strip()
  68. logger.debug(1, 'Determined P4PORT to be: %s' % ud.host)
  69. if not ud.host:
  70. raise FetchError('Could not determine P4PORT from P4CONFIG')
  71. if ud.path.find('/...') >= 0:
  72. ud.pathisdir = True
  73. else:
  74. ud.pathisdir = False
  75. cleanedpath = ud.path.replace('/...', '').replace('/', '.')
  76. cleanedhost = ud.host.replace(':', '.')
  77. ud.pkgdir = os.path.join(ud.dldir, cleanedhost, cleanedpath)
  78. ud.setup_revisons(d)
  79. ud.localfile = data.expand('%s_%s_%s.tar.gz' % (cleanedhost, cleanedpath, ud.revision), d)
  80. def _buildp4command(self, ud, d, command, depot_filename=None):
  81. """
  82. Build a p4 commandline. Valid commands are "changes", "print", and
  83. "files". depot_filename is the full path to the file in the depot
  84. including the trailing '#rev' value.
  85. """
  86. p4opt = ""
  87. if ud.user:
  88. p4opt += ' -u "%s"' % (ud.user)
  89. if ud.pswd:
  90. p4opt += ' -P "%s"' % (ud.pswd)
  91. if ud.host and not ud.usingp4config:
  92. p4opt += ' -p %s' % (ud.host)
  93. if hasattr(ud, 'revision') and ud.revision:
  94. pathnrev = '%s@%s' % (ud.path, ud.revision)
  95. else:
  96. pathnrev = '%s' % (ud.path)
  97. if depot_filename:
  98. if ud.pathisdir: # Remove leading path to obtain filename
  99. filename = depot_filename[len(ud.path)-1:]
  100. else:
  101. filename = depot_filename[depot_filename.rfind('/'):]
  102. filename = filename[:filename.find('#')] # Remove trailing '#rev'
  103. if command == 'changes':
  104. p4cmd = '%s%s changes -m 1 //%s' % (ud.basecmd, p4opt, pathnrev)
  105. elif command == 'print':
  106. if depot_filename != None:
  107. p4cmd = '%s%s print -o "p4/%s" "%s"' % (ud.basecmd, p4opt, filename, depot_filename)
  108. else:
  109. raise FetchError('No depot file name provided to p4 %s' % command, ud.url)
  110. elif command == 'files':
  111. p4cmd = '%s%s files //%s' % (ud.basecmd, p4opt, pathnrev)
  112. else:
  113. raise FetchError('Invalid p4 command %s' % command, ud.url)
  114. return p4cmd
  115. def _p4listfiles(self, ud, d):
  116. """
  117. Return a list of the file names which are present in the depot using the
  118. 'p4 files' command, including trailing '#rev' file revision indicator
  119. """
  120. p4cmd = self._buildp4command(ud, d, 'files')
  121. bb.fetch2.check_network_access(d, p4cmd)
  122. p4fileslist = runfetchcmd(p4cmd, d, True)
  123. p4fileslist = [f.rstrip() for f in p4fileslist.splitlines()]
  124. if not p4fileslist:
  125. raise FetchError('Unable to fetch listing of p4 files from %s@%s' % (ud.host, ud.path))
  126. count = 0
  127. filelist = []
  128. for filename in p4fileslist:
  129. item = filename.split(' - ')
  130. lastaction = item[1].split()
  131. logger.debug(1, 'File: %s Last Action: %s' % (item[0], lastaction[0]))
  132. if lastaction[0] == 'delete':
  133. continue
  134. filelist.append(item[0])
  135. return filelist
  136. def download(self, ud, d):
  137. """ Get the list of files, fetch each one """
  138. filelist = self._p4listfiles(ud, d)
  139. if not filelist:
  140. raise FetchError('No files found in depot %s@%s' % (ud.host, ud.path))
  141. bb.utils.remove(ud.pkgdir, True)
  142. bb.utils.mkdirhier(ud.pkgdir)
  143. for afile in filelist:
  144. p4fetchcmd = self._buildp4command(ud, d, 'print', afile)
  145. bb.fetch2.check_network_access(d, p4fetchcmd)
  146. runfetchcmd(p4fetchcmd, d, workdir=ud.pkgdir)
  147. runfetchcmd('tar -czf %s p4' % (ud.localpath), d, cleanup=[ud.localpath], workdir=ud.pkgdir)
  148. def clean(self, ud, d):
  149. """ Cleanup p4 specific files and dirs"""
  150. bb.utils.remove(ud.localpath)
  151. bb.utils.remove(ud.pkgdir, True)
  152. def supports_srcrev(self):
  153. return True
  154. def _revision_key(self, ud, d, name):
  155. """ Return a unique key for the url """
  156. return 'p4:%s' % ud.pkgdir
  157. def _latest_revision(self, ud, d, name):
  158. """ Return the latest upstream scm revision number """
  159. p4cmd = self._buildp4command(ud, d, "changes")
  160. bb.fetch2.check_network_access(d, p4cmd)
  161. tip = runfetchcmd(p4cmd, d, True)
  162. if not tip:
  163. raise FetchError('Could not determine the latest perforce changelist')
  164. tipcset = tip.split(' ')[1]
  165. logger.debug(1, 'p4 tip found to be changelist %s' % tipcset)
  166. return tipcset
  167. def sortable_revision(self, ud, d, name):
  168. """ Return a sortable revision number """
  169. return False, self._build_revision(ud, d)
  170. def _build_revision(self, ud, d):
  171. return ud.revision