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
2323import sys
2424import json
2525import shutil
26+ import importlib .util
2627
2728PACKAGE_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
3051class 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