forked from 8go/matrix-commander
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
developer scripts, not needed by end user
- Loading branch information
8go
committed
May 22, 2022
1 parent
38c80b9
commit d341e99
Showing
6 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/bash | ||
|
||
FN="matrix-commander.py" | ||
|
||
if ! [ -f "$FN" ]; then | ||
FN="../$FN" | ||
if ! [ -f "$FN" ]; then | ||
echo -n "ERROR: $(basename -- "$FN") not found. " | ||
echo "Neither in local nor in parent directory." | ||
exit 1 | ||
fi | ||
fi | ||
|
||
PREFIX="VERSION = " | ||
REGEX="^${PREFIX}\"20[0-9][0-9]-[0-9][0-9]-[0-9][0-9].*\"" | ||
|
||
if ! [ -f "$FN" ]; then | ||
echo "ERROR: File \"$FN\" not found." | ||
else | ||
COUNT=$(grep --count -e "$REGEX" $FN) | ||
if [ "$COUNT" == "1" ]; then | ||
# NEWVERSION="$PREFIX\"$(date +%Y-%m-%d-%H%M%S)\"" | ||
NEWVERSION="$PREFIX\"$(date +%Y-%m-%d)\"" | ||
sed -i "s/$REGEX/$NEWVERSION/" $FN | ||
RETURN=$? | ||
if [ "$RETURN" == "0" ]; then | ||
echo "SUCCESS: Modified file $FN by setting version to $NEWVERSION." | ||
exit 0 | ||
else | ||
echo "ERROR: could not change version to $NEWVERSION in $FN." | ||
fi | ||
else | ||
echo "Error while searching for $REGEX" | ||
grep -e "$PREFIX" $FN | ||
if [ "$COUNT" == "1" ]; then | ||
echo "ERROR: Version not found, expected 1 occurance." | ||
else | ||
echo "ERROR: Version found $COUNT times, expected 1 occurance." | ||
fi | ||
fi | ||
fi | ||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#!/usr/bin/python3 | ||
import re | ||
import shutil | ||
import subprocess | ||
import sys | ||
from datetime import datetime | ||
from os import R_OK, access | ||
from os.path import isfile | ||
|
||
# replace this pattern: | ||
# ``` | ||
# usage: ... | ||
# ``` | ||
# in the matrix-commander.py file with the output of | ||
# matrix-commander.py --help | ||
|
||
# datetime object containing current date and time | ||
now = datetime.now() | ||
date_string = now.strftime("%d%m%Y-%H%M%S") | ||
|
||
helpfile = "help.txt" | ||
filename = "matrix-commander.py" | ||
|
||
if isfile(filename) and access(filename, R_OK): | ||
# so that subprocess can execute it without PATH | ||
filename = "./" + filename | ||
helpfile = "./" + helpfile | ||
else: | ||
filename = "../" + filename | ||
helpfile = "../" + helpfile | ||
if not (isfile(filename) and access(filename, R_OK)): | ||
print( | ||
f"Error: file {filename[3:]} not found, neither in " | ||
"local nor in parent directory." | ||
) | ||
sys.exit(1) | ||
|
||
backupfile = filename + "." + date_string | ||
shutil.copy2(filename, backupfile) | ||
|
||
f = open(helpfile, "w") | ||
# tty size defaults to 80 columns | ||
bashCmd = [filename, "--help"] | ||
process = subprocess.Popen(bashCmd, stdout=f) | ||
_, error = process.communicate() | ||
if error: | ||
print(error) | ||
f.close() | ||
|
||
bashCmd = ["wc", "-L", helpfile] # max line length | ||
process = subprocess.Popen(bashCmd, stdout=subprocess.PIPE) | ||
output, error = process.communicate() | ||
if error: | ||
print(error) | ||
else: | ||
output = output.decode("utf-8").strip("\n") | ||
print(f"Maximum line length is {output}") | ||
|
||
with open(helpfile, "r+") as f: | ||
helptext = f.read() | ||
print(f"Length of new {helpfile} file is: {len(helptext)}") | ||
f.close() | ||
|
||
with open(filename, "r+") as f: | ||
text = f.read() | ||
print(f"Length of {filename} before: {len(text)}") | ||
text = re.sub( | ||
r"```\nusage: [\s\S]*?```", | ||
"```\n" | ||
+ helptext.translate( | ||
str.maketrans( | ||
{ | ||
"\\": r"\\", | ||
} | ||
) | ||
) | ||
+ "```", | ||
text, | ||
) | ||
|
||
print(f"Length of {filename} after: {len(text)}") | ||
f.seek(0) | ||
f.write(text) | ||
f.truncate() | ||
f.close() | ||
|
||
bashCmd = ["diff", filename, backupfile] | ||
process = subprocess.Popen(bashCmd, stdout=subprocess.PIPE) | ||
output, error = process.communicate() | ||
if error: | ||
print(error) | ||
else: | ||
output = output.decode("utf-8").strip("\n") | ||
print(f"Diff is:\n{output}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/python3 | ||
import re | ||
import shutil | ||
import subprocess | ||
import sys | ||
from datetime import datetime | ||
from os import R_OK, access | ||
from os.path import isfile | ||
|
||
# extract the README portion within the matrix-commander.py file | ||
# into a separate stand-alone README.md file | ||
|
||
# datetime object containing current date and time | ||
now = datetime.now() | ||
date_string = now.strftime("%d%m%Y-%H%M%S") | ||
|
||
readmemd = "README.md" | ||
filename = "matrix-commander.py" | ||
|
||
if isfile(filename) and access(filename, R_OK): | ||
# so that subprocess can execute it without PATH | ||
filename = "./" + filename | ||
readmemd = "./" + readmemd | ||
else: | ||
filename = "../" + filename | ||
readmemd = "../" + readmemd | ||
if not (isfile(filename) and access(filename, R_OK)): | ||
print( | ||
f"Error: file {filename[3:]} not found, neither in " | ||
"local nor in parent directory." | ||
) | ||
sys.exit(1) | ||
|
||
backupfile = readmemd + "." + date_string | ||
shutil.copy2(readmemd, backupfile) | ||
|
||
file = open(filename, "r").read() | ||
m = re.search(r'"""[\s\S]*?"""', file) | ||
if m.group(0) != "": | ||
text = m.group(0) | ||
text = text.split("\n", 5)[5] # remove first 5 lines | ||
new_file = open(readmemd, "w") | ||
new_file.write(text.strip('"')) | ||
new_file.close() | ||
print(f"New {readmemd} was generated.") | ||
|
||
bashCmd = ["diff", readmemd, backupfile] | ||
process = subprocess.Popen(bashCmd, stdout=subprocess.PIPE) | ||
output, error = process.communicate() | ||
if error: | ||
print(error) | ||
else: | ||
output = output.decode("utf-8").strip("\n") | ||
print(f"Diff is:\n{output}") | ||
|
||
else: | ||
print(f"FAILED: No new {readmemd} was generated.") |