From 18a033547b0d3849d88c87faf794d8bda55442d4 Mon Sep 17 00:00:00 2001 From: Georgy Savidov <60780909+Avgor46@users.noreply.github.com> Date: Sun, 5 Nov 2023 22:28:21 +0300 Subject: [PATCH] Add update_usage.py script (#164) --- CONTRIBUTING.md | 2 ++ README.md | 2 ++ update_usage.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100755 update_usage.py diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6f8aefe1..db88d766 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,3 +8,5 @@ Please follow the next recommendations for your pull requests: - use `cargo fmt` - check the output of `cargo clippy --all-features --all --tests` - run tests `cargo test` +- if you have updated usage of any casr tool, you could simply run + `update_usage.py` to change the `docs/usage.md` file properly diff --git a/README.md b/README.md index 21c45a8d..52d8665f 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,8 @@ Please follow the next recommendations for your pull requests: - use `cargo fmt` - check the output of `cargo clippy --all-features --all --tests` - run tests `cargo test` +- if you have updated usage of any casr tool, you could simply run + `update_usage.py` to change the `docs/usage.md` file properly ## Cite Us diff --git a/update_usage.py b/update_usage.py new file mode 100755 index 00000000..89f6ade3 --- /dev/null +++ b/update_usage.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import re, sys, os +import subprocess +from subprocess import Popen, PIPE + +if __name__ == "__main__": + with open('docs/usage.md') as f: + content = f.read() + + command = subprocess.run(["cargo", "build"]) + if command.returncode != 0: + print('Cargo build exited with code ' + str(command.returncode)) + exit(command.returncode) + + build_dir = os.path.dirname(os.path.realpath(__file__)) + target_dir = os.path.join(build_dir, "target", "debug") + + for p in os.listdir(target_dir): + if p.startswith("casr-") \ + and os.path.isfile(target := os.path.join(target_dir, p)) \ + and os.access(target, os.X_OK): + command = Popen([target, "-h"], stdout=PIPE, stderr=PIPE) + out, _ = command.communicate() + output = str(out, 'utf-8', errors='ignore') + splitted = output.split('\n\n') + number_of_sections = len(splitted) + if number_of_sections != 4 and number_of_sections != 3: + print('Bad format in help message: ' + p) + continue + for i in range(1, number_of_sections): + splitted[i] = '\n'.join([' ' + line for line in splitted[i].split('\n') if line]) + new_message = '\n\n'.join(splitted) + '\n\n' + content = re.sub(f'## {p}\n\n' + '(.|\n)*?\n\n(\s+.*\n\n?)+', \ + f'## {p}\n\n' + new_message, \ + content) + + with open('docs/usage.md', 'w') as f: + f.write(content)