Skip to content

Commit 29ff321

Browse files
scripts/: use pipcl.py's run() and log() functions.
Avoids code duplication, and shows file:line in diagnostics.
1 parent 8ce61a3 commit 29ff321

File tree

3 files changed

+57
-88
lines changed

3 files changed

+57
-88
lines changed

scripts/gh_release.py

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@
8989

9090
pymupdf_dir = os.path.abspath( f'{__file__}/../..')
9191

92+
sys.path.insert(0, pymupdf_dir)
93+
import pipcl
94+
del sys.path[0]
95+
96+
log = pipcl.log0
97+
run = pipcl.run
98+
99+
92100
def main():
93101

94102
log( '### main():')
@@ -586,49 +594,6 @@ def relpath(path, start=None):
586594
return os.path.relpath(path, start)
587595

588596

589-
def log(text, caller=0):
590-
'''
591-
Writes `text` to stdout with prefix showing caller path relative to
592-
pymupdf_dir and fn name.
593-
'''
594-
frame_record = inspect.stack( context=0)[ caller+1]
595-
filename = frame_record.filename
596-
line = frame_record.lineno
597-
function = frame_record.function
598-
prefix = f'{relpath(filename, pymupdf_dir)}:{line}:{function}(): '
599-
print(textwrap.indent(text, prefix), flush=1)
600-
601-
602-
def run(command, env_extra=None, check=1, timeout=None):
603-
'''
604-
Runs a command using subprocess.run().
605-
Args:
606-
command:
607-
The command to run.
608-
env_extra:
609-
None or dict containing extra environment variable settings to add
610-
to os.environ.
611-
check:
612-
Whether to raise exception if command fails.
613-
timeout:
614-
If not None, timeout in seconds; passed directory to
615-
subprocess.run(). Note that on MacOS subprocess.run() seems to
616-
leave processes running if timeout expires.
617-
'''
618-
env = None
619-
message = 'Running: '
620-
if env_extra:
621-
env = os.environ.copy()
622-
env.update(env_extra)
623-
message += '\n[environment:\n'
624-
for n, v in env_extra.items():
625-
message += f' {n}={shlex.quote(v)}\n'
626-
message += ']\n'
627-
message += f'{command}'
628-
log(message, caller=1)
629-
return subprocess.run(command, check=check, shell=1, env=env, timeout=timeout)
630-
631-
632597
def platform_tag():
633598
bits = cpu_bits()
634599
if platform.system() == 'Windows':

scripts/sysinstall.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@
8686

8787
import test as test_py
8888

89+
pymupdf_dir = os.path.abspath( f'{__file__}/../..')
90+
91+
sys.path.insert(0, pymupdf_dir)
92+
import pipcl
93+
del sys.path[0]
94+
95+
log = pipcl.log0
96+
8997
# Requirements for a system build and install:
9098
#
9199
# system packages (Debian names):
@@ -106,12 +114,12 @@
106114
def main():
107115

108116
if 1:
109-
print(f'## {__file__}: Starting.')
110-
print(f'{sys.executable=}')
111-
print(f'{platform.python_version()=}')
112-
print(f'{__file__=}')
113-
print(f'{sys.argv=}')
114-
print(f'{sysconfig.get_path("platlib")=}')
117+
log(f'## {__file__}: Starting.')
118+
log(f'{sys.executable=}')
119+
log(f'{platform.python_version()=}')
120+
log(f'{__file__=}')
121+
log(f'{sys.argv=}')
122+
log(f'{sysconfig.get_path("platlib")=}')
115123
run_command(f'python -V', check=0)
116124
run_command(f'python3 -V', check=0)
117125
run_command(f'sudo python -V', check=0)
@@ -132,7 +140,6 @@ def main():
132140
packages = True
133141
prefix = '/usr/local'
134142
pymupdf_do = True
135-
pymupdf_dir = os.path.abspath( f'{__file__}/../..')
136143
root = 'sysinstall_test'
137144
tesseract5 = True
138145
pytest_args = None
@@ -152,7 +159,7 @@ def main():
152159
except StopIteration:
153160
break
154161
if arg in ('-h', '--help'):
155-
print(__doc__)
162+
log(__doc__)
156163
return
157164
elif arg == '--mupdf-do': mupdf_do = int(next(args))
158165
elif arg == '--mupdf-dir': mupdf_dir = next(args)
@@ -161,7 +168,6 @@ def main():
161168
elif arg == '--packages': packages = int(next(args))
162169
elif arg == '--prefix': prefix = next(args)
163170
elif arg == '--pymupdf-do': pymupdf_do = int(next(args))
164-
elif arg == '--pymupdf-dir': pymupdf_dir = next(args)
165171
elif arg == '--root': root = next(args)
166172
elif arg == '--tesseract5': tesseract5 = int(next(args))
167173
elif arg == '--pytest-do': pytest_do = int(next(args))
@@ -191,23 +197,23 @@ def run(command):
191197
if mupdf_git:
192198
# Update existing checkout or do `git clone`.
193199
if os.path.exists(mupdf_dir):
194-
print(f'## Update MuPDF checkout {mupdf_dir}.')
200+
log(f'## Update MuPDF checkout {mupdf_dir}.')
195201
run(f'cd {mupdf_dir} && git pull && git submodule update --init')
196202
else:
197203
# No existing git checkout, so do a fresh clone.
198-
print(f'## Clone MuPDF into {mupdf_dir}.')
204+
log(f'## Clone MuPDF into {mupdf_dir}.')
199205
run(f'git clone --recursive --depth 1 --shallow-submodules {mupdf_git} {mupdf_dir}')
200206

