-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make scripts available via CLI #2426
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Home Assistant command line scripts.""" | ||
import importlib | ||
import os | ||
|
||
|
||
def run(args): | ||
"""Run a script.""" | ||
scripts = [fil[:-3] for fil in os.listdir(os.path.dirname(__file__)) | ||
if fil.endswith('.py') and fil != '__init__.py'] | ||
|
||
if not args: | ||
print('Please specify a script to run.') | ||
print('Available scripts:', ', '.join(scripts)) | ||
return 1 | ||
|
||
if args[0] not in scripts: | ||
print('Invalid script specified.') | ||
print('Available scripts:', ', '.join(scripts)) | ||
return 1 | ||
|
||
script = importlib.import_module('homeassistant.scripts.' + args[0]) | ||
return script.run(args[1:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Script to install/uninstall HA into OS X.""" | ||
import os | ||
import time | ||
|
||
|
||
def install_osx(): | ||
"""Setup to run via launchd on OS X.""" | ||
with os.popen('which hass') as inp: | ||
hass_path = inp.read().strip() | ||
|
||
with os.popen('whoami') as inp: | ||
user = inp.read().strip() | ||
|
||
cwd = os.path.dirname(__file__) | ||
template_path = os.path.join(cwd, 'startup', 'launchd.plist') | ||
|
||
with open(template_path, 'r', encoding='utf-8') as inp: | ||
plist = inp.read() | ||
|
||
plist = plist.replace("$HASS_PATH$", hass_path) | ||
plist = plist.replace("$USER$", user) | ||
|
||
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist") | ||
|
||
try: | ||
with open(path, 'w', encoding='utf-8') as outp: | ||
outp.write(plist) | ||
except IOError as err: | ||
print('Unable to write to ' + path, err) | ||
return | ||
|
||
os.popen('launchctl load -w -F ' + path) | ||
|
||
print("Home Assistant has been installed. \ | ||
Open it here: http://localhost:8123") | ||
|
||
|
||
def uninstall_osx(): | ||
"""Unload from launchd on OS X.""" | ||
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist") | ||
os.popen('launchctl unload ' + path) | ||
|
||
print("Home Assistant has been uninstalled.") | ||
|
||
|
||
def run(args): | ||
"""Handle OSX commandline script.""" | ||
commands = 'install', 'uninstall', 'restart' | ||
if not args or args[0] not in commands: | ||
print('Invalid command. Available commands:', ', '.join(commands)) | ||
return 1 | ||
|
||
if args[0] == 'install': | ||
install_osx() | ||
return 0 | ||
elif args[0] == 'uninstall': | ||
uninstall_osx() | ||
return 0 | ||
elif args[0] == 'restart': | ||
uninstall_osx() | ||
# A small delay is needed on some systems to let the unload finish. | ||
time.sleep(0.5) | ||
install_osx() | ||
return 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are two levels of 'argparse', here and in the script, wont duplicate --args be interpreted by the first argparse and never be passed to script.run?
(i.e -c or --config in the script)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's why the script arg in main uses the remain option. Try it by
passing help before and after invoking the dB migrate script
On Sat, Jul 2, 2016, 22:38 Johann Kellerman [email protected]
wrote: