blob: 3a6cd2951072a2d3563c762d1e7a0cdfefd97ed6 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: GPL-2.0-only
3#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004"""
5Bitbake "Fetch" implementation for osc (Opensuse build service client).
6Based on the svn "Fetch" implementation.
7
8"""
9
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010import logging
Andrew Geisslerc9f78652020-09-18 14:11:35 -050011import os
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012import bb
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013from bb.fetch2 import FetchMethod
14from bb.fetch2 import FetchError
15from bb.fetch2 import MissingParameterError
16from bb.fetch2 import runfetchcmd
17
Andrew Geisslerc9f78652020-09-18 14:11:35 -050018logger = logging.getLogger(__name__)
19
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020class Osc(FetchMethod):
21 """Class to fetch a module or modules from Opensuse build server
22 repositories."""
23
24 def supports(self, ud, d):
25 """
26 Check to see if a given url can be fetched with osc.
27 """
28 return ud.type in ['osc']
29
30 def urldata_init(self, ud, d):
31 if not "module" in ud.parm:
32 raise MissingParameterError('module', ud.url)
33
34 ud.module = ud.parm["module"]
35
36 # Create paths to osc checkouts
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080037 oscdir = d.getVar("OSCDIR") or (d.getVar("DL_DIR") + "/osc")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 relpath = self._strip_leading_slashes(ud.path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080039 ud.pkgdir = os.path.join(oscdir, ud.host)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040 ud.moddir = os.path.join(ud.pkgdir, relpath, ud.module)
41
42 if 'rev' in ud.parm:
43 ud.revision = ud.parm['rev']
44 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050045 pv = d.getVar("PV", False)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046 rev = bb.fetch2.srcrev_internal_helper(ud, d)
Andrew Geissler82c905d2020-04-13 13:39:40 -050047 if rev:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 ud.revision = rev
49 else:
50 ud.revision = ""
51
Brad Bishop6e60e8b2018-02-01 10:27:11 -050052 ud.localfile = d.expand('%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.path.replace('/', '.'), ud.revision))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053
54 def _buildosccommand(self, ud, d, command):
55 """
56 Build up an ocs commandline based on ud
57 command is "fetch", "update", "info"
58 """
59
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080060 basecmd = d.getVar("FETCHCMD_osc") or "/usr/bin/env osc"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061
62 proto = ud.parm.get('protocol', 'ocs')
63
64 options = []
65
66 config = "-c %s" % self.generate_config(ud, d)
67
68 if ud.revision:
69 options.append("-r %s" % ud.revision)
70
71 coroot = self._strip_leading_slashes(ud.path)
72
73 if command == "fetch":
74 osccmd = "%s %s co %s/%s %s" % (basecmd, config, coroot, ud.module, " ".join(options))
75 elif command == "update":
76 osccmd = "%s %s up %s" % (basecmd, config, " ".join(options))
77 else:
78 raise FetchError("Invalid osc command %s" % command, ud.url)
79
80 return osccmd
81
82 def download(self, ud, d):
83 """
84 Fetch url
85 """
86
87 logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
88
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 if os.access(os.path.join(d.getVar('OSCDIR'), ud.path, ud.module), os.R_OK):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090 oscupdatecmd = self._buildosccommand(ud, d, "update")
91 logger.info("Update "+ ud.url)
92 # update sources there
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 logger.debug(1, "Running %s", oscupdatecmd)
94 bb.fetch2.check_network_access(d, oscupdatecmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060095 runfetchcmd(oscupdatecmd, d, workdir=ud.moddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 else:
97 oscfetchcmd = self._buildosccommand(ud, d, "fetch")
98 logger.info("Fetch " + ud.url)
99 # check out sources there
100 bb.utils.mkdirhier(ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 logger.debug(1, "Running %s", oscfetchcmd)
102 bb.fetch2.check_network_access(d, oscfetchcmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600103 runfetchcmd(oscfetchcmd, d, workdir=ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105 # tar them up to a defined filename
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600106 runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d,
107 cleanup=[ud.localpath], workdir=os.path.join(ud.pkgdir + ud.path))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108
109 def supports_srcrev(self):
110 return False
111
112 def generate_config(self, ud, d):
113 """
114 Generate a .oscrc to be used for this run.
115 """
116
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 config_path = os.path.join(d.getVar('OSCDIR'), "oscrc")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118 if (os.path.exists(config_path)):
119 os.remove(config_path)
120
121 f = open(config_path, 'w')
122 f.write("[general]\n")
123 f.write("apisrv = %s\n" % ud.host)
124 f.write("scheme = http\n")
125 f.write("su-wrapper = su -c\n")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500126 f.write("build-root = %s\n" % d.getVar('WORKDIR'))
127 f.write("urllist = %s\n" % d.getVar("OSCURLLIST"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128 f.write("extra-pkgs = gzip\n")
129 f.write("\n")
130 f.write("[%s]\n" % ud.host)
131 f.write("user = %s\n" % ud.parm["user"])
132 f.write("pass = %s\n" % ud.parm["pswd"])
133 f.close()
134
135 return config_path