201207
if packages:
202208
# Install required system packages. We assume a Debian package system.
203209
#
204-
print('## Install system packages required by MuPDF.')
210+
log('## Install system packages required by MuPDF.')
205211
run(f'sudo apt update')
206212
run(f'sudo apt install {" ".join(g_sys_packages)}')
207213
# Ubuntu-22.04 has freeglut3-dev, not libglut-dev.
208214
run(f'sudo apt install libglut-dev | sudo apt install freeglut3-dev')
209215
if tesseract5:
210-
print(f'## Force installation of libtesseract-dev version 5.')
216+
log(f'## Force installation of libtesseract-dev version 5.')
211217
# https://stackoverflow.com/questions/76834972/how-can-i-run-pytesseract-python-library-in-ubuntu-22-04
212218
#
213219
run('sudo apt install -y software-properties-common')
@@ -220,12 +226,12 @@ def run(command):
220226
# Build+install MuPDF. We use mupd:Makefile's install-shared-python target.
221227
#
222228
if pip == 'sudo':
223-
print('## Installing Python packages required for building MuPDF and PyMuPDF.')
229+
log('## Installing Python packages required for building MuPDF and PyMuPDF.')
224230
run(f'sudo pip install --upgrade pip')
225231
names = test_py.wrap_get_requires_for_build_wheel(f'{__file__}/../..')
226232
run(f'sudo pip install {names}')
227233

228-
print('## Build and install MuPDF.')
234+
log('## Build and install MuPDF.')
229235
command = f'cd {mupdf_dir}'
230236
command += f' && {sudo}make'
231237
command += f' -j {multiprocessing.cpu_count()}'
@@ -246,10 +252,10 @@ def run(command):
246252

247253
# Build+install PyMuPDF.
248254
#
249-
print('## Build and install PyMuPDF.')
255+
log('## Build and install PyMuPDF.')
250256
def run(command):
251257
return run_command(command, doit=pymupdf_do)
252-
flags_freetype2 = run_command('pkg-config --cflags freetype2', capture_output=1).stdout.strip()
258+
flags_freetype2 = run_command('pkg-config --cflags freetype2', capture=1)
253259
compile_flags = f'-I {root_prefix}/include {flags_freetype2}'
254260
link_flags = f'-L {root_prefix}/lib'
255261
env = ''
@@ -258,7 +264,7 @@ def run(command):
258264
env += f'LDFLAGS="-L {root}/{prefix}/lib" '
259265
env += f'PYMUPDF_SETUP_MUPDF_BUILD= ' # Use system MuPDF.
260266
if use_installer:
261-
print(f'## Building wheel.')
267+
log(f'## Building wheel.')
262268
if pip == 'venv':
263269
venv_name = 'venv-pymupdf-sysinstall'
264270
run(f'pwd')
@@ -277,7 +283,7 @@ def run(command):
277283
wheel = glob.glob(f'dist/*')
278284
assert len(wheel) == 1, f'{wheel=}'
279285
wheel = wheel[0]
280-
print(f'## Installing wheel using `installer`.')
286+
log(f'## Installing wheel using `installer`.')
281287
pv = '.'.join(platform.python_version_tuple()[:2])
282288
p = f'{root_prefix}/lib/python{pv}'
283289
# `python -m installer` fails to overwrite existing files.
@@ -315,13 +321,10 @@ def run(command):
315321
if leaf in dirnames:
316322
pythonpath.append(os.path.join(dirpath, leaf))
317323
pythonpath = ':'.join(pythonpath)
318-
print(f'{pythonpath=}')
324+
log(f'{pythonpath=}')
319325
else:
320326
command = f'{env} pip install -vv --root {root} {os.path.abspath(pymupdf_dir)}'
321327
run( command)
322-
sys.path.insert(0, pymupdf_dir)
323-
import pipcl
324-
del sys.path[0]
325328
pythonpath = pipcl.install_dir(root)
326329

327330
# Show contents of installation directory. This is very slow on github,
@@ -330,7 +333,7 @@ def run(command):
330333

331334
# Run pytest tests.
332335
#
333-
print('## Run PyMuPDF pytest tests.')
336+
log('## Run PyMuPDF pytest tests.')
334337
def run(command):
335338
return run_command(command, doit=pytest_do)
336339
import gh_release
@@ -387,13 +390,11 @@ def run(command):
387390
run(command)
388391

389392

