summaryrefslogtreecommitdiff
path: root/LicenseCheck.py
blob: 88a2b77e0433c743c4ba0a536723693f6b838bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
# Version         : $Id$

#
# check license files and verify them against declared license in License tag
#

from Filter import addDetails, printError, printWarning
from Pkg import getstatusoutput, is_utf8
import AbstractCheck
import os
import stat
import Config
import rpm
import re

DEFAULT_VALID_LICENSES = ()
VALID_LICENSES = Config.getOption('ValidLicenses', DEFAULT_VALID_LICENSES)
license_regex = re.compile('\(([^)]+)\)|\s(?:and|or)\s')


class LicenseCheck(AbstractCheck.AbstractFilesCheck):
    def __init__(self):
        AbstractCheck.AbstractCheck.__init__(
                self, "LicenseCheck")

    def __checkLicenseFiles(self, pkg):
        for fname, pkgfile in pkg.files().items():
            if not fname.startswith('/usr/share/licenses/'):
                continue

            if not stat.S_ISREG(pkgfile.mode):
                continue

            basename = os.path.basename(fname) 
            st = getstatusoutput(('nomos', pkgfile.path), True)
            print st
            def split_license(license):
                return (x.strip() for x in
                        (l for l in license_regex.split(license) if l))

            if st[0]:
                for line in st[1].splitlines():
                    if 'license(s)' in line:
                        licenses = line.split('license(s) ')[1].split(",")
                        rpm_license = pkg[rpm.RPMTAG_LICENSE]
                        if not rpm_license:
                            continue
                        else:
                            if rpm_license not in VALID_LICENSES:
                                for l1 in split_license(rpm_license):
                                    if l1 in VALID_LICENSES and l1 == licenses[0]:
                                        continue
                                    sp = split_license(l1)
                                    if sp != licenses:
                                        printWarning(pkg, 'licenses-do-not-match', sp, licenses)



    def check(self, pkg):

        if pkg.isSource() or not pkg.licenseFiles():
            return

        self.__checkLicenseFiles(pkg)

check = LicenseCheck()

addDetails(
'licenses-do-not-match',
'''Licenses do not match.''',
'wrong-number-of-licenses',
'''Number of licenses do not match.''',
)

# Local variables:
# indent-tabs-mode: nil
# py-indent-offset: 4
# End:
# ex: ts=4 sw=4 et