#!/usr/bin/python3 # -*- coding: utf-8 -*- import argparse import os from spoc import publisher from spoc.cli import readable_size ACTION_LIST = 1 ACTION_INSTALL = 2 ACTION_UPGRADE = 3 ACTION_UNINSTALL = 4 ACTION_START = 5 ACTION_STOP = 6 ACTION_STATUS = 7 ACTION_PUBLISH = 8 ACTION_UNPUBLISH = 9 def listing(): raise NotImplementedException() def install(): raise NotImplementedException() def upgrade(): raise NotImplementedException() def uninstall(): raise NotImplementedException() def start(): raise NotImplementedException() def stop(): raise NotImplementedException() def status(): raise NotImplementedException() def publish(composefile, do_publish): # Check if publishing is needed and attempt to publish the application app_name = os.path.basename(os.path.dirname(filepath)) if not do_publish: try: publisher.get_app(app_name) except KeyError: do_publish = True if do_publish: print(f'Publishing application "{app_name}"') package = publisher.publish_app(app_name)['package'] print(f'Application "{app_name}" compressed from {readable_size(package["size"])} to {readable_size(package["dlsize"])}') else: print(f'Application "{app_name}" already published, skipping publishing') def unpublish(): raise NotImplementedException() parser = argparse.ArgumentParser(description='SPOC application manager') parser.set_defaults(action=None) subparsers = parser.add_subparsers() parser_list = subparsers.add_parser('list') parser_list.set_defaults(action=ACTION_LIST) parser_list.add_argument('type', choices=('installed', 'online', 'upgrades', 'published'), default='installed', const='installed', nargs='?') parser_install = subparsers.add_parser('install') parser_install.set_defaults(action=ACTION_INSTALL) parser_install.add_argument('app') parser_upgrade = subparsers.add_parser('upgrade') parser_upgrade.set_defaults(action=ACTION_UPGRADE) parser_upgrade.add_argument('app') parser_uninstall = subparsers.add_parser('uninstall') parser_uninstall.set_defaults(action=ACTION_UNINSTALL) parser_uninstall.add_argument('app') parser_start = subparsers.add_parser('start') parser_start.set_defaults(action=ACTION_START) parser_start.add_argument('app') parser_stop = subparsers.add_parser('stop') parser_stop.set_defaults(action=ACTION_STOP) parser_stop.add_argument('app') parser_status = subparsers.add_parser('status') parser_status.set_defaults(action=ACTION_STATUS) parser_status.add_argument('app') parser_publish = subparsers.add_parser('publish') parser_publish.set_defaults(action=ACTION_PUBLISH) parser_publish.add_argument('-f', '--force', action='store_true', help='Force republish already published application') parser_publish.add_argument('file') parser_unpublish = subparsers.add_parser('unpublish') parser_unpublish.set_defaults(action=ACTION_UNPUBLISH) parser_unpublish.add_argument('app') args = parser.parse_args() if args.action == ACTION_LIST: listing(args.type) elif args.action == ACTION_INSTALL: install(args.app) elif args.action == ACTION_UPGRADE: upgrade(args.app) elif args.action == ACTION_UNINSTALL: uninstall(args.app) elif args.action == ACTION_START: start(args.app) elif args.action == ACTION_STOP: stop(args.app) elif args.action == ACTION_STATUS: status(args.app) elif args.action == ACTION_PUBLISH: publish(args.file, args.force) elif args.action == ACTION_UNPUBLISH: unpublish(args.app) else: parser.print_usage()