blob: 6c60456b53cea3a2755e378851daed9b9e7960f0 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3"""
4Bitbake "Fetch" implementation for osc (Opensuse build service client).
5Based on the svn "Fetch" implementation.
6
7"""
8
9import os
10import sys
11import logging
12import bb
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013from bb.fetch2 import FetchMethod
14from bb.fetch2 import FetchError
15from bb.fetch2 import MissingParameterError
16from bb.fetch2 import runfetchcmd
17
18class Osc(FetchMethod):
19 """Class to fetch a module or modules from Opensuse build server
20 repositories."""
21
22 def supports(self, ud, d):
23 """
24 Check to see if a given url can be fetched with osc.
25 """
26 return ud.type in ['osc']
27
28 def urldata_init(self, ud, d):
29 if not "module" in ud.parm:
30 raise MissingParameterError('module', ud.url)
31
32 ud.module = ud.parm["module"]
33
34 # Create paths to osc checkouts
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080035 oscdir = d.getVar("OSCDIR") or (d.getVar("DL_DIR") + "/osc")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036 relpath = self._strip_leading_slashes(ud.path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080037 ud.pkgdir = os.path.join(oscdir, ud.host)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 ud.moddir = os.path.join(ud.pkgdir, relpath, ud.module)
39
40 if 'rev' in ud.parm:
41 ud.revision = ud.parm['rev']
42 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050043 pv = d.getVar("PV", False)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 rev = bb.fetch2.srcrev_internal_helper(ud, d)
45 if rev and rev != True:
46 ud.revision = rev
47 else:
48 ud.revision = ""
49
Brad Bishop6e60e8b2018-02-01 10:27:11 -050050 ud.localfile = d.expand('%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.path.replace('/', '.'), ud.revision))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051
52 def _buildosccommand(self, ud, d, command):
53 """
54 Build up an ocs commandline based on ud
55 command is "fetch", "update", "info"
56 """
57
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080058 basecmd = d.getVar("FETCHCMD_osc") or "/usr/bin/env osc"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059
60 proto = ud.parm.get('protocol', 'ocs')
61
62 options = []
63
64 config = "-c %s" % self.generate_config(ud, d)
65
66 if ud.revision:
67 options.append("-r %s" % ud.revision)
68
69 coroot = self._strip_leading_slashes(ud.path)
70
71 if command == "fetch":
72 osccmd = "%s %s co %s/%s %s" % (basecmd, config, coroot, ud.module, " ".join(options))
73 elif command == "update":
74 osccmd = "%s %s up %s" % (basecmd, config, " ".join(options))
75 else:
76 raise FetchError("Invalid osc command %s" % command, ud.url)
77
78 return osccmd
79
80 def download(self, ud, d):
81 """
82 Fetch url
83 """
84
85 logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
86
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 if os.access(os.path.join(d.getVar('OSCDIR'), ud.path, ud.module), os.R_OK):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088 oscupdatecmd = self._buildosccommand(ud, d, "update")
89 logger.info("Update "+ ud.url)
90 # update sources there
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091 logger.debug(1, "Running %s", oscupdatecmd)
92 bb.fetch2.check_network_access(d, oscupdatecmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060093 runfetchcmd(oscupdatecmd, d, workdir=ud.moddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 else:
95 oscfetchcmd = self._buildosccommand(ud, d, "fetch")
96 logger.info("Fetch " + ud.url)
97 # check out sources there
98 bb.utils.mkdirhier(ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099 logger.debug(1, "Running %s", oscfetchcmd)
100 bb.fetch2.check_network_access(d, oscfetchcmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600101 runfetchcmd(oscfetchcmd, d, workdir=ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 # tar them up to a defined filename
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600104 runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d,
105 cleanup=[ud.localpath], workdir=os.path.join(ud.pkgdir + ud.path))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106
107 def supports_srcrev(self):
108 return False
109
110 def generate_config(self, ud, d):
111 """
112 Generate a .oscrc to be used for this run.
113 """
114
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115 config_path = os.path.join(d.getVar('OSCDIR'), "oscrc")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 if (os.path.exists(config_path)):
117 os.remove(config_path)
118
119 f = open(config_path, 'w')
120 f.write("[general]\n")
121 f.write("apisrv = %s\n" % ud.host)
122 f.write("scheme = http\n")
123 f.write("su-wrapper = su -c\n")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 f.write("build-root = %s\n" % d.getVar('WORKDIR'))
125 f.write("urllist = %s\n" % d.getVar("OSCURLLIST"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 f.write("extra-pkgs = gzip\n")
127 f.write("\n")
128 f.write("[%s]\n" % ud.host)
129 f.write("user = %s\n" % ud.parm["user"])
130 f.write("pass = %s\n" % ud.parm["pswd"])
131 f.close()
132
133 return config_path