Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update_usage.py script #164

Merged
merged 6 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,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

Expand Down
39 changes: 39 additions & 0 deletions update_usage.py
Original file line number Diff line number Diff line change
@@ -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)

SweetVishnya marked this conversation as resolved.
Show resolved Hide resolved
with open('docs/usage.md', 'w') as f:
f.write(content)