Skip to content

Commit

Permalink
Add update_usage.py script (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avgor46 authored Nov 5, 2023
1 parent e1849e5 commit 18a0335
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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 @@ -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

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)

with open('docs/usage.md', 'w') as f:
f.write(content)

0 comments on commit 18a0335

Please sign in to comment.