Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 4 | """ |
| 5 | Bitbake "Fetch" implementation for osc (Opensuse build service client). |
| 6 | Based on the svn "Fetch" implementation. |
| 7 | |
| 8 | """ |
| 9 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | import logging |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 11 | import os |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | import bb |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | from bb.fetch2 import FetchMethod |
| 14 | from bb.fetch2 import FetchError |
| 15 | from bb.fetch2 import MissingParameterError |
| 16 | from bb.fetch2 import runfetchcmd |
| 17 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 18 | logger = logging.getLogger(__name__) |
| 19 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | class 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 37 | oscdir = d.getVar("OSCDIR") or (d.getVar("DL_DIR") + "/osc") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | relpath = self._strip_leading_slashes(ud.path) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 39 | ud.pkgdir = os.path.join(oscdir, ud.host) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 45 | pv = d.getVar("PV", False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | rev = bb.fetch2.srcrev_internal_helper(ud, d) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 47 | if rev: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | ud.revision = rev |
| 49 | else: |
| 50 | ud.revision = "" |
| 51 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 52 | ud.localfile = d.expand('%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.path.replace('/', '.'), ud.revision)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | |
| 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 60 | basecmd = d.getVar("FETCHCMD_osc") or "/usr/bin/env osc" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | |
| 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 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 87 | logger.debug2("Fetch: checking for module directory '" + ud.moddir + "'") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 89 | if os.access(os.path.join(d.getVar('OSCDIR'), ud.path, ud.module), os.R_OK): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | oscupdatecmd = self._buildosccommand(ud, d, "update") |
| 91 | logger.info("Update "+ ud.url) |
| 92 | # update sources there |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 93 | logger.debug("Running %s", oscupdatecmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | bb.fetch2.check_network_access(d, oscupdatecmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 95 | runfetchcmd(oscupdatecmd, d, workdir=ud.moddir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | 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) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 101 | logger.debug("Running %s", oscfetchcmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | bb.fetch2.check_network_access(d, oscfetchcmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 103 | runfetchcmd(oscfetchcmd, d, workdir=ud.pkgdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | # tar them up to a defined filename |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 106 | runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d, |
| 107 | cleanup=[ud.localpath], workdir=os.path.join(ud.pkgdir + ud.path)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 | |
| 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 117 | config_path = os.path.join(d.getVar('OSCDIR'), "oscrc") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 118 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 126 | f.write("build-root = %s\n" % d.getVar('WORKDIR')) |
| 127 | f.write("urllist = %s\n" % d.getVar("OSCURLLIST")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | 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 |