Esempio n. 1
0
def parse_license(sources_path):
    required_fields = ['Format:', 'Files:', 'Copyright:', 'License:']
    d_file = open(sources_path).read()
    if not all(field in d_file for field in required_fields):
        raise copyright.NotMachineReadableError
    with io.open(sources_path, mode='rt', encoding='utf-8') as f:
        return copyright.Copyright(f)
Esempio n. 2
0
def parse_license(sources_path):
    required_fields = [b"Format:", b"Files:", b"Copyright:", b"License:"]
    with open(sources_path, "rb") as f:
        d_file = f.read()
        if not all(field in d_file for field in required_fields):
            raise copyright.NotMachineReadableError
    with io.open(sources_path, mode="rb") as f:
        return copyright.Copyright(f)
Esempio n. 3
0
def parse_license(sources_path):
    required_fields = ['Format:', 'Files:', 'Copyright:', 'License:']
    d_file = open(sources_path).read()
    if not all(field in d_file for field in required_fields):
        raise Exception
    with io.open(sources_path, mode='rt', encoding='utf-8') as f:
        try:
            c = copyright.Copyright(f)
            return c
        except Exception as e:
            raise e
Esempio n. 4
0
def main():
    args = parse_args()

    if args.suppress_warnings:
        warnings.filterwarnings('ignore', module='debian.copyright')

    problems = {}
    parse_failures = []
    dump_failures = []

    total = 0
    for filename in sys.stdin:
        total += 1
        filename = filename.rstrip()
        with io.open(filename, mode='rt', encoding='utf-8') as f:
            try:
                c = copyright.Copyright(f)
            except Exception as e:
                problems.setdefault((1, 'Parse failures'), []).append(
                    (filename, e))
                continue

            try:
                c.dump()
            except Exception as e:
                problems.setdefault((2, 'Dump failures'), []).append(
                    (filename, e))

            if not c.header.known_format():
                problems.setdefault((3, 'Unknown format'), []).append(
                    (filename, c.header.format))

            invalid_globs = []
            globs_with_leading_dot_slash = []
            globs_with_trailing_comma = []
            globs_with_double_star = []
            for p in c.all_files_paragraphs():
                try:
                    p.files_pattern()
                except Exception as e:
                    invalid_globs.append(e)
                for glob in p.files:
                    if glob.startswith('./'):
                        globs_with_leading_dot_slash.append(glob)
                    if glob.endswith(','):
                        globs_with_trailing_comma.append(glob)
                    if '**' in glob:
                        globs_with_double_star.append(glob)

            if invalid_globs:
                problems.setdefault((4, 'Invalid glob'), []).append(
                    (filename, invalid_globs))
            if globs_with_leading_dot_slash:
                problems.setdefault((5, 'Globs with leading ./'), []).append(
                    (filename, globs_with_leading_dot_slash))
            if globs_with_trailing_comma:
                problems.setdefault((6, 'Globs with trailing ,'), []).append(
                    (filename, globs_with_trailing_comma))
            if globs_with_double_star:
                problems.setdefault((7, 'Globs with **'), []).append(
                    (filename, globs_with_double_star))

    f = codecs.getwriter(encoding='utf-8')(sys.stdout)
    for (_, heading), problems in sorted(problems.items()):
        f.write('\n%s: (%d / %d)\n' % (heading, len(problems), total))
        if not args.summary:
            for filename, problem in problems:
                f.write(' %s: %s\n' % (filename, problem))
Esempio n. 5
0
def parse_copyright(
    package: str,
    path: pathlib.Path,
    version: str,
    parsed: List[Package],
    not_parsed: List[Package],
):
    raw_copyright = ""

    try:
        with open(str(path), encoding="utf-8") as f:
            raw_copyright = f.read()
            f.seek(0)
            c = cr.Copyright(f, strict=False)
    except (AttributeError, cr.NotMachineReadableError):
        not_parsed.append(Package(package, version, [], raw_copyright))
        return
    except ValueError as e:
        if len(e.args) > 0 and e.args[0] == "value must not have blank lines":
            # will be raised in debian.deb822:1149
            not_parsed.append(Package(package, version, [], raw_copyright))
            return
        raise RuntimeError("unknown ValueError: {}".format(e))

    file_par = [
        p for p in c.all_paragraphs() if isinstance(p, cr.FilesParagraph)
    ]
    lice_par = [
        p for p in c.all_paragraphs() if isinstance(p, cr.LicenseParagraph)
    ]

    copyrights = []
    licenses = dict()

    for lp in lice_par:
        try:
            syn = lp.license.synopsis
        except cr.MachineReadableFormatError as e:
            not_parsed.append(Package(package, version, [], raw_copyright))
            print("{}: {}".format(package, e))
            return

        if syn not in licenses:
            li = License(lp.license.synopsis, lp.license.synopsis,
                         lp.license.text)
            licenses[lp.license.synopsis] = li

    for fp in file_par:
        try:
            syn = fp.license.synopsis
        except cr.MachineReadableFormatError as e:
            not_parsed.append(Package(package, version, [], raw_copyright))
            print("{}: {}".format(package, e))
            return
        except AttributeError as e:
            not_parsed.append(Package(package, version, [], raw_copyright))
            print("{}: {}".format(package, e))
            return

        if syn is None:
            not_parsed.append(Package(package, version, [], raw_copyright))
            print("Unknown License!! {}".format(fp.license.synopsis))
            return

        lic = licenses.get(syn, None)
        if fp.copyright is not None:
            cleansed = "\n".join(l.lstrip() for l in fp.copyright.split("\n"))
        else:
            cleansed = ""
        copyrights.append(Copyright(cleansed, fp.files, lic))

    parsed.append(Package(package, version, copyrights, raw_copyright))