Update: MicroPython使用AST构建下py文件抽象语法树以精确解析模块依赖
This commit is contained in:
@@ -100,26 +100,29 @@ if (fs_plus.isDirectorySync(DEFAULT_SRC_DIR)) {
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULT_DIR = path.resolve(ORIGIN_DIR, 'boards/default');
|
||||
|
||||
if (fs_plus.isDirectorySync(DEFAULT_DIR)) {
|
||||
const names = fs.readdirSync(DEFAULT_DIR);
|
||||
for (let name of names) {
|
||||
if (!['all', name].includes(options.type)) {
|
||||
continue;
|
||||
if (!!options.obfuscate) {
|
||||
const DEFAULT_DIR = path.resolve(ORIGIN_DIR, 'boards/default');
|
||||
|
||||
if (fs_plus.isDirectorySync(DEFAULT_DIR)) {
|
||||
const names = fs.readdirSync(DEFAULT_DIR);
|
||||
for (let name of names) {
|
||||
if (!['all', name].includes(options.type)) {
|
||||
continue;
|
||||
}
|
||||
const now = path.resolve(DEFAULT_DIR, name);
|
||||
if (!fs_plus.isDirectorySync(now)) {
|
||||
continue;
|
||||
}
|
||||
const examplesPath = path.resolve(now, 'examples');
|
||||
if (!fs_plus.isDirectorySync(examplesPath)) {
|
||||
continue;
|
||||
}
|
||||
let outputPath = path.resolve(examplesPath, 'map.json');
|
||||
let output = getExamples(examplesPath, !!options.obfuscate);
|
||||
fs_extra.outputJsonSync(outputPath, output, {
|
||||
spaces: ' '
|
||||
});
|
||||
}
|
||||
const now = path.resolve(DEFAULT_DIR, name);
|
||||
if (!fs_plus.isDirectorySync(now)) {
|
||||
continue;
|
||||
}
|
||||
const examplesPath = path.resolve(now, 'examples');
|
||||
if (!fs_plus.isDirectorySync(examplesPath)) {
|
||||
continue;
|
||||
}
|
||||
let outputPath = path.resolve(examplesPath, 'map.json');
|
||||
let output = getExamples(examplesPath, !!options.obfuscate);
|
||||
fs_extra.outputJsonSync(outputPath, output, {
|
||||
spaces: ' '
|
||||
});
|
||||
}
|
||||
}
|
||||
49
scripts/build-libraries.py
Normal file
49
scripts/build-libraries.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import ast
|
||||
import sys
|
||||
import json
|
||||
import os
|
||||
from os import path
|
||||
|
||||
|
||||
def extract_imports(file_path):
|
||||
with open(file_path, "r", encoding="utf-8") as file:
|
||||
tree = ast.parse(file.read(), filename=file_path)
|
||||
|
||||
imports = []
|
||||
|
||||
for node in ast.walk(tree):
|
||||
if isinstance(node, ast.Import):
|
||||
for alias in node.names:
|
||||
imports.append(alias.name)
|
||||
elif isinstance(node, ast.ImportFrom):
|
||||
imports.append(node.module)
|
||||
|
||||
return imports
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
working_path = os.getcwd()
|
||||
target_path = sys.argv[1]
|
||||
if not path.isabs(target_path):
|
||||
target_path = path.abspath(path.join(working_path, target_path))
|
||||
output = None
|
||||
if path.isfile(target_path):
|
||||
exit(0)
|
||||
output = {}
|
||||
content = os.listdir(target_path)
|
||||
for i in content:
|
||||
if i == "map.json":
|
||||
continue
|
||||
name = i[: i.find(".")]
|
||||
output[name] = {}
|
||||
extname = i[i.find(".") :]
|
||||
file_path = path.join(target_path, i)
|
||||
if path.isfile(file_path) and extname == ".py":
|
||||
output[name]["__require__"] = extract_imports(file_path)
|
||||
output[name]["__file__"] = True
|
||||
else:
|
||||
output[name]["__file__"] = False
|
||||
output[name]["__name__"] = i
|
||||
fw = open(path.join(target_path, "map.json"), "w", encoding="utf-8")
|
||||
json.dump(output, fw, indent=4, ensure_ascii=False)
|
||||
fw.close()
|
||||
Reference in New Issue
Block a user