perforce.py 6.6 KB

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