| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2015-2016 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import sys, os |
| 7 |
import configparser |
| 8 |
import shutil |
| 9 |
import typing as T |
| 10 |
|
| 11 |
from glob import glob |
| 12 |
from .wrap import (open_wrapdburl, read_and_decompress, WrapException, get_releases, |
| 13 |
get_releases_data, parse_patch_url) |
| 14 |
from .. import mesonlib, msubprojects |
| 15 |
|
| 16 |
if T.TYPE_CHECKING: |
| 17 |
import argparse |
| 18 |
|
| 19 |
# Note: when adding arguments, please also add them to the completion |
| 20 |
# scripts in $MESONSRC/data/shell-completions/ |
| 21 |
def add_arguments(parser: 'argparse.ArgumentParser') -> None: |
| 22 |
subparsers = parser.add_subparsers(title='Commands', dest='command') |
| 23 |
subparsers.required = True |
| 24 |
|
| 25 |
p = subparsers.add_parser('list', help='show all available projects') |
| 26 |
p.add_argument('--allow-insecure', default=False, action='store_true', |
| 27 |
help='Allow insecure server connections.') |
| 28 |
p.set_defaults(wrap_func=list_projects) |
| 29 |
|
| 30 |
p = subparsers.add_parser('search', help='search the db by name') |
| 31 |
p.add_argument('--allow-insecure', default=False, action='store_true', |
| 32 |
help='Allow insecure server connections.') |
| 33 |
p.add_argument('name') |
| 34 |
p.set_defaults(wrap_func=search) |
| 35 |
|
| 36 |
p = subparsers.add_parser('install', help='install the specified project') |
| 37 |
p.add_argument('--allow-insecure', default=False, action='store_true', |
| 38 |
help='Allow insecure server connections.') |
| 39 |
p.add_argument('name', nargs='+') |
| 40 |
p.set_defaults(wrap_func=install) |
| 41 |
|
| 42 |
p = msubprojects.add_wrap_update_parser(subparsers) |
| 43 |
p.set_defaults(wrap_func=msubprojects.run) |
| 44 |
|
| 45 |
p = subparsers.add_parser('info', help='show available versions of a project') |
| 46 |
p.add_argument('--allow-insecure', default=False, action='store_true', |
| 47 |
help='Allow insecure server connections.') |
| 48 |
p.add_argument('name') |
| 49 |
p.set_defaults(wrap_func=info) |
| 50 |
|
| 51 |
p = subparsers.add_parser('status', help='show installed and available versions of your projects') |
| 52 |
p.add_argument('--allow-insecure', default=False, action='store_true', |
| 53 |
help='Allow insecure server connections.') |
| 54 |
p.set_defaults(wrap_func=status) |
| 55 |
|
| 56 |
p = subparsers.add_parser('promote', help='bring a subsubproject up to the master project') |
| 57 |
p.add_argument('project_path') |
| 58 |
p.set_defaults(wrap_func=promote) |
| 59 |
|
| 60 |
p = subparsers.add_parser('update-db', help='Update list of projects available in WrapDB (Since 0.61.0)') |
| 61 |
p.add_argument('--allow-insecure', default=False, action='store_true', |
| 62 |
help='Allow insecure server connections.') |
| 63 |
p.set_defaults(wrap_func=update_db) |
| 64 |
|
| 65 |
def list_projects(options: 'argparse.Namespace') -> None: |
| 66 |
releases = get_releases(options.allow_insecure) |
| 67 |
for p in releases.keys(): |
| 68 |
print(p) |
| 69 |
|
| 70 |
def search(options: 'argparse.Namespace') -> None: |
| 71 |
name = options.name |
| 72 |
releases = get_releases(options.allow_insecure) |
| 73 |
for p, info in releases.items(): |
| 74 |
if p.find(name) != -1: |
| 75 |
print(p) |
| 76 |
else: |
| 77 |
for dep in info.get('dependency_names', []): |
| 78 |
if dep.find(name) != -1: |
| 79 |
print(f'Dependency {dep} found in wrap {p}') |
| 80 |
|
| 81 |
def get_latest_version(name: str, allow_insecure: bool) -> T.Tuple[str, str]: |
| 82 |
releases = get_releases(allow_insecure) |
| 83 |
info = releases.get(name) |
| 84 |
if not info: |
| 85 |
raise WrapException(f'Wrap {name} not found in wrapdb') |
| 86 |
latest_version = info['versions'][0] |
| 87 |
version, revision = latest_version.rsplit('-', 1) |
| 88 |
return version, revision |
| 89 |
|
| 90 |
def install_one(name: str, allow_insecure: bool) -> None: |
| 91 |
subproject_dir_name = mesonlib.get_subproject_dir() |
| 92 |
if os.path.isdir(os.path.join(subproject_dir_name, name)): |
| 93 |
raise SystemExit('Subproject directory for this project already exists.') |
| 94 |
wrapfile = os.path.join(subproject_dir_name, name + '.wrap') |
| 95 |
if os.path.exists(wrapfile): |
| 96 |
raise SystemExit(f'Wrap file for {name} already exists.') |
| 97 |
(version, revision) = get_latest_version(name, allow_insecure) |
| 98 |
url = open_wrapdburl(f'https://wrapdb.mesonbuild.com/v2/{name}_{version}-{revision}/{name}.wrap', allow_insecure, True, True) |
| 99 |
with open(wrapfile, 'wb') as f: |
| 100 |
f.write(read_and_decompress(url)) |
| 101 |
print(f'Installed {name} version {version} revision {revision}') |
| 102 |
|
| 103 |
def install(options: 'argparse.Namespace') -> None: |
| 104 |
subproject_dir_name = mesonlib.get_subproject_dir() |
| 105 |
if subproject_dir_name is None or not os.path.isdir(subproject_dir_name): |
| 106 |
raise SystemExit('Subprojects dir not found. Run this script in your source root directory.') |
| 107 |
|
| 108 |
failed = 0 |
| 109 |
|
| 110 |
for name in dict.fromkeys(options.name): |
| 111 |
try: |
| 112 |
install_one(name, options.allow_insecure) |
| 113 |
except WrapException as e: |
| 114 |
print(str(e), file=sys.stderr) |
| 115 |
failed += 1 |
| 116 |
|
| 117 |
if failed > 0: |
| 118 |
raise SystemExit(1) |
| 119 |
|
| 120 |
def get_current_version(wrapfile: str) -> T.Tuple[str, str, str, str, T.Optional[str]]: |
| 121 |
cp = configparser.ConfigParser(interpolation=None) |
| 122 |
cp.read(wrapfile) |
| 123 |
try: |
| 124 |
wrap_data = cp['wrap-file'] |
| 125 |
except KeyError: |
| 126 |
raise WrapException('Not a wrap-file, cannot have come from the wrapdb') |
| 127 |
try: |
| 128 |
patch_url = wrap_data['patch_url'] |
| 129 |
except KeyError: |
| 130 |
# We assume a wrap without a patch_url is probably just an pointer to upstream's |
| 131 |
# build files. The version should be in the tarball filename, even if it isn't |
| 132 |
# purely guaranteed. The wrapdb revision should be 1 because it just needs uploading once. |
| 133 |
branch = mesonlib.search_version(wrap_data['source_filename']) |
| 134 |
revision, patch_filename = '1', None |
| 135 |
else: |
| 136 |
branch, revision = parse_patch_url(patch_url) |
| 137 |
patch_filename = wrap_data['patch_filename'] |
| 138 |
return branch, revision, wrap_data['directory'], wrap_data['source_filename'], patch_filename |
| 139 |
|
| 140 |
def info(options: 'argparse.Namespace') -> None: |
| 141 |
name = options.name |
| 142 |
releases = get_releases(options.allow_insecure) |
| 143 |
info = releases.get(name) |
| 144 |
if not info: |
| 145 |
raise WrapException(f'Wrap {name} not found in wrapdb') |
| 146 |
print(f'Available versions of {name}:') |
| 147 |
for v in info['versions']: |
| 148 |
print(' ', v) |
| 149 |
|
| 150 |
def do_promotion(from_path: str, spdir_name: str) -> None: |
| 151 |
if os.path.isfile(from_path): |
| 152 |
assert from_path.endswith('.wrap') |
| 153 |
shutil.copy(from_path, spdir_name) |
| 154 |
elif os.path.isdir(from_path): |
| 155 |
sproj_name = os.path.basename(from_path) |
| 156 |
outputdir = os.path.join(spdir_name, sproj_name) |
| 157 |
if os.path.exists(outputdir): |
| 158 |
raise SystemExit(f'Output dir {outputdir} already exists. Will not overwrite.') |
| 159 |
|
| 160 |
subpdir = mesonlib.get_subproject_dir() |
| 161 |
if subpdir is not None: |
| 162 |
ignore = shutil.ignore_patterns(subpdir) |
| 163 |
else: |
| 164 |
ignore = None |
| 165 |
|
| 166 |
shutil.copytree(from_path, outputdir, ignore=ignore) |
| 167 |
|
| 168 |
def promote(options: 'argparse.Namespace') -> None: |
| 169 |
argument = options.project_path |
| 170 |
spdir_name = mesonlib.get_subproject_dir() |
| 171 |
if spdir_name is None: |
| 172 |
raise SystemExit('Subproject dir not found. Run this script in your source root directory.') |
| 173 |
sprojs = mesonlib.detect_subprojects(spdir_name) |
| 174 |
|
| 175 |
# check if the argument is a full path to a subproject directory or wrap file |
| 176 |
system_native_path_argument = argument.replace('/', os.sep) |
| 177 |
for matches in sprojs.values(): |
| 178 |
if system_native_path_argument in matches: |
| 179 |
do_promotion(system_native_path_argument, spdir_name) |
| 180 |
return |
| 181 |
|
| 182 |
# otherwise the argument is just a subproject basename which must be unambiguous |
| 183 |
if argument not in sprojs: |
| 184 |
raise SystemExit(f'Subproject {argument} not found in directory tree.') |
| 185 |
matches = sprojs[argument] |
| 186 |
if len(matches) > 1: |
| 187 |
print(f'There is more than one version of {argument} in tree. Please specify which one to promote:\n', file=sys.stderr) |
| 188 |
for s in matches: |
| 189 |
print(s, file=sys.stderr) |
| 190 |
raise SystemExit(1) |
| 191 |
do_promotion(matches[0], spdir_name) |
| 192 |
|
| 193 |
def status(options: 'argparse.Namespace') -> None: |
| 194 |
print('Subproject status') |
| 195 |
subdir = mesonlib.unwrap(mesonlib.get_subproject_dir(), "This should only happen in a non-native subproject") |
| 196 |
for w in glob(f'{subdir}/*.wrap'): |
| 197 |
name = os.path.basename(w)[:-5] |
| 198 |
try: |
| 199 |
(latest_branch, latest_revision) = get_latest_version(name, options.allow_insecure) |
| 200 |
except Exception: |
| 201 |
print('', name, 'not available in wrapdb.', file=sys.stderr) |
| 202 |
continue |
| 203 |
try: |
| 204 |
(current_branch, current_revision, _, _, _) = get_current_version(w) |
| 205 |
except Exception: |
| 206 |
print('', name, 'Wrap file not from wrapdb.', file=sys.stderr) |
| 207 |
continue |
| 208 |
if current_branch == latest_branch and current_revision == latest_revision: |
| 209 |
print('', name, f'up to date. Branch {current_branch}, revision {current_revision}.') |
| 210 |
else: |
| 211 |
print('', name, f'not up to date. Have {current_branch} {current_revision}, but {latest_branch} {latest_revision} is available.') |
| 212 |
|
| 213 |
def update_db(options: 'argparse.Namespace') -> None: |
| 214 |
data = get_releases_data(options.allow_insecure) |
| 215 |
subproject_dir_name = mesonlib.get_subproject_dir() |
| 216 |
if subproject_dir_name is None: |
| 217 |
raise SystemExit('Subproject dir not found. Run this script in your source root directory.') |
| 218 |
|
| 219 |
os.makedirs(subproject_dir_name, exist_ok=True) |
| 220 |
with open(os.path.join(subproject_dir_name, 'wrapdb.json'), 'wb') as f: |
| 221 |
f.write(data) |
| 222 |
|
| 223 |
def run(options: 'argparse.Namespace') -> int: |
| 224 |
options.wrap_func(options) |
| 225 |
return 0 |