clearcase.py 8.9 KB

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