perforce.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. # SPDX-License-Identifier: GPL-2.0-only
  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
  26. import logging
  27. import bb
  28. from bb.fetch2 import FetchMethod
  29. from bb.fetch2 import FetchError
  30. from bb.fetch2 import logger
  31. from bb.fetch2 import runfetchcmd
  32. class Perforce(FetchMethod):
  33. """ Class to fetch from perforce repositories """
  34. def supports(self, ud, d):
  35. """ Check to see if a given url can be fetched with perforce. """
  36. return ud.type in ['p4']
  37. def urldata_init(self, ud, d):
  38. """
  39. Initialize perforce specific variables within url data. If P4CONFIG is
  40. provided by the env, use it. If P4PORT is specified by the recipe, use
  41. its values, which may override the settings in P4CONFIG.
  42. """
  43. ud.basecmd = d.getVar("FETCHCMD_p4") or "/usr/bin/env p4"
  44. ud.dldir = d.getVar("P4DIR") or (d.getVar("DL_DIR") + "/p4")
  45. path = ud.url.split('://')[1]
  46. path = path.split(';')[0]
  47. delim = path.find('@');
  48. if delim != -1:
  49. (ud.user, ud.pswd) = path.split('@')[0].split(':')
  50. ud.path = path.split('@')[1]
  51. else:
  52. ud.path = path
  53. ud.usingp4config = False
  54. p4port = d.getVar('P4PORT')
  55. if p4port:
  56. logger.debug(1, 'Using recipe provided P4PORT: %s' % p4port)
  57. ud.host = p4port
  58. else:
  59. logger.debug(1, 'Trying to use P4CONFIG to automatically set P4PORT...')
  60. ud.usingp4config = True
  61. p4cmd = '%s info | grep "Server address"' % ud.basecmd
  62. bb.fetch2.check_network_access(d, p4cmd, ud.url)
  63. ud.host = runfetchcmd(p4cmd, d, True)
  64. ud.host = ud.host.split(': ')[1].strip()
  65. logger.debug(1, 'Determined P4PORT to be: %s' % ud.host)
  66. if not ud.host:
  67. raise FetchError('Could not determine P4PORT from P4CONFIG')
  68. if ud.path.find('/...') >= 0:
  69. ud.pathisdir = True
  70. else:
  71. ud.pathisdir = False
  72. cleanedpath = ud.path.replace('/...', '').replace('/', '.')
  73. cleanedhost = ud.host.replace(':', '.')
  74. ud.pkgdir = os.path.join(ud.dldir, cleanedhost, cleanedpath)
  75. ud.setup_revisions(d)
  76. ud.localfile = d.expand('%s_%s_%s.tar.gz' % (cleanedhost, cleanedpath, ud.revision))
  77. def _buildp4command(self, ud, d, command, depot_filename=None):
  78. """
  79. Build a p4 commandline. Valid commands are "changes", "print", and
  80. "files". depot_filename is the full path to the file in the depot
  81. including the trailing '#rev' value.
  82. """
  83. p4opt = ""
  84. if ud.user:
  85. p4opt += ' -u "%s"' % (ud.user)
  86. if ud.pswd:
  87. p4opt += ' -P "%s"' % (ud.pswd)
  88. if ud.host and not ud.usingp4config:
  89. p4opt += ' -p %s' % (ud.host)
  90. if hasattr(ud, 'revision') and ud.revision:
  91. pathnrev = '%s@%s' % (ud.path, ud.revision)
  92. else:
  93. pathnrev = '%s' % (ud.path)
  94. if depot_filename:
  95. if ud.pathisdir: # Remove leading path to obtain filename
  96. filename = depot_filename[len(ud.path)-1:]
  97. else:
  98. filename = depot_filename[depot_filename.rfind('/'):]
  99. filename = filename[:filename.find('#')] # Remove trailing '#rev'
  100. if command == 'changes':
  101. p4cmd = '%s%s changes -m 1 //%s' % (ud.basecmd, p4opt, pathnrev)
  102. elif command == 'print':
  103. if depot_filename != None:
  104. p4cmd = '%s%s print -o "p4/%s" "%s"' % (ud.basecmd, p4opt, filename, depot_filename)
  105. else:
  106. raise FetchError('No depot file name provided to p4 %s' % command, ud.url)
  107. elif command == 'files':
  108. p4cmd = '%s%s files //%s' % (ud.basecmd, p4opt, pathnrev)
  109. else:
  110. raise FetchError('Invalid p4 command %s' % command, ud.url)
  111. return p4cmd
  112. def _p4listfiles(self, ud, d):
  113. """
  114. Return a list of the file names which are present in the depot using the
  115. 'p4 files' command, including trailing '#rev' file revision indicator
  116. """
  117. p4cmd = self._buildp4command(ud, d, 'files')
  118. bb.fetch2.check_network_access(d, p4cmd, ud.url)
  119. p4fileslist = runfetchcmd(p4cmd, d, True)
  120. p4fileslist = [f.rstrip() for f in p4fileslist.splitlines()]
  121. if not p4fileslist:
  122. raise FetchError('Unable to fetch listing of p4 files from %s@%s' % (ud.host, ud.path))
  123. count = 0
  124. filelist = []
  125. for filename in p4fileslist:
  126. item = filename.split(' - ')
  127. lastaction = item[1].split()
  128. logger.debug(1, 'File: %s Last Action: %s' % (item[0], lastaction[0]))
  129. if lastaction[0] == 'delete':
  130. continue
  131. filelist.append(item[0])
  132. return filelist
  133. def download(self, ud, d):
  134. """ Get the list of files, fetch each one """
  135. filelist = self._p4listfiles(ud, d)
  136. if not filelist:
  137. raise FetchError('No files found in depot %s@%s' % (ud.host, ud.path))
  138. bb.utils.remove(ud.pkgdir, True)
  139. bb.utils.mkdirhier(ud.pkgdir)
  140. for afile in filelist:
  141. p4fetchcmd = self._buildp4command(ud, d, 'print', afile)
  142. bb.fetch2.check_network_access(d, p4fetchcmd, ud.url)
  143. runfetchcmd(p4fetchcmd, d, workdir=ud.pkgdir)
  144. runfetchcmd('tar -czf %s p4' % (ud.localpath), d, cleanup=[ud.localpath], workdir=ud.pkgdir)
  145. def clean(self, ud, d):
  146. """ Cleanup p4 specific files and dirs"""
  147. bb.utils.remove(ud.localpath)
  148. bb.utils.remove(ud.pkgdir, True)
  149. def supports_srcrev(self):
  150. return True
  151. def _revision_key(self, ud, d, name):
  152. """ Return a unique key for the url """
  153. return 'p4:%s' % ud.pkgdir
  154. def _latest_revision(self, ud, d, name):
  155. """ Return the latest upstream scm revision number """
  156. p4cmd = self._buildp4command(ud, d, "changes")
  157. bb.fetch2.check_network_access(d, p4cmd, ud.url)
  158. tip = runfetchcmd(p4cmd, d, True)
  159. if not tip:
  160. raise FetchError('Could not determine the latest perforce changelist')
  161. tipcset = tip.split(' ')[1]
  162. logger.debug(1, 'p4 tip found to be changelist %s' % tipcset)
  163. return tipcset
  164. def sortable_revision(self, ud, d, name):
  165. """ Return a sortable revision number """
  166. return False, self._build_revision(ud, d)
  167. def _build_revision(self, ud, d):
  168. return ud.revision