Skip to content

Commit 5edd7cb

Browse files
committed
tools: add utility functions to read/write wrap files
1 parent 7a7ba0f commit 5edd7cb

File tree

7 files changed

+45
-62
lines changed

7 files changed

+45
-62
lines changed

tools/create_release.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from __future__ import annotations
1818
import io
1919
import sys
20-
import configparser
2120
import shutil
2221
import hashlib
2322
import requests
@@ -27,7 +26,7 @@
2726
import json
2827

2928
from pathlib import Path
30-
from utils import is_ci, is_debianlike
29+
from utils import is_ci, is_debianlike, read_wrap, write_wrap
3130

3231
class CreateRelease:
3332
def __init__(self, repo: T.Optional[str], token: T.Optional[str], tag: str):
@@ -45,9 +44,7 @@ def __init__(self, repo: T.Optional[str], token: T.Optional[str], tag: str):
4544
self.create_wrap_file()
4645

4746
def read_wrap(self) -> None:
48-
filename = Path('subprojects', f'{self.name}.wrap')
49-
self.wrap = configparser.ConfigParser(interpolation=None)
50-
self.wrap.read(filename)
47+
self.wrap = read_wrap(self.name)
5148
self.wrap_section = self.wrap[self.wrap.sections()[0]]
5249

5350
def create_patch_zip(self) -> None:
@@ -104,15 +101,7 @@ def create_wrap_file(self) -> None:
104101
self.wrap_section['wrapdb_version'] = self.version
105102

106103
filename = Path(self.tempdir, f'{self.name}.wrap')
107-
108-
# configparser write() adds multiple trailing newlines, collapse them
109-
buf = io.StringIO()
110-
self.wrap.write(buf)
111-
buf.seek(0)
112-
newbuf = buf.read().rstrip('\n') + '\n'
113-
114-
with open(filename, 'w') as f:
115-
f.write(newbuf)
104+
write_wrap(filename, self.wrap)
116105

117106
print('Generated wrap file:')
118107
print(filename.read_text())

tools/format.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
# limitations under the License.
1616

1717
from __future__ import annotations
18-
from configparser import ConfigParser
1918
import json
2019
from pathlib import Path
2120
import subprocess
2221

23-
from utils import format_meson
22+
from utils import format_meson, read_wrap
2423

2524
FORMAT_FILES = {'meson.build', 'meson_options.txt', 'meson.options'}
2625

@@ -33,8 +32,7 @@ def main() -> None:
3332
files = []
3433
for name, info in releases.items():
3534
if f'{name}_{info["versions"][0]}' not in tags:
36-
config = ConfigParser(interpolation=None)
37-
config.read(f'subprojects/{name}.wrap', encoding='utf-8')
35+
config = read_wrap(name)
3836
patch_dir_name = config['wrap-file'].get('patch_directory')
3937
if patch_dir_name:
4038
patch_dir = Path('subprojects', 'packagefiles', patch_dir_name)

tools/internalize_sources.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,12 @@
1616

1717
from __future__ import annotations
1818
from argparse import ArgumentParser
19-
import configparser
2019
from hashlib import sha256
21-
from io import StringIO
2220
import json
2321
from pathlib import Path
2422
import subprocess
2523

26-
def wrap_path(name: str) -> Path:
27-
return Path('subprojects', f'{name}.wrap')
28-
29-
30-
def read_wrap(name: str) -> configparser.ConfigParser:
31-
wrap = configparser.ConfigParser(interpolation=None)
32-
wrap.read(wrap_path(name), encoding='utf-8')
33-
return wrap
34-
24+
from utils import read_wrap, write_wrap, wrap_path
3525

3626
class Internalizer:
3727
def __init__(self, all=False):
@@ -81,10 +71,7 @@ def rewrite_wraps(self) -> None:
8171
wf = wrap['wrap-file']
8272
wf['source_fallback_url'] = wf['source_url']
8373
wf['source_url'] = f'https://github.com/mesonbuild/wrapdb/releases/download/{tag}/{wf["source_filename"]}'
84-
buf = StringIO()
85-
wrap.write(buf)
86-
with wrap_path(name).open('w') as fh:
87-
fh.write(buf.getvalue().strip() + '\n')
74+
write_wrap(wrap_path(name), wrap)
8875
print(f'Rewrote source_url for {len(self.rewrite)} projects.')
8976

