clearcase.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. """
  2. BitBake 'Fetch' clearcase implementation
  3. The clearcase fetcher is used to retrieve files from a ClearCase repository.
  4. Usage in the recipe:
  5. SRC_URI = "ccrc://cc.example.org/ccrc;vob=/example_vob;module=/example_module"
  6. SRCREV = "EXAMPLE_CLEARCASE_TAG"
  7. PV = "${@d.getVar("SRCREV", False).replace("/", "+")}"
  8. The fetcher uses the rcleartool or cleartool remote client, depending on which one is available.
  9. Supported SRC_URI options are:
  10. - vob
  11. (required) The name of the clearcase VOB (with prepending "/")
  12. - module
  13. The module in the selected VOB (with prepending "/")
  14. The module and vob parameters are combined to create
  15. the following load rule in the view config spec:
  16. load <vob><module>
  17. - proto
  18. http or https
  19. Related variables:
  20. CCASE_CUSTOM_CONFIG_SPEC
  21. Write a config spec to this variable in your recipe to use it instead
  22. of the default config spec generated by this fetcher.
  23. Please note that the SRCREV loses its functionality if you specify
  24. this variable. SRCREV is still used to label the archive after a fetch,
  25. but it doesn't define what's fetched.
  26. User credentials:
  27. cleartool:
  28. The login of cleartool is handled by the system. No special steps needed.
  29. rcleartool:
  30. In order to use rcleartool with authenticated users an `rcleartool login` is
  31. necessary before using the fetcher.
  32. """
  33. # Copyright (C) 2014 Siemens AG
  34. #
  35. # SPDX-License-Identifier: GPL-2.0-only
  36. #
  37. import os
  38. import shutil
  39. import bb
  40. from bb.fetch2 import FetchMethod
  41. from bb.fetch2 import FetchError
  42. from bb.fetch2 import MissingParameterError
  43. from bb.fetch2 import ParameterError
  44. from bb.fetch2 import runfetchcmd
  45. from bb.fetch2 import logger
  46. class ClearCase(FetchMethod):
  47. """Class to fetch urls via 'clearcase'"""
  48. def init(self, d):
  49. pass
  50. def supports(self, ud, d):
  51. """
  52. Check to see if a given url can be fetched with Clearcase.
  53. """
  54. return ud.type in ['ccrc']
  55. def debug(self, msg):
  56. logger.debug(1, "ClearCase: %s", msg)
  57. def urldata_init(self, ud, d):
  58. """
  59. init ClearCase specific variable within url data
  60. """
  61. ud.proto = "https"
  62. if 'protocol' in ud.parm:
  63. ud.proto = ud.parm['protocol']
  64. if not ud.proto in ('http', 'https'):
  65. raise ParameterError("Invalid protocol type", ud.url)
  66. ud.vob = ''
  67. if 'vob' in ud.parm:
  68. ud.vob = ud.parm['vob']
  69. else:
  70. msg = ud.url+": vob must be defined so the fetcher knows what to get."
  71. raise MissingParameterError('vob', msg)
  72. if 'module' in ud.parm:
  73. ud.module = ud.parm['module']
  74. else:
  75. ud.module = ""
  76. ud.basecmd = d.getVar("FETCHCMD_ccrc") or "/usr/bin/env cleartool || rcleartool"
  77. if d.getVar("SRCREV") == "INVALID":
  78. raise FetchError("Set a valid SRCREV for the clearcase fetcher in your recipe, e.g. SRCREV = \"/main/LATEST\" or any other label of your choice.")
  79. ud.label = d.getVar("SRCREV", False)
  80. ud.customspec = d.getVar("CCASE_CUSTOM_CONFIG_SPEC")
  81. ud.server = "%s://%s%s" % (ud.proto, ud.host, ud.path)
  82. ud.identifier = "clearcase-%s%s-%s" % ( ud.vob.replace("/", ""),
  83. ud.module.replace("/", "."),
  84. ud.label.replace("/", "."))
  85. ud.viewname = "%s-view%s" % (ud.identifier, d.getVar("DATETIME", d, True))
  86. ud.csname = "%s-config-spec" % (ud.identifier)
  87. ud.ccasedir = os.path.join(d.getVar("DL_DIR"), ud.type)
  88. ud.viewdir = os.path.join(ud.ccasedir, ud.viewname)
  89. ud.configspecfile = os.path.join(ud.ccasedir, ud.csname)
  90. ud.localfile = "%s.tar.gz" % (ud.identifier)
  91. self.debug("host = %s" % ud.host)
  92. self.debug("path = %s" % ud.path)
  93. self.debug("server = %s" % ud.server)
  94. self.debug("proto = %s" % ud.proto)
  95. self.debug("type = %s" % ud.type)
  96. self.debug("vob = %s" % ud.vob)
  97. self.debug("module = %s" % ud.module)
  98. self.debug("basecmd = %s" % ud.basecmd)
  99. self.debug("label = %s" % ud.label)
  100. self.debug("ccasedir = %s" % ud.ccasedir)
  101. self.debug("viewdir = %s" % ud.viewdir)
  102. self.debug("viewname = %s" % ud.viewname)
  103. self.debug("configspecfile = %s" % ud.configspecfile)
  104. self.debug("localfile = %s" % ud.localfile)
  105. ud.localfile = os.path.join(d.getVar("DL_DIR"), ud.localfile)
  106. def _build_ccase_command(self, ud, command):
  107. """
  108. Build up a commandline based on ud
  109. command is: mkview, setcs, rmview
  110. """
  111. options = []
  112. if "rcleartool" in ud.basecmd:
  113. options.append("-server %s" % ud.server)
  114. basecmd = "%s %s" % (ud.basecmd, command)
  115. if command == 'mkview':
  116. if not "rcleartool" in ud.basecmd:
  117. # Cleartool needs a -snapshot view
  118. options.append("-snapshot")
  119. options.append("-tag %s" % ud.viewname)
  120. options.append(ud.viewdir)
  121. elif command == 'rmview':
  122. options.append("-force")
  123. options.append("%s" % ud.viewdir)
  124. elif command == 'setcs':
  125. options.append("-overwrite")
  126. options.append(ud.configspecfile)
  127. else:
  128. raise FetchError("Invalid ccase command %s" % command)
  129. ccasecmd = "%s %s" % (basecmd, " ".join(options))
  130. self.debug("ccasecmd = %s" % ccasecmd)
  131. return ccasecmd
  132. def _write_configspec(self, ud, d):
  133. """
  134. Create config spec file (ud.configspecfile) for ccase view
  135. """
  136. config_spec = ""
  137. custom_config_spec = d.getVar("CCASE_CUSTOM_CONFIG_SPEC", d)
  138. if custom_config_spec is not None:
  139. for line in custom_config_spec.split("\\n"):
  140. config_spec += line+"\n"
  141. bb.warn("A custom config spec has been set, SRCREV is only relevant for the tarball name.")
  142. else:
  143. config_spec += "element * CHECKEDOUT\n"
  144. config_spec += "element * %s\n" % ud.label
  145. config_spec += "load %s%s\n" % (ud.vob, ud.module)
  146. logger.info("Using config spec: \n%s" % config_spec)
  147. with open(ud.configspecfile, 'w') as f:
  148. f.write(config_spec)
  149. def _remove_view(self, ud, d):
  150. if os.path.exists(ud.viewdir):
  151. cmd = self._build_ccase_command(ud, 'rmview');
  152. logger.info("cleaning up [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
  153. bb.fetch2.check_network_access(d, cmd, ud.url)
  154. output = runfetchcmd(cmd, d, workdir=ud.ccasedir)
  155. logger.info("rmview output: %s", output)
  156. def need_update(self, ud, d):
  157. if ("LATEST" in ud.label) or (ud.customspec and "LATEST" in ud.customspec):
  158. ud.identifier += "-%s" % d.getVar("DATETIME",d, True)
  159. return True
  160. if os.path.exists(ud.localpath):
  161. return False
  162. return True
  163. def supports_srcrev(self):
  164. return True
  165. def sortable_revision(self, ud, d, name):
  166. return False, ud.identifier
  167. def download(self, ud, d):
  168. """Fetch url"""
  169. # Make a fresh view
  170. bb.utils.mkdirhier(ud.ccasedir)
  171. self._write_configspec(ud, d)
  172. cmd = self._build_ccase_command(ud, 'mkview')
  173. logger.info("creating view [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
  174. bb.fetch2.check_network_access(d, cmd, ud.url)
  175. try:
  176. runfetchcmd(cmd, d)
  177. except FetchError as e:
  178. if "CRCLI2008E" in e.msg:
  179. raise FetchError("%s\n%s\n" % (e.msg, "Call `rcleartool login` in your console to authenticate to the clearcase server before running bitbake."))
  180. else:
  181. raise e
  182. # Set configspec: Setting the configspec effectively fetches the files as defined in the configspec
  183. cmd = self._build_ccase_command(ud, 'setcs');
  184. logger.info("fetching data [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
  185. bb.fetch2.check_network_access(d, cmd, ud.url)
  186. output = runfetchcmd(cmd, d, workdir=ud.viewdir)
  187. logger.info("%s", output)
  188. # Copy the configspec to the viewdir so we have it in our source tarball later
  189. shutil.copyfile(ud.configspecfile, os.path.join(ud.viewdir, ud.csname))
  190. # Clean clearcase meta-data before tar
  191. runfetchcmd('tar -czf "%s" .' % (ud.localpath), d, cleanup = [ud.localpath], workdir = ud.viewdir)
  192. # Clean up so we can create a new view next time
  193. self.clean(ud, d);
  194. def clean(self, ud, d):
  195. self._remove_view(ud, d)
  196. bb.utils.remove(ud.configspecfile)