clearcase.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. # This program is free software; you can redistribute it and/or modify
  38. # it under the terms of the GNU General Public License version 2 as
  39. # published by the Free Software Foundation.
  40. #
  41. # This program is distributed in the hope that it will be useful,
  42. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. # GNU General Public License for more details.
  45. #
  46. # You should have received a copy of the GNU General Public License along
  47. # with this program; if not, write to the Free Software Foundation, Inc.,
  48. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  49. #
  50. import os
  51. import sys
  52. import shutil
  53. import bb
  54. from bb import data
  55. from bb.fetch2 import FetchMethod
  56. from bb.fetch2 import FetchError
  57. from bb.fetch2 import runfetchcmd
  58. from bb.fetch2 import logger
  59. from distutils import spawn
  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", True) or spawn.find_executable("cleartool") or spawn.find_executable("rcleartool")
  91. if data.getVar("SRCREV", d, True) == "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", True)
  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(data.getVar("DL_DIR", d, True), 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(data.getVar("DL_DIR", d, True), 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. os.chdir(ud.ccasedir)
  166. cmd = self._build_ccase_command(ud, 'rmview');
  167. logger.info("cleaning up [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
  168. bb.fetch2.check_network_access(d, cmd, ud.url)
  169. output = runfetchcmd(cmd, d)
  170. logger.info("rmview output: %s", output)
  171. def need_update(self, ud, d):
  172. if ("LATEST" in ud.label) or (ud.customspec and "LATEST" in ud.customspec):
  173. ud.identifier += "-%s" % d.getVar("DATETIME",d, True)
  174. return True
  175. if os.path.exists(ud.localpath):
  176. return False
  177. return True
  178. def supports_srcrev(self):
  179. return True
  180. def sortable_revision(self, ud, d, name):
  181. return False, ud.identifier
  182. def download(self, ud, d):
  183. """Fetch url"""
  184. # Make a fresh view
  185. bb.utils.mkdirhier(ud.ccasedir)
  186. self._write_configspec(ud, d)
  187. cmd = self._build_ccase_command(ud, 'mkview')
  188. logger.info("creating view [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
  189. bb.fetch2.check_network_access(d, cmd, ud.url)
  190. try:
  191. runfetchcmd(cmd, d)
  192. except FetchError as e:
  193. if "CRCLI2008E" in e.msg:
  194. raise FetchError("%s\n%s\n" % (e.msg, "Call `rcleartool login` in your console to authenticate to the clearcase server before running bitbake."))
  195. else:
  196. raise e
  197. # Set configspec: Setting the configspec effectively fetches the files as defined in the configspec
  198. os.chdir(ud.viewdir)
  199. cmd = self._build_ccase_command(ud, 'setcs');
  200. logger.info("fetching data [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
  201. bb.fetch2.check_network_access(d, cmd, ud.url)
  202. output = runfetchcmd(cmd, d)
  203. logger.info("%s", output)
  204. # Copy the configspec to the viewdir so we have it in our source tarball later
  205. shutil.copyfile(ud.configspecfile, os.path.join(ud.viewdir, ud.csname))
  206. # Clean clearcase meta-data before tar
  207. runfetchcmd('tar -czf "%s" .' % (ud.localpath), d, cleanup = [ud.localpath])
  208. # Clean up so we can create a new view next time
  209. self.clean(ud, d);
  210. def clean(self, ud, d):
  211. self._remove_view(ud, d)
  212. bb.utils.remove(ud.configspecfile)