Skip to content

Commit

Permalink
developer scripts, not needed by end user
Browse files Browse the repository at this point in the history
  • Loading branch information
8go committed May 22, 2022
1 parent 38c80b9 commit d341e99
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ optional arguments:
information program will continue to run. This is
useful for having version number in the log files.
You are running version 2022-05-22. Enjoy, star on Github and contribute by
You are running version 2022-05-21. Enjoy, star on Github and contribute by
submitting a Pull Request.
```

Expand Down
4 changes: 2 additions & 2 deletions matrix-commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@
information program will continue to run. This is
useful for having version number in the log files.
You are running version 2022-05-22. Enjoy, star on Github and contribute by
You are running version 2022-05-21. Enjoy, star on Github and contribute by
submitting a Pull Request.
```
Expand Down Expand Up @@ -845,7 +845,7 @@
HAVE_NOTIFY = False

# version number
VERSION = "2022-05-22"
VERSION = "2022-05-21"
# matrix-commander
PROG_WITHOUT_EXT = os.path.splitext(os.path.basename(__file__))[0]
# matrix-commander.py
Expand Down
2 changes: 1 addition & 1 deletion lintmc.sh → scripts/lintmc.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# tiny script to lint matrix-commander.py

FN=matrix-commander.py
FN="matrix-commander.py"

if ! [ -f "$FN" ]; then
FN="../$FN"
Expand Down
42 changes: 42 additions & 0 deletions scripts/update-1-version.sh
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
94 changes: 94 additions & 0 deletions scripts/update-2-help.py
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}")
57 changes: 57 additions & 0 deletions scripts/update-3-readme.py
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.")

0 comments on commit d341e99

Please sign in to comment.