Skip to content

Commit 3cc1b79

Browse files
committed
Get rid of imp, started giving deprecation warning every time in Python 3.7 in Ubuntu 19.04.
Please python stop torturing me with refactors. Make ./run -u blow up if executable not found, otherwise I go crazy. Get ./test-gdb back to life after the ./run relative path refactor, forgot to test this.
1 parent 300671c commit 3cc1b79

19 files changed

Lines changed: 108 additions & 113 deletions

README.adoc

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3910,20 +3910,10 @@ Similarly to <<qemu-user-mode-does-not-show-stdout-immediately>>, QEMU error mes
39103910
In particular, it does not say anything if you pass it a non-existing executable:
39113911

39123912
....
3913-
./build-userland --clean userland/c/hello.c
3914-
./run --userland userland/c/hello.c
3915-
echo $?
3916-
....
3917-
3918-
does not output anything, except for the `1` exit status.
3919-
3920-
If you run however the raw command without a pipe manually, it shows a helpful error message:
3921-
3922-
....
3923-
Error while loading /path/to/linux-kernel-module-cheat/out/userland/default/x86_64/c/hello.out: No such file or directory
3913+
qemu-x86_64 asdf | cat
39243914
....
39253915

3926-
Tested in de77c62c091f6418e73b64e8a0a19639c587a103 + 1.
3916+
So we just check ourselves manually
39273917

39283918
== Kernel module utilities
39293919

bisect-linux-boot-gem5

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env python3
22

3-
import imp
43
import os
54
import shutil
65
import sys
76

87
import common
9-
build_linux = imp.load_source('build-linux', os.path.join(kwargs['root_dir'], 'build_linux'))
10-
run = imp.load_source('run', os.path.join(kwargs['root_dir'], 'run'))
8+
9+
build_linux = common.import_path_relative_root('build-linux')
10+
run = common.import_path_relative_root('run')
1111

