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