blob: c56d875300be0d63f049f4345ee25bdec307de83 [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
17import sys
18import logging
19import bb
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020from bb.fetch2 import FetchMethod
21from bb.fetch2 import FetchError
22from bb.fetch2 import runfetchcmd
23from bb.fetch2 import logger
24
25class Bzr(FetchMethod):
26 def supports(self, ud, d):
27 return ud.type in ['bzr']
28
29 def urldata_init(self, ud, d):
30 """
31 init bzr specific variable within url data
32 """
33 # Create paths to bzr checkouts
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080034 bzrdir = d.getVar("BZRDIR") or (d.getVar("DL_DIR") + "/bzr")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035 relpath = self._strip_leading_slashes(ud.path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080036 ud.pkgdir = os.path.join(bzrdir, ud.host, relpath)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037
Brad Bishop6e60e8b2018-02-01 10:27:11 -050038 ud.setup_revisions(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039
40 if not ud.revision:
41 ud.revision = self.latest_revision(ud, d)
42
Brad Bishop6e60e8b2018-02-01 10:27:11 -050043 ud.localfile = d.expand('bzr_%s_%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.revision))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044
45 def _buildbzrcommand(self, ud, d, command):
46 """
47 Build up an bzr commandline based on ud
48 command is "fetch", "update", "revno"
49 """
50
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080051 basecmd = d.getVar("FETCHCMD_bzr") or "/usr/bin/env bzr"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052
53 proto = ud.parm.get('protocol', 'http')
54
55 bzrroot = ud.host + ud.path
56
57 options = []
58
59 if command == "revno":
60 bzrcmd = "%s revno %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
61 else:
62 if ud.revision:
63 options.append("-r %s" % ud.revision)
64
65 if command == "fetch":
66 bzrcmd = "%s branch %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
67 elif command == "update":
68 bzrcmd = "%s pull %s --overwrite" % (basecmd, " ".join(options))
69 else:
70 raise FetchError("Invalid bzr command %s" % command, ud.url)
71
72 return bzrcmd
73
74 def download(self, ud, d):
75 """Fetch url"""
76
77 if os.access(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir), '.bzr'), os.R_OK):
78 bzrcmd = self._buildbzrcommand(ud, d, "update")
79 logger.debug(1, "BZR Update %s", ud.url)
80 bb.fetch2.check_network_access(d, bzrcmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060081 runfetchcmd(bzrcmd, d, workdir=os.path.join(ud.pkgdir, os.path.basename(ud.path)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082 else:
83 bb.utils.remove(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir)), True)
84 bzrcmd = self._buildbzrcommand(ud, d, "fetch")
85 bb.fetch2.check_network_access(d, bzrcmd, ud.url)
86 logger.debug(1, "BZR Checkout %s", ud.url)
87 bb.utils.mkdirhier(ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088 logger.debug(1, "Running %s", bzrcmd)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060089 runfetchcmd(bzrcmd, d, workdir=ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090
91 scmdata = ud.parm.get("scmdata", "")
92 if scmdata == "keep":
93 tar_flags = ""
94 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060095 tar_flags = "--exclude='.bzr' --exclude='.bzrtags'"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
97 # tar them up to a defined filename
Patrick Williamsc0f7c042017-02-23 20:41:17 -060098 runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, os.path.basename(ud.pkgdir)),
99 d, cleanup=[ud.localpath], workdir=ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100
101 def supports_srcrev(self):
102 return True
103
104 def _revision_key(self, ud, d, name):
105 """
106 Return a unique key for the url
107 """
108 return "bzr:" + ud.pkgdir
109
110 def _latest_revision(self, ud, d, name):
111 """
112 Return the latest upstream revision number
113 """
114 logger.debug(2, "BZR fetcher hitting network for %s", ud.url)
115
116 bb.fetch2.check_network_access(d, self._buildbzrcommand(ud, d, "revno"), ud.url)
117
118 output = runfetchcmd(self._buildbzrcommand(ud, d, "revno"), d, True)
119
120 return output.strip()
121
122 def sortable_revision(self, ud, d, name):
123 """
124 Return a sortable revision number which in our case is the revision number
125 """
126
127 return False, self._build_revision(ud, d)
128
129 def _build_revision(self, ud, d):
130 return ud.revision