diff --git a/utils/info_md.py b/utils/info_md.py new file mode 100755 index 00000000..d8babcdf --- /dev/null +++ b/utils/info_md.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 + +import hashlib +import os +import subprocess +import sys +"""Module for generating the Info.md file found in the database directory.""" + +info_md_header = """ +# Details + +Last updated on {human_date} ({iso8601_date}). + +Created using [Project X-Ray](https://github.com/SymbiFlow/prjxray) version [{commit_hash_short}](https://github.com/SymbiFlow/prjxray/commit/{commit_hash_long}). + +Latest commit was; +``` +{commit_latest} +``` + +""" + +info_md_section = """ + +## Database for [{part_line}]({part_line}/) + +### Settings + +Created using following [settings/{part_line}.sh (sha256: {settings_sha256})](https://github.com/SymbiFlow/prjxray/blob/{commit_hash_long}/settings/{part_line}.sh) +```shell +{settings_contents} +``` + +### [Results]({part_line}/) + +Results have checksums; + +""" + +info_md_file = " * [`{file_sha256} {file_short_path}`]({file_real_path})\n" + + +def sha256(s): + m = hashlib.sha256() + m.update(s) + return m.hexdigest() + + +def sha256_file(p): + return sha256(open(p, 'rb').read()) + + +def run(c): + o = subprocess.check_output(c, shell=True) + return o.decode('utf-8').strip() + + +def main(argv): + + info_md = [] + + info_md.append(open('database/README.md').read()) + + v = {} + v['human_date'] = run('TZ=UTC date') + v['iso8601_date'] = run('TZ=UTC date --iso-8601=seconds') + v['commit_latest'] = run('git log -1') + v['commit_hash_short'] = run('git log -1 --pretty=%h') + v['commit_hash_long'] = run('git log -1 --pretty=%H') + + info_md.append(info_md_header.format(**v)) + + for part_line in sorted(os.listdir('database')): + if part_line.startswith('.'): + continue + part_path = os.path.join('database', part_line) + + if not os.path.isdir(part_path): + continue + + files = list(os.listdir(part_path)) + files.sort() + + settings_path = os.path.join('settings', part_line + '.sh') + settings_raw = open(settings_path, 'rb').read() + + w = {} + w['commit_hash_long'] = v['commit_hash_long'] + w['part_line'] = part_line + w['settings_contents'] = settings_raw.decode('utf-8') + w['settings_sha256'] = sha256(settings_raw) + + info_md.append(info_md_section.format(**w)) + + for dirpath, dirnames, filenames in os.walk(part_path): + dirnames.sort() + filenames.sort() + + for f in filenames: + p = os.path.join(dirpath, f) + + x = {} + x['file_real_path'] = './' + p + x['file_short_path'] = os.path.join( + part_line, os.path.relpath(p, part_path)) + x['file_sha256'] = sha256_file(p) + info_md.append(info_md_file.format(**x)) + + with open(os.path.join('database', 'Info.md'), 'w') as f: + f.write("".join(info_md)) + + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv))