Files
linux-kernel-module-cheat/lkmc/import_path.py
Ciro Santilli 六四事件 法轮功 1ab7fbf607 Fix import_path circular dependency by splitting it out.
Use import thread_pool instead from, from is evil.

Fix poweroff.out path for ./trace-boot.
2019-05-12 00:00:02 +00:00

34 lines
1.0 KiB
Python

#!/usr/bin/env python3
import importlib
import os
import sys
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def import_path(path):
'''
https://stackoverflow.com/questions/2601047/import-a-python-module-without-the-py-extension
https://stackoverflow.com/questions/31773310/what-does-the-first-argument-of-the-imp-load-source-method-do
'''
module_name = os.path.basename(path).replace('-', '_')
spec = importlib.util.spec_from_loader(
module_name,
importlib.machinery.SourceFileLoader(module_name, path)
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
sys.modules[module_name] = module
return module
def import_path_relative_root(basename):
return import_path(os.path.join(root_dir, basename))
def import_path_main(basename):
'''
Import an object of the Main class of a given file.
By convention, we call the main object of all our CLI scripts as Main.
'''
return import_path_relative_root(basename).Main()