Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright BitBake Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | """ |
| 7 | Bitbake "Fetch" implementation for osc (Opensuse build service client). |
| 8 | Based on the svn "Fetch" implementation. |
| 9 | |
| 10 | """ |
| 11 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | import logging |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 13 | import os |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 14 | import re |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | import bb |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | from bb.fetch2 import FetchMethod |
| 17 | from bb.fetch2 import FetchError |
| 18 | from bb.fetch2 import MissingParameterError |
| 19 | from bb.fetch2 import runfetchcmd |
| 20 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 21 | logger = logging.getLogger(__name__) |
| 22 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 23 | class Osc(FetchMethod): |
| 24 | """Class to fetch a module or modules from Opensuse build server |
| 25 | repositories.""" |
| 26 | |
| 27 | def supports(self, ud, d): |
| 28 | """ |
| 29 | Check to see if a given url can be fetched with osc. |
| 30 | """ |
| 31 | return ud.type in ['osc'] |
| 32 | |
| 33 | def urldata_init(self, ud, d): |
| 34 | if not "module" in ud.parm: |
| 35 | raise MissingParameterError('module', ud.url) |
| 36 | |
| 37 | ud.module = ud.parm["module"] |
| 38 | |
| 39 | # Create paths to osc checkouts |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 40 | oscdir = d.getVar("OSCDIR") or (d.getVar("DL_DIR") + "/osc") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | relpath = self._strip_leading_slashes(ud.path) |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 42 | ud.oscdir = oscdir |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 43 | ud.pkgdir = os.path.join(oscdir, ud.host) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | ud.moddir = os.path.join(ud.pkgdir, relpath, ud.module) |
| 45 | |
| 46 | if 'rev' in ud.parm: |
| 47 | ud.revision = ud.parm['rev'] |
| 48 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 49 | pv = d.getVar("PV", False) |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 50 | rev = bb.fetch2.srcrev_internal_helper(ud, d, '') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 51 | if rev: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | ud.revision = rev |
| 53 | else: |
| 54 | ud.revision = "" |
| 55 | |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 56 | ud.localfile = d.expand('%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), relpath.replace('/', '.'), ud.revision)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | |
| 58 | def _buildosccommand(self, ud, d, command): |
| 59 | """ |
| 60 | Build up an ocs commandline based on ud |
| 61 | command is "fetch", "update", "info" |
| 62 | """ |
| 63 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 64 | basecmd = d.getVar("FETCHCMD_osc") or "/usr/bin/env osc" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 66 | proto = ud.parm.get('protocol', 'https') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | |
| 68 | options = [] |
| 69 | |
| 70 | config = "-c %s" % self.generate_config(ud, d) |
| 71 | |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 72 | if getattr(ud, 'revision', ''): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | options.append("-r %s" % ud.revision) |
| 74 | |
| 75 | coroot = self._strip_leading_slashes(ud.path) |
| 76 | |
| 77 | if command == "fetch": |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 78 | osccmd = "%s %s -A %s://%s co %s/%s %s" % (basecmd, config, proto, ud.host, coroot, ud.module, " ".join(options)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | elif command == "update": |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 80 | osccmd = "%s %s -A %s://%s up %s" % (basecmd, config, proto, ud.host, " ".join(options)) |
| 81 | elif command == "api_source": |
| 82 | osccmd = "%s %s -A %s://%s api source/%s/%s" % (basecmd, config, proto, ud.host, coroot, ud.module) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | else: |
| 84 | raise FetchError("Invalid osc command %s" % command, ud.url) |
| 85 | |
| 86 | return osccmd |
| 87 | |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 88 | def _latest_revision(self, ud, d, name): |
| 89 | """ |
| 90 | Fetch latest revision for the given package |
| 91 | """ |
| 92 | api_source_cmd = self._buildosccommand(ud, d, "api_source") |
| 93 | |
| 94 | output = runfetchcmd(api_source_cmd, d) |
| 95 | match = re.match(r'<directory ?.* rev="(\d+)".*>', output) |
| 96 | if match is None: |
| 97 | raise FetchError("Unable to parse osc response", ud.url) |
| 98 | return match.groups()[0] |
| 99 | |
| 100 | def _revision_key(self, ud, d, name): |
| 101 | """ |
| 102 | Return a unique key for the url |
| 103 | """ |
| 104 | # Collapse adjacent slashes |
| 105 | slash_re = re.compile(r"/+") |
| 106 | rev = getattr(ud, 'revision', "latest") |
| 107 | return "osc:%s%s.%s.%s" % (ud.host, slash_re.sub(".", ud.path), name, rev) |
| 108 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | def download(self, ud, d): |
| 110 | """ |
| 111 | Fetch url |
| 112 | """ |
| 113 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 114 | logger.debug2("Fetch: checking for module directory '" + ud.moddir + "'") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 116 | if os.access(ud.moddir, os.R_OK): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | oscupdatecmd = self._buildosccommand(ud, d, "update") |
| 118 | logger.info("Update "+ ud.url) |
| 119 | # update sources there |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 120 | logger.debug("Running %s", oscupdatecmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 121 | bb.fetch2.check_network_access(d, oscupdatecmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 122 | runfetchcmd(oscupdatecmd, d, workdir=ud.moddir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 123 | else: |
| 124 | oscfetchcmd = self._buildosccommand(ud, d, "fetch") |
| 125 | logger.info("Fetch " + ud.url) |
| 126 | # check out sources there |
| 127 | bb.utils.mkdirhier(ud.pkgdir) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 128 | logger.debug("Running %s", oscfetchcmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 129 | bb.fetch2.check_network_access(d, oscfetchcmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 130 | runfetchcmd(oscfetchcmd, d, workdir=ud.pkgdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 131 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 132 | # tar them up to a defined filename |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 133 | runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d, |
| 134 | cleanup=[ud.localpath], workdir=os.path.join(ud.pkgdir + ud.path)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 135 | |
| 136 | def supports_srcrev(self): |
| 137 | return False |
| 138 | |
| 139 | def generate_config(self, ud, d): |
| 140 | """ |
| 141 | Generate a .oscrc to be used for this run. |
| 142 | """ |
| 143 | |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 144 | config_path = os.path.join(ud.oscdir, "oscrc") |
| 145 | if not os.path.exists(ud.oscdir): |
| 146 | bb.utils.mkdirhier(ud.oscdir) |
| 147 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | if (os.path.exists(config_path)): |
| 149 | os.remove(config_path) |
| 150 | |
| 151 | f = open(config_path, 'w') |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 152 | proto = ud.parm.get('protocol', 'https') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 153 | f.write("[general]\n") |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 154 | f.write("apiurl = %s://%s\n" % (proto, ud.host)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | f.write("su-wrapper = su -c\n") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 156 | f.write("build-root = %s\n" % d.getVar('WORKDIR')) |
| 157 | f.write("urllist = %s\n" % d.getVar("OSCURLLIST")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | f.write("extra-pkgs = gzip\n") |
| 159 | f.write("\n") |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 160 | f.write("[%s://%s]\n" % (proto, ud.host)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 161 | f.write("user = %s\n" % ud.parm["user"]) |
| 162 | f.write("pass = %s\n" % ud.parm["pswd"]) |
| 163 | f.close() |
| 164 | |
| 165 | return config_path |