1212
parser = self.get_argparse(
1313
argparse_args={

build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ Which components to build. Default: qemu-buildroot
403403
args = self.get_common_args()
404404
args.update(extra_args)
405405
args['show_time'] = False
406-
self.import_path_main(component_file)(**args)
406+
common.import_path_main(component_file)(**args)
407407
return f
408408

409409
def timed_main(self):

build-baremetal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Build the baremetal examples with crosstool-NG.
133133
in_name, in_ext = os.path.splitext(in_basename)
134134
if (
135135
os.path.isfile(in_path) and
136-
in_ext in (self.env['c_ext'], self.env['asm_ext'])
136+
in_ext in self.env['build_in_exts']
137137
):
138138
out = os.path.join(out_dir, in_name + self.env['baremetal_build_ext'])
139139
src = os.path.join(self.env['baremetal_source_dir'], in_path)

build-userland

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Default: build all examples that have their package dependencies met, e.g.:
8888
for path, in_dirnames, in_filenames in self.sh.walk(target):
8989
for in_filename in in_filenames:
9090
in_ext = os.path.splitext(in_filename)[1]
91-
if not in_ext in self.env['userland_in_exts']:
91+
if not in_ext in self.env['build_in_exts']:
9292
continue
9393
in_path = os.path.join(path, in_filename)
9494
error = thread_pool.submit({

build-userland-in-tree

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
#!/usr/bin/env python3
22

3-
import imp
43
import os
54
import subprocess
65

7-
git_root = subprocess.check_output([
8-
'git',
9-
'rev-parse',
10-
'--show-toplevel',
11-
]).decode().rstrip()
12-
build_userland = imp.load_source(
13-
'build_userland',
14-
os.path.join(git_root, 'build-userland')
15-
)
6+
import common
7+
8+
build_userland = common.import_path_relative_root('build-userland')
169

1710
class Main(build_userland.Main):
1811
def __init__(self):

cli_function.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
import argparse
1212
import bisect
1313
import collections
14-
import imp
1514
import os
1615
import sys
1716

17+
import common
18+
1819
class _Argument:
1920
def __init__(
2021
self,
@@ -188,7 +189,7 @@ def _get_args(self, kwargs):
188189
if config_file is not None:
189190
if os.path.exists(config_file):
190191
config_configs = {}
191-
config = imp.load_source('config', config_file)
192+
config = common.import_path(config_file)
192193
if self.extra_config_params is None:
193194
config.set_args(config_configs)
194195
else:

common.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import enum
99
import functools
1010
import glob
11-
import imp
11+
import importlib
1212
import inspect
1313
import itertools
1414
import json
@@ -105,7 +105,7 @@ def git_sha(repo_path):
105105
consts['header_ext'] = '.h'
106106
consts['kernel_module_ext'] = '.ko'
107107
consts['obj_ext'] = '.o'
108-
consts['userland_in_exts'] = [
108+
consts['build_in_exts'] = [
109109
consts['asm_ext'],
110110
consts['c_ext'],
111111
consts['cxx_ext'],
@@ -136,6 +136,32 @@ def git_sha(repo_path):
136136
consts['emulator_choices'].add(consts['emulator_short_to_long_dict'][key])
137137
consts['host_arch'] = platform.processor()
138138

139+
def import_path(path):
140+
'''
141+
https://stackoverflow.com/questions/2601047/import-a-python-module-without-the-py-extension
142+
https://stackoverflow.com/questions/31773310/what-does-the-first-argument-of-the-imp-load-source-method-do
143+
'''
144+
module_name = os.path.basename(path).replace('-', '_')
145+
spec = importlib.util.spec_from_loader(
146+
module_name,
147+
importlib.machinery.SourceFileLoader(module_name, path)
148+
)
149+
module = importlib.util.module_from_spec(spec)
150+
spec.loader.exec_module(module)
151+
sys.modules[module_name] = module
152+
return module
153+
154+
def import_path_relative_root(basename):
155+
return import_path(os.path.join(consts['root_dir'], basename))
156+
157+
def import_path_main(basename):
158+
'''
159+
Import an object of the Main class of a given file.
160+
161+
By convention, we call the main object of all our CLI scripts as Main.
162+
'''
163+
return import_path_relative_root(basename).Main()
164+
139165
class ExitLoop(Exception):
140166
pass
141167

@@ -905,6 +931,8 @@ def join(*paths):
905931
env['source_path'] = source_path
906932
break
907933
env['image'] = path
934+
elif env['userland'] is not None:
935+
env['image'] = self.resolve_userland_executable(env['userland'])
908936
else:
909937
if env['emulator'] == 'gem5':
910938
env['image'] = env['vmlinux']
@@ -1084,24 +1112,6 @@ def github_make_request(
10841112
_json = {}
10851113
return _json
10861114

1087-
def import_path(self, basename):
1088-
'''
1089-
https://stackoverflow.com/questions/2601047/import-a-python-module-without-the-py-extension
1090-
https://stackoverflow.com/questions/31773310/what-does-the-first-argument-of-the-imp-load-source-method-do
1091-
'''
1092-
return imp.load_source(
1093-
basename.replace('-', '_'),
1094-
os.path.join(self.env['root_dir'], basename)
1095-
)
1096-
1097-
def import_path_main(self, path):
1098-
'''
1099-
Import an object of the Main class of a given file.
1100-
1101-
By convention, we call the main object of all our CLI scripts as Main.
1102-
'''
1103-
return self.import_path(path).Main()
1104-
11051115
def is_arch_supported(self, arch):
11061116
return self.supported_archs is None or arch in self.supported_archs
11071117

@@ -1270,6 +1280,8 @@ def resolve_executable(
12701280
12711281
If the input path is a file, add the executable extension automatically.
12721282
'''
1283+
if not self.env['dry_run'] and not os.path.exists(in_path):
1284+
raise Exception('Input path does not exist: ' + in_path)
12731285
if self.is_subpath(in_path, magic_in_dir):
12741286
# Abspath needed to remove the trailing `/.` which makes e.g. rmrf fail.
12751287
out = os.path.abspath(os.path.join(

run

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,12 @@ Extra options to append at the end of the emulator command line.
404404
if not self.env['dry_run']:
405405
raise Exception('Root filesystem not found. Did you build it? ' \
406406
'Tried to use: ' + self.env['disk_image'])
407-
def raise_image_not_found():
407+
def raise_image_not_found(image):
408408
if not self.env['dry_run']:
409409
raise Exception('Executable image not found. Did you build it? ' \
410-
'Tried to use: ' + self.env['image'])
411-
if self.env['image'] is None:
412-
raise Exception('Baremetal ELF file not found. Tried:\n' + '\n'.join(paths))
410+
'Tried to use: ' + image)
411+
if not os.path.exists(self.env['image']):
412+
raise_image_not_found(self.env['image'])
413413
cmd = debug_vm.copy()
414414
if self.env['emulator'] == 'gem5':
415415
if self.env['quiet']:
@@ -430,7 +430,7 @@ Extra options to append at the end of the emulator command line.
430430
self.env['userland'] is None
431431
):
432432
if not os.path.exists(self.env['linux_image']):
433-
raise_image_not_found()
433+
raise_image_not_found(self.env['image'])
434434
self.sh.run_cmd([os.path.join(self.env['extract_vmlinux'], self.env['linux_image'])])
435435
os.makedirs(os.path.dirname(self.env['gem5_readfile']), exist_ok=True)
436436
self.sh.write_string_to_file(self.env['gem5_readfile'], self.env['gem5_readfile'])
@@ -457,7 +457,7 @@ Extra options to append at the end of the emulator command line.
457457
if self.env['userland'] is not None:
458458
cmd.extend([
459459
self.env['gem5_se_file'], LF,
460-
'--cmd', self.resolve_userland_executable(self.env['userland']), LF,
460+
'--cmd', self.env['image'], LF,
461461
])
462462
if self.env['userland_args'] is not None:
463463
cmd.extend(['--options', self.env['userland_args'], LF])
@@ -551,8 +551,6 @@ Extra options to append at the end of the emulator command line.
551551
debug_args
552552
)
553553
else:
554-
if not os.path.exists(self.env['image']):
555-
raise_image_not_found()
556554
extra_emulator_args.extend(extra_qemu_args)
557555
self.make_run_dirs()
558556
if self.env['debug_vm']:
@@ -725,7 +723,7 @@ Extra options to append at the end of the emulator command line.
725723
cmd.extend(self.env['extra_emulator_args'])
726724
if self.env['userland'] and self.env['emulator'] in ('qemu', 'native'):
727725
# The program and arguments must come at the every end of the CLI.
728-
cmd.extend([self.resolve_userland_executable(self.env['userland']), LF])
726+
cmd.extend([self.env['image'], LF])
729727
if self.env['userland_args'] is not None:
730728
cmd.extend(self.sh.shlex_split(self.env['userland_args']))
731729
if debug_vm or self.env['terminal']:

run-gdb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python3
22

3-
import imp
43
import os
54
import signal
65
import subprocess
@@ -34,7 +33,7 @@ class GdbTestcase:
3433
self.child.setecho(False)
3534
self.child.waitnoecho()
3635
self.child.expect(self.prompt)
37-
test = imp.load_source('test', test_script_path)
36+
test = common.import_path(test_script_path)
3837
exception = None
3938
try:
4039
test.test(self)

0 commit comments

Comments
 (0)