Blame SOURCES/pyproject_preprocess_record.py

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