117 lines
3.7 KiB
Python
Executable File
117 lines
3.7 KiB
Python
Executable File
import os
|
|
import sys
|
|
import argparse
|
|
import logging
|
|
import json
|
|
import requests
|
|
|
|
# Identity attributes
|
|
# {'username' : '', 'full_name' : '', 'email' : '', public_key : '' }
|
|
|
|
logging.basicConfig(format='%(levelname)s\t- %(message)s')
|
|
logger = logging.getLogger()
|
|
logger.setLevel(logging.INFO)
|
|
|
|
def load_state():
|
|
state = {}
|
|
state_dir = os.path.join(os.path.expanduser('~'), '.sio')
|
|
state_file = os.path.join(state_dir, 'state.json')
|
|
|
|
logger.debug('State folder: ' + state_dir)
|
|
logger.debug('State file: ' + state_file)
|
|
|
|
if os.path.exists(state_file):
|
|
logger.debug('Loading state')
|
|
with open(state_file,'r') as f:
|
|
state = json.loads(f.read())
|
|
|
|
if state is None:
|
|
state = {}
|
|
|
|
return state
|
|
|
|
def parse_env(state):
|
|
if 'REP_ADDRESS' in os.environ:
|
|
state['REP_ADDRESS'] = os.getenv('REP_ADDRESS')
|
|
logger.debug('Setting REP_ADDRESS from Environment to: ' + state['REP_ADDRESS'])
|
|
|
|
if 'REP_PUB_KEY' in os.environ:
|
|
rep_pub_key = os.getenv('REP_PUB_KEY')
|
|
logger.debug('Loading REP_PUB_KEY fron: ' + state['REP_PUB_KEY'])
|
|
if os.path.exists(rep_pub_key):
|
|
with open(rep_pub_key, 'r') as f:
|
|
state['REP_PUB_KEY'] = f.read()
|
|
logger.debug('Loaded REP_PUB_KEY from Environment')
|
|
return state
|
|
|
|
def parse_args(state):
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-k", '--key', nargs=1, help="Path to the key file")
|
|
parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository")
|
|
parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true")
|
|
parser.add_argument("-c", "--command", help="Command to execute")
|
|
parser.add_argument('arg0', nargs='?', default=None)
|
|
parser.add_argument('arg1', nargs='?', default=None)
|
|
parser.add_argument('arg2', nargs='?', default=None)
|
|
parser.add_argument('arg3', nargs='?', default=None)
|
|
parser.add_argument('arg4', nargs='?', default=None)
|
|
parser.add_argument('arg5', nargs='?', default=None)
|
|
|
|
args = parser.parse_args()
|
|
if args.verbose:
|
|
logger.setLevel(logging.DEBUG)
|
|
logger.info('Setting log level to DEBUG')
|
|
|
|
if args.key:
|
|
if not os.path.exists(args.key[0]) or not os.path.isfile(args.key[0]):
|
|
logger.error(f'Key file not found or invalid: {args.key[0]}')
|
|
sys.exit(1)
|
|
|
|
with open(args.key[0], 'r') as f:
|
|
state['REP_PUB_KEY'] = f.read()
|
|
logger.info('Overriding REP_PUB_KEY from command line')
|
|
|
|
if args.repo:
|
|
state['REP_ADDRESS'] = args.repo[0]
|
|
logger.info('Overriding REP_ADDRESS from command line')
|
|
|
|
#if args.command:
|
|
# logger.info("Command: " + args.command)
|
|
|
|
return state#, {'command': args.command, 'arg0': args.arg0, 'arg1': args.arg1, 'arg2': args.arg2, 'arg3': args.arg3, 'arg4': args.arg4, 'arg5': args.arg5}
|
|
|
|
def save(state):
|
|
state_dir = os.path.join(os.path.expanduser('~'), '.sio')
|
|
state_file = os.path.join(state_dir, 'state.json')
|
|
|
|
if not os.path.exists(state_dir):
|
|
logger.debug('Creating state folder')
|
|
os.mkdir(state_dir)
|
|
|
|
with open(state_file, 'w') as f:
|
|
f.write(json.dumps(state, indent=4))
|
|
|
|
#Main function for checking commands
|
|
def main(args):
|
|
|
|
state = load_state()
|
|
state = parse_env(state)
|
|
#state, args = parse_args(state)
|
|
state = parse_args(state)
|
|
|
|
if 'REP_ADDRESS' not in state:
|
|
logger.error("Must define Repository Address")
|
|
sys.exit(1)
|
|
|
|
# if 'REP_PUB_KEY' not in state:
|
|
# logger.error("Must set the Repository Public Key")
|
|
# sys.exit(1)
|
|
|
|
save(state)
|
|
|
|
return state
|
|
|
|
if __name__ == '__main__':
|
|
main(sys.argv)
|