blob: 86f8ddf47bcb8d163471e5420aee8613aad5b51e [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
Andrew Geisslerd5838332022-05-27 11:33:10 -050012import re
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013import bb
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014from bb.fetch2 import FetchMethod
15from bb.fetch2 import FetchError
16from bb.fetch2 import MissingParameterError
17from bb.fetch2 import runfetchcmd
18
Andrew Geisslerc9f78652020-09-18 14:11:35 -050019logger = logging.getLogger(__name__)
20
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021class Osc(FetchMethod):
22 """Class to fetch a module or modules from Opensuse build server
23 repositories."""
24
25 def supports(self, ud, d):
26 """
27 Check to see if a given url can be fetched with osc.
28 """
29 return ud.type in ['osc']
30
31 def urldata_init(self, ud, d):
32 if not "module" in ud.parm:
33 raise MissingParameterError('module', ud.url)
34
35 ud.module = ud.parm["module"]
36
37 # Create paths to osc checkouts
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080038 oscdir = d.getVar("OSCDIR") or (d.getVar("DL_DIR") + "/osc")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 relpath = self._strip_leading_slashes(ud.path)
Andrew Geisslerd5838332022-05-27 11:33:10 -050040 ud.oscdir = oscdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080041 ud.pkgdir = os.path.join(oscdir, ud.host)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042 ud.moddir = os.path.join(ud.pkgdir, relpath, ud.module)
43
44 if 'rev' in ud.parm:
45 ud.revision = ud.parm['rev']
46 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047 pv = d.getVar("PV", False)
Patrick Williams03907ee2022-05-01 06:28:52 -050048 rev = bb.fetch2.srcrev_internal_helper(ud, d, '')
Andrew Geissler82c905d2020-04-13 13:39:40 -050049 if rev:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 ud.revision = rev
51 else:
52 ud.revision = ""
53
Andrew Geisslerd5838332022-05-27 11:33:10 -050054 ud.localfile = d.expand('%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), relpath.replace('/', '.'), ud.revision))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055
56 def _buildosccommand(self, ud, d, command):
57 """
58 Build up an ocs commandline based on ud
59 command is "fetch", "update", "info"
60 """
61
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080062 basecmd = d.getVar("FETCHCMD_osc") or "/usr/bin/env osc"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063
Andrew Geisslerd5838332022-05-27 11:33:10 -050064 proto = ud.parm.get('protocol', 'https')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065
66 options = []
67
68 config = "-c %s" % self.generate_config(ud, d)
69
Andrew Geisslerd5838332022-05-27 11:33:10 -050070 if getattr(ud, 'revision', ''):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 options.append("-r %s" % ud.revision)
72
73 coroot = self._strip_leading_slashes(ud.path)
74
75 if command == "fetch":
Andrew Geisslerd5838332022-05-27 11:33:10 -050076 osccmd = "%s %s -A %s://%s co %s/%s %s" % (basecmd, config, proto, ud.host, coroot, ud.module, " ".join(options))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 elif command == "update":
Andrew Geisslerd5838332022-05-27 11:33:10 -050078 osccmd = "%s %s -A %s://%s up %s" % (basecmd, config, proto, ud.host, " ".join(options))
79 elif command == "api_source":
80 osccmd = "%s %s -A %s://%s api source/%s/%s" % (basecmd, config, proto, ud.host, coroot, ud.module)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 else:
82 raise FetchError("Invalid osc command %s" % command, ud.url)
83
84 return osccmd
85
Andrew Geisslerd5838332022-05-27 11:33:10 -050086 def _latest_revision(self, ud, d, name):
87 """
88 Fetch latest revision for the given package
89 """
90 api_source_cmd = self._buildosccommand(ud, d, "api_source")
91
92 output = runfetchcmd(api_source_cmd, d)
93 match = re.match(r'<directory ?.* rev="(\d+)".*>', output)
94 if match is None:
95 raise FetchError("Unable to parse osc response", ud.url)
96 return match.groups()[0]
97
98 def _revision_key(self, ud, d, name):
99 """
100 Return a unique key for the url
101 """
102 # Collapse adjacent slashes
103 slash_re = re.compile(r"/+")
104 rev = getattr(ud, 'revision', "latest")
105 return "osc:%s%s.%s.%s" % (ud.host, slash_re.sub(".", ud.path), name, rev)
106
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 def download(self, ud, d):
108 """
109 Fetch url
110 """
111
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600112 logger.debug2("Fetch: checking for module directory '" + ud.moddir + "'")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113
Andrew Geisslerd5838332022-05-27 11:33:10 -0500114 if os.access(ud.moddir, os.R_OK):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115 oscupdatecmd = self._buildosccommand(ud, d, "update")
116 logger.info("Update "+ ud.url)
117 # update sources there
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600118 logger.debug("Running %s", oscupdatecmd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119 bb.fetch2.check_network_access(d, oscupdatecmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600120 runfetchcmd(oscupdatecmd, d, workdir=ud.moddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 else:
122 oscfetchcmd = self._buildosccommand(ud, d, "fetch")
123 logger.info("Fetch " + ud.url)
124 # check out sources there
125 bb.utils.mkdirhier(ud.pkgdir)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600126 logger.debug("Running %s", oscfetchcmd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500127 bb.fetch2.check_network_access(d, oscfetchcmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600128 runfetchcmd(oscfetchcmd, d, workdir=ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130 # tar them up to a defined filename
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600131 runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d,
132 cleanup=[ud.localpath], workdir=os.path.join(ud.pkgdir + ud.path))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133
134 def supports_srcrev(self):
135 return False
136
137 def generate_config(self, ud, d):
138 """
139 Generate a .oscrc to be used for this run.
140 """
141
Andrew Geisslerd5838332022-05-27 11:33:10 -0500142 config_path = os.path.join(ud.oscdir, "oscrc")
143 if not os.path.exists(ud.oscdir):
144 bb.utils.mkdirhier(ud.oscdir)
145
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146 if (os.path.exists(config_path)):
147 os.remove(config_path)
148
149 f = open(config_path, 'w')
Andrew Geisslerd5838332022-05-27 11:33:10 -0500150 proto = ud.parm.get('protocol', 'https')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151 f.write("[general]\n")
Andrew Geisslerd5838332022-05-27 11:33:10 -0500152 f.write("apiurl = %s://%s\n" % (proto, ud.host))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153 f.write("su-wrapper = su -c\n")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500154 f.write("build-root = %s\n" % d.getVar('WORKDIR'))
155 f.write("urllist = %s\n" % d.getVar("OSCURLLIST"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 f.write("extra-pkgs = gzip\n")
157 f.write("\n")
Andrew Geisslerd5838332022-05-27 11:33:10 -0500158 f.write("[%s://%s]\n" % (proto, ud.host))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159 f.write("user = %s\n" % ud.parm["user"])
160 f.write("pass = %s\n" % ud.parm["pswd"])
161 f.close()
162
163 return config_path