import json
import os
import re
import sys
from plumbum.cmd import cargo
from plumbum import local, FG
def eprint(*args, **kwargs):
kwargs.setdefault('file', sys.stderr)
print(*args, **kwargs)
def load_analysis():
with local.cwd(os.path.join(os.path.dirname(__file__), '../..')):
eprint('running `rustc -Z save-analysis` on c2rust-refactor...')
cargo['rustc', '-p', 'c2rust-refactor','--lib',
'--', '-Z', 'save-analysis'] & FG
files = local.path('target/debug/deps/save-analysis') // 'libc2rust_refactor-*.json'
newest_time = None
newest_file = None
for f in files:
mtime = f.stat().st_mtime
if newest_time is None or newest_time < mtime:
newest_time = mtime
newest_file = f
assert newest_file is not None, 'analysis json not found'
with open(newest_file) as f:
eprint('loading analysis...')
j = json.load(f)
eprint('done (%d defs)' % len(j['defs']))
return j
HEADER_RE = re.compile(r'# `(.*)` Command')
def generate_commands():
out = ''
ana = load_analysis()
cmds = []
for d in ana['defs']:
docs = d.get('docs')
if docs is None:
continue
first, _, rest = docs.strip().partition('\n')
m = HEADER_RE.match(first)
if not m:
continue
name = m.group(1)
content = rest
cmds.append((name, content))
eprint('found %d commands' % len(cmds))
cmds.sort(key=lambda x: x[0])
out += '# Refactoring Commands\n\n'
for name, _ in cmds:
out += ' * [`%s`](#%s)\n' % (name, name)
out += '\n'
for name, content in cmds:
out += '## `%s`\n\n%s\n\n\n' % (name, content)
return out
def main():
print(generate_commands())
if __name__ == '__main__':
main()