9077

tools/sanity_checks.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import shutil
3030

3131
from pathlib import Path
32-
from utils import Version, ci_group, is_ci, is_alpinelike, is_debianlike, is_macos, is_windows, is_msys, FormattingError, format_meson
32+
from utils import Version, ci_group, is_ci, is_alpinelike, is_debianlike, is_macos, is_windows, is_msys, read_wrap, FormattingError, format_meson
3333

3434
PERMITTED_FILES = {'generator.sh', 'meson.build', 'meson_options.txt', 'meson.options', 'LICENSE.build'}
3535
PER_PROJECT_PERMITTED_FILES: dict[str, set[str]] = {
@@ -279,8 +279,7 @@ def test_releases(self) -> None:
279279
extra_checks = latest_tag not in self.tags
280280

281281
# Make sure we can load wrap file
282-
config = configparser.ConfigParser(interpolation=None)
283-
config.read(f'subprojects/{name}.wrap', encoding='utf-8')
282+
config = read_wrap(name)
284283

285284
# Basic checks
286285
with self.subTest(step='basic'):
@@ -692,8 +691,7 @@ def test_meson_version_deps(self) -> None:
692691
self.report_meson_version_deps(name)
693692

694693
def report_meson_version_deps(self, name: str, builddir: str = '_build') -> None:
695-
wrap = configparser.ConfigParser(interpolation=None)
696-
wrap.read(f'subprojects/{name}.wrap', encoding='utf-8')
694+
wrap = read_wrap(name)
697695
patch_dir = self.get_patch_path(wrap['wrap-file'])
698696
if not patch_dir:
699697
# only check projects maintained downstream

tools/update-packagefiles.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
import shutil
2121
from pathlib import Path
2222

23-
def read_wrap(filename: Path) -> configparser.SectionProxy:
24-
wrap = configparser.ConfigParser(interpolation=None)
25-
wrap.read(filename)
26-
return wrap[wrap.sections()[0]]
23+
from utils import read_wrap
2724

2825
def read_archive_files(path: Path, base_path: Path) -> set[Path]:
2926
if path.suffix == '.zip':
@@ -36,7 +33,8 @@ def read_archive_files(path: Path, base_path: Path) -> set[Path]:
3633

3734
if __name__ == '__main__':
3835
for f in Path('subprojects').glob('*.wrap'):
39-
wrap_section = read_wrap(f)
36+
wrap = read_wrap(f.stem)
37+
wrap_section = wrap[wrap.sections()[0]]
4038
patch_directory = wrap_section.get('patch_directory')
4139
if not patch_directory:
4240
continue

tools/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
# limitations under the License.
1414

1515
from __future__ import annotations
16+
import configparser
1617
from contextlib import contextmanager
1718
import functools
19+
import io
1820
import operator
1921
import os
2022
from pathlib import Path
@@ -104,6 +106,21 @@ def __cmp(self, other: 'Version', comparator: T.Callable[[T.Any, T.Any], bool])
104106
# versions are equal, so compare revisions
105107
return comparator(self._r, other._r)
106108

109+
def wrap_path(name: str) -> Path:
110+
return Path('subprojects', f'{name}.wrap')
111+
112+
def read_wrap(name: str) -> configparser.ConfigParser:
113+
config = configparser.ConfigParser(interpolation=None)
114+
config.read(wrap_path(name), encoding='utf-8')
115+
return config
116+
117+
def write_wrap(path: Path, config: configparser.ConfigParser) -> None:
118+
# configparser write() adds multiple trailing newlines, collapse them
119+
buf = io.StringIO()
120+
config.write(buf)
121+
with path.open('w', encoding='utf-8') as f:
122+
f.write(buf.getvalue().rstrip('\n') + '\n')
123+
107124
@contextmanager
108125
def ci_group(title):
109126
if is_ci() or sys.stdout.isatty():

tools/versions.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
from __future__ import annotations
1818
from argparse import ArgumentParser, Namespace
19-
from configparser import ConfigParser
2019
from functools import cache
2120
from hashlib import sha256
2221
from itertools import count
2322
import json
2423
import os
24+
from pathlib import Path
2525
import re
2626
import subprocess
2727
import sys
@@ -30,6 +30,8 @@
3030

3131
import requests
3232

33+
from utils import read_wrap, wrap_path
34+
3335
WRAP_URL_TEMPLATE = (
3436
'https://github.com/mesonbuild/wrapdb/blob/master/subprojects/{0}.wrap'
3537
)
@@ -127,18 +129,11 @@ def get_wrap_versions() -> dict[str, str]:
127129
}
128130

