def get_project_modules(self, exclude_dirs=None) -> list: """ 获得项目所有可导入的模块路径. 第三方模块分为项目模块和外部模块. 本程序只负责分析项目模块的依赖关系, 因此通过本方法过滤 掉外部模块的路径. 例如: import sys # builtin module import src.downloader # project module 那么本方法只收录 ['src.downloader'], 不收录 ['sys']. IN: prjdir: str. an absolute project directory. e.g. 'D:/myprj/' OT: prj_modules: list. e.g. ['testflight.test_app_launcher', 'testflight .downloader', ...] """ all_files = file_sniffer.findall_files(self.prjdir) all_pyfiles = [x for x in all_files if x.endswith('.py')] # -> ['D:/myprj/src/app.py', 'D:/myprj/src/downloader.py', ...] if exclude_dirs: # DEL (2019-07-31): 效益较低. 未来将会移除. lk.loga(exclude_dirs) for adir in exclude_dirs: all_files = file_sniffer.findall_files( file_sniffer.prettify_dir(abspath(adir))) # lk.loga(all_files) pyfiles = (x for x in all_files if x.endswith('.py')) for f in pyfiles: all_pyfiles.remove(f) prj_modules = [self.get_module_by_filepath(x) for x in all_pyfiles] # -> ['src.app', 'src.downloader', ...] lk.loga(len(all_pyfiles), prj_modules) return prj_modules
def main(prjdir, pyfile): """ 假设测试项目为 testflight, 启动文件为 testflight/test_app_launcher.py. 项目结构为: testflight |-downloader.py |-parser.py |-test_app_launcher.py # <- here is the launch file. 本模块所有代码设计均可参照测试项目源码来理解. IN: prjdir: project directory. e.g. '../testflight/', make sure it exists. pyfile: the launch file. e.g. '../testflight/test_app_launcher.py', make sure it exists. exclude_dirs: None/iterable. 设置要排除的目录, 目前仅被用于 src.analyser .ModuleAnalyser#get_project_modules() (原本是想提升初始化效率, 实际提升不 大). 未来会考虑移除该参数. OT: """ assert exists(prjdir) and exists(pyfile) # prettify paths prjdir = file_sniffer.prettify_dir(abspath(prjdir)) # '../testflight/' -> 'D:/myprj/testflight/' pyfile = file_sniffer.prettify_file(abspath(pyfile)) # '../testflight/test_app_launcher.py' # -> 'D:/myprj/testflight/test_app_launcher.py' runner = VirtualRunner(prjdir, pyfile) runner.main()
def main(prjdir, pyfile, exclude_dirs=None): """ 假设测试项目为 testflight, 启动文件为 testflight/test_app_launcher.py. 项目结构为: testflight |-downloader.py |-parser.py |-test_app_launcher.py # <- here is the launch file. 本模块所有代码设计均可参照测试项目源码来理解. IN: prjdir: project directory. e.g. '../testflight/', make sure it exists. pyfile: the launch file. e.g. '../testflight/test_app_launcher.py', make sure it exists. exclude_dirs: None/iterable. 设置要排除的目录, 目前仅被用于 src.analyser .ModuleAnalyser#get_project_modules() (原本是想提升初始化效率, 实际提升不 大). 未来会考虑移除该参数. OT: """ assert exists(prjdir) and exists(pyfile) # prettify paths prjdir = file_sniffer.prettify_dir(abspath(prjdir)) # '../testflight/' -> 'D:/myprj/testflight/' pyfile = file_sniffer.prettify_file(abspath(pyfile)) # '../testflight/test_app_launcher.py' # -> 'D:/myprj/testflight/test_app_launcher.py' ast_analyser = AstAnalyser(pyfile) global ast_tree, ast_indents ast_tree = ast_analyser.main() # -> {lino: [(obj_type, obj_val), ...]} ast_indents = ast_analyser.get_lineno_indent_dict(pyfile) # -> {lino: indent} module_analyser = ModuleAnalyser(prjdir, pyfile, exclude_dirs) runner = VirtualRunner(module_analyser) runner.main()