clearcase.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 sys
  39. import shutil
  40. import bb
  41. from bb.fetch2 import FetchMethod
  42. from bb.fetch2 import FetchError
  43. from bb.fetch2 import runfetchcmd
  44. from bb.fetch2 import logger
  45. class ClearCase(FetchMethod):
  46. """Class to fetch urls via 'clearcase'"""
  47. def init(self, d):
  48. pass
  49. def supports(self, ud, d):
  50. """
  51. Check to see if a given url can be fetched with Clearcase.
  52. """
  53. return ud.type in ['ccrc']
  54. def debug(self, msg):
  55. logger.debug(1, "ClearCase: %s", msg)
  56. def urldata_init(self, ud, d):
  57. """
  58. init ClearCase specific variable within url data
  59. """
  60. ud.proto = "https"
  61. if 'protocol' in ud.parm:
  62. ud.proto = ud.parm['protocol']
  63. if not ud.proto in ('http', 'https'):
  64. raise fetch2.ParameterError("Invalid protocol type", ud.url)
  65. ud.vob = ''
  66. if 'vob' in ud.parm:
  67. ud.vob = ud.parm['vob']
  68. else:
  69. msg = ud.url+": vob must be defined so the fetcher knows what to get."
  70. raise MissingParameterError('vob', msg)
  71. if 'module' in ud.parm:
  72. ud.module = ud.parm['module']
  73. else:
  74. ud.module = ""
  75. ud.basecmd = d.getVar("FETCHCMD_ccrc") or "/usr/bin/env cleartool || rcleartool"
  76. if d.getVar("SRCREV") == "INVALID":
  77. 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.")
  78. ud.label = d.getVar("SRCREV", False)
  79. ud.customspec = d.getVar("CCASE_CUSTOM_CONFIG_SPEC")
  80. ud.server = "%s://%s%s" % (ud.proto, ud.host, ud.path)
  81. ud.identifier = "clearcase-%s%s-%s" % ( ud.vob.replace("/", ""),
  82. ud.module.replace("/", "."),
  83. ud.label.replace("/", "."))
  84. ud.viewname = "%s-view%s" % (ud.identifier, d.getVar("DATETIME", d, True))
  85. ud.csname = "%s-config-spec" % (ud.identifier)
  86. ud.ccasedir = os.path.join(d.getVar("DL_DIR"), ud.type)
  87. ud.viewdir = os.path.join(ud.ccasedir, ud.viewname)
  88. ud.configspecfile = os.path.join(ud.ccasedir, ud.csname)
  89. ud.localfile = "%s.tar.gz" % (ud.identifier)
  90. self.debug("host = %s" % ud.host)
  91. self.debug("path = %s" % ud.path)
  92. self.debug("server = %s" % ud.server)
  93. self.debug("proto = %s" % ud.proto)
  94. self.debug("type = %s" % ud.type)
  95. self.debug("vob = %s" % ud.vob)
  96. self.debug("module = %s" % ud.module)
  97. self.debug("basecmd = %s" % ud.basecmd)
  98. self.debug("label = %s" % ud.label)
  99. self.debug("ccasedir = %s" % ud.ccasedir)
  100. self.debug("viewdir = %s" % ud.viewdir)
  101. self.debug("viewname = %s" % ud.viewname)
  102. self.debug("configspecfile = %s" % ud.configspecfile)
  103. self.debug("localfile = %s" % ud.localfile)
  104. ud.localfile = os.path.join(d.getVar("DL_DIR"), ud.localfile)
  105. def _build_ccase_command(self, ud, command):
  106. """
  107. Build up a commandline based on ud
  108. command is: mkview, setcs, rmview
  109. """
  110. options = []
  111. if "rcleartool" in ud.basecmd:
  112. options.append("-server %s" % ud.server)
  113. basecmd = "%s %s" % (ud.basecmd, command)
  114. if command is 'mkview':
  115. if not "rcleartool" in ud.basecmd:
  116. # Cleartool needs a -snapshot view
  117. options.append("-snapshot")
  118. options.append("-tag %s" % ud.viewname)
  119. options.append(ud.viewdir)
  120. elif command is 'rmview':
  121. options.append("-force")
  122. options.append("%s" % ud.viewdir)
  123. elif command is 'setcs':
  124. options.append("-overwrite")
  125. options.append(ud.configspecfile)
  126. else:
  127. raise FetchError("Invalid ccase command %s" % command)
  128. ccasecmd = "%s %s" % (basecmd, " ".join(options))
  129. self.debug("ccasecmd = %s" % ccasecmd)
  130. return ccasecmd
  131. def _write_configspec(self, ud, d):
  132. """
  133. Create config spec file (ud.configspecfile) for ccase view
  134. """
  135. config_spec = ""
  136. custom_config_spec = d.getVar("CCASE_CUSTOM_CONFIG_SPEC", d)
  137. if custom_config_spec is not None:
  138. for line in custom_config_spec.split("\\n"):
  139. config_spec += line+"\n"
  140. bb.warn("A custom config spec has been set, SRCREV is only relevant for the tarball name.")
  141. else:
  142. config_spec += "element * CHECKEDOUT\n"
  143. config_spec += "element * %s\n" % ud.label
  144. config_spec += "load %s%s\n" % (ud.vob, ud.module)
  145. logger.info("Using config spec: \n%s" % config_spec)
  146. with open(ud.configspecfile, 'w') as f:
  147. f.write(config_spec)
  148. def _remove_view(self, ud, d):
  149. if os.path.exists(ud.viewdir):
  150. cmd = self._build_ccase_command(ud, 'rmview');
  151. logger.info("cleaning up [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
  152. bb.fetch2.check_network_access(d, cmd, ud.url)
  153. output = runfetchcmd(cmd, d, workdir=ud.ccasedir)
  154. logger.info("rmview output: %s", output)
  155. def need_update(self, ud, d):
  156. if ("LATEST" in ud.label) or (ud.customspec and "LATEST" in ud.customspec):
  157. ud.identifier += "-%s" % d.getVar("DATETIME",d, True)
  158. return True
  159. if os.path.exists(ud.localpath):
  160. return False
  161. return True
  162. def supports_srcrev(self):
  163. return True
  164. def sortable_revision(self, ud, d, name):
  165. return False, ud.identifier
  166. def download(self, ud, d):
  167. """Fetch url"""
  168. # Make a fresh view
  169. bb.utils.mkdirhier(ud.ccasedir)
  170. self._write_configspec(ud, d)
  171. cmd = self._build_ccase_command(ud, 'mkview')
  172. logger.info("creating view [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
  173. bb.fetch2.check_network_access(d, cmd, ud.url)
  174. try:
  175. runfetchcmd(cmd, d)
  176. except FetchError as e:
  177. if "CRCLI2008E" in e.msg:
  178. raise FetchError("%s\n%s\n" % (e.msg, "Call `rcleartool login` in your console to authenticate to the clearcase server before running bitbake."))
  179. else:
  180. raise e
  181. # Set configspec: Setting the configspec effectively fetches the files as defined in the configspec
  182. cmd = self._build_ccase_command(ud, 'setcs');
  183. logger.info("fetching data [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
  184. bb.fetch2.check_network_access(d, cmd, ud.url)
  185. output = runfetchcmd(cmd, d, workdir=ud.viewdir)
  186. logger.info("%s", output)
  187. # Copy the configspec to the viewdir so we have it in our source tarball later
  188. shutil.copyfile(ud.configspecfile, os.path.join(ud.viewdir, ud.csname))
  189. # Clean clearcase meta-data before tar
  190. runfetchcmd('tar -czf "%s" .' % (ud.localpath), d, cleanup = [ud.localpath])
  191. # Clean up so we can create a new view next time
  192. self.clean(ud, d);
  193. def clean(self, ud, d):
  194. self._remove_view(ud, d)
  195. bb.utils.remove(ud.configspecfile)