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