perforce.py 6.6 KB

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