1 """
2 Defines classes handling license information : the actual license text, but also license properties (variables substituted when installing license headers in source files), and a license descriptor.
3 """
4 import glob
5 import logging
6 import os
7 import re
8
10 """
11 Class holding license descriptions.
12
13 - A brief label identifying a licence (GPLv2, MIT, CeCILL-B),
14 - A filename with the actuel license text template,
15 - A set of properties : (name,value) tuples used to insert project specific information in license templates (copyright years and holders, project name).
16 """
17
18 __logger=logging.getLogger('lichk')
19
21 """
22 Builds a new LicenseInfo instance from a license template file.
23
24 License template files a formatted as follows :
25 - the first line contains the C{'label: '} keyword followed by single word label for the license,
26 - the second line contains the C{'description: '} keyword followed by a short description of the license,
27 - the third line contains only the C{'contents: '} keyword,
28 - all subsequent lines are considered to be the text of the license itself.
29 Inside the license text, constructs of the form C{${propname}} denote properties. They are replaced with their actuel values when license headers are generated in source files.
30
31 @param filename: the name of the license template file.
32 """
33 self.filename=filename
34 self.label=None
35 self.description=None
36 self.properties={}
37 self.__buildFromFile(filename)
38
40 """
41 Returns the label of this LicenseInfo instance.
42
43 @returns: the label defined for the license held by this LicenseInfo.
44 @rtype: string
45 """
46 return self.label
47
49 """
50 Returns the description of this LicenseInfo instance.
51
52 @returns: the description defined for the license held by this LicenseInfo.
53 @rtype: string
54 """
55 return self.description
56
58 """
59 Returns the proprty names found in the text of this license.
60
61 @returns: a list of properties found when parsing the license template file.
62 @rtype: [string,]
63 """
64 propnames=self.properties.keys()
65 propnames.sort()
66 return propnames
67
69 self.__logger.debug("Reading license info from file %s",filename)
70 fd=open(filename)
71
72 line=fd.readline()[:-1]
73 (desc,contents)=re.split(':',line)
74 desc=desc.strip()
75 contents=contents.strip()
76 if desc == 'label' :
77 self.label=contents
78 else :
79 self.__logger.warn("No label found in license file : %s"%filename)
80 fd.close()
81
82
83 line=fd.readline()[:-1]
84 (desc,contents)=re.split(':',line)
85 desc=desc.strip()
86 contents=contents.strip()
87 if desc == 'description' :
88 self.description=contents
89 else :
90 self.__logger.warn("No description found in license file : %s"%filename)
91 fd.close()
92
93
94 line=fd.readline()[:-1]
95 (desc,contents)=re.split(':',line)
96 desc=desc.strip()
97 if desc == 'contents' :
98
99 for line in fd :
100
101 propnames=re.findall(r'\${(\w+)}',line)
102 for propname in propnames:
103 self.properties[propname]=True
104
105 else :
106 self.__logger.warn("No contents found in license file : %s"%filename)
107 fd.close()
108 fd.close()
109
110
112 """
113 Class loading license information from a set of files in a template directory.
114 """
115
116 - def __init__(self,templatedir=os.path.join(os.path.dirname(__file__),'data','templates')) :
117 """
118 Builds a new instance of LicenseChecker.
119
120 @param templatedir: the name of a license template directory. In this directory all files ending with C{.txt} will be parsed. If not specified the directory is taken to be the C{templates} subdirectory in this module's C{data} subdirectory. If C{templatedir=None} no directory will be used, and no license information will be initialised.
121 """
122 self.licenses={}
123 if templatedir:
124 licensefiles=glob.glob(os.path.join(templatedir,'*.txt'))
125 for licensefile in licensefiles :
126 self.addLicense(licensefile)
127
129 """
130 Adds a license to the list of known licenses.
131
132 @param filename: a string with the filename of a license template.
133
134 @see: L{LicenseInfo.__init__()}
135 """
136 newlicense=LicenseInfo(filename)
137 self.licenses[newlicense.getLabel()]=newlicense
138
140 """
141 Returns information about all known licenses.
142
143 @returns: a list of all known LicenseInfo instances.
144 @rtype: [LicenseInfo,]
145 """
146 return self.licenses.values()
147
149 """
150 Returns all known license labels.
151
152 @returns: the sorted list of labels of all known licenses.
153 @rtype: [string,]
154 """
155 labels=self.licenses.keys()
156 labels.sort()
157 return labels
158
160 """
161 Returns the LicenseInfo for a given label.
162
163 @param label: a string with a known license label.
164 @returns: the LicenseInfo instance of the license with the given label or C{None} if the label is unknown.
165 @rtype: LicenseInfo
166 """
167 li=None
168 try :
169 li=self.licenses[label]
170 except KeyError:
171 pass
172 return li
173