【MicroPython】生成Q(string)符号表文件 - py\makeqstrdefs.py
- 脚本使用格式
python py/makeqstrdefs.py [command] [mode] [input-file] [output-directory] [output-file]
command:
pp, split, cat
mode:
qstr, compress
- 输入选项解析
if __name__ == "__main__":
if len(sys.argv) < 6:
print("usage: %s command mode input_filename output_dir output_file" % sys.argv[0])
sys.exit(2)
class Args:
pass
args = Args()
args.command = sys.argv[1]
if args.command == "pp":
# 创建字典 {k:v,}
named_args = {
s: []
for s in [
"pp",
"output",
"cflags",
"cxxflags",
"sources",
"changed_sources",
"dependencies",
]
}
# 向字典k中追加v
for arg in sys.argv[1:]:
if arg in named_args:
current_tok = arg
else:
named_args[current_tok].append(arg)
if not named_args["pp"] or len(named_args["output"]) != 1:
print("usage: %s %s ..." % (sys.argv[0], " ... ".join(named_args)))
sys.exit(2)
for k, v in named_args.items():
# 构建类对象args
setattr(args, k, v)
preprocess()
sys.exit(0)
- 文件预处理
def preprocess():
# 选择源文件列表
# 遍历列表args.changed_sources元素检查是否在列表args.dependencies中
if any(src in args.dependencies for src in args.changed_sources):
sources = args.sources
elif any(args.changed_sources):
sources = args.changed_sources
else:
sources = args.sources
csources = []
cxxsources = []
# 归类收集文件
for source in sources:
if is_cxx_source(source):
cxxsources.append(source)
elif is_c_source(source):
csources.append(source)
try:
os.makedirs(os.path.dirname(args.output[0]))
except OSError:
pass
def pp(flags):
def run(files):
return subprocess.check_output(args.pp + flags + files)
return run
try:
cpus = multiprocessing.cpu_count()
except NotImplementedError:
cpus = 1
p = multiprocessing.dummy.Pool(cpus)
with open(args.output[0], "wb") as out_file:
for flags, sources in (
(args.cflags, csources),
(args.cxxflags, cxxsources),
):
batch_size = (len(sources) + cpus - 1) // cpus
chunks = [sources[i : i + batch_size] for i in range(0, len(sources), batch_size or 1)]
for output in p.imap(pp(flags), chunks):
out_file.write(output)
- 提取MP_QSTR_开头的字符串转换成Q(string)形式
def write_out(fname, output):
if output:
for m, r in [("/", "__"), ("\\", "__"), (":", "@"), ("..", "@@")]:
fname = fname.replace(m, r)
with open(args.output_dir + "/" + fname + "." + args.mode, "w") as f:
f.write("\n".join(output) + "\n")
def process_file(f):
re_line = re.compile(r"#[line]*\s\d+\s\"([^\"]+)\"")
if args.mode == _MODE_QSTR:
re_match = re.compile(r"MP_QSTR_[_a-zA-Z0-9]+")
elif args.mode == _MODE_COMPRESS:
re_match = re.compile(r'MP_COMPRESSED_ROM_TEXT\("([^"]*)"\)')
output = []
last_fname = None
for line in f:
if line.isspace():
continue
# match gcc-like output (# n "file") and msvc-like output (#line n "file")
if line.startswith(("# ", "#line")):
m = re_line.match(line)
assert m is not None
fname = m.group(1)
if not is_c_source(fname) and not is_cxx_source(fname):
continue
if fname != last_fname:
write_out(last_fname, output)
output = []
last_fname = fname
continue
# 新增非"#","#line"时使用给定的输出文件名
else:
last_fname = args.output_file
for match in re_match.findall(line):
if args.mode == _MODE_QSTR:
name = match.replace("MP_QSTR_", "")
output.append("Q(" + name + ")")
elif args.mode == _MODE_COMPRESS:
output.append(match)
if last_fname:
write_out(last_fname, output)
return ""
$ python py/makeqstrdefs.py split "qstr" test.c skull boyer.c
- 为了能供脚本py\makeqstrdata.py使用,需增加QCFG配置
def cat_together():
import glob
import hashlib
# 构建MD5算法的hashlib对象
hasher = hashlib.md5()
all_lines = []
outf = open(args.output_dir + "/out", "wb")
# glob查找文件
for fname in glob.glob(args.output_dir + "/*." + args.mode):
with open(fname, "rb") as f:
lines = f.readlines()
all_lines += lines
# 排序
all_lines.sort()
# 换行转换原来字符串
all_lines = b"\n".join(all_lines)
outf.write(all_lines)
outf.close()
hasher.update(all_lines)
# hash值十六进制输出
new_hash = hasher.hexdigest()
# print(new_hash)
old_hash = None
try:
with open(args.output_file + ".hash") as f:
old_hash = f.read()
except IOError:
pass
mode_full = "QSTR"
if args.mode == _MODE_COMPRESS:
mode_full = "Compressed data"
if old_hash != new_hash:
print(mode_full, "updated")
try:
# rename below might fail if file exists
os.remove(args.output_file)
except:
pass
os.rename(args.output_dir + "/out", args.output_file)
with open(args.output_file + ".hash", "w") as f:
f.write(new_hash)
else:
print(mode_full, "not updated")
$ python py/makeqstrdefs.py cat "qstr" test.c skull boyer.c
【MicroPython】生成Q(string)符号表文件 - py\makeqstrdefs.py的更多相关文章
- (原创)Xilinx的ISE生成模块ngc网表文件
ISE中,右击“Synthesize”,选中“Process Properties”,将“Xilinx Specific Options:-iobuf”的对勾取消. 将取消模块的ioBuff,因为模块 ...
- 程序减肥,strip,eu-strip 及其符号表
程序减肥,strip,eu-strip 及其符号表 我们要给我们生成的可执行文件和DSO瘦身,因为这样可以节省更多的磁盘空间,所以我们移除了debug信息,移除了符号表信息,同时我们还希望万一出事了, ...
- GCC 符号表小结【转】
转自:https://blog.csdn.net/swedenfeng/article/details/53417085 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...
- IDA 与VC 加载符号表
将Windbg路径下的symsrv.yes 拷贝到ida 的安装目录,重新分析ntoskrnl.exe, 加载本地的符号表 添加环境变量 变量名:_NT_SYMBOL_PATH变量值:SRV*{$P ...
- ELF格式文件符号表全解析及readelf命令使用方法
http://blog.csdn.net/edonlii/article/details/8779075 1. 读取ELF文件头: $ readelf -h signELF Header: Magi ...
- mybatis根据表逆向自动化生成代码(自动生成实体类、mapper文件、mapper.xml文件)
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- [转载][FPGA]Quartus代码保护-生成网表文件
0. 简介 当项目过程中,不想给甲方源码时,该如何?我们可以用网表文件qxp或者vqm对资源进行保护. 下面讲解这两个文件的具体生成步骤: 1. 基本概念 QuartusII的qxp文件为Quartu ...
- c++的符号表的肤浅认识
符号表是编译期产生的一个hash列表,随着可执行文件在一起 示例程序 int a = 10; int b; void foo(){ static int c=100; } int main(){ in ...
- iOS 符号表恢复 & 逆向支付宝
推荐序 本文介绍了恢复符号表的技巧,并且利用该技巧实现了在 Xcode 中对目标程序下符号断点调试,该技巧可以显著地减少逆向分析时间.在文章的最后,作者以支付宝为例,展示出通过在 UIAlertVie ...
- 符号表 symbol table 符号 地址 互推
https://zh.wikipedia.org/wiki/符号表 https://en.wikipedia.org/wiki/Symbol_table 在计算机科学中,符号表是一种用于语言翻译器(例 ...
随机推荐
- 华企盾DSC备用服务器无法启动,日志显示“主服务器停机超过十天”
出现该问题有三种情况: 1.主服务器未启动或授权到期: 2.主服务器申请的在线授权且ERP上存在到期的相同序列号: 3.备用服务器的数据库与主服务器连的不是同一个(检查IP和端口以及数据库名).
- IDEA插件(1 UI美化)
一.IDEA 插件怎么安装?(图文讲解) IntelliJ IDEA 支持丰富的插件,熟练使用相关插件,能够有效提高我们的开发效率以及用户体验.那么,要如何在 IDEA 中安装插件呢?这里有两种方式: ...
- 简单几行实现sliver上线提醒
准备魔改sliver去掉一些特征什么的,这里记录一下最简单实现上线消息通过企业微信机器人提醒的方式,这很简单也有很多不足还需要接着改的 protobuf中对消息Beacon和Session的定义如下, ...
- Linux:LVM磁盘扩容实战
1.列出分区表 命令:fdisk -l 查看到目前挂载了 vda1.vda2.vda3,并且查看到还有vdb 100G没有挂载,以及现在的LV大小仅为29G root@JumpServer-DB-T0 ...
- 文心一言 VS 讯飞星火 VS chatgpt (172)-- 算法导论13.3 1题
一.用go语言,在 RB-INSERT 的第 16 行,将新插人的结点 z 着为红色.注意到,如果将 z 着为黑色,则红黑树的性质4就不会被破坏.那么为什么不选择将 z 着为黑色呢? 文心一言: 在红 ...
- JAVA17安装体验JFX17抢先体验
JAVA17安装体验JFX17抢先体验 java17版本是长期支持版,至少更新5年以上.而且商用免费!这里我就来体验一把. 一.下载配置 java 17 官网下载地址:https://www.orac ...
- 基于FPGA的电子琴设计(按键和蜂鸣器)---第一版---郝旭帅电子设计团队
本篇为各位朋友介绍基于FPGA的电子琴设计(按键和蜂鸣器)----第一版. 功能说明: 外部输入七个按键,分别对应音符的"1.2.3.4.5.6.7",唱作do.re.mi.fa. ...
- 【Python】人工智能-机器学习——不调库手撕贝叶斯分类问题
1. 作业内容描述 1.1 背景 数据集大小150 该数据有4个属性,分别如下 Sepal.Length:花萼长度(cm) Sepal.Width:花萼宽度单位(cm) Petal.Length:花瓣 ...
- ThreadLocal真的会造成内存泄漏吗?
ThreadLoca在并发场景中,应用非常多.那ThreadLocal是不是真的会造成内存泄漏?今天给大家做一个分享,个人见解,仅供参考. 1.ThreadLocal的基本原理 简单介绍一下Threa ...
- MongoDB经典故障系列六:CPU利用率太高怎么办?
每逢电商大促,全民狂欢,但热闹是属于疯狂剁手的人们.而开发者们有的缺是"高流量.高访问.高并发"三高下带来的种种问题.为了应对大促期间的高I/O情况,企业会选择MongoDB云数据 ...