Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | """ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 2 | BitBake 'Fetch' implementation for perforce |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 3 | |
| 4 | """ |
| 5 | |
| 6 | # Copyright (C) 2003, 2004 Chris Larson |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 7 | # Copyright (C) 2016 Kodak Alaris, Inc. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 9 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | # |
| 11 | # Based on functions from the base bb module, Copyright 2003 Holger Schurig |
| 12 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | import os |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | import bb |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | from bb.fetch2 import FetchMethod |
| 16 | from bb.fetch2 import FetchError |
| 17 | from bb.fetch2 import logger |
| 18 | from bb.fetch2 import runfetchcmd |
| 19 | |
| 20 | class Perforce(FetchMethod): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 21 | """ Class to fetch from perforce repositories """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | def supports(self, ud, d): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 23 | """ Check to see if a given url can be fetched with perforce. """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | return ud.type in ['p4'] |
| 25 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | def urldata_init(self, ud, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | """ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | Initialize perforce specific variables within url data. If P4CONFIG is |
| 29 | provided by the env, use it. If P4PORT is specified by the recipe, use |
| 30 | its values, which may override the settings in P4CONFIG. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | """ |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 32 | ud.basecmd = d.getVar("FETCHCMD_p4") or "/usr/bin/env p4" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 34 | ud.dldir = d.getVar("P4DIR") or (d.getVar("DL_DIR") + "/p4") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 36 | path = ud.url.split('://')[1] |
| 37 | path = path.split(';')[0] |
| 38 | delim = path.find('@'); |
| 39 | if delim != -1: |
| 40 | (ud.user, ud.pswd) = path.split('@')[0].split(':') |
| 41 | ud.path = path.split('@')[1] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 43 | ud.path = path |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 45 | ud.usingp4config = False |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 46 | p4port = d.getVar('P4PORT') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 48 | if p4port: |
| 49 | logger.debug(1, 'Using recipe provided P4PORT: %s' % p4port) |
| 50 | ud.host = p4port |
| 51 | else: |
| 52 | logger.debug(1, 'Trying to use P4CONFIG to automatically set P4PORT...') |
| 53 | ud.usingp4config = True |
| 54 | p4cmd = '%s info | grep "Server address"' % ud.basecmd |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 55 | bb.fetch2.check_network_access(d, p4cmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 56 | ud.host = runfetchcmd(p4cmd, d, True) |
| 57 | ud.host = ud.host.split(': ')[1].strip() |
| 58 | logger.debug(1, 'Determined P4PORT to be: %s' % ud.host) |
| 59 | if not ud.host: |
| 60 | raise FetchError('Could not determine P4PORT from P4CONFIG') |
| 61 | |
| 62 | if ud.path.find('/...') >= 0: |
| 63 | ud.pathisdir = True |
| 64 | else: |
| 65 | ud.pathisdir = False |
| 66 | |
| 67 | cleanedpath = ud.path.replace('/...', '').replace('/', '.') |
| 68 | cleanedhost = ud.host.replace(':', '.') |
| 69 | ud.pkgdir = os.path.join(ud.dldir, cleanedhost, cleanedpath) |
| 70 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 71 | ud.setup_revisions(d) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 72 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 73 | ud.localfile = d.expand('%s_%s_%s.tar.gz' % (cleanedhost, cleanedpath, ud.revision)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 74 | |
| 75 | def _buildp4command(self, ud, d, command, depot_filename=None): |
| 76 | """ |
| 77 | Build a p4 commandline. Valid commands are "changes", "print", and |
| 78 | "files". depot_filename is the full path to the file in the depot |
| 79 | including the trailing '#rev' value. |
| 80 | """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | p4opt = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 83 | if ud.user: |
| 84 | p4opt += ' -u "%s"' % (ud.user) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 86 | if ud.pswd: |
| 87 | p4opt += ' -P "%s"' % (ud.pswd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 89 | if ud.host and not ud.usingp4config: |
| 90 | p4opt += ' -p %s' % (ud.host) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 91 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 92 | if hasattr(ud, 'revision') and ud.revision: |
| 93 | pathnrev = '%s@%s' % (ud.path, ud.revision) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 95 | pathnrev = '%s' % (ud.path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 97 | if depot_filename: |
| 98 | if ud.pathisdir: # Remove leading path to obtain filename |
| 99 | filename = depot_filename[len(ud.path)-1:] |
| 100 | else: |
| 101 | filename = depot_filename[depot_filename.rfind('/'):] |
| 102 | filename = filename[:filename.find('#')] # Remove trailing '#rev' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 104 | if command == 'changes': |
| 105 | p4cmd = '%s%s changes -m 1 //%s' % (ud.basecmd, p4opt, pathnrev) |
| 106 | elif command == 'print': |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame^] | 107 | if depot_filename is not None: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 108 | p4cmd = '%s%s print -o "p4/%s" "%s"' % (ud.basecmd, p4opt, filename, depot_filename) |
| 109 | else: |
| 110 | raise FetchError('No depot file name provided to p4 %s' % command, ud.url) |
| 111 | elif command == 'files': |
| 112 | p4cmd = '%s%s files //%s' % (ud.basecmd, p4opt, pathnrev) |
| 113 | else: |
| 114 | raise FetchError('Invalid p4 command %s' % command, ud.url) |
| 115 | |
| 116 | return p4cmd |
| 117 | |
| 118 | def _p4listfiles(self, ud, d): |
| 119 | """ |
| 120 | Return a list of the file names which are present in the depot using the |
| 121 | 'p4 files' command, including trailing '#rev' file revision indicator |
| 122 | """ |
| 123 | p4cmd = self._buildp4command(ud, d, 'files') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 124 | bb.fetch2.check_network_access(d, p4cmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 125 | p4fileslist = runfetchcmd(p4cmd, d, True) |
| 126 | p4fileslist = [f.rstrip() for f in p4fileslist.splitlines()] |
| 127 | |
| 128 | if not p4fileslist: |
| 129 | raise FetchError('Unable to fetch listing of p4 files from %s@%s' % (ud.host, ud.path)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 130 | |
| 131 | count = 0 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 132 | filelist = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 133 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 134 | for filename in p4fileslist: |
| 135 | item = filename.split(' - ') |
| 136 | lastaction = item[1].split() |
| 137 | logger.debug(1, 'File: %s Last Action: %s' % (item[0], lastaction[0])) |
| 138 | if lastaction[0] == 'delete': |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 139 | continue |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 140 | filelist.append(item[0]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 142 | return filelist |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 143 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 144 | def download(self, ud, d): |
| 145 | """ Get the list of files, fetch each one """ |
| 146 | filelist = self._p4listfiles(ud, d) |
| 147 | if not filelist: |
| 148 | raise FetchError('No files found in depot %s@%s' % (ud.host, ud.path)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 149 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 150 | bb.utils.remove(ud.pkgdir, True) |
| 151 | bb.utils.mkdirhier(ud.pkgdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 152 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 153 | for afile in filelist: |
| 154 | p4fetchcmd = self._buildp4command(ud, d, 'print', afile) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 155 | bb.fetch2.check_network_access(d, p4fetchcmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 156 | runfetchcmd(p4fetchcmd, d, workdir=ud.pkgdir) |
| 157 | |
| 158 | runfetchcmd('tar -czf %s p4' % (ud.localpath), d, cleanup=[ud.localpath], workdir=ud.pkgdir) |
| 159 | |
| 160 | def clean(self, ud, d): |
| 161 | """ Cleanup p4 specific files and dirs""" |
| 162 | bb.utils.remove(ud.localpath) |
| 163 | bb.utils.remove(ud.pkgdir, True) |
| 164 | |
| 165 | def supports_srcrev(self): |
| 166 | return True |
| 167 | |
| 168 | def _revision_key(self, ud, d, name): |
| 169 | """ Return a unique key for the url """ |
| 170 | return 'p4:%s' % ud.pkgdir |
| 171 | |
| 172 | def _latest_revision(self, ud, d, name): |
| 173 | """ Return the latest upstream scm revision number """ |
| 174 | p4cmd = self._buildp4command(ud, d, "changes") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 175 | bb.fetch2.check_network_access(d, p4cmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 176 | tip = runfetchcmd(p4cmd, d, True) |
| 177 | |
| 178 | if not tip: |
| 179 | raise FetchError('Could not determine the latest perforce changelist') |
| 180 | |
| 181 | tipcset = tip.split(' ')[1] |
| 182 | logger.debug(1, 'p4 tip found to be changelist %s' % tipcset) |
| 183 | return tipcset |
| 184 | |
| 185 | def sortable_revision(self, ud, d, name): |
| 186 | """ Return a sortable revision number """ |
| 187 | return False, self._build_revision(ud, d) |
| 188 | |
| 189 | def _build_revision(self, ud, d): |
| 190 | return ud.revision |
| 191 | |