summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndreas Fried <andreas.fried@kit.edu>2021-09-17 16:45:46 +0200
committerAndreas Fried <andreas.fried@kit.edu>2021-09-17 16:45:46 +0200
commit61f23b0e05096e08c39a60f1ee9bc515326309ef (patch)
treebb6959ab28445058d61f518063ad2de4e0b84c3d
parent59681e23515548335533ef0b9f9619f7058db9d4 (diff)
Update scripts to python3, replacing imp.
Debian bullseye will not ship python2 by default anymore, and at any rate there is not going to be just /usr/bin/python. So we might as well go ahead and update our scripts.
-rwxr-xr-xscripts/gen_ir.py23
-rw-r--r--scripts/irops.py1
-rw-r--r--scripts/jinja2/environment.py4
-rwxr-xr-xsupport/statev_sql.py2
4 files changed, 22 insertions, 8 deletions
diff --git a/scripts/gen_ir.py b/scripts/gen_ir.py
index 16127cf..6ce0adb 100755
--- a/scripts/gen_ir.py
+++ b/scripts/gen_ir.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
#
# This file is part of libFirm.
# Copyright (C) 2012 Karlsruhe Institute of Technology.
@@ -8,9 +8,24 @@ sys.dont_write_bytecode = True
import argparse
from jinja2 import Environment
import filters
-import imp
import jinjautil
+# Since python3, importing an arbitrary file became somewhat more involved. This piece of code is from "spack" at
+# https://github.com/epfl-scitas/spack/blob/releases/humagne/lib/spack/llnl/util/lang.py
+# and licensed under Apache-2.0.
+def load_module_from_file(module_name, module_path):
+ if sys.version_info[0] == 3 and sys.version_info[1] >= 5:
+ import importlib.util
+ spec = importlib.util.spec_from_file_location(module_name, module_path)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ else:
+ import importlib.machinery
+ loader = importlib.machinery.SourceFileLoader(module_name, module_path)
+ module = loader.load_module()
+
+ return module
+
def main(argv):
description = 'Generate code/docu from node specification'
@@ -38,9 +53,9 @@ def main(argv):
loader.includedirs += config.includedirs
# Load specfile
- imp.load_source('spec', config.specfile)
+ load_module_from_file('spec', config.specfile)
for num, extrafile in enumerate(config.extra):
- imp.load_source('extra%s' % (num,), extrafile)
+ load_module_from_file('extra%s' % (num,), extrafile)
env = Environment(loader=loader, keep_trailing_newline=True)
env.globals.update(jinjautil.exports)
diff --git a/scripts/irops.py b/scripts/irops.py
index 8d65ad7..b9ca48c 100644
--- a/scripts/irops.py
+++ b/scripts/irops.py
@@ -1,7 +1,6 @@
from jinjautil import export_filter, export
from jinja2._compat import string_types
from filters import arguments
-import imp
import sys
diff --git a/scripts/jinja2/environment.py b/scripts/jinja2/environment.py
index 06156b8..feb6df3 100644
--- a/scripts/jinja2/environment.py
+++ b/scripts/jinja2/environment.py
@@ -637,9 +637,9 @@ class Environment(object):
warn(Warning('py_compile has no effect on pypy or Python 3'))
py_compile = False
else:
- import imp
+ import importlib.util
import marshal
- py_header = imp.get_magic() + \
+ py_header = importlib.util.MAGIC_NUMBER + \
u'\xff\xff\xff\xff'.encode('iso-8859-15')
# Python 3.3 added a source filesize to the header
diff --git a/support/statev_sql.py b/support/statev_sql.py
index 666b4c5..0e2c85a 100755
--- a/support/statev_sql.py
+++ b/support/statev_sql.py
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
#
# This file is part of libFirm.
# Copyright (C) 2012 Karlsruhe Institute of Technology.