include update func. in main script and display version in hass device info
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import ast
|
||||
import os
|
||||
import subprocess
|
||||
import config
|
||||
|
||||
|
||||
def get_assignments(filename):
|
||||
@@ -10,6 +11,7 @@ def get_assignments(filename):
|
||||
assignments = {node.targets[0].id: ast.literal_eval(node.value) for node in ast.walk(tree) if isinstance(node, ast.Assign)}
|
||||
return assignments
|
||||
|
||||
|
||||
def update_config(current_config, example_config):
|
||||
current_assignments = get_assignments(current_config)
|
||||
example_assignments = get_assignments(example_config)
|
||||
@@ -21,30 +23,51 @@ def update_config(current_config, example_config):
|
||||
for var, value in missing_assignments.items():
|
||||
f.write('\n{} = {!r}'.format(var, value))
|
||||
|
||||
def display_config_differences(current_config, example_config):
|
||||
|
||||
def display_config_differences(current_config, example_config, display=True):
|
||||
current_assignments = get_assignments(current_config)
|
||||
example_assignments = get_assignments(example_config)
|
||||
|
||||
missing_assignments = {var: value for var, value in example_assignments.items() if var not in current_assignments}
|
||||
|
||||
if missing_assignments:
|
||||
print("Missing variables:")
|
||||
for var, value in missing_assignments.items():
|
||||
print('\n{} = {!r}'.format(var, value))
|
||||
if display:
|
||||
print("Missing variables:")
|
||||
for var, value in missing_assignments.items():
|
||||
print('\n{} = {!r}'.format(var, value))
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def update_config_version(version):
|
||||
with open('config.py', 'r') as f:
|
||||
lines = f.readlines()
|
||||
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
os.chdir(script_dir)
|
||||
with open('config.py', 'w') as f:
|
||||
print(":: Updating config version to {}".format(version))
|
||||
for line in lines:
|
||||
if 'version = ' in line:
|
||||
f.write('version = "{}"\n'.format(version))
|
||||
else:
|
||||
f.write(line)
|
||||
|
||||
print(":: Updating git repository")
|
||||
result = subprocess.run(['git', '-C', script_dir, 'pull'], check=True, universal_newlines=True, stdout=subprocess.PIPE)
|
||||
print(result.stdout)
|
||||
|
||||
if display_config_differences('config.py', 'config.py.example'):
|
||||
print(":: Updating config.py")
|
||||
update_config('config.py', 'config.py.example')
|
||||
else:
|
||||
print(":: No config.py updates needed")
|
||||
def do_update(version=config.version, git_update=True, config_update=True):
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
os.chdir(script_dir)
|
||||
print("Current version: {}".format(config.version))
|
||||
if git_update:
|
||||
print(":: Updating git repository")
|
||||
result = subprocess.run(['git', '-C', script_dir, 'pull'], check=True, universal_newlines=True, stdout=subprocess.PIPE)
|
||||
print(result.stdout)
|
||||
|
||||
if display_config_differences('config.py', 'config.py.example') and config_update:
|
||||
print(":: Updating config.py")
|
||||
update_config('config.py', 'config.py.example')
|
||||
|
||||
if version != config.version:
|
||||
update_config_version(version)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
do_update()
|
||||
Reference in New Issue
Block a user