-
Notifications
You must be signed in to change notification settings - Fork 0
/
flice.py
105 lines (90 loc) · 3.57 KB
/
flice.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
#
# Copyright 2010
# Ionuț C. Arțăriși <mapleoin@fedoraproject.org>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import os.path
import re
import urllib
import yum.packages
import rpmUtils.transaction
import func_module
FEDORA_LICENSES = 'https://fedoraproject.org/w/index.php?title=Licensing:Main&action=raw§ion=%s'
CODE_SECTION = 8
# Section numbers SHOULD be viewable here? But the index api apparently only
# uses integer numbers
# https://fedoraproject.org/w/api.php?text={{Licensing:Main}}&action=parse&prop=sections
# LICENSE_SECTIONS = [8, 13, 16,
# fails for any value >19 and so skips Fonts section
# All this part should go away if we can get a more structured Licenses list
def lpages(licenses_url, section):
'''Retrieves the license page
'''
f = urllib.urlopen(licenses_url % section)
license_page = f.read()
f.close()
return license_page
def get_licenses_from_page(license_page):
# We have to rely on the structure of the current wiki page and hope it
# never changes
regy = re.compile(r"""\|-\n\| # beginning of a good row
.*?\|\| # skip the first column
\s*(.*?)\s* # get our column and strip whitespace
\|\| # end our column
""", re.X)
licenses = set()
# First one is the table header
[licenses.add(l) for l in re.findall(regy, license_page)[1:]]
return licenses
class Flice(func_module.FuncModule):
# Update these if need be.
version = "0.0.1"
api_version = "0.0.1"
description = "check if an srpm's license is approved by Fedora"
lpage = lpages(FEDORA_LICENSES, CODE_SECTION)
good_licenses = get_licenses_from_page(lpage)
def check(self, srpm_path):
"""Check the SRPM on the given path for Fedora License compliance
Receives a 'file://' or 'http(s)://' path to a SRPM, downloads the
file if necessary, extracts the yum metadata and compares to our
list of licenses.
"""
if 'file://' in srpm_path:
pkg_path = srpm_path.replace('file://','')
if not os.path.isfile(pkg_path):
return "Couldn't find the srpm file: %s." % pkg_path
elif 'http' in srpm_path:
try:
pkg_path = urllib.urlretrieve(srpm_path)[0]
except:
return "Couldn't open the url: %s" % pkg_path
else:
return "Don't know how to open that."
ts = rpmUtils.transaction.initReadOnlyTransaction()
package = yum.packages.YumLocalPackage(ts, pkg_path)
pkglicense = set(re.split("\s+and\s+", package.license))
if pkglicense.issubset(self.good_licenses):
return (package.license, 'Good.')
else:
return (package.license, 'Probably Bad.')
def register_method_args(self):
return {
'check': {
'args' : {
'srpm_path':{
'type':'string',
'optional':False,
'default':'',
'description':'file:// path or http:// to the srpm'
},
},
'description': 'Check the license of a srpm for '
'Fedora compliance'
}
}