Skip to content

Commit

Permalink
Store config file in central location after installing
Browse files Browse the repository at this point in the history
Copy the config file to a central location
`/usr/local/etc/auto-selfcontrol` to normalize homebrew and manual
installations.
  • Loading branch information
andreasgrill committed May 17, 2020
1 parent e6313c0 commit ee6ec83
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 43 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ Run from this specific repository

./auto-selfcontrol <config|activate|help>

Or create a symlink in your `/usr/local/bin` folder to access it from anywhere.
Optionally create a symlink in your `/usr/local/bin` folder to access it from anywhere:

sudo ln -s ./auto-selfcontrol /usr/local/bin/auto-selfcontrol

## Usage

Expand All @@ -55,6 +57,9 @@ To remove the application (if installed with Homebrew):

Or, manually, by removing the directory where you installed the files.

sudo unlink /usr/local/bin/auto-selfcontrol
rm -rf ~/auto-selfcontrol

You also need to remove the automatic schedule by executing the following command in the Terminal:

sudo rm /Library/LaunchDaemons/com.parrot-bytes.auto-selfcontrol.plist
Expand Down Expand Up @@ -140,3 +145,4 @@ If you've installed another version of Python (e.g., using Homebrew), you'll nee
sudo /usr/bin/python auto-selfcontrol.py

There are also other options, including installing `pyobjc` on your own Python version (`pip install pyobjc`). [See this thread for alternative solutions](https://stackoverflow.com/questions/1614648/importerror-no-module-named-foundation#1616361).

26 changes: 13 additions & 13 deletions auto-selfcontrol
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#!/usr/bin/env bash
# Auto-SelfControl basic command-line interface

CONFIG_DIR="/usr/local/etc/auto-selfcontrol"
CONFIG_FILE="$CONFIG_DIR/config.json"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

# Check if auto-selfcontrol was installed through brew and set the config.json location accordingly
if [[ $DIR == /usr/local/* ]]; then
mkdir -p /usr/local/etc/auto-selfcontrol || true
CONFIG_FILE="/usr/local/etc/auto-selfcontrol/config.json"
else
CONFIG_FILE="$DIR/config.json"
fi

b=$(tput bold)
n=$(tput sgr0)
HELP_TEXT="Auto-SelfControl
Expand All @@ -25,16 +33,8 @@ if [[ $1 ]]; then
config|edit|set|conf*)
# If no "config.json" found
if [[ ! -f $CONFIG_FILE ]]; then
mkdir -p $CONFIG_DIR
# If existing "config.json" in the cwd, copy it
if [[ -f config.json ]]; then
cp "config.json" $CONFIG_FILE
echo "Copied config.json from the current directory to $CONFIG_DIR"
# else download sample config from github repository
else
curl -L -s "https://raw.githubusercontent.com/andreasgrill/auto-selfcontrol/master/config.json" -o $CONFIG_FILE
echo "Downloaded sample configuration in $CONFIG_FILE"
fi
curl -L -s "https://raw.githubusercontent.com/andreasgrill/auto-selfcontrol/master/config.json" -o $CONFIG_FILE
echo "Downloaded sample configuration in $CONFIG_FILE"
fi
echo "Opening $CONFIG_FILE"
# Opening with default editor set as $EDITOR
Expand All @@ -47,7 +47,7 @@ if [[ $1 ]]; then
;;
# Install plist config
activate|install)
sudo /usr/bin/python /usr/local/bin/auto-selfcontrol.py
sudo /usr/bin/python $DIR/auto-selfcontrol.py
exit 0
;;
-h|--help|help|*)
Expand Down
79 changes: 50 additions & 29 deletions auto-selfcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ def load_config(path):
return config


def find_config():
"""Looks for the config.json and returns its path"""
local_config_file = "{path}/config.json".format(
path=os.path.dirname(os.path.realpath(__file__)))
global_config_file = "{path}/config.json".format(
path=SETTINGS_DIR)

if os.path.exists(local_config_file):
return local_config_file

if os.path.exists(global_config_file):
return global_config_file

exit_with_error(
"There was no config file found, please create a config file.")


def detect_api(config):
"""Return the supported API version of the SelfControl"""
try:
Expand All @@ -55,6 +72,23 @@ def detect_api(config):
return Api.V2


def run(settings_dir):
"""Load config and start SelfControl"""
run_config = "{path}/run_config.json".format(path=settings_dir)
if not os.path.exists(run_config):
exit_with_error(
"Run config file could not be found in installation location, please make sure that you have Auto-SelfControl installed with '--install'")

api = detect_api(CONFIG)
print("Detected API v{version}".format(version=api))

config = load_config(run_config)
if api is Api.V2:
run_api_v2(config)
elif api is Api.V3:
run_api_v3(config, settings_dir)


def run_api_v2(config):
"""Start SelfControl (< 3.0) with custom parameters, depending on the weekday and the config"""

Expand Down Expand Up @@ -283,7 +317,7 @@ def execSelfControl(config, arguments):
return output


def install(config):
def install(config, settings_dir):
""" installs auto-selfcontrol """
print("> Start installation of Auto-SelfControl")

Expand All @@ -302,6 +336,13 @@ def install(config):

subprocess.call(["launchctl", "load", "-w", launchplist_path])

print("> Save run configuration")
if not os.path.exists(settings_dir):
os.makedirs(settings_dir)

with open("{dir}/run_config.json".format(dir=settings_dir), 'w') as fp:
fp.write(json.dumps(config))

print("> Installed\n")


Expand Down Expand Up @@ -366,10 +407,6 @@ def exit_with_error(message):


if __name__ == "__main__":
CONFIG_DIRS = [
os.path.join(SETTINGS_DIR),
os.path.dirname(os.path.realpath(__file__))
]
sys.excepthook = excepthook

syslog.openlog("Auto-SelfControl")
Expand All @@ -384,33 +421,17 @@ def exit_with_error(message):
dest="run", default=False)
(OPTS, ARGS) = PARSER.parse_args()

CONFIG_FILES = filter(lambda p: os.path.exists(p), map(
lambda d: os.path.join(d, 'config.json'), CONFIG_DIRS))

if len(CONFIG_FILES) is 0:
exit_with_error(
"There was no config file found, please create a config file.")

if len(CONFIG_FILES) > 1:
print("> Multiple config files were found, use config file with path: {path}".format(
path=CONFIG_FILES[0]))

CONFIG = load_config(CONFIG_FILES[0])

api = detect_api(CONFIG)
print("Detected API v{version}".format(version=api))

if not os.path.exists(SETTINGS_DIR):
os.makedirs(SETTINGS_DIR)

if OPTS.run:
if api is Api.V2:
run_api_v2(CONFIG)
elif api is Api.V3:
run_api_v3(CONFIG, SETTINGS_DIR)
run(SETTINGS_DIR)
else:
CONFIG_FILE = find_config()
CONFIG = load_config(CONFIG_FILE)

api = detect_api(CONFIG)
print("Detected API v{version}".format(version=api))

check_config(CONFIG)
install(CONFIG)
install(CONFIG, SETTINGS_DIR)
if api is Api.V3 and \
not check_if_running(api, CONFIG) and \
any(s for s in CONFIG["block-schedules"] if is_schedule_active(s)):
Expand Down

0 comments on commit ee6ec83

Please sign in to comment.