390-
def run_command(command, capture_output=False, check=True, doit=True):
393+
def run_command(command, capture=False, check=True, doit=True):
391394
if doit:
392-
print(f'## Running: {command}')
393-
sys.stdout.flush()
394-
return subprocess.run(command, shell=1, check=check, text=1, capture_output=capture_output)
395+
return pipcl.run(command, capture=capture, check=check, caller=2)
395396
else:
396-
print(f'## Would have run: {command}')
397+
log(f'## Would have run: {command}', caller=2)
397398

398399

399400
if __name__ == '__main__':

scripts/test.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@
135135

136136
pymupdf_dir = os.path.abspath( f'{__file__}/../..')
137137

138+
sys.path.insert(0, pymupdf_dir)
139+
import pipcl
140+
del sys.path[0]
141+
142+
log = pipcl.log0
143+
run = pipcl.run
144+
138145

139146
def main(argv):
140147

@@ -402,7 +409,7 @@ def build(
402409
if venv_quick:
403410
log(f'{venv_quick=}: Not installing packages with pip: {names}')
404411
else:
405-
gh_release.run( f'python -m pip install --upgrade {names}')
412+
run( f'python -m pip install --upgrade {names}')
406413
build_isolation_text = ' --no-build-isolation'
407414

408415
env_extra = dict()
@@ -413,9 +420,9 @@ def build(
413420
if build_flavour:
414421
env_extra['PYMUPDF_SETUP_FLAVOUR'] = build_flavour
415422
if wheel:
416-
gh_release.run(f'pip wheel{build_isolation_text} -v {pymupdf_dir}', env_extra=env_extra)
423+
run(f'pip wheel{build_isolation_text} -v {pymupdf_dir}', env_extra=env_extra)
417424
else:
418-
gh_release.run(f'pip install{build_isolation_text} -v {pymupdf_dir}', env_extra=env_extra)
425+
run(f'pip install{build_isolation_text} -v {pymupdf_dir}', env_extra=env_extra)
419426

420427

421428
def build_pyodide_wheel(pyodide_build_version=None):
@@ -452,14 +459,14 @@ def build_pyodide_wheel(pyodide_build_version=None):
452459
env_extra['PYMUPDF_SETUP_MUPDF_TESSERACT'] = '0'
453460
setup = pyodide_setup(pymupdf_dir, pyodide_build_version=pyodide_build_version)
454461
command = f'{setup} && pyodide build --exports pyinit'
455-
gh_release.run(command, env_extra=env_extra)
462+
run(command, env_extra=env_extra)
456463

457464
# Copy wheel into `wheelhouse/` so it is picked up as a workflow
458465
# artifact.
459466
#
460-
gh_release.run(f'ls -l {pymupdf_dir}/dist/')
461-
gh_release.run(f'mkdir -p {pymupdf_dir}/wheelhouse && cp -p {pymupdf_dir}/dist/* {pymupdf_dir}/wheelhouse/')
462-
gh_release.run(f'ls -l {pymupdf_dir}/wheelhouse/')
467+
run(f'ls -l {pymupdf_dir}/dist/')
468+
run(f'mkdir -p {pymupdf_dir}/wheelhouse && cp -p {pymupdf_dir}/dist/* {pymupdf_dir}/wheelhouse/')
469+
run(f'ls -l {pymupdf_dir}/wheelhouse/')
463470

464471

465472
def pyodide_setup(
@@ -655,7 +662,7 @@ def test(
655662
if venv_quick:
656663
log(f'{venv_quick=}: Not installing test packages: {gh_release.test_packages}')
657664
else:
658-
gh_release.run(f'pip install --upgrade {gh_release.test_packages}')
665+
run(f'pip install --upgrade {gh_release.test_packages}')
659666
run_compound_args = ''
660667
if implementations:
661668
run_compound_args += f' -i {implementations}'
@@ -664,9 +671,9 @@ def test(
664671
env_extra = None
665672
if valgrind:
666673
log('Installing valgrind.')
667-
gh_release.run(f'sudo apt update')
668-
gh_release.run(f'sudo apt install --upgrade valgrind')
669-
gh_release.run(f'valgrind --version')
674+
run(f'sudo apt update')
675+
run(f'sudo apt install --upgrade valgrind')
676+
run(f'valgrind --version')
670677

671678
log('Running PyMuPDF tests under valgrind.')
672679
command = (
@@ -714,7 +721,7 @@ def test(
714721
f.write(text2)
715722

716723
log(f'Running tests with tests/run_compound.py and pytest.')
717-
gh_release.run(command, env_extra=env_extra, timeout=timeout)
724+
run(command, env_extra=env_extra, timeout=timeout)
718725

719726
except subprocess.TimeoutExpired as e:
720727
log(f'Timeout when running tests.')
@@ -768,10 +775,6 @@ def wrap_get_requires_for_build_wheel(dir_):
768775
return ' '.join(ret)
769776

770777

771-
def log(text):
772-
gh_release.log(text, caller=1)
773-
774-
775778
if __name__ == '__main__':
776779
try:
777780
sys.exit(main(sys.argv))

0 commit comments

Comments
 (0)