Skip to content

Commit

Permalink
Add installer and uninstaller
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed May 26, 2019
1 parent 8458cac commit a39e36f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
6 changes: 6 additions & 0 deletions install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from time_machine_ignore import install, main


if __name__ == "__main__":
install()
main()
55 changes: 53 additions & 2 deletions time_machine_ignore.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,67 @@
import os
import pickle
from pathlib import Path
from subprocess import DEVNULL, CalledProcessError, Popen, check_output
from shutil import rmtree
from subprocess import DEVNULL, CalledProcessError, Popen, call, check_output

BIN_DIR = "/usr/local/bin"
HOME_DIR = str(Path.home())
CACHE_DIR = os.path.join(HOME_DIR, "Library", "Caches", "com.samuelmeuli.time-machine-ignore")
LAUNCH_AGENTS_DIR = os.path.join(HOME_DIR, "Library", "LaunchAgents")
SCRIPT_NAME = os.path.basename(__file__)
SCRIPT_PATH = os.path.realpath(__file__)
CURRENT_DIR = os.path.dirname(SCRIPT_PATH)

LABEL = "com.samuelmeuli.time-machine-ignore"
PLIST_NAME = LABEL + ".plist"
PLIST_PATH = os.path.join(CURRENT_DIR, PLIST_NAME)

SCRIPT_LINK = os.path.join(BIN_DIR, SCRIPT_NAME)
PLIST_LINK = os.path.join(LAUNCH_AGENTS_DIR, PLIST_NAME)

CACHE_DIR = os.path.join(HOME_DIR, "Library", "Caches", LABEL)
CACHE_PATH = os.path.join(CACHE_DIR, "excluded")

# Paths to exclude from the Git repo search
IGNORED_PATHS = [os.path.join(HOME_DIR, ".Trash"), os.path.join(HOME_DIR, "Library")]


def install():
# Set up launchd user agent (runs script periodically)
print("Setting up agent…")
create_hard_link(SCRIPT_PATH, SCRIPT_LINK)
create_hard_link(PLIST_PATH, PLIST_LINK)
call(["launchctl", "load", PLIST_LINK])


def uninstall():
# Remove created backup exclusions
paths_cached = read_cache()
for exclusion_to_remove in paths_cached:
remove_exclusion(exclusion_to_remove)

# Remove cache
print("Removing files…")
rmtree(CACHE_DIR)

# Remove launchd user agent
print("Removing agent…")
call(["launchctl", "unload", PLIST_LINK])
delete_hard_link(SCRIPT_LINK)
delete_hard_link(PLIST_LINK)

print("Done")


def create_hard_link(src, dest):
delete_hard_link(dest)
os.link(src, dest)


def delete_hard_link(path):
if os.path.isfile(path):
os.unlink(path)


def add_exclusion(path):
"""Exclude the specified path from future Time Machine backups
Expand Down
5 changes: 5 additions & 0 deletions uninstall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from time_machine_ignore import uninstall


if __name__ == "__main__":
uninstall()

0 comments on commit a39e36f

Please sign in to comment.