Warning, file /sysadmin/repo-metadata/verify-repo-metadata.py was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 #!/usr/bin/python3 0002 import os 0003 import sys 0004 import yaml 0005 import argparse 0006 import regex 0007 0008 # Gather the command line arguments we need 0009 parser = argparse.ArgumentParser(description='Verifies the metadata files') 0010 parser.add_argument('--metadata-path', help='Path to the metadata to check', required=True) 0011 args = parser.parse_args() 0012 0013 # Make sure our configuration file exists 0014 if not os.path.exists( args.metadata_path ): 0015 print("Unable to locate specified metadata location: %s".format(args.metadata_path)) 0016 sys.exit(1) 0017 0018 # Regular expression used to validate project names 0019 # Taken from Gitlab itself (with slight adaptation to be Python compatible) 0020 projectRegex = regex.compile('^[\p{Alnum}\u00A9-\u1f9ff_][\p{Alnum}\p{Pd}\u00A9-\u1f9ff_\. ]*$', flags=regex.V1) 0021 0022 # Start a list of used project identifiers and names 0023 projectIdentifiersInUse = [] 0024 projectNamesInUse = [] 0025 0026 # Start going over the location in question... 0027 for currentPath, subdirectories, filesInFolder in os.walk( args.metadata_path, topdown=False, followlinks=False ): 0028 # Do we have a metadata.yaml file? 0029 if 'metadata.yaml' not in filesInFolder: 0030 # We're not interested then.... 0031 continue 0032 0033 # Now that we know we have something to work with.... 0034 # Lets load the current metadata up 0035 metadataPath = os.path.join( currentPath, 'metadata.yaml' ) 0036 metadataFile = open( metadataPath, 'r' ) 0037 metadata = yaml.safe_load( metadataFile ) 0038 0039 # Trim the base path to the metadata off to make it a bit nicer to read 0040 currentLocation = currentPath[len(args.metadata_path):] 0041 0042 # check sanity of the description and make sure that it is less then 250 0043 # (part of gitlab restriction) 0044 if metadata['description'] is not None and len(metadata['description']) > 250: 0045 print(currentLocation + ": Project description is longer than 250 characters") 0046 0047 # check the description and ensure that it is not empty 0048 # currently warning, but still best to list it out 0049 if metadata['description'] is not None and len(metadata['description']) == 0: 0050 print(currentLocation + ": Project description is empty") 0051 0052 # Check if name have supported characters only 0053 if projectRegex.match(metadata['name']) is None: 0054 print(currentLocation + ": Project name contains characters not allowed by Gitlab - " + metadata['name']) 0055 0056 # Make sure the name is not already in use 0057 if metadata['name'] in projectNamesInUse: 0058 print(currentLocation + ": Project name is already in use - " + metadata['name']) 0059 0060 # Make sure that identifier is not empty 0061 identifier = metadata.get('identifier') 0062 if identifier is None: 0063 print(currentLocation + ": CRITICAL ERROR - Project identifier is not set") 0064 sys.exit(1) 0065 0066 # Make sure the identifier is not in use already 0067 if identifier in projectIdentifiersInUse: 0068 print(currentLocation + ": CRITICAL ERROR - Project identifier is already in use - " + metadata['identifier']) 0069 sys.exit(1) 0070 0071 # All checks done for this project 0072 # Add it to the list of identifiers and names in use 0073 projectNamesInUse.append( metadata['name'] ) 0074 projectIdentifiersInUse.append( identifier ) 0075 0076 # All done! 0077 sys.exit(0)