blob: fc558f50b047c7fa7ce676859c8fb94d2372bf49 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""
2BitBake 'Fetch' implementation for bzr.
3
4"""
5
6# Copyright (C) 2007 Ross Burton
7# Copyright (C) 2007 Richard Purdie
8#
9# Classes for obtaining upstream sources for the
10# BitBake build tools.
11# Copyright (C) 2003, 2004 Chris Larson
12#
Brad Bishopc342db32019-05-15 21:57:59 -040013# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015
16import os
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017import bb
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018from bb.fetch2 import FetchMethod
19from bb.fetch2 import FetchError
20from bb.fetch2 import runfetchcmd
21from bb.fetch2 import logger
22
23class Bzr(FetchMethod):
24 def supports(self, ud, d):
25 return ud.type in ['bzr']
26
27 def urldata_init(self, ud, d):
28 """
29 init bzr specific variable within url data
30 """
31 # Create paths to bzr checkouts
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080032 bzrdir = d.getVar("BZRDIR") or (d.getVar("DL_DIR") + "/bzr")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033 relpath = self._strip_leading_slashes(ud.path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080034 ud.pkgdir = os.path.join(bzrdir, ud.host, relpath)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036 ud.setup_revisions(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037
38 if not ud.revision:
39 ud.revision = self.latest_revision(ud, d)
40
Brad Bishop6e60e8b2018-02-01 10:27:11 -050041 ud.localfile = d.expand('bzr_%s_%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.revision))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042
43 def _buildbzrcommand(self, ud, d, command):
44 """
45 Build up an bzr commandline based on ud
46 command is "fetch", "update", "revno"
47 """
48
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080049 basecmd = d.getVar("FETCHCMD_bzr") or "/usr/bin/env bzr"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050
51 proto = ud.parm.get('protocol', 'http')
52
53 bzrroot = ud.host + ud.path
54
55 options = []
56
57 if command == "revno":
58 bzrcmd = "%s revno %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
59 else:
60 if ud.revision:
61 options.append("-r %s" % ud.revision)
62
63 if command == "fetch":
64 bzrcmd = "%s branch %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
65 elif command == "update":
66 bzrcmd = "%s pull %s --overwrite" % (basecmd, " ".join(options))
67 else:
68 raise FetchError("Invalid bzr command %s" % command, ud.url)
69
70 return bzrcmd
71
72 def download(self, ud, d):
73 """Fetch url"""
74
75 if os.access(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir), '.bzr'), os.R_OK):
76 bzrcmd = self._buildbzrcommand(ud, d, "update")
Andrew Geisslerd1e89492021-02-12 15:35:20 -060077 logger.debug("BZR Update %s", ud.url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 bb.fetch2.check_network_access(d, bzrcmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060079 runfetchcmd(bzrcmd, d, workdir=os.path.join(ud.pkgdir, os.path.basename(ud.path)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 else:
81 bb.utils.remove(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir)), True)
82 bzrcmd = self._buildbzrcommand(ud, d, "fetch")
83 bb.fetch2.check_network_access(d, bzrcmd, ud.url)
Andrew Geisslerd1e89492021-02-12 15:35:20 -060084 logger.debug("BZR Checkout %s", ud.url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 bb.utils.mkdirhier(ud.pkgdir)
Andrew Geisslerd1e89492021-02-12 15:35:20 -060086 logger.debug("Running %s", bzrcmd)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060087 runfetchcmd(bzrcmd, d, workdir=ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088
89 scmdata = ud.parm.get("scmdata", "")
90 if scmdata == "keep":
91 tar_flags = ""
92 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060093 tar_flags = "--exclude='.bzr' --exclude='.bzrtags'"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094
95 # tar them up to a defined filename
Patrick Williamsc0f7c042017-02-23 20:41:17 -060096 runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, os.path.basename(ud.pkgdir)),
97 d, cleanup=[ud.localpath], workdir=ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098
99 def supports_srcrev(self):
100 return True
101
102 def _revision_key(self, ud, d, name):
103 """
104 Return a unique key for the url
105 """
106 return "bzr:" + ud.pkgdir
107
108 def _latest_revision(self, ud, d, name):
109 """
110 Return the latest upstream revision number
111 """
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600112 logger.debug2("BZR fetcher hitting network for %s", ud.url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113
114 bb.fetch2.check_network_access(d, self._buildbzrcommand(ud, d, "revno"), ud.url)
115
116 output = runfetchcmd(self._buildbzrcommand(ud, d, "revno"), d, True)
117
118 return output.strip()
119
120 def sortable_revision(self, ud, d, name):
121 """
122 Return a sortable revision number which in our case is the revision number
123 """
124
125 return False, self._build_revision(ud, d)
126
127 def _build_revision(self, ud, d):
128 return ud.revision