5
5
#
6
6
# The cppp library is free software; you can redistribute it
7
7
# 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
9
9
# of the License, or (at your option) any later version.
10
10
#
11
11
# The cppp library is distributed in the hope that it will be
23
23
import sys
24
24
import json
25
25
import shutil
26
+ import importlib .util
26
27
27
28
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
29
50
30
51
class Package :
31
52
"""
@@ -48,10 +69,15 @@ def __init__(self, pkginfo_filepath) -> None:
48
69
try :
49
70
_subpackages = data ["subpackages" ]
50
71
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 )
55
81
except KeyError :
56
82
pass
57
83
self .filelist = self .get_file_list ()
@@ -80,7 +106,7 @@ def copy_files(self, dest):
80
106
os .chdir (fwd )
81
107
if (os .path .exists (dest )):
82
108
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 )
84
110
progressbar = ProgressBar (len (self .filelist ))
85
111
for file in self .filelist :
86
112
relpath = os .path .relpath (file , fwd )
0 commit comments