From b8c0d89c5c67b8bd673421f059d2a7297500985d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Gr=C4=99dowski?= Date: Wed, 29 May 2019 01:14:42 +0200 Subject: [PATCH] Add support for OSX --- .gitignore | 4 +++- README.md | 8 ++++++-- check_repos.py | 27 ++++++++++++++------------ consts.yaml | 8 ++++++-- install.sh | 17 +++++++++++------ prepare_service.py | 7 +++++-- templates/check_repos.plist.j2 | 35 ++++++++++++++++++++++++++++++++++ 7 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 templates/check_repos.plist.j2 diff --git a/.gitignore b/.gitignore index c839f3a..d1bdd05 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .vscode/ ENV/ __pycache__/ -check_repos.service \ No newline at end of file +*.service +*.plist +logs/ \ No newline at end of file diff --git a/README.md b/README.md index 78d9237..5d70a89 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,17 @@ This script will check if current commits of specified local repos are the same as on remote. -## Requirements (on Linux) +## Requirements on Linux - `python>=3.6` - `python3-pip` - `sudo` - `git` +### Additional requirement on OSX + +- `pygobject3` + ## Installation Prepare `yaml` file with repos to watch: @@ -25,7 +29,7 @@ You can install it with one command but it will require to enter `root` password Place this repository in some cozy place from where it can be started. -`install.sh` script will work only on Linux and create systemd service, enable it and start. +`install.sh` script will work only on Linux and OSX and create systemd (launchd on OSX) service, enable it and start. In consequence `check_repos.py` will check for repositories indicated in `repos.yaml` file. > NOTE: Python packages has (as far as I know) to be installed globally, not in virtualenv. diff --git a/check_repos.py b/check_repos.py index 891034d..af9682d 100755 --- a/check_repos.py +++ b/check_repos.py @@ -4,9 +4,6 @@ import subprocess import time -from gi import require_version -require_version('Notify', '0.7') -from gi.repository import Notify from PIL import Image from pystray import Icon, Menu, MenuItem @@ -14,6 +11,20 @@ from lib.cli import args from lib import utils +from sys import platform +if platform == 'darwin': + def notify(title: str, text: str): + os.system("""osascript -e 'display notification "{}" with title "{}"'""".format(text, title)) +else: + from gi import require_version + require_version('Notify', '0.7') + from gi.repository import Notify + + def notify(title: str, text: str): + Notify.init(title) + n = Notify.Notification.new('', text) + n.set_urgency(Notify.Urgency.CRITICAL) + n.show() curr_dir = os.path.dirname(os.path.realpath(__file__)) @@ -39,12 +50,6 @@ def repo_is_up_to_date(repo: utils.Repo): return returncode == 0 -def send_notification(text): - n = Notify.Notification.new('', text) - n.set_urgency(Notify.Urgency.CRITICAL) - n.show() - - def void(): pass @@ -53,8 +58,6 @@ def setup(icon: Icon): icon.visible = True is_up_to_date = True - Notify.init(MESSAGE) - while True: output = [] wrong_output = [] @@ -84,7 +87,7 @@ def setup(icon: Icon): [MenuItem(f'{text}', action=void) for text in output] if was_up_to_date: msg = "\n".join(output + wrong_output) - send_notification(msg) + notify(MESSAGE, msg) if wrong_paths: _items.extend([MenuItem(f'{text}', action=void) for text in wrong_output]) diff --git a/consts.yaml b/consts.yaml index a65c315..f51deff 100644 --- a/consts.yaml +++ b/consts.yaml @@ -1,5 +1,9 @@ main_script_name: check_repos.py -service_template_path: templates/check_repos.service.j2 -service_rendered_path: check_repos.service +service_template_path: + linux: templates/check_repos.service.j2 + darwin: templates/check_repos.plist.j2 +service_rendered_path: + linux: check_repos.service + darwin: check_repos.plist repos_list_filename: repos.yaml default_branch_name: master \ No newline at end of file diff --git a/install.sh b/install.sh index 6fea3c9..b601dcd 100755 --- a/install.sh +++ b/install.sh @@ -6,12 +6,17 @@ pip3 install -r requirements.txt python3 prepare_service.py -sudo cp check_repos.service /etc/systemd/system +if [ "$(uname)" == "Darwin" ]; then + launchctl unload check_repos.plist + launchctl load check_repos.plist +else + sudo cp check_repos.service /etc/systemd/system -sudo systemctl stop check_repos.service -sudo systemctl disable check_repos.service -sudo systemctl enable check_repos.service -sudo systemctl daemon-reload -sudo systemctl start check_repos.service + sudo systemctl stop check_repos.service + sudo systemctl disable check_repos.service + sudo systemctl enable check_repos.service + sudo systemctl daemon-reload + sudo systemctl start check_repos.service +fi echo "Service installed and started" \ No newline at end of file diff --git a/prepare_service.py b/prepare_service.py index c7f3e9a..2ac185f 100644 --- a/prepare_service.py +++ b/prepare_service.py @@ -1,4 +1,5 @@ import os +from sys import platform from lib import utils @@ -8,9 +9,10 @@ def main(): - template = utils.load_template(CONSTS.get('service_template_path')) + template = utils.load_template(CONSTS.get('service_template_path').get(platform)) path_to_script = os.path.join(curr_dir, CONSTS.get('main_script_name')) + path_to_script_dir = os.path.abspath(os.path.join(path_to_script, '..')) path_to_repos_list_file = os.path.join(curr_dir, CONSTS.get('repos_list_filename')) username = utils.get_username() display = utils.get_display() @@ -18,13 +20,14 @@ def main(): rendered = template.render( path_to_script=path_to_script, + path_to_script_dir=path_to_script_dir, path_to_repos_list_file=path_to_repos_list_file, username=username, display=display, user_uid=user_uid ) - utils.save_to_file(rendered, CONSTS.get('service_rendered_path')) + utils.save_to_file(rendered, CONSTS.get('service_rendered_path').get(platform)) if __name__ == "__main__": diff --git a/templates/check_repos.plist.j2 b/templates/check_repos.plist.j2 new file mode 100644 index 0000000..994ce9f --- /dev/null +++ b/templates/check_repos.plist.j2 @@ -0,0 +1,35 @@ + + + + + + Label + check_repos.plist + + RunAtLoad + + + WorkingDirectory + {{ path_to_script_dir }} + + StandardErrorPath + {{ path_to_script_dir }}/logs/stderr.log + + StandardOutPath + {{ path_to_script_dir }}/logs/stdout.log + + EnvironmentVariables + + PATH + + + + ProgramArguments + + {{ path_to_script }} + -f + {{ path_to_repos_list_file }} + + + + \ No newline at end of file