Skip to content

Commit 1e7f6f0

Browse files
Charlie Hutchesondcbaker
authored andcommitted
make wrap search custom subprojects dirs
Changes from Dylan: - Don't use Path - merge the lint fixes - Fix some typing issues - Handle non-meson projects - Remove some code duplication by putting `get_subproject_dir` in utils
1 parent f4ad0ef commit 1e7f6f0

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

mesonbuild/utils/universal.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class _VerPickleLoadable(Protocol):
108108
'get_compiler_for_source',
109109
'get_filenames_templates_dict',
110110
'get_rsp_threshold',
111+
'get_subproject_dir',
111112
'get_variable_regex',
112113
'get_wine_shortpath',
113114
'git',
@@ -2065,6 +2066,8 @@ def detect_subprojects(spdir_name: str, current_dir: str = '',
20652066
continue
20662067
append_this = True
20672068
if os.path.isdir(trial):
2069+
spdir_name = get_subproject_dir(trial) or 'subprojects'
2070+
20682071
detect_subprojects(spdir_name, trial, result)
20692072
elif trial.endswith('.wrap') and os.path.isfile(trial):
20702073
basename = os.path.splitext(basename)[0]
@@ -2502,3 +2505,23 @@ def __get__(self, instance: object, cls: T.Type) -> _T:
25022505
value = self.__func(instance)
25032506
setattr(instance, self.__name, value)
25042507
return value
2508+
2509+
2510+
def get_subproject_dir(directory: str = '.') -> T.Optional[str]:
2511+
"""Get the name of the subproject directory for a specific project.
2512+
2513+
If the subproject does not have a meson.build file, it is called in an
2514+
invalid directory, it returns None
2515+
2516+
:param directory: Where to search, defaults to current working directory
2517+
:return: the name of the subproject directory or None.
2518+
"""
2519+
from ..ast import IntrospectionInterpreter
2520+
from ..interpreterbase.exceptions import InvalidArguments
2521+
intr = IntrospectionInterpreter(directory, '', 'none')
2522+
try:
2523+
intr.load_root_meson_file()
2524+
except InvalidArguments: # Root meson file cannot be found
2525+
return None
2526+
2527+
return intr.extract_subproject_dir() or 'subprojects'

mesonbuild/wrap/wrap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def parse_patch_url(patch_url: str) -> T.Tuple[str, str]:
164164
else:
165165
raise WrapException(f'Invalid wrapdb URL {patch_url}')
166166

167+
167168
class WrapException(MesonException):
168169
pass
169170

mesonbuild/wrap/wraptool.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from glob import glob
1212
from .wrap import (open_wrapdburl, read_and_decompress, WrapException, get_releases,
1313
get_releases_data, parse_patch_url)
14-
from pathlib import Path
15-
1614
from .. import mesonlib, msubprojects
1715

1816
if T.TYPE_CHECKING:
@@ -91,11 +89,12 @@ def get_latest_version(name: str, allow_insecure: bool) -> T.Tuple[str, str]:
9189

9290
def install(options: 'argparse.Namespace') -> None:
9391
name = options.name
94-
if not os.path.isdir('subprojects'):
92+
subproject_dir_name = mesonlib.get_subproject_dir()
93+
if subproject_dir_name is None or not os.path.isdir(subproject_dir_name):
9594
raise SystemExit('Subprojects dir not found. Run this script in your source root directory.')
96-
if os.path.isdir(os.path.join('subprojects', name)):
95+
if os.path.isdir(os.path.join(subproject_dir_name, name)):
9796
raise SystemExit('Subproject directory for this project already exists.')
98-
wrapfile = os.path.join('subprojects', name + '.wrap')
97+
wrapfile = os.path.join(subproject_dir_name, name + '.wrap')
9998
if os.path.exists(wrapfile):
10099
raise SystemExit('Wrap file already exists.')
101100
(version, revision) = get_latest_version(name, options.allow_insecure)
@@ -143,11 +142,20 @@ def do_promotion(from_path: str, spdir_name: str) -> None:
143142
outputdir = os.path.join(spdir_name, sproj_name)
144143
if os.path.exists(outputdir):
145144
raise SystemExit(f'Output dir {outputdir} already exists. Will not overwrite.')
146-
shutil.copytree(from_path, outputdir, ignore=shutil.ignore_patterns('subprojects'))
145+
146+
subpdir = mesonlib.get_subproject_dir()
147+
if subpdir is not None:
148+
ignore = shutil.ignore_patterns(subpdir)
149+
else:
150+
ignore = None
151+
152+
shutil.copytree(from_path, outputdir, ignore=ignore)
147153

148154
def promote(options: 'argparse.Namespace') -> None:
149155
argument = options.project_path
150-
spdir_name = 'subprojects'
156+
spdir_name = mesonlib.get_subproject_dir()
157+
if spdir_name is None:
158+
raise SystemExit('Subproject dir not found. Run this script in your source root directory.')
151159
sprojs = mesonlib.detect_subprojects(spdir_name)
152160

153161
# check if the argument is a full path to a subproject directory or wrap file
@@ -170,7 +178,9 @@ def promote(options: 'argparse.Namespace') -> None:
170178

171179
def status(options: 'argparse.Namespace') -> None:
172180
print('Subproject status')
173-
for w in glob('subprojects/*.wrap'):
181+
subdir = mesonlib.get_subproject_dir()
182+
assert subdir is not None, "This should only happen in a non-native subproject"
183+
for w in glob(f'{subdir}/*.wrap'):
174184
name = os.path.basename(w)[:-5]
175185
try:
176186
(latest_branch, latest_revision) = get_latest_version(name, options.allow_insecure)
@@ -189,8 +199,12 @@ def status(options: 'argparse.Namespace') -> None:
189199

190200
def update_db(options: 'argparse.Namespace') -> None:
191201
data = get_releases_data(options.allow_insecure)
192-
Path('subprojects').mkdir(exist_ok=True)
193-
with Path('subprojects/wrapdb.json').open('wb') as f:
202+
subproject_dir_name = mesonlib.get_subproject_dir()
203+
if subproject_dir_name is None:
204+
raise SystemExit('Subproject dir not found. Run this script in your source root directory.')
205+
206+
os.makedirs(subproject_dir_name, exist_ok=True)
207+
with open(os.path.join(subproject_dir_name, 'wrapdb.json'), 'wb') as f:
194208
f.write(data)
195209

196210
def run(options: 'argparse.Namespace') -> int:

0 commit comments

Comments
 (0)