blob: 658502f9a64bde7d9951707ba95274291a1f0609 [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#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License version 2 as
15# published by the Free Software Foundation.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License along
23# with this program; if not, write to the Free Software Foundation, Inc.,
24# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26import os
27import sys
28import logging
29import bb
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030from bb.fetch2 import FetchMethod
31from bb.fetch2 import FetchError
32from bb.fetch2 import runfetchcmd
33from bb.fetch2 import logger
34
35class Bzr(FetchMethod):
36 def supports(self, ud, d):
37 return ud.type in ['bzr']
38
39 def urldata_init(self, ud, d):
40 """
41 init bzr specific variable within url data
42 """
43 # Create paths to bzr checkouts
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080044 bzrdir = d.getVar("BZRDIR") or (d.getVar("DL_DIR") + "/bzr")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045 relpath = self._strip_leading_slashes(ud.path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080046 ud.pkgdir = os.path.join(bzrdir, ud.host, relpath)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 ud.setup_revisions(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049
50 if not ud.revision:
51 ud.revision = self.latest_revision(ud, d)
52
Brad Bishop6e60e8b2018-02-01 10:27:11 -050053 ud.localfile = d.expand('bzr_%s_%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.revision))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054
55 def _buildbzrcommand(self, ud, d, command):
56 """
57 Build up an bzr commandline based on ud
58 command is "fetch", "update", "revno"
59 """
60
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080061 basecmd = d.getVar("FETCHCMD_bzr") or "/usr/bin/env bzr"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062
63 proto = ud.parm.get('protocol', 'http')
64
65 bzrroot = ud.host + ud.path
66
67 options = []
68
69 if command == "revno":
70 bzrcmd = "%s revno %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
71 else:
72 if ud.revision:
73 options.append("-r %s" % ud.revision)
74
75 if command == "fetch":
76 bzrcmd = "%s branch %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
77 elif command == "update":
78 bzrcmd = "%s pull %s --overwrite" % (basecmd, " ".join(options))
79 else:
80 raise FetchError("Invalid bzr command %s" % command, ud.url)
81
82 return bzrcmd
83
84 def download(self, ud, d):
85 """Fetch url"""
86
87 if os.access(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir), '.bzr'), os.R_OK):
88 bzrcmd = self._buildbzrcommand(ud, d, "update")
89 logger.debug(1, "BZR Update %s", ud.url)
90 bb.fetch2.check_network_access(d, bzrcmd, ud.url)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060091 runfetchcmd(bzrcmd, d, workdir=os.path.join(ud.pkgdir, os.path.basename(ud.path)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 else:
93 bb.utils.remove(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir)), True)
94 bzrcmd = self._buildbzrcommand(ud, d, "fetch")
95 bb.fetch2.check_network_access(d, bzrcmd, ud.url)
96 logger.debug(1, "BZR Checkout %s", ud.url)
97 bb.utils.mkdirhier(ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 logger.debug(1, "Running %s", bzrcmd)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060099 runfetchcmd(bzrcmd, d, workdir=ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100
101 scmdata = ud.parm.get("scmdata", "")
102 if scmdata == "keep":
103 tar_flags = ""
104 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600105 tar_flags = "--exclude='.bzr' --exclude='.bzrtags'"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106
107 # tar them up to a defined filename
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600108 runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, os.path.basename(ud.pkgdir)),
109 d, cleanup=[ud.localpath], workdir=ud.pkgdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110
111 def supports_srcrev(self):
112 return True
113
114 def _revision_key(self, ud, d, name):
115 """
116 Return a unique key for the url
117 """
118 return "bzr:" + ud.pkgdir
119
120 def _latest_revision(self, ud, d, name):
121 """
122 Return the latest upstream revision number
123 """
124 logger.debug(2, "BZR fetcher hitting network for %s", ud.url)
125
126 bb.fetch2.check_network_access(d, self._buildbzrcommand(ud, d, "revno"), ud.url)
127
128 output = runfetchcmd(self._buildbzrcommand(ud, d, "revno"), d, True)
129
130 return output.strip()
131
132 def sortable_revision(self, ud, d, name):
133 """
134 Return a sortable revision number which in our case is the revision number
135 """
136
137 return False, self._build_revision(ud, d)
138
139 def _build_revision(self, ud, d):
140 return ud.revision