Skip to content

Commit cab3084

Browse files
committed
Fix some bugs in C++ Plus Dist Utils.
修改: CPPPPKG 修改: cpppdist.py
1 parent 068a9df commit cab3084

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

CPPPPKG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
],
1616
"webpage": "https://github.com/cppp-project/cppp-reiconv",
1717
"subpackages": {
18-
"build-aux": "build-aux"
18+
"build-aux": { "path": "build-aux", "ignore": true }
1919
},
2020
"license": [
2121
"LGPLv3"

cpppdist.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
# The cppp library is free software; you can redistribute it
77
# and/or modify it under the terms of the GNU Lesser General Public
8-
# License as published by the Free Software Foundation; either version 2.1
8+
# License as published by the Free Software Foundation; either version 3
99
# of the License, or (at your option) any later version.
1010
#
1111
# The cppp library is distributed in the hope that it will be
@@ -23,9 +23,30 @@
2323
import sys
2424
import json
2525
import shutil
26+
import importlib.util
2627

2728
PACKAGE_INFO = "CPPPPKG"
28-
PROGNAME = "cpppdist"
29+
PROGNAME = "cpppdist.py"
30+
31+
def import_file(path):
32+
"""
33+
Import a Python file from it's path
34+
35+
Args:
36+
path (str): Python file's path
37+
38+
Raises:
39+
ModuleNotFoundError: When module invalid, load error or not found.
40+
41+
Returns:
42+
ModuleType: module
43+
"""
44+
spec = importlib.util.spec_from_file_location(os.path.basename(path).replace(".py", ""), path)
45+
if(spec == None):
46+
raise ModuleNotFoundError("Invalid module or module not found: " + path)
47+
module = importlib.util.module_from_spec(spec)
48+
spec.loader.exec_module(module)
49+
return module
2950

3051
class Package:
3152
"""
@@ -48,10 +69,15 @@ def __init__(self, pkginfo_filepath) -> None:
4869
try:
4970
_subpackages = data["subpackages"]
5071
for pkgname in _subpackages:
51-
pkgpath = _subpackages[pkgname]
52-
pkgmodule_path = pkgpath.replace("/",".").replace("\\",".").strip() + "." + PROGNAME
53-
pkgmodule = eval(f"__import__(pkgmodule_path).{PROGNAME}")
54-
self.subpackages[pkgpath] = pkgmodule.package
72+
pkgpath = _subpackages[pkgname]["path"]
73+
try:
74+
pkgmodule = import_file(os.path.join(pkgpath, PROGNAME))
75+
self.subpackages[pkgpath] = pkgmodule.package
76+
except Exception as e:
77+
if(not _subpackages[pkgname]["ignore"]):
78+
raise
79+
else:
80+
print(f"DEBUG: Ignore a subpackage '{pkgname}': {e}", file=sys.stderr)
5581
except KeyError:
5682
pass
5783
self.filelist = self.get_file_list()
@@ -80,7 +106,7 @@ def copy_files(self, dest):
80106
os.chdir(fwd)
81107
if(os.path.exists(dest)):
82108
shutil.rmtree(dest)
83-
print(f"Copying package '{self.name}' to '{dest}' ... ", file=sys.stderr)
109+
print(f"Copy package '{self.name}' to '{dest}' ... ", file=sys.stderr)
84110
progressbar = ProgressBar(len(self.filelist))
85111
for file in self.filelist:
86112
relpath = os.path.relpath(file, fwd)

0 commit comments

Comments
 (0)