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 |
| 11 | import bb |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | from bb.fetch2 import FetchMethod |
| 13 | from bb.fetch2 import FetchError |
| 14 | from bb.fetch2 import MissingParameterError |
| 15 | from bb.fetch2 import runfetchcmd |
| 16 | |
| 17 | class Osc(FetchMethod): |
| 18 | """Class to fetch a module or modules from Opensuse build server |
| 19 | repositories.""" |
| 20 | |
| 21 | def supports(self, ud, d): |
| 22 | """ |
| 23 | Check to see if a given url can be fetched with osc. |
| 24 | """ |
| 25 | return ud.type in ['osc'] |
| 26 | |
| 27 | def urldata_init(self, ud, d): |
| 28 | if not "module" in ud.parm: |
| 29 | raise MissingParameterError('module', ud.url) |
| 30 | |
| 31 | ud.module = ud.parm["module"] |
| 32 | |
| 33 | # Create paths to osc checkouts |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 34 | oscdir = d.getVar("OSCDIR") or (d.getVar("DL_DIR") + "/osc") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | relpath = self._strip_leading_slashes(ud.path) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 36 | ud.pkgdir = os.path.join(oscdir, ud.host) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | ud.moddir = os.path.join(ud.pkgdir, relpath, ud.module) |
| 38 | |
| 39 | if 'rev' in ud.parm: |
| 40 | ud.revision = ud.parm['rev'] |
| 41 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 42 | pv = d.getVar("PV", False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | rev = bb.fetch2.srcrev_internal_helper(ud, d) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 44 | if rev: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | ud.revision = rev |
| 46 | else: |
| 47 | ud.revision = "" |
| 48 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 49 | 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] | 50 | |
| 51 | def _buildosccommand(self, ud, d, command): |
| 52 | """ |
| 53 | Build up an ocs commandline based on ud |
| 54 | command is "fetch", "update", "info" |
| 55 | """ |
| 56 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 57 | basecmd = d.getVar("FETCHCMD_osc") or "/usr/bin/env osc" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | |
| 59 | proto = ud.parm.get('protocol', 'ocs') |
| 60 | |
| 61 | options = [] |
| 62 | |
| 63 | config = "-c %s" % self.generate_config(ud, d) |
| 64 | |
| 65 | if ud.revision: |
| 66 | options.append("-r %s" % ud.revision) |
| 67 | |
| 68 | coroot = self._strip_leading_slashes(ud.path) |
| 69 | |
| 70 | if command == "fetch": |
| 71 | osccmd = "%s %s co %s/%s %s" % (basecmd, config, coroot, ud.module, " ".join(options)) |
| 72 | elif command == "update": |
| 73 | osccmd = "%s %s up %s" % (basecmd, config, " ".join(options)) |
| 74 | else: |
| 75 | raise FetchError("Invalid osc command %s" % command, ud.url) |
| 76 | |
| 77 | return osccmd |
| 78 | |
| 79 | def download(self, ud, d): |
| 80 | """ |
| 81 | Fetch url |
| 82 | """ |
| 83 | |
| 84 | logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'") |
| 85 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 86 | 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] | 87 | oscupdatecmd = self._buildosccommand(ud, d, "update") |
| 88 | logger.info("Update "+ ud.url) |
| 89 | # update sources there |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | logger.debug(1, "Running %s", oscupdatecmd) |
| 91 | bb.fetch2.check_network_access(d, oscupdatecmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 92 | runfetchcmd(oscupdatecmd, d, workdir=ud.moddir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | else: |
| 94 | oscfetchcmd = self._buildosccommand(ud, d, "fetch") |
| 95 | logger.info("Fetch " + ud.url) |
| 96 | # check out sources there |
| 97 | bb.utils.mkdirhier(ud.pkgdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 98 | logger.debug(1, "Running %s", oscfetchcmd) |
| 99 | bb.fetch2.check_network_access(d, oscfetchcmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 100 | runfetchcmd(oscfetchcmd, d, workdir=ud.pkgdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | # tar them up to a defined filename |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 103 | runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d, |
| 104 | cleanup=[ud.localpath], workdir=os.path.join(ud.pkgdir + ud.path)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | |
| 106 | def supports_srcrev(self): |
| 107 | return False |
| 108 | |
| 109 | def generate_config(self, ud, d): |
| 110 | """ |
| 111 | Generate a .oscrc to be used for this run. |
| 112 | """ |
| 113 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 114 | config_path = os.path.join(d.getVar('OSCDIR'), "oscrc") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | if (os.path.exists(config_path)): |
| 116 | os.remove(config_path) |
| 117 | |
| 118 | f = open(config_path, 'w') |
| 119 | f.write("[general]\n") |
| 120 | f.write("apisrv = %s\n" % ud.host) |
| 121 | f.write("scheme = http\n") |
| 122 | f.write("su-wrapper = su -c\n") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 123 | f.write("build-root = %s\n" % d.getVar('WORKDIR')) |
| 124 | f.write("urllist = %s\n" % d.getVar("OSCURLLIST")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | f.write("extra-pkgs = gzip\n") |
| 126 | f.write("\n") |
| 127 | f.write("[%s]\n" % ud.host) |
| 128 | f.write("user = %s\n" % ud.parm["user"]) |
| 129 | f.write("pass = %s\n" % ud.parm["pswd"]) |
| 130 | f.close() |
| 131 | |
| 132 | return config_path |