diff options
| -rwxr-xr-x | reposync.py | 9 | ||||
| -rwxr-xr-x | systemd-service-create.py | 48 |
2 files changed, 52 insertions, 5 deletions
diff --git a/reposync.py b/reposync.py index 083ef74..6aaf47d 100755 --- a/reposync.py +++ b/reposync.py @@ -6,11 +6,10 @@ import os repoFile = open("repositories.txt", "r") while(True): - line = repoFile.readline() + line = repoFile.readline().strip() if not line: break - line = line.strip() now = datetime.now().strftime("[%H:%M:%S] ") if not os.path.isdir(line): print("\033[0;31m%s\033[0;0m \033[0;34m%s\033[0;0m is not a directory!"%(now, line)) @@ -22,11 +21,11 @@ while(True): # Thanks to https://stackoverflow.com/a/65535263/12036073 if list(repo.iter_commits(f'{branch}..{branch}@{{u}}')): - print("\033[0;31m%s\033[0;34m%s\033[0;0m\'s origin was updated, pulling..."%(now, line)) + print(f'\033[0;31m{now}\033[0;34m{line}\033[0;0m\'s origin was updated, pulling...') repo.remotes.origin.pull() else: - print("\033[0;32m%s\033[0;34m%s\033[0;0m is up to date"%(now, line)) + print(f'\033[0;32m{now}\033[0;34m{line}\033[0;0m is up to date') else: - print("\033[0;31m%s\033[0;0m Repository \033[0;34m%s\033[0;0m must not be bare!"%(now, line)) + print(f'\033[0;31m{now}\033[0;0m Repository \033[0;34m{line}\033[0;0m must not be bare!') repoFile.close() diff --git a/systemd-service-create.py b/systemd-service-create.py new file mode 100755 index 0000000..4527bd4 --- /dev/null +++ b/systemd-service-create.py @@ -0,0 +1,48 @@ +#!/bin/python3 +import os + +# This tests root permissions by trying to write a file (it deletes it afterwards) +# "Easer to Ask Forgiveness than permission" https://stackoverflow.com/a/2806932/12036073 +try: + test = open(r"/etc/test", "w") + test.close(); + os.remove(r"/etc/test") +except PermissionError as e: + exit("You must run this script as root!") + +cwd = os.getcwd() + +if "home" in cwd: + print("Warning: you are running this script from the home directory! If you use SELinux, the service won't work (https://serverfault.com/a/957087/592409)!") + +service_file = open(r"/etc/systemd/system/reposync.service", "w") +service_file.write( +f"""[Unit] +Description=Python script for synchronizing repositories + +[Service] +Type=oneshot +WorkingDirectory={cwd} +ExecStart={cwd}/reposync.py +""") +service_file.close() + +timer_file = open(r"/etc/systemd/system/reposync.timer", "w") +timer_file.write( +f"""[Unit] +Description=Python script for synchronizing repositories + +[Timer] +OnBootSec=10min +OnUnitActiveSec=10min + +[Install] +WantedBy=timers.target +""") +timer_file.close() + +print( +"""reposync.service and reposync.timer files created successfully! +You'll now need to run the following to activate them (as root): systemctl daemon-reload && systemctl start reposync.timer +To remove them, execute (as root) this: rm -f /etc/systemd/system/reposync.* +""") |
