Blame pyproject_preprocess_record.py

894e212
import argparse
894e212
import csv
894e212
import json
894e212
import os
894e212
from pathlib import PosixPath
894e212
894e212
from pyproject_save_files import BuildrootPath
894e212
894e212
894e212
def read_record(record_path):
894e212
    """
894e212
    A generator yielding individual RECORD triplets.
894e212
894e212
    https://www.python.org/dev/peps/pep-0376/#record
894e212
894e212
    The triplet is str-path, hash, size -- the last two optional.
894e212
    We will later care only for the paths anyway.
894e212
894e212
    Example:
894e212
894e212
        >>> g = read_record(PosixPath('./test_RECORD'))
894e212
        >>> next(g)
894e212
        ['../../../bin/__pycache__/tldr.cpython-....pyc', '', '']
894e212
        >>> next(g)
894e212
        ['../../../bin/tldr', 'sha256=...', '12766']
894e212
        >>> next(g)
894e212
        ['../../../bin/tldr.py', 'sha256=...', '12766']
894e212
    """
894e212
    with open(record_path, newline="", encoding="utf-8") as f:
894e212
        yield from csv.reader(
894e212
            f, delimiter=",", quotechar='"', lineterminator=os.linesep
894e212
        )
894e212
894e212
894e212
def parse_record(record_path, record_content):
894e212
    """
894e212
    Returns a list with BuildrootPaths parsed from record_content
894e212
894e212
    params:
894e212
    record_path: RECORD BuildrootPath
894e212
    record_content: list of RECORD triplets
894e212
                    first item is a str-path relative to directory where dist-info directory is
894e212
                    (it can also be absolute according to the standard, but not from pip)
894e212
894e212
    Examples:
228f394
        >>> parse_record(BuildrootPath('/usr/lib/python3.7/site-packages/requests-2.22.0.dist-info/RECORD'),
228f394
        ...                            [('requests/sessions.py', 'sha256=xxx', '666')])
228f394
        ['/usr/lib/python3.7/site-packages/requests/sessions.py']
894e212
228f394
        >>> parse_record(BuildrootPath('/usr/lib/python3.7/site-packages/tldr-0.5.dist-info/RECORD'),
228f394
        ...                            [('../../../bin/tldr', 'sha256=yyy', '777')])
228f394
        ['/usr/bin/tldr']
894e212
    """
894e212
    sitedir = record_path.parent.parent  # trough the dist-info directory
894e212
    # / with absolute right operand will remove the left operand
894e212
    # any .. parts are resolved via normpath
894e212
    return [str((sitedir / row[0]).normpath()) for row in record_content]
894e212
894e212
894e212
def save_parsed_record(record_path, parsed_record, output_file):
894e212
    content = {}
894e212
    if output_file.is_file():
894e212
        content = json.loads(output_file.read_text())
894e212
    content[str(record_path)] = parsed_record
894e212
    output_file.write_text(json.dumps(content))
894e212
894e212
894e212
def main(cli_args):
894e212
    record_path = BuildrootPath.from_real(cli_args.record, root=cli_args.buildroot)
894e212
    parsed_record = parse_record(record_path, read_record(cli_args.record))
894e212
    save_parsed_record(record_path, parsed_record, cli_args.output)
894e212
894e212
894e212
def argparser():
894e212
    parser = argparse.ArgumentParser()
894e212
    r = parser.add_argument_group("required arguments")
894e212
    r.add_argument("--buildroot", type=PosixPath, required=True)
894e212
    r.add_argument("--record", type=PosixPath, required=True)
894e212
    r.add_argument("--output", type=PosixPath, required=True)
894e212
    return parser
894e212
894e212
894e212
if __name__ == "__main__":
894e212
    cli_args = argparser().parse_args()
894e212
    main(cli_args)