Blame SOURCES/pyproject_preprocess_record.py

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