blob: 3e567155dcc0fea9826b301c6417bf8da74e79a5 [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
10import os
11import sys
12import logging
13import 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
19class 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 Bishop1a4b7ee2018-12-16 17:11:34 -080036 oscdir = d.getVar("OSCDIR") or (d.getVar("DL_DIR") + "/osc")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037 relpath = self._strip_leading_slashes(ud.path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080038 ud.pkgdir = os.path.join(oscdir, ud.host)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 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 Williamsd8c66bc2016-06-20 12:57:21 -050044 pv = d.getVar("PV", False)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045 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 Bishop6e60e8b2018-02-01 10:27:11 -050051 ud.localfile = d.expand('%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.path.replace('/', '.'), ud.revision))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052
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 Bishop1a4b7ee2018-12-16 17:11:34 -080059 basecmd = d.getVar("FETCHCMD_osc") or "/usr/bin/env osc"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060
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 Bishop6e60e8b2018-02-01 10:27:11 -050088 if os.access(os.path.join(d.getVar('OSCDIR'), ud.path, ud.module), os.R_OK):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089 oscupdatecmd = self._buildosccommand(ud, d, "update")
90 logger.info("Update "+ ud.url)
91 # update sources there
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 logger.debug(1, "Running %s", oscupdatecmd)
93 bb.fetch2.check_network_access(d, oscupdatecmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060094 runfetchcmd(oscupdatecmd, d, workdir=ud.moddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095 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 Williamsc124f4f2015-09-15 14:41:29 -0500100 logger.debug(1, "Running %s", oscfetchcmd)
101 bb.fetch2.check_network_access(d, oscfetchcmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600102 runfetchcmd(oscfetchcmd, d, workdir=ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 # tar them up to a defined filename
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600105 runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d,
106 cleanup=[ud.localpath], workdir=os.path.join(ud.pkgdir + ud.path))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107
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 Bishop6e60e8b2018-02-01 10:27:11 -0500116 config_path = os.path.join(d.getVar('OSCDIR'), "oscrc")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117 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 Bishop6e60e8b2018-02-01 10:27:11 -0500125 f.write("build-root = %s\n" % d.getVar('WORKDIR'))
126 f.write("urllist = %s\n" % d.getVar("OSCURLLIST"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500127 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