blob: 36beab6a5bc76faa37c9ec883f71df9fcf7cdb94 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3"""
4BitBake 'Fetch' clearcase implementation
5
6The clearcase fetcher is used to retrieve files from a ClearCase repository.
7
8Usage in the recipe:
9
10 SRC_URI = "ccrc://cc.example.org/ccrc;vob=/example_vob;module=/example_module"
11 SRCREV = "EXAMPLE_CLEARCASE_TAG"
12 PV = "${@d.getVar("SRCREV", False).replace("/", "+")}"
13
14The fetcher uses the rcleartool or cleartool remote client, depending on which one is available.
15
16Supported SRC_URI options are:
17
18- vob
19 (required) The name of the clearcase VOB (with prepending "/")
20
21- module
22 The module in the selected VOB (with prepending "/")
23
24 The module and vob parameters are combined to create
25 the following load rule in the view config spec:
26 load <vob><module>
27
28- proto
29 http or https
30
31Related variables:
32
33 CCASE_CUSTOM_CONFIG_SPEC
34 Write a config spec to this variable in your recipe to use it instead
35 of the default config spec generated by this fetcher.
36 Please note that the SRCREV loses its functionality if you specify
37 this variable. SRCREV is still used to label the archive after a fetch,
38 but it doesn't define what's fetched.
39
40User credentials:
41 cleartool:
42 The login of cleartool is handled by the system. No special steps needed.
43
44 rcleartool:
45 In order to use rcleartool with authenticated users an `rcleartool login` is
46 necessary before using the fetcher.
47"""
48# Copyright (C) 2014 Siemens AG
49#
50# This program is free software; you can redistribute it and/or modify
51# it under the terms of the GNU General Public License version 2 as
52# published by the Free Software Foundation.
53#
54# This program is distributed in the hope that it will be useful,
55# but WITHOUT ANY WARRANTY; without even the implied warranty of
56# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57# GNU General Public License for more details.
58#
59# You should have received a copy of the GNU General Public License along
60# with this program; if not, write to the Free Software Foundation, Inc.,
61# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
62#
63
64import os
65import sys
66import shutil
67import bb
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068from bb.fetch2 import FetchMethod
69from bb.fetch2 import FetchError
70from bb.fetch2 import runfetchcmd
71from bb.fetch2 import logger
72from distutils import spawn
73
74class ClearCase(FetchMethod):
75 """Class to fetch urls via 'clearcase'"""
76 def init(self, d):
77 pass
78
79 def supports(self, ud, d):
80 """
81 Check to see if a given url can be fetched with Clearcase.
82 """
83 return ud.type in ['ccrc']
84
85 def debug(self, msg):
86 logger.debug(1, "ClearCase: %s", msg)
87
88 def urldata_init(self, ud, d):
89 """
90 init ClearCase specific variable within url data
91 """
92 ud.proto = "https"
93 if 'protocol' in ud.parm:
94 ud.proto = ud.parm['protocol']
95 if not ud.proto in ('http', 'https'):
96 raise fetch2.ParameterError("Invalid protocol type", ud.url)
97
98 ud.vob = ''
99 if 'vob' in ud.parm:
100 ud.vob = ud.parm['vob']
101 else:
102 msg = ud.url+": vob must be defined so the fetcher knows what to get."
103 raise MissingParameterError('vob', msg)
104
105 if 'module' in ud.parm:
106 ud.module = ud.parm['module']
107 else:
108 ud.module = ""
109
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110 ud.basecmd = d.getVar("FETCHCMD_ccrc") or spawn.find_executable("cleartool") or spawn.find_executable("rcleartool")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112 if d.getVar("SRCREV") == "INVALID":
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 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.")
114
115 ud.label = d.getVar("SRCREV", False)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 ud.customspec = d.getVar("CCASE_CUSTOM_CONFIG_SPEC")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117
118 ud.server = "%s://%s%s" % (ud.proto, ud.host, ud.path)
119
120 ud.identifier = "clearcase-%s%s-%s" % ( ud.vob.replace("/", ""),
121 ud.module.replace("/", "."),
122 ud.label.replace("/", "."))
123
124 ud.viewname = "%s-view%s" % (ud.identifier, d.getVar("DATETIME", d, True))
125 ud.csname = "%s-config-spec" % (ud.identifier)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500126 ud.ccasedir = os.path.join(d.getVar("DL_DIR"), ud.type)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500127 ud.viewdir = os.path.join(ud.ccasedir, ud.viewname)
128 ud.configspecfile = os.path.join(ud.ccasedir, ud.csname)
129 ud.localfile = "%s.tar.gz" % (ud.identifier)
130
131 self.debug("host = %s" % ud.host)
132 self.debug("path = %s" % ud.path)
133 self.debug("server = %s" % ud.server)
134 self.debug("proto = %s" % ud.proto)
135 self.debug("type = %s" % ud.type)
136 self.debug("vob = %s" % ud.vob)
137 self.debug("module = %s" % ud.module)
138 self.debug("basecmd = %s" % ud.basecmd)
139 self.debug("label = %s" % ud.label)
140 self.debug("ccasedir = %s" % ud.ccasedir)
141 self.debug("viewdir = %s" % ud.viewdir)
142 self.debug("viewname = %s" % ud.viewname)
143 self.debug("configspecfile = %s" % ud.configspecfile)
144 self.debug("localfile = %s" % ud.localfile)
145
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146 ud.localfile = os.path.join(d.getVar("DL_DIR"), ud.localfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147
148 def _build_ccase_command(self, ud, command):
149 """
150 Build up a commandline based on ud
151 command is: mkview, setcs, rmview
152 """
153 options = []
154
155 if "rcleartool" in ud.basecmd:
156 options.append("-server %s" % ud.server)
157
158 basecmd = "%s %s" % (ud.basecmd, command)
159
160 if command is 'mkview':
161 if not "rcleartool" in ud.basecmd:
162 # Cleartool needs a -snapshot view
163 options.append("-snapshot")
164 options.append("-tag %s" % ud.viewname)
165 options.append(ud.viewdir)
166
167 elif command is 'rmview':
168 options.append("-force")
169 options.append("%s" % ud.viewdir)
170
171 elif command is 'setcs':
172 options.append("-overwrite")
173 options.append(ud.configspecfile)
174
175 else:
176 raise FetchError("Invalid ccase command %s" % command)
177
178 ccasecmd = "%s %s" % (basecmd, " ".join(options))
179 self.debug("ccasecmd = %s" % ccasecmd)
180 return ccasecmd
181
182 def _write_configspec(self, ud, d):
183 """
184 Create config spec file (ud.configspecfile) for ccase view
185 """
186 config_spec = ""
187 custom_config_spec = d.getVar("CCASE_CUSTOM_CONFIG_SPEC", d)
188 if custom_config_spec is not None:
189 for line in custom_config_spec.split("\\n"):
190 config_spec += line+"\n"
191 bb.warn("A custom config spec has been set, SRCREV is only relevant for the tarball name.")
192 else:
193 config_spec += "element * CHECKEDOUT\n"
194 config_spec += "element * %s\n" % ud.label
195 config_spec += "load %s%s\n" % (ud.vob, ud.module)
196
197 logger.info("Using config spec: \n%s" % config_spec)
198
199 with open(ud.configspecfile, 'w') as f:
200 f.write(config_spec)
201
202 def _remove_view(self, ud, d):
203 if os.path.exists(ud.viewdir):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204 cmd = self._build_ccase_command(ud, 'rmview');
205 logger.info("cleaning up [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
206 bb.fetch2.check_network_access(d, cmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600207 output = runfetchcmd(cmd, d, workdir=ud.ccasedir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208 logger.info("rmview output: %s", output)
209
210 def need_update(self, ud, d):
211 if ("LATEST" in ud.label) or (ud.customspec and "LATEST" in ud.customspec):
212 ud.identifier += "-%s" % d.getVar("DATETIME",d, True)
213 return True
214 if os.path.exists(ud.localpath):
215 return False
216 return True
217
218 def supports_srcrev(self):
219 return True
220
221 def sortable_revision(self, ud, d, name):
222 return False, ud.identifier
223
224 def download(self, ud, d):
225 """Fetch url"""
226
227 # Make a fresh view
228 bb.utils.mkdirhier(ud.ccasedir)
229 self._write_configspec(ud, d)
230 cmd = self._build_ccase_command(ud, 'mkview')
231 logger.info("creating view [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
232 bb.fetch2.check_network_access(d, cmd, ud.url)
233 try:
234 runfetchcmd(cmd, d)
235 except FetchError as e:
236 if "CRCLI2008E" in e.msg:
237 raise FetchError("%s\n%s\n" % (e.msg, "Call `rcleartool login` in your console to authenticate to the clearcase server before running bitbake."))
238 else:
239 raise e
240
241 # Set configspec: Setting the configspec effectively fetches the files as defined in the configspec
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500242 cmd = self._build_ccase_command(ud, 'setcs');
243 logger.info("fetching data [VOB=%s label=%s view=%s]", ud.vob, ud.label, ud.viewname)
244 bb.fetch2.check_network_access(d, cmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600245 output = runfetchcmd(cmd, d, workdir=ud.viewdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500246 logger.info("%s", output)
247
248 # Copy the configspec to the viewdir so we have it in our source tarball later
249 shutil.copyfile(ud.configspecfile, os.path.join(ud.viewdir, ud.csname))
250
251 # Clean clearcase meta-data before tar
252
253 runfetchcmd('tar -czf "%s" .' % (ud.localpath), d, cleanup = [ud.localpath])
254
255 # Clean up so we can create a new view next time
256 self.clean(ud, d);
257
258 def clean(self, ud, d):
259 self._remove_view(ud, d)
260 bb.utils.remove(ud.configspecfile)