clearcase.py 9.5 KB

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