def add_default(version=None, overwrite=False): """Implement the subcommand to add default metadata to modules Add the default metadata to any plugin which lacks it. :kwarg version: If given, the metadata must be at least this version. Otherwise, treat the module as not having existing metadata. :kwarg overwrite: If True, overwrite any existing metadata. Otherwise, do not modify files which have metadata at an appropriate version """ # List of all plugins plugins = module_loader.all(path_only=True) plugins = ((os.path.splitext((os.path.basename(p)))[0], p) for p in plugins) plugins = (p for p in plugins if p[0] not in NONMODULE_MODULE_NAMES) # Iterate through each plugin processed = set() diagnostic_messages = [] for name, filename in (info for info in plugins if info[0] not in processed): try: write_metadata(filename, DEFAULT_METADATA, version, overwrite) except ParseError as e: diagnostic_messages.append(e.args[0]) continue processed.add(name) if diagnostic_messages: pprint(diagnostic_messages) return 0
def report(version=None): """Implement the report subcommand Print out all the modules that have metadata and all the ones that do not. :kwarg version: If given, the metadata must be at least this version. Otherwise return it as not having metadata """ # List of all plugins plugins = module_loader.all(path_only=True) plugins = ((os.path.splitext((os.path.basename(p)))[0], p) for p in plugins) plugins = (p for p in plugins if p[0] not in NONMODULE_MODULE_NAMES) plugins = list(plugins) no_metadata, has_metadata, support, status = metadata_summary( plugins, version=version) print('== Has metadata ==') pprint(sorted(has_metadata)) print('') print('== Has no metadata ==') pprint(sorted(no_metadata)) print('') print('== Supported by core ==') pprint(sorted(support['core'])) print('== Supported by value certified ==') pprint(sorted(support['certified'])) print('== Supported by value network ==') pprint(sorted(support['network'])) print('== Supported by community ==') pprint(sorted(support['community'])) print('') print('== Status: stableinterface ==') pprint(sorted(status['stableinterface'])) print('== Status: preview ==') pprint(sorted(status['preview'])) print('== Status: deprecated ==') pprint(sorted(status['deprecated'])) print('== Status: removed ==') pprint(sorted(status['removed'])) print('') print('== Summary ==') print('No Metadata: {0} Has Metadata: {1}'.format( len(no_metadata), len(has_metadata))) print( 'Support level: core: {0} community: {1} certified: {2} network: {3}' .format(len(support['core']), len(support['community']), len(support['certified']), len(support['network']))) print( 'Status StableInterface: {0} Status Preview: {1} Status Deprecated: {2} Status Removed: {3}' .format(len(status['stableinterface']), len(status['preview']), len(status['deprecated']), len(status['removed']))) return 0
def upgrade_metadata(version=None): """Implement the subcommand to upgrade the default metadata in modules. :kwarg version: If given, the version of the metadata to upgrade to. If not given, upgrade to the latest format version. """ if version is None: # Number larger than any of the defined metadata formats. version = 9999999 requested_version = StrictVersion(version) # List all plugins plugins = module_loader.all(path_only=True) plugins = ((os.path.splitext((os.path.basename(p)))[0], p) for p in plugins) plugins = (p for p in plugins if p[0] not in NONMODULE_MODULE_NAMES) processed = set() diagnostic_messages = [] for name, filename in (info for info in plugins if info[0] not in processed): # For each plugin, read the existing metadata with open(filename, 'rb') as f: module_data = f.read() metadata = extract_metadata(module_data=module_data, offsets=True)[0] # If the metadata isn't the requested version, convert it to the new # version if 'metadata_version' not in metadata or metadata['metadata_version'] != version: # # With each iteration of metadata, add a new conditional to # upgrade from the previous version # if 'metadata_version' not in metadata: # First version, pre-1.0 final metadata metadata = convert_metadata_pre_1_0_to_1_0(metadata) if metadata['metadata_version'] == '1.0' and StrictVersion('1.0') < requested_version: metadata = convert_metadata_1_0_to_1_1(metadata) if metadata['metadata_version'] == '1.1' and StrictVersion('1.1') < requested_version: # 1.1 version => XXX. We don't yet have anything beyond 1.1 # so there's nothing here pass # Replace the existing metadata with the new format try: write_metadata(filename, metadata, version, overwrite=True) except ParseError as e: diagnostic_messages.append(e.args[0]) continue processed.add(name) if diagnostic_messages: pprint(diagnostic_messages) return 0