diff --git a/publish.py b/publish.py index 9e7a09b..11a7aec 100644 --- a/publish.py +++ b/publish.py @@ -65,7 +65,8 @@ def is_data_equal(old, new): def should_publish_data(): - current_contents = json.load(open('service-types.json')) + with open('service-types.json') as fh: + current_contents = json.load(fh) try: response = requests.get(HTTP_LOCATION) @@ -73,8 +74,9 @@ def should_publish_data(): existing_contents = response.json() except (requests.HTTPError, requests.ConnectionError) as e: print( - 'Failed to fetch current service-types.json. Assuming data' - f' needs to be published. Error: {str(e)}' + f'Failed to fetch current service-types.json. Assuming data ' + f'needs to be published. Error: {str(e)}', + file=sys.stderr, ) return (True, current_contents['version']) @@ -95,7 +97,10 @@ def main(): if not os.path.exists(OUTDIR): os.makedirs(OUTDIR) elif not os.path.isdir(OUTDIR): - print(f'{OUTDIR} exists but is not a directory. Aborting!') + print( + f'{OUTDIR} exists but is not a directory. Aborting!', + file=sys.stderr, + ) return 1 # It's fine to always copy the json schema @@ -109,8 +114,9 @@ def main(): to_copy += glob.glob('service-types.json*') else: print( - f'Data in existing file matches {latest_version} data.' - ' Not publishing' + f'Data in existing file matches {latest_version} data. ' + f'Not publishing', + file=sys.stderr, ) for filename in to_copy: diff --git a/transform.py b/transform.py index a74439e..dcf78dc 100644 --- a/transform.py +++ b/transform.py @@ -43,8 +43,8 @@ def create_local_registry(): if uri.startswith('https://specs.openstack.org'): # The URI arrives with fragment removed. We assume no querystring. filename = uri.split('/')[-1] - with open(filename) as f: - return referencing.Resource.from_contents(json.load(f)) + with open(filename) as fh: + return referencing.Resource.from_contents(json.load(fh)) # We shouldn't have any external URIs. Scream bloody murder if someone # tries. raise referencing.exceptions.NoSuchResource(ref=uri) @@ -80,7 +80,8 @@ def main(): ) args = parser.parse_args() - mapping = yaml.safe_load(open('service-types.yaml')) + with open('service-types.yaml') as fh: + mapping = yaml.safe_load(fh) # we are using a TZ-naive timestamp for legacy reasons, but we should # probably revisit this as TZ-naive timestamps are a menace @@ -124,7 +125,9 @@ def main(): if not service.get('api_reference'): service['api_reference'] = API_REF_FMT.format(service=service_type) - schema = json.load(open('published-schema.json')) + with open('published-schema.json') as fh: + schema = json.load(fh) + registry = create_local_registry() valid = validate.validate_all(schema, mapping, registry=registry) @@ -133,11 +136,10 @@ def main(): output = json.dumps(mapping, indent=2, sort_keys=True) output.replace(' \n', '\n') unversioned_filename = 'service-types.json' - versioned_filename = 'service-types.json.{version}'.format( - version=mapping['version'] - ) + versioned_filename = f'service-types.json.{mapping["version"]}' for filename in (unversioned_filename, versioned_filename): - open(filename, 'w').write(output) + with open(filename, 'w') as fh: + fh.write(output) return int(not valid) diff --git a/validate.py b/validate.py index 168bc63..e1e267a 100644 --- a/validate.py +++ b/validate.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import sys + import jsonschema @@ -44,8 +46,9 @@ def validate_unique_tokens(data): if service.get('secondary', False): continue if service['project'] in projects: - yield "'{service}' is duplicate service from '{project}'".format( - service=service['service_type'], project=service['project'] + yield ( + f"'{service['service_type']}' duplicates service from " + f"'{service['project']}'" ) projects.append(service['project']) for alias in aliases: @@ -54,15 +57,15 @@ def validate_unique_tokens(data): def validate_all(schema, data, registry): - """Runs all validators, printing any errors to stdout. + """Runs all validators, printing any errors to stderr. :return: True if all checks passed; False if any checks failed. """ ret = True for msg in validate_schema(schema, data, registry): - print(msg) + print(msg, file=sys.stderr) ret = False for msg in validate_unique_tokens(data): - print(msg) + print(msg, file=sys.stderr) ret = False return ret