Skip to content

Commit 15555ca

Browse files
committed
mmpack-build: Move SourceStrapSpecs in dedicated module
Change-Id: Ie0d8b91cf2ef0693504f09fd1898d38a1ce0f1f2
1 parent 5d87d4a commit 15555ca

File tree

3 files changed

+59
-48
lines changed

3 files changed

+59
-48
lines changed

Diff for: src/mmpack_build/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ mmpack_build_sources = files(
3737
'pe_utils.py',
3838
'prefix.py',
3939
'provide.py',
40+
'source_strap_specs.py',
4041
'source_tarball.py',
4142
'src_package.py',
4243
'syspkg_manager.py',

Diff for: src/mmpack_build/source_strap_specs.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# @mindmaze_header@
2+
"""
3+
Representation of loaded sources-strap file
4+
"""
5+
6+
import re
7+
from os.path import join as join_path
8+
from typing import Any, Dict, List, Optional
9+
10+
import yaml
11+
from .common import Assert, get_name_version_from_srcdir
12+
13+
14+
def _multiple_replace(lookup: Dict[str, str], text: str) -> str:
15+
# Create a regular expression from all of the dictionary keys
16+
regex = re.compile('|'.join(lookup.keys()))
17+
18+
# For each match, look up the corresponding value in the dictionary
19+
return regex.sub(lambda match: lookup[match.group(0)], text)
20+
21+
22+
class SourceStrapSpecs:
23+
"""source-strap config"""
24+
25+
def __init__(self, srcdir: str):
26+
name, version = get_name_version_from_srcdir(srcdir)
27+
lookup = {
28+
'@MMPACK_NAME@': name,
29+
'@MMPACK_VERSION@': version,
30+
}
31+
try:
32+
path = join_path(srcdir, 'mmpack/sources-strap')
33+
with open(path, encoding='utf-8') as stream:
34+
content = _multiple_replace(lookup, stream.read())
35+
specs = yaml.load(content, Loader=yaml.BaseLoader)
36+
except FileNotFoundError:
37+
specs = {}
38+
39+
self.depends: List[str] = specs.pop('depends', [])
40+
self.upstream_method: Optional[str] = specs.pop('method', None)
41+
self.upstream_url: Optional[str] = specs.pop('url', None)
42+
self.patches: List[str] = specs.pop('patches', [])
43+
self._opts: Dict[str, str] = specs
44+
45+
self._validate()
46+
47+
def get(self, *args) -> Any:
48+
"""Get optional value"""
49+
return self._opts.get(*args)
50+
51+
def _validate(self):
52+
if self.upstream_method:
53+
if self.upstream_method not in ('git', 'tar'):
54+
raise Assert('Invalid method ' + self.upstream_method)
55+
56+
if not self.upstream_url:
57+
raise Assert('upstream method specified but url missing')

Diff for: src/mmpack_build/source_tarball.py

+1-48
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
from typing import Dict, Iterator, List, NamedTuple, Optional
1414
from zipfile import ZipFile
1515

16-
import yaml
17-
1816
from .common import *
1917
from .errors import DownloadError, MMPackBuildError, ShellException
2018
from .file_utils import filetype
2119
from .prefix import new_mmpack_prefix_context, prefix_install, run_build_script
20+
from .source_strap_specs import SourceStrapSpecs
2221
from .workspace import Workspace, cached_download, find_project_root_folder
2322

2423

@@ -32,52 +31,6 @@ class ProjectSource(NamedTuple):
3231
srcdir: str
3332

3433

35-
def _multiple_replace(lookup: Dict[str, str], text: str) -> str:
36-
# Create a regular expression from all of the dictionary keys
37-
regex = re.compile('|'.join(lookup.keys()))
38-
39-
# For each match, look up the corresponding value in the dictionary
40-
return regex.sub(lambda match: lookup[match.group(0)], text)
41-
42-
43-
class SourceStrapSpecs:
44-
"""source-strap config"""
45-
46-
def __init__(self, srcdir: str):
47-
name, version = get_name_version_from_srcdir(srcdir)
48-
lookup = {
49-
'@MMPACK_NAME@': name,
50-
'@MMPACK_VERSION@': version,
51-
}
52-
try:
53-
path = join_path(srcdir, 'mmpack/sources-strap')
54-
with open(path, encoding='utf-8') as stream:
55-
content = _multiple_replace(lookup, stream.read())
56-
specs = yaml.load(content, Loader=yaml.BaseLoader)
57-
except FileNotFoundError:
58-
specs = {}
59-
60-
self.depends: List[str] = specs.pop('depends', [])
61-
self.upstream_method: Optional[str] = specs.pop('method', None)
62-
self.upstream_url: Optional[str] = specs.pop('url', None)
63-
self.patches: List[str] = specs.pop('patches', [])
64-
self._opts: Dict[str, str] = specs
65-
66-
self._validate()
67-
68-
def get(self, *args) -> Any:
69-
"""Get optional value"""
70-
return self._opts.get(*args)
71-
72-
def _validate(self):
73-
if self.upstream_method:
74-
if self.upstream_method not in ('git', 'tar'):
75-
raise Assert('Invalid method ' + self.upstream_method)
76-
77-
if not self.upstream_url:
78-
raise Assert('upstream method specified but url missing')
79-
80-
8134
def _strip_leading_comp_tar_iter(tar: TarFile) -> Iterator[TarInfo]:
8235
for info in tar.getmembers():
8336
info = copy(info)

0 commit comments

Comments
 (0)