Update util script

This commit is contained in:
Louis Thurman 2023-03-07 00:32:26 +00:00
parent 7d0de39b84
commit 17065ed00d
3 changed files with 82 additions and 30 deletions

1
.gitignore vendored
View File

@ -12,4 +12,5 @@ Library-.*
*.aux *.aux
*.log *.log
*.orig *.orig
*.mid
*~ *~

81
auto_render.py Normal file
View File

@ -0,0 +1,81 @@
#!/usr/bin/python3 -u
import logging
import pathlib
import subprocess
import sys
import time
win_root = pathlib.Path(r"C:\Users\louis\GitKraken\dottes")
lin_root = pathlib.Path("~/dottes")
file_update_times = {}
def run_continuous():
logging.info("Run continous")
pwd = pathlib.Path()
while True:
for obj_path in pwd.rglob("*"):
obj_path_str = str(obj_path)
if not obj_path.is_file():
continue
if not obj_path_str.lower().endswith(".abc"):
continue
if obj_path_str not in file_update_times:
file_update_times[obj_path_str] = obj_path.lstat().st_mtime
else:
if obj_path.lstat().st_mtime > file_update_times[obj_path_str]:
file_update_times[obj_path_str] = obj_path.lstat().st_mtime
convert_single(lin_path=obj_path_str)
def convert_single(win_path: str = None, lin_path: str = None):
if win_path:
input_path = win_path
win_abc_file_path = pathlib.Path(win_path)
lin_abc_file_path = lin_root / win_abc_file_path.relative_to(win_root)
elif lin_path:
input_path = lin_path
lin_abc_file_path = pathlib.Path(lin_path)
else:
raise Exception("Argument win_path or lin_path must be given")
logging.info(f"Converting {input_path}")
out_file_name = lin_abc_file_path.name.rsplit(".", 1)[0] + ".mid"
output_path = lin_abc_file_path.parent / out_file_name
if output_path.exists():
output_path.unlink()
r = subprocess.run(
[
"abc2midi",
lin_abc_file_path.as_posix(),
"-o", output_path.as_posix()
]
)
if r.returncode != 0:
logging.error(f"Error whilst convering {lin_abc_file_path}")
else:
logging.info(f"Converted {input_path} > {output_path}")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
if len(sys.argv) > 1:
if sys.platform == "win32":
convert_single(win_path=sys.argv[1])
elif sys.platform == "linux":
convert_single(lin_path=sys.argv[1])
else:
raise Exception(f"Unknown platform '{sys.platform}', don't know how to interpret path")
else:
run_continuous()

View File

@ -1,30 +0,0 @@
import os
import pathlib
import subprocess
import sys
win_root = pathlib.Path(r"C:\Users\louis\GitKraken\dottes")
lin_root = pathlib.Path("~/dottes")
try:
win_abc_file_path = pathlib.Path(sys.argv[1])
except IndexError:
win_abc_file_path = pathlib.Path(input())
if not str(win_abc_file_path).lower().endswith(".abc"):
input("Doesn't look like an abc file.")
exit(1)
lin_abc_file_path = lin_root / win_abc_file_path.relative_to(win_root)
out_file_name = lin_abc_file_path.name.rsplit(".", 1)[0] + ".mid"
lin_output_path = lin_abc_file_path.parent / out_file_name
win_output_path = win_abc_file_path.parent / out_file_name
if win_output_path.exists():
win_output_path.unlink()
command = f"abc2midi {lin_abc_file_path.as_posix()} -o {lin_output_path.as_posix()}"
print("Command:", command)
r = subprocess.run([r"wsl"], input=command.encode())
os.system(str(win_output_path))