#!/usr/bin/env python3 # # Copyright 2020 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. # # Final all .whl files in a directory, and make a index.html page # in PEP503 (https://www.python.org/dev/peps/pep-0503/) format import argparse import datetime import email import hashlib import html import logging import os import sys import zipfile parser = argparse.ArgumentParser() parser.add_argument('toplevel', help="directory to index") parser.add_argument('-d', '--debug', dest="debug", action='store_true') parser.add_argument('-o', '--output', dest="output", default='index.html', help="Output filename, - for stdout") args = parser.parse_args() level = logging.DEBUG if args.debug else logging.INFO logging.basicConfig(level=level) class NotAWheelException(Exception): pass class NoMetadataException(Exception): pass class NoRequirementsException(Exception): pass class BadFormatException(Exception): pass def get_requirements(filename): # This is an implementation of the description on finding # requirements from a wheel provided by chrahunt at: # https://github.com/pypa/pip/issues/7586#issuecomment-573534655 with zipfile.ZipFile(filename) as zip: metadata = None names = zip.namelist() for name in names: if name.endswith('.dist-info/METADATA'): metadata = zip.open(name) # finish loop and sanity check we got the right one? break if not metadata: return NoMetadataException parsed = email.message_from_binary_file(metadata) requirements = parsed.get_all('Requires-Python') if not requirements: raise NoRequirementsException if len(requirements) > 1: print("Multiple requirements headers found?") raise BadFormatException return html.escape(requirements[0]) def get_sha256(filename): sha256 = hashlib.sha256() with open(filename, "rb") as f: for b in iter(lambda: f.read(4096), b''): sha256.update(b) return(sha256.hexdigest()) def create_index(path, files): project = os.path.basename(path) output = ''' %s