129131

130-
def get_wrap_contents(name: str) -> ConfigParser:
131-
'''Return a ConfigParser loaded with the specified wrap.'''
132-
wrap = ConfigParser(interpolation=None)
133-
wrap.read(f'subprojects/{name}.wrap', encoding='utf-8')
134-
return wrap
135-
136-
137132
def get_port_wraps() -> set[str]:
138133
'''Return the names of wraps that have a patch directory.'''
139134
ports = set()
140135
for name, info in get_releases().items():
141-
wrap = get_wrap_contents(name)
136+
wrap = read_wrap(name)
142137
if wrap.has_option('wrap-file', 'patch_directory'):
143138
ports.add(name)
144139
return ports
@@ -148,8 +143,8 @@ def update_wrap(name: str, old_ver: str, new_ver: str) -> None:
148143
'''Try to update the specified wrap file from old_ver to new_ver.'''
149144

150145
# read wrap file
151-
filename = f'subprojects/{name}.wrap'
152-
with open(filename) as f:
146+
path = wrap_path(name)
147+
with path.open(encoding='utf-8') as f:
153148
lines = f.readlines()
154149

155150
# update versions
@@ -191,7 +186,7 @@ def update_wrap(name: str, old_ver: str, new_ver: str) -> None:
191186
break
192187

193188
# write
194-
with open(filename, 'w') as f:
189+
with path.open('w', encoding='utf-8') as f:
195190
f.write(''.join(lines))
196191

197192

@@ -298,20 +293,21 @@ def do_commit(args: Namespace) -> None:
298293
else:
299294
raise ValueError("Can't autogenerate commit message; specify -m")
300295

301-
commit_files = [
302-
'ci_config.json', 'releases.json', f'subprojects/{name}.wrap'
296+
commit_files: list[str | Path] = [
297+
'ci_config.json', 'releases.json', wrap_path(name)
303298
]
304-
patch_dir = get_wrap_contents(name).get(
299+
patch_dir = read_wrap(name).get(
305300
'wrap-file', 'patch_directory', fallback=None
306301
)
307302
if patch_dir:
308303
commit_files.append(f'subprojects/packagefiles/{patch_dir}')
309304

310305
# suppress Git summary output and recreate it ourselves so we can also
311306
# show the diffstat, to confirm we've committed the right files
312-
subprocess.check_call(
313-
['git', 'commit', '-m', f'{name}: {args.message}', '-q'] + commit_files
314-
)
307+
cmd: list[str | Path] = [
308+
'git', 'commit', '-m', f'{name}: {args.message}', '-q'
309+
]
310+
subprocess.check_call(cmd + commit_files)
315311
subprocess.check_call(['git', 'log', '-1', '--format=[%h] %s'])
316312
subprocess.check_call(['git', 'diff', '--stat', 'HEAD^..HEAD'])
317313

@@ -381,7 +377,7 @@ def do_list(args: Namespace) -> None:
381377
'wrapdb': cur_vers[name],
382378
'upstream': upstream_vers.get(name),
383379
'port': name in ports,
384-
'source': get_wrap_contents(name).get(
380+
'source': read_wrap(name).get(
385381
'wrap-file', 'source_url'
386382
)
387383
} for name in wraps

0 commit comments

Comments
 (0)