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