For a project which gets deployed to a RO filesystem I want to also deploy pre-compiled .pyc files for all of the Python source.
Thanks to the compileall module this already works, but due to whatever reasons caused by a complex build machinery, those .pyc files can become invalid accidentally (because of unwanted file modifications, source relocation, etc)
So what I'd like to do is to check if there's an already existing pyc file for each with would have been created, if it's being found and if it's valid (i.e. correct source path embedded, correct bytecode, etc).
Is there an integrated way to check "precompilation validity" (like python3 -m compileall --check)? Or what would be a pragmatic way to do this by hand? (e.g. by inspecting some trace or whatever).
The most reliable way I can imagine would be to compare a readily deployed tree to a copy where I deleted and re-created all files using compileall..
You can look at bytecode metadata using that disassembler called
pydisasmthat comes withxdisusing option--format(or-F) option withheader:If you want to get the information from Python, the internal routine that handles the header is called
show_module_header().Be warned though that the last release of
xdison PyPI is over a year old, so use code from github. In fact, I just fixed a bug for this option that prevented more header information from showing.I will be talking at BlackHat Asia mid April 2024, so I plan to have a new xdis release sometime a little before that.