diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7c640c7 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,27 @@ +os: + - linux +language: python +python: + # - "2.6" + # - "2.7" + # - "3.3" + - "3.4" + - "3.5" + - "3.5-dev" # 3.5 development branch + - "3.6" + - "3.6-dev" # 3.6 development branch + # - "3.7-dev" # 3.7 development branch +# command to install dependencies +install: + - pip install --upgrade pip + - pip install coverage + - pip install python-coveralls + - pip install pytest + - pip install pytest-cov + - pip install pytest-pep8 + - pip install -r requirements.txt +script: + - python setup.py install + - pytest -v --cov-report term-missing --pep8 # or py.test for Python versions 3.5 and below +after_success: + # - coveralls diff --git a/deploy/deploy.py b/deploy/deploy.py index 2678b9c..c6cd0d3 100644 --- a/deploy/deploy.py +++ b/deploy/deploy.py @@ -22,6 +22,7 @@ cli_root_dir = os.path.dirname(this_dir) module_json = os.path.join(cli_root_dir, 'module.json') + def main(): ''' This script updates, versions, and builds new versions of the Kubos CLI. @@ -68,10 +69,11 @@ def get_module_version(): def bump_and_write_version(version): version_fields = version.split('.') - if len(version_fields) == 4: #Add a patch number - version_fields[3] = str(int(version_fields[3]) + 1) #bump the version number by 1 and store it as a string + if len(version_fields) == 4: # Add a patch number + # bump the version number by 1 and store it as a string + version_fields[3] = str(int(version_fields[3]) + 1) version = '.'.join(version_fields) - elif len(version_fields) == 3:# bump the version number + elif len(version_fields) == 3: # bump the version number version = version + '.1' with open(module_json, 'r') as module_file: @@ -84,19 +86,23 @@ def bump_and_write_version(version): sort_keys=True, indent=4, separators=(',', ': ')) - ) + ) return version def commit_and_push(version_number): - run_cmd('git', 'config', '--global', 'user.name', os.environ['GITHUB_USERNAME']) - run_cmd('git', 'config', '--global', 'user.email', os.environ['GITHUB_EMAIL']) + run_cmd('git', 'config', '--global', 'user.name', + os.environ['GITHUB_USERNAME']) + run_cmd('git', 'config', '--global', + 'user.email', os.environ['GITHUB_EMAIL']) run_cmd('git', 'add', 'module.json') print 'Committing the version update...' - run_cmd('git', 'commit', '-m', '"Bump version to %s. [ci skip]"' % version_number) #we want ci to skip to prevent an infinite release cycle. + # we want ci to skip to prevent an infinite release cycle. + run_cmd('git', 'commit', '-m', + '"Bump version to %s. [ci skip]"' % version_number) print 'Pushing the commit to origin...' - run_cmd('git', 'push', 'origin', 'master') #push the commit + run_cmd('git', 'push', 'origin', 'master') # push the commit def build_wheel(): diff --git a/deploy/release.py b/deploy/release.py index 74fc216..a2e19db 100644 --- a/deploy/release.py +++ b/deploy/release.py @@ -43,20 +43,21 @@ def create_release(version): This function returns the URI template for uploading a release asset ''' headers = { - 'Content-type': 'application/json', - 'Accept': 'application/json' - } - - data = { - 'tag_name': version, - 'target_commitish': 'master', - 'name': version, - 'body': '', + 'Content-type': 'application/json', + 'Accept': 'application/json' + } + + data = { + 'tag_name': version, + 'target_commitish': 'master', + 'name': version, + 'body': '', 'draft': False, 'prerelease': False - } + } - res = requests.post(release_endpoint, auth=auth, headers=headers, data=json.dumps(data)) + res = requests.post(release_endpoint, auth=auth, + headers=headers, data=json.dumps(data)) res.raise_for_status() return res.json()['upload_url'] @@ -67,9 +68,9 @@ def upload_wheel(version, uri_template): ''' print 'Uploading the wheel build...' headers = { - 'Content-type': 'application/octet-stream', - 'Accept': 'application/json' - } + 'Content-type': 'application/octet-stream', + 'Accept': 'application/json' + } template = URITemplate(uri_template) wheel_path = get_wheel_file_path() @@ -84,7 +85,8 @@ def get_wheel_file_path(): dist_dir = os.path.join(this_dir, '..', 'dist') if os.path.isdir(dist_dir): for _file in os.listdir(dist_dir): - if _file.endswith('.whl'): #Running in a CD environment, there will only be a single wheel build in the dist/ folder + # Running in a CD environment, there will only be a single wheel build in the dist/ folder + if _file.endswith('.whl'): return os.path.join(dist_dir, _file) print 'Unable to find the wheel build under directory %s.. Aborting.' % dist_dir sys.exit(1) @@ -93,4 +95,3 @@ def get_wheel_file_path(): def github_release(version): uri_template = create_release(version) upload_wheel(version, uri_template) - diff --git a/kubos/completion/complete.py b/kubos/completion/complete.py index 5d98b69..4b8716c 100644 --- a/kubos/completion/complete.py +++ b/kubos/completion/complete.py @@ -4,6 +4,7 @@ import sys import os + def main(): ''' This script works by coming up with a list of possible completions and printing @@ -19,7 +20,8 @@ def main(): class Completer(object): - JSON_FILE = os.path.join(os.path.expanduser('~'), '.kubos', 'completion', 'options.json') + JSON_FILE = os.path.join(os.path.expanduser( + '~'), '.kubos', 'completion', 'options.json') def __init__(self): if os.path.isfile(self.JSON_FILE): @@ -27,11 +29,11 @@ def __init__(self): self.arg_data = json.loads(_fil.read()) else: self.arg_data = None - self.args = sys.argv[2:] #chop off the initial 'python kubos' arguments + # chop off the initial 'python kubos' arguments + self.args = sys.argv[2:] self.subcommands = self.get_current_subcommands() self.load_targets() - def load_targets(self): ''' Load the available targets, based on the current project's platform type @@ -40,13 +42,11 @@ def load_targets(self): targets = self.load_target_list(platform) self.arg_data['subcommands']['target']['set_target']['choices'] = targets - def get_completions(self): # Only completing the subcommands and their args is supported right now. # Completing the global options (--config, --target, etc.) should be supported in the future. return self.eval_subcommands() - def eval_subcommands(self): ''' Returns list of possible subcommand, and subcommand specific arguments based @@ -57,27 +57,29 @@ def eval_subcommands(self): ''' num_args = len(self.args) if num_args == 0: - #nothing has been entered - return every subcommand + # nothing has been entered - return every subcommand return self.subcommands else: possible_arguments = [] - #get all the possible subcommand completions for the entered text + # get all the possible subcommand completions for the entered text possible_subcommands = self.get_current_subcommand_completion() subcommand = self.get_current_subcommand() if subcommand is not None: - #gets all possible argument values for the subcommand - possible_arguments = self.get_valid_subcommand_argument_list(subcommand) - #try to get an argument following the subcommand + # gets all possible argument values for the subcommand + possible_arguments = self.get_valid_subcommand_argument_list( + subcommand) + # try to get an argument following the subcommand arg = self.get_next_arg() if arg is not None: - #drop other subcommand completions - they're already typing an argument for the subcommand + # drop other subcommand completions - they're already typing an argument for the subcommand possible_subcommands = [] - possible_arguments = self.get_completions_from_list(arg, possible_arguments) + possible_arguments = self.get_completions_from_list( + arg, possible_arguments) if self.is_valid_subcommand_arg(subcommand, arg): - return [] #if we've already completed a complete and valid argument, stop suggesting it. + # if we've already completed a complete and valid argument, stop suggesting it. + return [] return possible_arguments + possible_subcommands - def get_completions_from_list(self, val, option_list): ''' Generic function for returning all values from option_list that start with @@ -89,15 +91,13 @@ def get_completions_from_list(self, val, option_list): ret_list.append(option) return ret_list - def get_current_subcommand_completion(self): ''' Returns all possible subcommand name completions for the next argument ''' - arg_val = self.args[0] #we should get the subcommand name first + arg_val = self.args[0] # we should get the subcommand name first return self.get_completions_from_list(arg_val, self.subcommands) - def get_next_arg(self): ''' pop the next arg off the front of the provided arguments and return it @@ -106,7 +106,6 @@ def get_next_arg(self): return self.args.pop(0) return None - def get_current_subcommand(self): ''' Returns the subcommand name if next argument is a valid subcommand or None if it isn't @@ -117,7 +116,6 @@ def get_current_subcommand(self): else: return None - def is_valid_subcommand_arg(self, subcommand, arg): ''' Returns True if arg is a valid argument for subcommand, otherwise it returns False @@ -127,7 +125,6 @@ def is_valid_subcommand_arg(self, subcommand, arg): return True return False - def get_valid_subcommand_argument_list(self, subcommand): ''' Returns a list of the valid argument completions for subcommand. @@ -144,7 +141,6 @@ def get_valid_subcommand_argument_list(self, subcommand): choices += args[arg]['choices'] return choices - def get_current_subcommands(self): ''' This function contains the try/except because it's the first function @@ -157,7 +153,6 @@ def get_current_subcommands(self): except TypeError: sys.exit(1) - ################################################################ # CLI DUPLICATED FUNCTIONS ################################################################ @@ -178,22 +173,22 @@ def get_platform(self): else: return 'linux' else: - #This project doesn't have a dependencies field. This is most likely running in a unit testing context + # This project doesn't have a dependencies field. This is most likely running in a unit testing context return None else: - #There is no module.json + # There is no module.json return None - def load_target_list(self, platform): - KUBOS_TARGET_CACHE_FILE = os.path.join(os.path.expanduser('~'), '.kubos', 'targets.json') + KUBOS_TARGET_CACHE_FILE = os.path.join( + os.path.expanduser('~'), '.kubos', 'targets.json') if not os.path.isfile(KUBOS_TARGET_CACHE_FILE): return None with open(KUBOS_TARGET_CACHE_FILE, 'r') as json_file: data = json.loads(json_file.read()) linux_targets = data['linux-targets'] - rt_targets = data['rt-targets'] - if platform == None: #if no platform is listed in the module.json, dont restrict the target type + rt_targets = data['rt-targets'] + if platform == None: # if no platform is listed in the module.json, dont restrict the target type return linux_targets + rt_targets elif platform == 'linux': return linux_targets @@ -203,4 +198,3 @@ def load_target_list(self, platform): if __name__ == '__main__': main() - diff --git a/kubos/init.py b/kubos/init.py index fd9e096..388a1b4 100644 --- a/kubos/init.py +++ b/kubos/init.py @@ -27,32 +27,40 @@ from kubos.utils.constants import KUBOS_RT_EXAMPLE_DIR, KUBOS_LINUX_EXAMPLE_DIR, KUBOS_SRC_DIR from kubos.utils import sdk_utils + def addOptions(parser): parser.add_argument('proj_name', nargs=1, help='specify the project name') group = parser.add_mutually_exclusive_group() - group.add_argument('-l', '--linux', action='store_true', help='Initialize Kubos SDK project for KubOS Linux') - group.add_argument('-r', '--rt', action='store_true', default=True, help='Initialize Kubos SDK project for KubOS RT') + group.add_argument('-l', '--linux', action='store_true', + help='Initialize Kubos SDK project for KubOS Linux') + group.add_argument('-r', '--rt', action='store_true', default=True, + help='Initialize Kubos SDK project for KubOS RT') + def execCommand(args, following_args): - proj_name = vars(args)['proj_name'][0] #vars returns a dict of args. proj_name is a list since nargs=1 + # vars returns a dict of args. proj_name is a list since nargs=1 + proj_name = vars(args)['proj_name'][0] logging.info('Initializing project: %s ...' % proj_name) proj_name_dir = os.path.join(os.getcwd(), proj_name) if os.path.isdir(proj_name_dir): - logging.warning('The project directory %s already exists. Not overwritting the current directory' % proj_name_dir) + logging.warning( + 'The project directory %s already exists. Not overwritting the current directory' % proj_name_dir) sys.exit(1) - #Copy in the correct example directory based on the desired OS + # Copy in the correct example directory based on the desired OS example_dir = KUBOS_LINUX_EXAMPLE_DIR if args.linux else KUBOS_RT_EXAMPLE_DIR - shutil.copytree(example_dir, proj_name_dir, ignore=shutil.ignore_patterns('.git')) + shutil.copytree(example_dir, proj_name_dir, + ignore=shutil.ignore_patterns('.git')) - #change project name in module.json + # change project name in module.json module_json = os.path.join(proj_name_dir, 'module.json') with open(module_json, 'r') as init_module_json: module_data = json.load(init_module_json) module_data['name'] = proj_name - module_data['repository']['url'] = 'git://' #These fields print warnings if they're - module_data['homepage'] = 'https://' #left empty + # These fields print warnings if they're + module_data['repository']['url'] = 'git://' + module_data['homepage'] = 'https://' # left empty with open(module_json, 'w') as final_module_json: str_module_data = json.dumps(module_data, indent=4, @@ -61,7 +69,7 @@ def execCommand(args, following_args): os.chdir(proj_name_dir) sdk_utils.link_global_cache_to_project(proj_name_dir) - #remove the troublesome rt dependencies if needed + # remove the troublesome rt dependencies if needed proj_type = sdk_utils.get_project_type() if proj_type == 'rt': remove_unruly_rt_dependencies() @@ -75,13 +83,15 @@ def remove_unruly_rt_dependencies(): are initialized. ''' - dependency_list = ['cmocka'] #add new module names to the list if new build issues are found in the future. + # add new module names to the list if new build issues are found in the future. + dependency_list = ['cmocka'] for dep in dependency_list: path = os.path.join(os.getcwd(), 'yotta_modules', dep) if os.path.islink(path): os.unlink(path) + def get_target_list(): ''' This is a helper function for getting a list of all the globally linked @@ -97,5 +107,3 @@ def get_target_list(): data = json.load(json_file) available_target_list.append(data['name']) return available_target_list - - diff --git a/kubos/link.py b/kubos/link.py index 87d697b..02b8954 100644 --- a/kubos/link.py +++ b/kubos/link.py @@ -19,15 +19,15 @@ from kubos.utils.sdk_utils import * from kubos.init import remove_unruly_rt_dependencies + def addOptions(parser): parser.add_argument('-a', '--all', action='store_true', default=False, - help='Link all modules (and targets) from the global cache into the local project.') + help='Link all modules (and targets) from the global cache into the local project.') parser.add_argument('module_or_path', default=None, nargs='?', - help='Link a globally installed (or globally linked) module into '+ - 'the current module\'s dependencies. If ommited, globally '+ - 'link the current module.' - ) - + help='Link a globally installed (or globally linked) module into ' + + 'the current module\'s dependencies. If ommited, globally ' + + 'link the current module.' + ) def execCommand(args, following_args): @@ -48,6 +48,5 @@ def execCommand(args, following_args): if proj_type == 'rt': remove_unruly_rt_dependencies() else: - #pass in the args argparse.Namespace object - not the dictionary from above + # pass in the args argparse.Namespace object - not the dictionary from above link.execCommand(args, following_args) - diff --git a/kubos/main.py b/kubos/main.py index bbf2175..0620977 100644 --- a/kubos/main.py +++ b/kubos/main.py @@ -27,12 +27,14 @@ def splitList(l, at_value): r[-1].append(x) return r -def setup_yotta(): # override and setup certain yotta functions. + +def setup_yotta(): # override and setup certain yotta functions. from . import override for module_name in override.file_list: module = importlib.import_module('.' + module_name, 'kubos.override') module.exec_override() + def main(): setup_yotta() @@ -52,10 +54,11 @@ def main(): # subcommands, and allow lazy loading of subcommand modules: parser = options.parser.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter, - description='Kubos-CLI For working with Kubos Projects.\n'+ + description='Kubos-CLI For working with Kubos Projects.\n' + 'For more detailed help on each subcommand, run: kubos --help' ) - subparser = parser.add_subparsers(dest='subcommand_name', metavar='') + subparser = parser.add_subparsers( + dest='subcommand_name', metavar='') # add re-usable top-level options which subcommands may also accept options.verbosity.addTo(parser) @@ -66,84 +69,97 @@ def main(): options.target.addTo(parser) options.config.addTo(parser) - local_config = sdk_config.load_config() - add_kubos_command = functools.partial(kubos_options.command.add_command, local_config, subparser, 'kubos') #add our own implemented commands - add_yotta_command = functools.partial(kubos_options.command.add_command, local_config, subparser, 'yotta') #add from the default yotta commands + add_kubos_command = functools.partial( + kubos_options.command.add_command, local_config, subparser, 'kubos') # add our own implemented commands + # add from the default yotta commands + add_yotta_command = functools.partial( + kubos_options.command.add_command, local_config, subparser, 'yotta') add_kubos_command('init', 'init', 'Create a new module.') add_yotta_command('build', 'build', - 'Build the current module. Options can be passed to the underlying '+ - 'build tool by passing them after --, e.g. to do a verbose build '+ - 'which will display each command as it is run, use:\n'+ - ' yotta build -- -v\n\n'+ - 'The programs or libraries to build can be specified (by default '+ - 'only the libraries needed by the current module and the current '+ - "module's own tests are built). For example, to build the tests of "+ - 'all dependencies, run:\n yotta build all_tests\n\n', - 'Build the current module.' - ) + 'Build the current module. Options can be passed to the underlying ' + + 'build tool by passing them after --, e.g. to do a verbose build ' + + 'which will display each command as it is run, use:\n' + + ' yotta build -- -v\n\n' + + 'The programs or libraries to build can be specified (by default ' + + 'only the libraries needed by the current module and the current ' + + "module's own tests are built). For example, to build the tests of " + + 'all dependencies, run:\n yotta build all_tests\n\n', + 'Build the current module.' + ) add_kubos_command('link', 'link', - 'Symlink a module to be used into another module.\n\n'+ - 'Use: "yotta link" in a module to link it globally, then use "yotta '+ - 'link " to link it into the module where you want to use '+ - 'it.\n\n'+ - '"yotta link ../path/to/module" is also supported, which will create '+ - 'the global link and a link into the current module in a single step.', - 'Symlink a module' - ) + 'Symlink a module to be used into another module.\n\n' + + 'Use: "yotta link" in a module to link it globally, then use "yotta ' + + 'link " to link it into the module where you want to use ' + + 'it.\n\n' + + '"yotta link ../path/to/module" is also supported, which will create ' + + 'the global link and a link into the current module in a single step.', + 'Symlink a module' + ) add_yotta_command('link-target', 'link_target', - 'Symlink a target to be used into another module.\n\n'+ - 'Use: "yotta link" in a target to link it globally, then use "yotta '+ - 'link-target " to link it into the module where you want to use '+ - 'it.\n\n'+ - '"yotta link ../path/to/target" is also supported, which will create '+ - 'the global link and a link into the current module in a single step.', - 'Symlink a target' - ) - add_kubos_command('update', 'update', 'Download newer versions of the KubOS Modules') + 'Symlink a target to be used into another module.\n\n' + + 'Use: "yotta link" in a target to link it globally, then use "yotta ' + + 'link-target " to link it into the module where you want to use ' + + 'it.\n\n' + + '"yotta link ../path/to/target" is also supported, which will create ' + + 'the global link and a link into the current module in a single step.', + 'Symlink a target' + ) + add_kubos_command('update', 'update', + 'Download newer versions of the KubOS Modules') add_kubos_command('target', 'target', 'Set or display the target device.') - add_yotta_command('debug', 'debug', 'Attach a debugger to the current target. Requires target support.') + add_yotta_command( + 'debug', 'debug', 'Attach a debugger to the current target. Requires target support.') add_yotta_command('test', 'test_subcommand', - 'Run the tests for the current module on the current target. A build '+ - 'will be run first, and options to the build subcommand are also '+ - 'accepted by test.\nThis subcommand requires the target to provide a '+ - '"test" script that will be used to run each test. Modules may also '+ - 'define a "testReporter" script, which will be piped the output from '+ - 'each test, and may produce a summary.', - 'Run the tests for the current module on the current target. Requires target support for cross-compiling targets.' - ) + 'Run the tests for the current module on the current target. A build ' + + 'will be run first, and options to the build subcommand are also ' + + 'accepted by test.\nThis subcommand requires the target to provide a ' + + '"test" script that will be used to run each test. Modules may also ' + + 'define a "testReporter" script, which will be piped the output from ' + + 'each test, and may produce a summary.', + 'Run the tests for the current module on the current target. Requires target support for cross-compiling targets.' + ) add_yotta_command('start', 'start', - 'Launch the compiled program (available for executable modules only). Requires target support for cross-compiling targets.' - ) - add_yotta_command('list', 'list', 'List the dependencies of the current module, or the inherited targets of the current target.') - add_yotta_command('outdated', 'outdated', 'Display information about dependencies which have newer versions available.') + 'Launch the compiled program (available for executable modules only). Requires target support for cross-compiling targets.' + ) + add_yotta_command( + 'list', 'list', 'List the dependencies of the current module, or the inherited targets of the current target.') + add_yotta_command('outdated', 'outdated', + 'Display information about dependencies which have newer versions available.') add_yotta_command('remove', 'remove', - 'Remove the downloaded version of a dependency module or target, or '+ - 'un-link a linked module or target (see yotta link --help for details '+ - 'of linking). This command does not modify your module.json file.', - 'Remove or unlink a dependency without removing it from module.json.' - ) - add_yotta_command('licenses', 'licenses', 'List the licenses of the current module and its dependencies.') - add_yotta_command('clean', 'clean', 'Remove files created by yotta and the build.') - add_yotta_command('config', 'config', 'Display the target configuration info.') - add_yotta_command('shrinkwrap', 'shrinkwrap', 'Create a yotta-shrinkwrap.json file to freeze dependency versions.') - add_kubos_command('version', 'version', 'Display the current active version of the cli and KubOS source repo.') - add_kubos_command('use', 'use', 'Set a new version of the KubOS modules to build your projects against.') - add_kubos_command('versions', 'versions', 'Display the available versions of the KubOS source.') + 'Remove the downloaded version of a dependency module or target, or ' + + 'un-link a linked module or target (see yotta link --help for details ' + + 'of linking). This command does not modify your module.json file.', + 'Remove or unlink a dependency without removing it from module.json.' + ) + add_yotta_command('licenses', 'licenses', + 'List the licenses of the current module and its dependencies.') + add_yotta_command('clean', 'clean', + 'Remove files created by yotta and the build.') + add_yotta_command('config', 'config', + 'Display the target configuration info.') + add_yotta_command('shrinkwrap', 'shrinkwrap', + 'Create a yotta-shrinkwrap.json file to freeze dependency versions.') + add_kubos_command('version', 'version', + 'Display the current active version of the cli and KubOS source repo.') + add_kubos_command( + 'use', 'use', 'Set a new version of the KubOS modules to build your projects against.') + add_kubos_command('versions', 'versions', + 'Display the available versions of the KubOS source.') # short synonyms, subparser.choices is a dictionary, so use update() to # merge in the keys from another dictionary short_commands = { - 'up':subparser.choices['update'], - 'ln':subparser.choices['link'], - 'v':subparser.choices['version'], - 'ls':subparser.choices['list'], - 'rm':subparser.choices['remove'], - 'unlink':subparser.choices['remove'], - 'unlink-target':subparser.choices['remove'], - 'lics':subparser.choices['licenses'], - 'run':subparser.choices['start'], - 'flash':subparser.choices['start'] + 'up': subparser.choices['update'], + 'ln': subparser.choices['link'], + 'v': subparser.choices['version'], + 'ls': subparser.choices['list'], + 'rm': subparser.choices['remove'], + 'unlink': subparser.choices['remove'], + 'unlink-target': subparser.choices['remove'], + 'lics': subparser.choices['licenses'], + 'run': subparser.choices['start'], + 'flash': subparser.choices['start'] } subparser.choices.update(short_commands) @@ -151,12 +167,14 @@ def main(): # argument - subcommands get raw access to arguments following '--', and # may pass them on to (for example) the build tool being used split_args = splitList(sys.argv, '--') - following_args = functools.reduce(lambda x,y: x + ['--'] + y, split_args[1:], [])[1:] + following_args = functools.reduce( + lambda x, y: x + ['--'] + y, split_args[1:], [])[1:] # complete all the things :) argcomplete.autocomplete( - parser, - exclude = list(short_commands.keys()) + ['-d', '--debug', '-v', '--verbose'] + parser, + exclude=list(short_commands.keys()) + + ['-d', '--debug', '-v', '--verbose'] ) # when args are passed directly we need to strip off the program name diff --git a/kubos/options/command.py b/kubos/options/command.py index 52fd6e2..d7c01e5 100644 --- a/kubos/options/command.py +++ b/kubos/options/command.py @@ -1,10 +1,12 @@ import argparse import importlib + class SDKCommand(object): def __init__(self, config, base_module, name, module_name, description, help=None): self.config = config - self.base_module = base_module #yotta or kubos - This is the base module we are importing the command from + # yotta or kubos - This is the base module we are importing the command from + self.base_module = base_module self.name = name self.module_name = module_name self.description = description @@ -21,7 +23,8 @@ def execCommand(self, args, following_args): return self.module.execCommand(args, following_args) def onParserAdded(self, parser): - self.module = importlib.import_module('.' + self.module_name, self.base_module) + self.module = importlib.import_module( + '.' + self.module_name, self.base_module) self.module.addOptions(parser) parser.set_defaults(command=self.execCommand) @@ -30,4 +33,5 @@ def add_command(config, subparser, *args, **kwargs): m = _command_class(config, *args, **kwargs) m.addToSubparser(subparser) + _command_class = SDKCommand diff --git a/kubos/override/reimplemented_modules/cmakegen.py b/kubos/override/reimplemented_modules/cmakegen.py index 6520812..d92a592 100644 --- a/kubos/override/reimplemented_modules/cmakegen.py +++ b/kubos/override/reimplemented_modules/cmakegen.py @@ -15,16 +15,18 @@ from yotta.lib.cmakegen import * + class KubosCMakeGen(CMakeGen): def _kCheckImmediateDeps(self, deps): # make a copy so we don't recurse new_deps = {} dep_items = [i for i in deps.items()] for name, component in dep_items: - if component.isTestDependency(): continue + if component.isTestDependency(): + continue if component.getLinkWholeArchive(): # surround 'whole archive' libs with the proper linker args - new_key = ('${YOTTA_LINK_WHOLE_ARCHIVE_PRE} ' \ + new_key = ('${YOTTA_LINK_WHOLE_ARCHIVE_PRE} ' '%s ${YOTTA_LINK_WHOLE_ARCHIVE_POST}') % name new_deps[new_key] = deps[name] else: @@ -45,4 +47,5 @@ def generateSubDirList(self, builddir, dirname, source_files, component, resource_subdirs, is_executable) + CMakeGen = KubosCMakeGen diff --git a/kubos/override/reimplemented_modules/component.py b/kubos/override/reimplemented_modules/component.py index 177c645..508fa10 100644 --- a/kubos/override/reimplemented_modules/component.py +++ b/kubos/override/reimplemented_modules/component.py @@ -15,8 +15,10 @@ from yotta.lib.component import * + class KubosComponent(Component): def getLinkWholeArchive(self): return self.description.get('linkWholeArchive', False) + Component = KubosComponent diff --git a/kubos/override/reimplemented_modules/detect.py b/kubos/override/reimplemented_modules/detect.py index 2efbf56..5c08791 100644 --- a/kubos/override/reimplemented_modules/detect.py +++ b/kubos/override/reimplemented_modules/detect.py @@ -28,6 +28,7 @@ # settings, , load and save settings, internal from yotta.lib import settings + def defaultTarget(ignore_set_target=False): set_target = settings.getProperty('build', 'target') if set_target: diff --git a/kubos/override/yotta_lib.py b/kubos/override/yotta_lib.py index 210ffbb..50b90bc 100644 --- a/kubos/override/yotta_lib.py +++ b/kubos/override/yotta_lib.py @@ -23,6 +23,7 @@ import yotta.lib.access from reimplemented_modules import access, cmakegen, component, detect + def exec_override(): yotta.lib.access = access yotta.lib.detect = detect diff --git a/kubos/sdk_config.py b/kubos/sdk_config.py index ab6289e..c23a33f 100644 --- a/kubos/sdk_config.py +++ b/kubos/sdk_config.py @@ -26,6 +26,7 @@ from kubos.utils.sdk_utils import get_sdk_attribute + def load_config(): return _config_class() @@ -39,11 +40,12 @@ class KubosSDKConfig(object): def __init__(self): self.appdirs = AppDirs('kubos') - self.config_path = os.path.join(self.appdirs.user_config_dir, 'kubos-cli.json') + self.config_path = os.path.join( + self.appdirs.user_config_dir, 'kubos-cli.json') self.sdk_version = load_sdk_version() self.load_config() #thread = threading.Thread(target=self.ping) - #thread.start() + # thread.start() def load_config(self): self.config = {} @@ -62,13 +64,16 @@ def ping(self): if 'uuid' in self.config: uid = self.config['uuid'] else: - uid = uuid.uuid4().hex #uuid4 generates a completely random uuid + uid = uuid.uuid4().hex # uuid4 generates a completely random uuid self.config['uuid'] = uid self.save_config() data = self.json_template % (time.time(), uid) try: - requests.post("https://drvpjfu9ci.execute-api.us-east-1.amazonaws.com/prod/AnalyticsTest", data=data) # This URL needs to be changed to the production DynamoDB endpoint + # This URL needs to be changed to the production DynamoDB endpoint + requests.post( + "https://drvpjfu9ci.execute-api.us-east-1.amazonaws.com/prod/AnalyticsTest", data=data) except: pass + _config_class = KubosSDKConfig diff --git a/kubos/target.py b/kubos/target.py index d3200ca..28b39ad 100644 --- a/kubos/target.py +++ b/kubos/target.py @@ -25,17 +25,22 @@ from kubos.utils.constants import GLOBAL_TARGET_PATH from kubos.utils.sdk_utils import * + def addOptions(parser): proj_type = get_project_type() choices = load_target_list(proj_type) - parser.add_argument('set_target', nargs='?', default=None, choices=choices, help='Set a new target board or display the current target') - parser.add_argument('-l', '--list', action='store_true', default=False, help='List all of the available target names') + parser.add_argument('set_target', nargs='?', default=None, choices=choices, + help='Set a new target board or display the current target') + parser.add_argument('-l', '--list', action='store_true', + default=False, help='List all of the available target names') def execCommand(args, following_args): args = vars(args) - target = args['set_target'] #Confusingly the set_target key is the target the user wants to set, no the currently set target - default_target = args['target'] #this is either the currently set target or the default x86-linux-native target + # Confusingly the set_target key is the target the user wants to set, no the currently set target + target = args['set_target'] + # this is either the currently set target or the default x86-linux-native target + default_target = args['target'] if args['list']: print_target_list() elif target != None: @@ -53,7 +58,8 @@ def show_target(default_target): target.displayCurrentTarget(target_args) else: logging.warning('No target currently set') - logging.info('Use the "kubos target " command to set a target') + logging.info( + 'Use the "kubos target " command to set a target') print_target_list() @@ -63,11 +69,11 @@ def set_target(new_target): if new_target in available_target_list: new_target_args = argparse.Namespace(target_or_path=new_target, - config=None, - target=new_target, - set_target=new_target, - save_global=False, - no_install=False) + config=None, + target=new_target, + set_target=new_target, + save_global=False, + no_install=False) target.execCommand(new_target_args, '') logging.info('Target Successfully Set to: %s' % new_target) else: @@ -76,6 +82,7 @@ def set_target(new_target): print_target_list() sys.exit(1) + def print_target_list(): proj_type = get_project_type() target_list = load_target_list(proj_type) diff --git a/kubos/test/test_build.py b/kubos/test/test_build.py index 640a96d..f26b0f7 100644 --- a/kubos/test/test_build.py +++ b/kubos/test/test_build.py @@ -19,7 +19,8 @@ import unittest import yotta.build -from kubos.test.utils import KubosTestCase +from kubos.test.utils import KubosTestCase + class KubosBuildTest(KubosTestCase): ''' @@ -27,6 +28,7 @@ class KubosBuildTest(KubosTestCase): this test only looks to make sure that the yotta implementation is called. There's a separate yotta unit test to test the build functionality. ''' + def setUp(self): super(KubosBuildTest, self).setUp() self.test_function = mock.MagicMock() @@ -34,14 +36,13 @@ def setUp(self): self.test_command = 'build' sys.argv.append(self.test_command) - def test_build(self): kubos.main() self.assert_default_yotta_call() - def tearDown(self): super(KubosBuildTest, self).tearDown() + if __name__ == '__main__': unittest.main() diff --git a/kubos/test/test_init.py b/kubos/test/test_init.py index 0365455..f71e4fb 100644 --- a/kubos/test/test_init.py +++ b/kubos/test/test_init.py @@ -23,7 +23,8 @@ from kubos.test import utils from kubos.utils import constants from mock import patch -from yotta.test.cli.test_target import Test_Module_JSON #A dummy module.json config +from yotta.test.cli.test_target import Test_Module_JSON # A dummy module.json config + class KubosInitTest(utils.KubosTestCase): @@ -31,26 +32,29 @@ def setUp(self): super(KubosInitTest, self).setUp() self.proj_name = 'test-project' self.args = argparse.Namespace() - self.args.proj_name = [self.proj_name] # argparse returns proj_name as an array + # argparse returns proj_name as an array + self.args.proj_name = [self.proj_name] self.args.linux = False - linux_module_json = os.path.join(constants.KUBOS_LINUX_EXAMPLE_DIR, 'module.json') - rt_module_json = os.path.join(constants.KUBOS_RT_EXAMPLE_DIR, 'module.json') + linux_module_json = os.path.join( + constants.KUBOS_LINUX_EXAMPLE_DIR, 'module.json') + rt_module_json = os.path.join( + constants.KUBOS_RT_EXAMPLE_DIR, 'module.json') - #Set up the linux example module + # Set up the linux example module if not os.path.isdir(constants.KUBOS_LINUX_EXAMPLE_DIR): os.makedirs(constants.KUBOS_LINUX_EXAMPLE_DIR) if not os.path.isfile(linux_module_json): with open(linux_module_json, 'w') as mod_json: mod_json.write(Test_Module_JSON) - #Set up the rt example module + # Set up the rt example module if not os.path.isdir(constants.KUBOS_RT_EXAMPLE_DIR): os.makedirs(constants.KUBOS_RT_EXAMPLE_DIR) if not os.path.isfile(rt_module_json): with open(rt_module_json, 'w') as mod_json: mod_json.write(Test_Module_JSON) - #Set up a dummy global module and target cache + # Set up a dummy global module and target cache if not os.path.isdir(constants.GLOBAL_TARGET_PATH): os.makedirs(constants.GLOBAL_TARGET_PATH) if not os.path.isdir(constants.GLOBAL_MODULE_PATH): @@ -78,13 +82,13 @@ def test_overwrite_existing(self): self.proj_dir = os.path.join(self.base_dir, self.proj_name) with self.assertRaises(SystemExit): kubos.init.execCommand(self.args, None) - #we cd into the project directory to change the module.json file in execCommand + # we cd into the project directory to change the module.json file in execCommand os.chdir(self.base_dir) kubos.init.execCommand(self.args, None) - def tearDown(self): super(KubosInitTest, self).tearDown() + if __name__ == '__main__': unittest.main() diff --git a/kubos/test/test_link.py b/kubos/test/test_link.py index 6a362ab..be65228 100644 --- a/kubos/test/test_link.py +++ b/kubos/test/test_link.py @@ -19,17 +19,18 @@ from kubos import link + class LinkTest(unittest.TestCase): def test_add_options(self): parser = MagicMock() parser.add_argument = MagicMock() link.addOptions(parser) calls = [ - call('-a', '--all', action='store_true', default=False, \ - help='Link all modules (and targets) from the global cache into the local project.'), - call('module_or_path', default=None, nargs='?', \ - help='Link a globally installed (or globally linked) module into '+ \ - 'the current module\'s dependencies. If ommited, globally '+ \ + call('-a', '--all', action='store_true', default=False, + help='Link all modules (and targets) from the global cache into the local project.'), + call('module_or_path', default=None, nargs='?', + help='Link a globally installed (or globally linked) module into ' + + 'the current module\'s dependencies. If ommited, globally ' + 'link the current module.'), ] parser.add_argument.assert_has_calls(calls) @@ -60,4 +61,4 @@ def __init__(self): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/kubos/test/test_sdk_utils.py b/kubos/test/test_sdk_utils.py index 9e95091..715f82e 100644 --- a/kubos/test/test_sdk_utils.py +++ b/kubos/test/test_sdk_utils.py @@ -20,7 +20,7 @@ import sys import unittest -from yotta.test.cli.util import Test_Trivial_Lib # +from yotta.test.cli.util import Test_Trivial_Lib from yotta.test.cli.test_target import Test_Module_JSON from yotta import link, link_target @@ -29,6 +29,7 @@ from sets import Set + class KubosSdkUtilsTest(KubosTestCase): def setUp(self): @@ -60,54 +61,52 @@ def setUp(self): with open(self.target_json, 'w') as target_file: target_file.write(Test_Module_JSON) - @mock.patch('yotta.link.execCommand', mock.MagicMock()) @mock.patch('yotta.link_target.execCommand', mock.MagicMock()) def test_link_target_to_proj(self): json_data = json.loads(Test_Module_JSON) - expected_args = { 'save_global': True, - 'module_or_path': json_data['name'], - 'target': 'x86-linux-native,*', - 'no_install': False, - 'target_or_path': json_data['name'], - 'config': None} - sdk_utils.run_link(self.module_json, self.dir_a) #Link the module to an arbitrary location + expected_args = {'save_global': True, + 'module_or_path': json_data['name'], + 'target': 'x86-linux-native,*', + 'no_install': False, + 'target_or_path': json_data['name'], + 'config': None} + # Link the module to an arbitrary location + sdk_utils.run_link(self.module_json, self.dir_a) link.execCommand.assert_called() link_target.execCommand.assert_not_called() args, kwargs = link.execCommand.call_args[0] self.assertEqual(expected_args, vars(args)) - @mock.patch('yotta.link.execCommand', mock.MagicMock()) @mock.patch('yotta.link_target.execCommand', mock.MagicMock()) def test_link_local_to_global_cache(self): - expected_args = { 'save_global': True, - 'module_or_path': None, - 'target': 'x86-linux-native,*', - 'no_install': False, - 'target_or_path': None, - 'config': None} - sdk_utils.run_link(self.target_json, None) #link test target to the "global cache" of targets + expected_args = {'save_global': True, + 'module_or_path': None, + 'target': 'x86-linux-native,*', + 'no_install': False, + 'target_or_path': None, + 'config': None} + # link test target to the "global cache" of targets + sdk_utils.run_link(self.target_json, None) link.execCommand.assert_not_called() link_target.execCommand.assert_called_once() args, kwargs = link_target.execCommand.call_args[0] self.assertEqual(expected_args, vars(args)) - @mock.patch('kubos.utils.sdk_utils.run_link', mock.MagicMock()) def test_link_entities_discovery(self): sys.argv.append('link') sdk_utils.link_entities(self.base_dir, None) self.assertEqual(sdk_utils.run_link.call_count, 2) call_list = sdk_utils.run_link.call_args_list - expected_args = [ self.module_json, self.target_json ] + expected_args = [self.module_json, self.target_json] idx = 0 for call in call_list: args, kwargs = call[0] self.assertTrue(args in expected_args) idx = idx + 1 - def test_get_all_eligible_targets(self): ''' setup target hierarchy: @@ -132,11 +131,10 @@ def test_get_all_eligible_targets(self): with open(os.path.join(self.dir_d, target_json), 'w') as target_file: target_file.write(inherit_json % ('target_c', 'target_b')) - #Now the actual testing bits + # Now the actual testing bits targets = sdk_utils.get_all_eligible_targets(self.base_dir) self.assertEqual(targets, expected_targets) - def tearDown(self): super(KubosSdkUtilsTest, self).tearDown() diff --git a/kubos/test/test_target.py b/kubos/test/test_target.py index 607efc7..357d5b9 100644 --- a/kubos/test/test_target.py +++ b/kubos/test/test_target.py @@ -21,6 +21,8 @@ from kubos.utils.constants import * test_global_target = 'test-global-target' + + class Args(): def __init__(self): self.set_target = None @@ -28,6 +30,7 @@ def __init__(self): self.list = None self.plain = [] + class TargetTest(unittest.TestCase): @patch('kubos.target.show_target') def test_shows_target_by_default(self, show_target): @@ -65,7 +68,7 @@ def test_sets_the_target(self, get_target_list, info, namespace, execCommand): get_target_list.assert_called() info.assert_has_calls(calls) namespace.assert_called_with(target_or_path=new_target, config=None, target=new_target, - set_target=new_target, save_global=False, no_install=False) + set_target=new_target, save_global=False, no_install=False) execCommand.assert_called_with(new_target_args, '') @patch('sys.exit') @@ -80,8 +83,10 @@ def test_displays_error_when_invalid_target(self, get_target_list, info, error, target.set_target(new_target) get_target_list.assert_called() - info.assert_called_with('Setting Target: %s' % new_target.split('@')[0]) - error.assert_called_with('Requested target %s not available.' % new_target) + info.assert_called_with('Setting Target: %s' % + new_target.split('@')[0]) + error.assert_called_with( + 'Requested target %s not available.' % new_target) print_target_list.assert_called() sysexit.assert_called_with(1) @@ -122,5 +127,6 @@ def test_get_target_list(self, listdir, join): listdir.assert_called_with(test_global_target) join.assert_called_with(test_global_target, subdir, 'target.json') + if __name__ == '__main__': unittest.main() diff --git a/kubos/test/test_update.py b/kubos/test/test_update.py index 49b92a5..d43da61 100644 --- a/kubos/test/test_update.py +++ b/kubos/test/test_update.py @@ -20,10 +20,12 @@ from kubos import update from kubos.utils.constants import * + class Args(): def __init__(self): self.component = None + class UpdateTest(unittest.TestCase): @patch('kubos.update.update_source_modules') def test_exec_command_update_source(self, update_source_modules): @@ -69,7 +71,8 @@ def test_update_cli_error(self, info, check_call, error): check_call.assert_called_with(update.INSTALL_COMMAND) info.assert_called_with('Updating the Kubos CLI...') - error.assert_called_with('There was an issue updating the Kubos CLI module. See the above log for the error details.') + error.assert_called_with( + 'There was an issue updating the Kubos CLI module. See the above log for the error details.') @patch('kubos.utils.git_utils.get_tag_list') @patch('kubos.utils.git_utils.get_latest_tag') @@ -107,10 +110,12 @@ def test_update_source_modules(self, isdir, chdir, info, start_spinner, clone_re ] info.assert_has_calls(info_calls) start_spinner.assert_called() - clone_repo.assert_called_with(update.KUBOS_SRC_DIR, update.KUBOS_SRC_URL) + clone_repo.assert_called_with( + update.KUBOS_SRC_DIR, update.KUBOS_SRC_URL) clone_example_repo_calls = [ call(update.KUBOS_RT_EXAMPLE_DIR, update.KUBOS_RT_EXAMPLE_URL), - call(update.KUBOS_LINUX_EXAMPLE_DIR, update.KUBOS_LINUX_EXAMPLE_URL) + call(update.KUBOS_LINUX_EXAMPLE_DIR, + update.KUBOS_LINUX_EXAMPLE_URL) ] clone_example_repo.assert_has_calls(clone_example_repo_calls) stop_spinner.assert_called_with(spinner) @@ -122,5 +127,6 @@ def test_update_source_modules(self, isdir, chdir, info, start_spinner, clone_re get_latest_tag.assert_called_with(tag_list) get_tag_list.assert_called_with(src_repo) + if __name__ == '__main__': unittest.main() diff --git a/kubos/test/test_use.py b/kubos/test/test_use.py index b51a704..a5dc17b 100644 --- a/kubos/test/test_use.py +++ b/kubos/test/test_use.py @@ -20,11 +20,13 @@ from kubos import use from kubos.utils.constants import * + class Args(): def __init__(self): self.set_version = None self.branch = None + class UseTest(unittest.TestCase): @patch('kubos.utils.sdk_utils.link_to_global_cache') @patch('kubos.utils.sdk_utils.purge_global_cache') @@ -64,5 +66,6 @@ def test_exec_command_with_version(self, get_repo, check_provided_version, purge purge_global_cache.assert_called() link_to_global_cache.assert_called_with(KUBOS_SRC_DIR) + if __name__ == '__main__': unittest.main() diff --git a/kubos/test/test_version.py b/kubos/test/test_version.py index a1bab92..2209e67 100644 --- a/kubos/test/test_version.py +++ b/kubos/test/test_version.py @@ -19,13 +19,14 @@ from kubos import version + class VersionTest(unittest.TestCase): def test_add_options(self): parser = MagicMock() parser.add_argument = MagicMock() version.addOptions(parser) - parser.add_argument.assert_called_with('-l', '--list', action='store_true', \ - default=False, help='List all of the locally available KubOS source versions') + parser.add_argument.assert_called_with('-l', '--list', action='store_true', + default=False, help='List all of the locally available KubOS source versions') @patch('kubos.utils.git_utils.get_active_kubos_version') @patch('logging.info') @@ -37,7 +38,8 @@ def test_exec_command(self, info, get_active_kubos_version): get_active_kubos_version.assert_called() calls = [ - call('Kubos-CLI version : %s' % 'v' + get_installed_version('kubos-cli')), + call('Kubos-CLI version : %s' % + 'v' + get_installed_version('kubos-cli')), call('Kubos Source version : %s' % kubos_version) ] info.assert_has_calls(calls) @@ -57,12 +59,13 @@ def test_exec_command_with_no_version_and_directory(self, info, get_active_kubos get_tag_list.assert_called() print_tag_list.assert_called() calls = [ - call('Kubos-CLI version : %s' % 'v' + get_installed_version('kubos-cli')), + call('Kubos-CLI version : %s' % + 'v' + get_installed_version('kubos-cli')), call('Kubos Source version : %s' % kubos_version), call('There\'s not an active Kubos source version..'), call('The available versions are:'), - call('Please run kubos use (with one of the above versions)' + \ - 'to checkout a version of the source before working with a project.') + call('Please run kubos use (with one of the above versions)' + + 'to checkout a version of the source before working with a project.') ] info.assert_has_calls(calls) @@ -76,7 +79,8 @@ def test_exec_command_with_no_version_and_no_directory(self, info, get_active_ku version.execCommand(None, None) calls = [ - call('Kubos-CLI version : %s' % 'v' + get_installed_version('kubos-cli')), + call('Kubos-CLI version : %s' % + 'v' + get_installed_version('kubos-cli')), call('Kubos Source version : %s' % kubos_version), call('There are not any local versions of the kubos source currently.'), call('Please run `kubos update` to pull the kubos source before running `kubos version` again') diff --git a/kubos/test/test_versions.py b/kubos/test/test_versions.py index cc825db..d5f9312 100644 --- a/kubos/test/test_versions.py +++ b/kubos/test/test_versions.py @@ -20,6 +20,7 @@ from kubos import versions from kubos.utils.constants import * + class VersionsTest(unittest.TestCase): @patch('kubos.utils.git_utils.print_tag_list') @patch('kubos.utils.git_utils.get_latest_tag') @@ -28,6 +29,7 @@ class VersionsTest(unittest.TestCase): @patch('logging.info') def test_exec_command(self, info, get_repo, get_tag_list, get_latest_tag, print_tag_list): filterArg = 'test-filter' + class Args(): def __init__(self): self.filter = filterArg @@ -57,7 +59,8 @@ def __init__(self): def test_exec_command_with_no_directory(self, info): versions.execCommand(None, None) - info.assert_called_with('No versions are locally available. Please run `kubos update` to pull all of the available source versions.') + info.assert_called_with( + 'No versions are locally available. Please run `kubos update` to pull all of the available source versions.') if __name__ == '__main__': diff --git a/kubos/test/utils.py b/kubos/test/utils.py index a57a8dc..bb6e5ca 100644 --- a/kubos/test/utils.py +++ b/kubos/test/utils.py @@ -22,8 +22,8 @@ class KubosTestCase(unittest.TestCase): - test_arg = None #Additional command line argument needed by any specific test cases - test_command = None #Not all test cases define a 'test_command' + test_arg = None # Additional command line argument needed by any specific test cases + test_command = None # Not all test cases define a 'test_command' def setUp(self): ''' @@ -37,7 +37,6 @@ def setUp(self): self.base_dir = tempfile.mkdtemp() os.chdir(self.base_dir) - def assert_default_yotta_call(self): ''' This tests to ensure that the default execCommand from yotta was called @@ -49,7 +48,6 @@ def assert_default_yotta_call(self): arg_dict = vars(args[0]) self.assertTrue(arg_dict['subcommand_name'] == self.test_command) - def tearDown(self): ''' cd back to the starting directory of the test, then try to remove the @@ -62,6 +60,5 @@ def tearDown(self): sys.argv.remove(self.test_command) except ValueError: pass - if self.test_arg in sys.argv: # Not all tests requrire an additional argument + if self.test_arg in sys.argv: # Not all tests requrire an additional argument sys.argv.remove(self.test_arg) - diff --git a/kubos/update.py b/kubos/update.py index 3f56749..3ac7d79 100644 --- a/kubos/update.py +++ b/kubos/update.py @@ -24,23 +24,30 @@ from yotta.options import parser from kubos.utils import git_utils, \ - sdk_utils, \ - status_spinner + sdk_utils, \ + status_spinner from kubos.utils.constants import * KUBOS_CLI_REPO_URL = 'git+https://github.com/kubos/kubos-cli' -INSTALL_COMMAND = ['sudo', 'pip', 'install', '--upgrade', KUBOS_CLI_REPO_URL] +INSTALL_COMMAND = ['sudo', 'pip', 'install', '--upgrade', KUBOS_CLI_REPO_URL] + def addOptions(parser): - parser.add_argument('set_version', nargs='?', default=None, help='Specify a version of the kubos source to use.') - parser.add_argument('-l', '--latest', action='store_true', default=False, help='Default to the most recent release of Kubos modules') + parser.add_argument('set_version', nargs='?', default=None, + help='Specify a version of the kubos source to use.') + parser.add_argument('-l', '--latest', action='store_true', default=False, + help='Default to the most recent release of Kubos modules') group = parser.add_mutually_exclusive_group() - group.add_argument('-s', '--source', dest='component', action='store_const', const='s', default=False, help='Update only the source Kubos modules') - group.add_argument('-c', '--cli', dest='component', action='store_const', const='c', default=False, help='Update the Kubos CLI.') - group.add_argument('-a', '--all', dest='component', action='store_const', const='a', default=False, help='Update both the Kubos source modules and the Kubos CLI') - group.add_argument('-t', '--tab-completion', dest='component', action='store_const', const='t', default=False, help='Update the tab completion definitions') - #The default behavior is to only update the source modules. + group.add_argument('-s', '--source', dest='component', action='store_const', + const='s', default=False, help='Update only the source Kubos modules') + group.add_argument('-c', '--cli', dest='component', action='store_const', + const='c', default=False, help='Update the Kubos CLI.') + group.add_argument('-a', '--all', dest='component', action='store_const', const='a', + default=False, help='Update both the Kubos source modules and the Kubos CLI') + group.add_argument('-t', '--tab-completion', dest='component', action='store_const', + const='t', default=False, help='Update the tab completion definitions') + # The default behavior is to only update the source modules. parser.set_defaults(component='s') @@ -60,8 +67,9 @@ def update_cli(): if return_code == 0: logging.info('Successfully updated the Kubos CLI module') else: - #The subprocess stdout/stderr is printed to the console. Any errors that occur will be visible there. - logging.error('There was an issue updating the Kubos CLI module. See the above log for the error details.') + # The subprocess stdout/stderr is printed to the console. Any errors that occur will be visible there. + logging.error( + 'There was an issue updating the Kubos CLI module. See the above log for the error details.') def update_source_modules(args): @@ -72,7 +80,8 @@ def update_source_modules(args): spinner = status_spinner.start_spinner() src_repo = git_utils.clone_repo(KUBOS_SRC_DIR, KUBOS_SRC_URL) git_utils.clone_example_repo(KUBOS_RT_EXAMPLE_DIR, KUBOS_RT_EXAMPLE_URL) - git_utils.clone_example_repo(KUBOS_LINUX_EXAMPLE_DIR, KUBOS_LINUX_EXAMPLE_URL) + git_utils.clone_example_repo( + KUBOS_LINUX_EXAMPLE_DIR, KUBOS_LINUX_EXAMPLE_URL) status_spinner.stop_spinner(spinner) set_version = args.set_version if set_version: @@ -87,21 +96,26 @@ def update_source_modules(args): def update_tab_completions(): logging.info("Setting up kubos tab completions...") if os.path.isdir(COMPLETION_LINK_DEST): - shutil.rmtree(COMPLETION_LINK_DEST) #shutil.copytree requires the dest to not exist + # shutil.copytree requires the dest to not exist + shutil.rmtree(COMPLETION_LINK_DEST) try: shutil.copytree(COMPLETION_RESOURCE_DIR, COMPLETION_LINK_DEST) except OSError: - logging.info('The link %s to %s already exists.' % (COMPLETION_RESOURCE_DIR, COMPLETION_LINK_DEST)) + logging.info('The link %s to %s already exists.' % + (COMPLETION_RESOURCE_DIR, COMPLETION_LINK_DEST)) completion_file = os.path.join(COMPLETION_LINK_DEST, 'kubos_completion') - shell = os.environ['SHELL'] if 'SHELL' in os.environ else None - username = os.environ['USER'] if 'USER' in os.environ else None + shell = os.environ['SHELL'] if 'SHELL' in os.environ else None + username = os.environ['USER'] if 'USER' in os.environ else None if 'bash' not in shell: - logging.warning('Currently Bash is the only officially supported shell for tab completions.') - logging.warning("If you want to manually continue source the %s file to configure the kubos completions via the complete command." % completion_file) - logging.warning("Beware: This is not supported and there is no guarantee this will actually work with your current shell: %s" % shell) + logging.warning( + 'Currently Bash is the only officially supported shell for tab completions.') + logging.warning( + "If you want to manually continue source the %s file to configure the kubos completions via the complete command." % completion_file) + logging.warning( + "Beware: This is not supported and there is no guarantee this will actually work with your current shell: %s" % shell) sys.exit(1) - if username != 'vagrant': #this is done during the vagrant provisioning - logging.info("If this is your first time setting up tab completion you will need to add the following line to your bash startup file of choice (.bashrc, .profile, etc") + if username != 'vagrant': # this is done during the vagrant provisioning + logging.info( + "If this is your first time setting up tab completion you will need to add the following line to your bash startup file of choice (.bashrc, .profile, etc") logging.info("source %s" % completion_file) - diff --git a/kubos/use.py b/kubos/use.py index f20226a..5a16aaf 100644 --- a/kubos/use.py +++ b/kubos/use.py @@ -16,19 +16,22 @@ from yotta.options import parser from kubos.utils import git_utils, sdk_utils -from kubos.utils.constants import KUBOS_SRC_DIR +from kubos.utils.constants import KUBOS_SRC_DIR + def addOptions(parser): group = parser.add_mutually_exclusive_group(required=True) - #Nargs has to be optional for the mutually exclusive arguments - but the required=True - #specifies that at least one of them has to be provided or argparse will thrown an error - group.add_argument('-b', '--branch', nargs='?', default=None, help='Set the branch flag to specify to checkout a branch, not a tag') - group.add_argument('set_version', nargs='?', default=None, help='Set a specific version of the KubOS modules to build your projects against.') + # Nargs has to be optional for the mutually exclusive arguments - but the required=True + # specifies that at least one of them has to be provided or argparse will thrown an error + group.add_argument('-b', '--branch', nargs='?', default=None, + help='Set the branch flag to specify to checkout a branch, not a tag') + group.add_argument('set_version', nargs='?', default=None, + help='Set a specific version of the KubOS modules to build your projects against.') def execCommand(args, following_args): version = args.set_version - branch = args.branch + branch = args.branch kubos_repo = git_utils.get_repo(KUBOS_SRC_DIR) if branch: git_utils.checkout_and_update_version(branch, kubos_repo) diff --git a/kubos/utils/constants.py b/kubos/utils/constants.py index 632e7c1..5125495 100644 --- a/kubos/utils/constants.py +++ b/kubos/utils/constants.py @@ -32,14 +32,15 @@ KUBOS_RESOURCE_DIR = os.path.join(resource_filename(__name__, ''), '..') SDK_MODULE_JSON = os.path.join(KUBOS_RESOURCE_DIR, 'module.json') -GLOBAL_TARGET_PATH = os.path.join('/', 'usr', 'local', 'lib', 'yotta_targets') -GLOBAL_MODULE_PATH = os.path.join('/', 'usr', 'local', 'lib', 'yotta_modules') +GLOBAL_TARGET_PATH = os.path.join('/', 'usr', 'local', 'lib', 'yotta_targets') +GLOBAL_MODULE_PATH = os.path.join('/', 'usr', 'local', 'lib', 'yotta_modules') LINUX_KEY = 'linux-targets' -RT_KEY = 'rt-targets' +RT_KEY = 'rt-targets' COMPLETION_RESOURCE_DIR = os.path.join(KUBOS_RESOURCE_DIR, 'completion') -COMPLETION_SCRIPT = os.path.join(COMPLETION_RESOURCE_DIR, 'kubos_completion.bash') +COMPLETION_SCRIPT = os.path.join( + COMPLETION_RESOURCE_DIR, 'kubos_completion.bash') COMPLETION_OPTIONS_FILE = os.path.join(COMPLETION_RESOURCE_DIR, 'options.json') COMPLETION_PYTHON_FILE = os.path.join(COMPLETION_RESOURCE_DIR, 'completion.py') diff --git a/kubos/utils/git_utils.py b/kubos/utils/git_utils.py index a54a624..46bdd19 100644 --- a/kubos/utils/git_utils.py +++ b/kubos/utils/git_utils.py @@ -23,6 +23,7 @@ from kubos.utils import sdk_utils from kubos.utils.constants import * + def get_repo(path): repo = git.Repo(path) return repo @@ -47,7 +48,7 @@ def filter_cd_generated_tags(display_num, tag_list): This function filters only the most recent display_num number of cd_generated_versions from tag_list. ''' filtered_tags = [] - ga_release_version = re.compile('v?\d+\.\d+\.\d+') + ga_release_version = re.compile('v?\d+\.\d+\.\d+') cd_generated_version = re.compile('v?\d+\.\d+\.\d+\.\d+') for tag in tag_list: if cd_generated_version.match(tag): @@ -63,7 +64,7 @@ def filter_cd_generated_tags(display_num, tag_list): def print_tag_list(tag_list, filter=True): active_version = get_active_kubos_version() - if filter: #filter the CD Generated versions + if filter: # filter the CD Generated versions tag_list = filter_cd_generated_tags(SHOW_NUMBER_CD_VERSIONS, tag_list) for tag in tag_list: @@ -73,7 +74,7 @@ def print_tag_list(tag_list, filter=True): def get_latest_tag(tag_list): - latest_tag = '0.0.0' #Set to a dummy tag that will be less than any other valid tag + latest_tag = '0.0.0' # Set to a dummy tag that will be less than any other valid tag for tag in tag_list: if packaging.version.parse(tag) > packaging.version.parse(latest_tag): latest_tag = tag @@ -82,23 +83,27 @@ def get_latest_tag(tag_list): def fetch_tags(repo): origin = repo.remotes.origin - logging.info('Checking for newer releases...') #Tags mark new KubOS releases + # Tags mark new KubOS releases + logging.info('Checking for newer releases...') origin.fetch() def checkout_and_update_version(ref, repo): - tag_expr = re.compile('v?\d+\.\d+\.\d+\.*') #Tags follow the v?X.X.X convention + # Tags follow the v?X.X.X convention + tag_expr = re.compile('v?\d+\.\d+\.\d+\.*') is_tag = tag_expr.match(ref) logging.info("Checking out '%s'" % ref) if not is_tag: - logging.warning('Kubos branches are not guaranteed to be stable. Proceed with caution.') + logging.warning( + 'Kubos branches are not guaranteed to be stable. Proceed with caution.') try: repo.git.checkout(ref) - if repo.git_dir == os.path.join(KUBOS_SRC_DIR, '.git'): #only set the version file for kubos source checkouts, not for example checkouts + # only set the version file for kubos source checkouts, not for example checkouts + if repo.git_dir == os.path.join(KUBOS_SRC_DIR, '.git'): update_version_file(ref) except: logging.error('There was an error checking out branch "%s"' % ref) - logging.debug('The error details are: %s' % sys.exc_info()[0]) + logging.debug('The error details are: %s' % sys.exc_info()[0]) def update_version_file(version): @@ -113,7 +118,7 @@ def clone_example_repo(repo_dir, repo_url): specify a specific version of the example repo. ''' repo = clone_repo(repo_dir, repo_url) - tag_list = get_tag_list(repo) + tag_list = get_tag_list(repo) latest_tag = get_latest_tag(tag_list) checkout_and_update_version(latest_tag, repo) @@ -127,11 +132,12 @@ def clone_repo(repo_dir, repo_url): repo = git.Repo(repo_dir) logging.info('Repo %s already exists' % repo_url) fetch_tags(repo) - #Link the modules/targets from the kubos repo to the default, Global location + # Link the modules/targets from the kubos repo to the default, Global location sdk_utils.link_to_global_cache(KUBOS_SRC_DIR) return repo except git.exc.GitCommandError as e: - logging.error('Error: there was an error accessing the remote git repository...') + logging.error( + 'Error: there was an error accessing the remote git repository...') logging.debug('The specific error is: \n\n %s' % e) @@ -152,20 +158,21 @@ def set_active_kubos_version(set_tag, repo): found = True break if not found: - logging.error('The requested version "%s" is not an available version.' % set_tag) + logging.error( + 'The requested version "%s" is not an available version.' % set_tag) logging.info('Available versions are: ') print_tag_list(tag_list) sys.exit(1) def check_provided_version(requested_version, repo): - #the repo paramenter allows this function to be used for the example project as well + # the repo paramenter allows this function to be used for the example project as well active_version = get_active_kubos_version() if requested_version == active_version: - logging.info('The requested version: %s is already active. There\'s nothing to do..' % requested_version) + logging.info( + 'The requested version: %s is already active. There\'s nothing to do..' % requested_version) return set_active_kubos_version(requested_version, repo) if active_version: logging.info('Deactivating Kubos source version: %s' % active_version) logging.info('Activating Kubos source version %s' % requested_version) - diff --git a/kubos/utils/sdk_utils.py b/kubos/utils/sdk_utils.py index 41c041f..25272b2 100644 --- a/kubos/utils/sdk_utils.py +++ b/kubos/utils/sdk_utils.py @@ -25,6 +25,7 @@ from sets import Set from kubos.utils.constants import * + def get_sdk_attribute(attr): sdk_data = json.load(open(SDK_MODULE_JSON, 'r')) if attr in sdk_data: @@ -32,6 +33,7 @@ def get_sdk_attribute(attr): else: return None + def get_module_name(path): if not os.path.isfile(path): return None @@ -41,8 +43,8 @@ def get_module_name(path): def is_module_or_target_root(entity_name): - #Determine if the directory name is at the root of a module or target - #The module.json and target.json file names mark the "root" of a module + # Determine if the directory name is at the root of a module or target + # The module.json and target.json file names mark the "root" of a module marker_names = ['module.json', 'target.json'] return entity_name in marker_names @@ -66,10 +68,11 @@ def link_entities(src, dst): roots ie. where a module.json or target.json file exists once a root is found, the linking function is called. ''' - logging.disable(logging.WARNING) #suppress yotta warning for linking non-required modules and targets + logging.disable( + logging.WARNING) # suppress yotta warning for linking non-required modules and targets for subdir in os.listdir(src): - #loop through the subdirectories of src - #but don't go down the rabbit hole of yotta_module symlinks + # loop through the subdirectories of src + # but don't go down the rabbit hole of yotta_module symlinks if "yotta_modules" in subdir: continue if "yotta_targets" in subdir: @@ -78,12 +81,12 @@ def link_entities(src, dst): continue cur_dir = os.path.join(src, subdir) if is_module_or_target_root(subdir): - #if we're pointing to a target.json or module.json - link the module and return - #NOTE: This assumes there are not nested modules + # if we're pointing to a target.json or module.json - link the module and return + # NOTE: This assumes there are not nested modules run_link(cur_dir, dst) return elif os.path.isdir(cur_dir): - #if we're looking at a subdirectory recursively search for a module root + # if we're looking at a subdirectory recursively search for a module root link_entities(cur_dir, dst) @@ -94,12 +97,12 @@ def run_link(src, dst): ''' link_module = yotta.link if 'module' in src else yotta.link_target if dst: - #we're linking to a project from the global cache so we need to link the module by name + # we're linking to a project from the global cache so we need to link the module by name entity_name = get_module_name(src) path = dst else: - #we're linking to the global cache, the default behavior of a None module/target name - #is linking it to the global cache. + # we're linking to the global cache, the default behavior of a None module/target name + # is linking it to the global cache. entity_name = None path = os.path.dirname(src) start_dir = os.getcwd() @@ -140,7 +143,7 @@ def get_target_lists(): rt_list = [] target_list = get_all_eligible_targets(GLOBAL_TARGET_PATH) - #TODO: Get a better way of determining linux targets + # TODO: Get a better way of determining linux targets for target in target_list: if 'linux' in target: linux_list.append(target) @@ -158,9 +161,9 @@ def get_all_eligible_targets(path): The other targets in the hierarchy are not meant to be built against ''' inherit_key = 'inherits' - name_key = 'name' + name_key = 'name' ineligible_set = Set() - complete_set = Set() + complete_set = Set() target_dir_list = os.listdir(path) for subdir in target_dir_list: @@ -168,7 +171,7 @@ def get_all_eligible_targets(path): if name_key in json_data: complete_set.add(json_data['name']) if inherit_key in json_data: - #The target this current target depends on is an ineligible target + # The target this current target depends on is an ineligible target target_dependency = json_data[inherit_key].keys() ineligible_set.add(*target_dependency) return complete_set - ineligible_set @@ -191,7 +194,7 @@ def refresh_target_cache(): rt_targets, linux_targets = get_target_lists() data[LINUX_KEY] = linux_targets - data[RT_KEY] = rt_targets + data[RT_KEY] = rt_targets with open(KUBOS_TARGET_CACHE_FILE, 'w') as target_file: target_file.write(json.dumps(data)) @@ -202,8 +205,8 @@ def load_target_list(platform): with open(KUBOS_TARGET_CACHE_FILE, 'r') as json_file: data = json.loads(json_file.read()) linux_targets = data[LINUX_KEY] - rt_targets = data[RT_KEY] - if platform == None: #if no platform is listed in the module.json, dont restrict the target type + rt_targets = data[RT_KEY] + if platform == None: # if no platform is listed in the module.json, dont restrict the target type return linux_targets + rt_targets elif platform == 'linux': return linux_targets @@ -232,8 +235,8 @@ def get_project_type(): else: return 'linux' else: - #This project doesn't have a dependencies field. This is most likely running in a unit testing context + # This project doesn't have a dependencies field. This is most likely running in a unit testing context return None else: - #There is no module.json + # There is no module.json return None diff --git a/kubos/version.py b/kubos/version.py index aa144bc..739618a 100644 --- a/kubos/version.py +++ b/kubos/version.py @@ -24,12 +24,16 @@ from kubos.utils import git_utils from kubos.utils.constants import KUBOS_SRC_DIR + def addOptions(parser): - parser.add_argument('-l', '--list', action='store_true', default=False, help='List all of the locally available KubOS source versions') + parser.add_argument('-l', '--list', action='store_true', default=False, + help='List all of the locally available KubOS source versions') + def execCommand(args, following_args): kubos_version = git_utils.get_active_kubos_version() - logging.info('Kubos-CLI version : %s' % 'v' + get_installed_version('kubos-cli')) + logging.info('Kubos-CLI version : %s' % + 'v' + get_installed_version('kubos-cli')) logging.info('Kubos Source version : %s' % kubos_version) if not kubos_version: @@ -39,8 +43,10 @@ def execCommand(args, following_args): logging.info('There\'s not an active Kubos source version..') logging.info('The available versions are:') git_utils.print_tag_list(version_list, filter=True) - logging.info('Please run kubos use (with one of the above versions)' + \ - 'to checkout a version of the source before working with a project.') + logging.info('Please run kubos use (with one of the above versions)' + + 'to checkout a version of the source before working with a project.') else: - logging.info('There are not any local versions of the kubos source currently.') - logging.info('Please run `kubos update` to pull the kubos source before running `kubos version` again') + logging.info( + 'There are not any local versions of the kubos source currently.') + logging.info( + 'Please run `kubos update` to pull the kubos source before running `kubos version` again') diff --git a/kubos/versions.py b/kubos/versions.py index f289871..d185cfb 100644 --- a/kubos/versions.py +++ b/kubos/versions.py @@ -23,18 +23,20 @@ from kubos.utils import git_utils from kubos.utils.constants import * + def addOptions(parser): - parser.add_argument('-a', '--all-versions', dest='filter', action='store_false', default=True, help='Show all available versions') + parser.add_argument('-a', '--all-versions', dest='filter', + action='store_false', default=True, help='Show all available versions') def execCommand(args, following_args): if not os.path.isdir(KUBOS_SRC_DIR): - logging.info('No versions are locally available. Please run `kubos update` to pull all of the available source versions.') + logging.info( + 'No versions are locally available. Please run `kubos update` to pull all of the available source versions.') return 1 repo = git_utils.get_repo(KUBOS_SRC_DIR) tag_list = git_utils.get_tag_list(repo) - latest = git_utils.get_latest_tag(tag_list) + latest = git_utils.get_latest_tag(tag_list) logging.info('Available versions are:') git_utils.print_tag_list(tag_list, filter=args.filter) logging.info('The most recent release is: %s' % latest) -