98 lines
3.0 KiB
Python
98 lines
3.0 KiB
Python
"""Command Line utility for use with POCloud driver files."""
|
|
|
|
import json
|
|
import click
|
|
import boto3
|
|
|
|
CONFIG_FILE_NAME = "driverConfig.json"
|
|
CONFIG_OUTPUT_FILENAME = "config.txt"
|
|
DRIVER_BUCKET = 'pocloud-drivers'
|
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
try:
|
|
with open(CONFIG_FILE_NAME, 'r') as config_file:
|
|
driver_config = json.load(config_file)
|
|
except IOError:
|
|
print("Missing {} file.".format(CONFIG_FILE_NAME))
|
|
exit()
|
|
except ValueError as e:
|
|
print("There seems to be an error in the JSON syntax of the {} file:".format(CONFIG_FILE_NAME))
|
|
print(e)
|
|
exit()
|
|
|
|
def get_driver_files():
|
|
"""Find all the files that should be included with the driver."""
|
|
files = [driver_config['driverFilename']]
|
|
for x in driver_config['additionalDriverFiles']:
|
|
files.append(x)
|
|
return files
|
|
|
|
@click.group()
|
|
def cli():
|
|
pass
|
|
|
|
@click.command()
|
|
def upload():
|
|
"""Upload the file to AWS S3 bucket."""
|
|
all_files = get_driver_files()
|
|
all_files.append("config.txt")
|
|
for single_file in all_files:
|
|
try:
|
|
open_file = open(single_file, 'rb')
|
|
file_key = "{}/{}".format(driver_config['s3BucketName'], single_file)
|
|
s3.Bucket(DRIVER_BUCKET).put_object(Key=file_key, Body=open_file)
|
|
s3.ObjectAcl(DRIVER_BUCKET, file_key).put(ACL='public-read')
|
|
click.echo("Uploaded {} to S3 Bucket {}".format(single_file, driver_config['s3BucketName']))
|
|
except IOError:
|
|
click.echo("ERROR: The file {} does not exist.".format(single_file))
|
|
|
|
|
|
|
|
@click.command()
|
|
@click.option('--dry-run', default=False, help='Perform a dry run of the script without making changes')
|
|
@click.option('--version', default="", help='Sets the version number of the driver code')
|
|
def prepare(dry_run, version):
|
|
"""Perform the configuration of the driver."""
|
|
files = get_driver_files()
|
|
file_object = {}
|
|
for i in range(0, len(files)):
|
|
file_object["file{}".format(i+1)] = files[i]
|
|
|
|
if version == "+1":
|
|
version = int(driver_config['version']) + 1
|
|
driver_config['version'] += 1
|
|
click.echo("Version set to {}".format(version))
|
|
elif version != "":
|
|
version = int(version)
|
|
click.echo("Version set to {}".format(version))
|
|
driver_config['version'] = version
|
|
else:
|
|
version = driver_config['version']
|
|
|
|
config_object = {
|
|
"driverFileName": driver_config['driverFilename'],
|
|
"deviceName": driver_config['name'],
|
|
"driverId": driver_config['driverId'],
|
|
"releaseVersion": "{}".format(version),
|
|
"files": file_object
|
|
}
|
|
|
|
if dry_run:
|
|
click.echo("==== DRY RUN: {} ====".format(CONFIG_OUTPUT_FILENAME))
|
|
click.echo(json.dumps(config_object, indent=4))
|
|
else:
|
|
with open(CONFIG_OUTPUT_FILENAME, 'w') as config_out:
|
|
json.dump(config_object, config_out, indent=4)
|
|
|
|
with open(CONFIG_FILE_NAME, 'w') as input_config_out:
|
|
json.dump(driver_config, input_config_out, indent=4)
|
|
|
|
|
|
|
|
cli.add_command(upload)
|
|
cli.add_command(prepare)
|
|
|
|
if __name__ == '__main__':
|
|
cli()
|