1""" 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# SPDX-License-Identifier: GPL-2.0-only 14# 15 16import os 17import bb 18from 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 32 bzrdir = d.getVar("BZRDIR") or (d.getVar("DL_DIR") + "/bzr") 33 relpath = self._strip_leading_slashes(ud.path) 34 ud.pkgdir = os.path.join(bzrdir, ud.host, relpath) 35 36 ud.setup_revisions(d) 37 38 if not ud.revision: 39 ud.revision = self.latest_revision(ud, d) 40 41 ud.localfile = d.expand('bzr_%s_%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.revision)) 42 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 49 basecmd = d.getVar("FETCHCMD_bzr") or "/usr/bin/env bzr" 50 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") 77 logger.debug("BZR Update %s", ud.url) 78 bb.fetch2.check_network_access(d, bzrcmd, ud.url) 79 runfetchcmd(bzrcmd, d, workdir=os.path.join(ud.pkgdir, os.path.basename(ud.path))) 80 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) 84 logger.debug("BZR Checkout %s", ud.url) 85 bb.utils.mkdirhier(ud.pkgdir) 86 logger.debug("Running %s", bzrcmd) 87 runfetchcmd(bzrcmd, d, workdir=ud.pkgdir) 88 89 scmdata = ud.parm.get("scmdata", "") 90 if scmdata == "keep": 91 tar_flags = "" 92 else: 93 tar_flags = "--exclude='.bzr' --exclude='.bzrtags'" 94 95 # tar them up to a defined filename 96 runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, os.path.basename(ud.pkgdir)), 97 d, cleanup=[ud.localpath], workdir=ud.pkgdir) 98 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 """ 112 logger.debug2("BZR fetcher hitting network for %s", ud.url) 113 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 129