Skip to content

Commit

Permalink
Add support for OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgredowski committed Jun 5, 2019
1 parent 1c567c2 commit b8c0d89
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.vscode/
ENV/
__pycache__/
check_repos.service
*.service
*.plist
logs/
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
27 changes: 15 additions & 12 deletions check_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@
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

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__))

Expand All @@ -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

Expand All @@ -53,8 +58,6 @@ def setup(icon: Icon):
icon.visible = True
is_up_to_date = True

Notify.init(MESSAGE)

while True:
output = []
wrong_output = []
Expand Down Expand Up @@ -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])
Expand Down
8 changes: 6 additions & 2 deletions consts.yaml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 11 additions & 6 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 5 additions & 2 deletions prepare_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from sys import platform

from lib import utils

Expand All @@ -8,23 +9,25 @@


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()
user_uid = utils.get_uid()

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__":
Expand Down
35 changes: 35 additions & 0 deletions templates/check_repos.plist.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

<key>Label</key>
<string>check_repos.plist</string>

<key>RunAtLoad</key>
<true/>

<key>WorkingDirectory</key>
<string>{{ path_to_script_dir }}</string>

<key>StandardErrorPath</key>
<string>{{ path_to_script_dir }}/logs/stderr.log</string>

<key>StandardOutPath</key>
<string>{{ path_to_script_dir }}/logs/stdout.log</string>

<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string><![CDATA[/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin]]></string>
</dict>

<key>ProgramArguments</key>
<array>
<string>{{ path_to_script }}</string>
<string>-f</string>
<string>{{ path_to_repos_list_file }}</string>
</array>

</dict>
</plist>

0 comments on commit b8c0d89

Please sign in to comment.