]> git.donarmstrong.com Git - cran2deb.git/blob - tags/gsoc_final/R/license.R
c392e11fa7fb0f1ae969208d7880abc1d430f1b4
[cran2deb.git] / tags / gsoc_final / R / license.R
1 is_acceptable_license <- function(license) {
2     # determine if license text is acceptable
3
4     if (length(grep('^file ',license))) {
5         # skip file licenses
6         return(FALSE)
7     }
8     license <- license_text_reduce(license)
9     action = db_license_override_name(license)
10     if (!is.null(action)) {
11         return(action)
12     }
13     license <- license_text_further_reduce(license)
14     action = db_license_override_name(license)
15     if (!is.null(action)) {
16         warn('Accepting/rejecting wild license as',license,'. FIX THE PACKAGE!')
17         return(action)
18     }
19     license <- license_text_extreme_reduce(license)
20     action = db_license_override_name(license)
21     if (!is.null(action)) {
22         warn('Accepting/rejecting wild license as',license,'. FIX THE PACKAGE!')
23         return(action)
24     }
25     # TODO: file {LICENSE,LICENCE} (+ maybe COPYING?)
26     error('Wild license',license,'did not match classic rules; rejecting')
27     return(F)
28 }
29
30 license_text_reduce <- function(license) {
31     # these reduction steps are sound for all conformant R license
32     # specifications.
33
34     # compress spaces into a single space
35     license = gsub('[[:space:]]+',' ',license)
36     # make all characters lower case
37     license = tolower(license)
38     # don't care about versions of licenses
39     license = chomp(sub('\\( ?[<=>!]+ ?[0-9.-]+ ?\\)',''
40                     ,sub('-[0-9.-]+','',license)))
41     # remove any extra space introduced
42     license = chomp(gsub('[[:space:]]+',' ',license))
43     return(license)
44 }
45
46 license_text_further_reduce <- function(license) {
47     # these reduction steps are heuristic and may lead to
48     # in correct acceptances, if care is not taken.
49
50     # uninteresting urls
51     license = gsub('http://www.gnu.org/[[:alnum:]/._-]*','',license)
52     license = gsub('http://www.x.org/[[:alnum:]/._-]*','',license)
53     license = gsub('http://www.opensource.org/[[:alnum:]/._-]*','',license)
54     # remove all punctuation
55     license = gsub('[[:punct:]]+','',license)
56     # remove any extra space introduced
57     license = chomp(gsub('[[:space:]]+',' ',license))
58     # redundant
59     license = gsub('the','',license)
60     license = gsub('see','',license)
61     license = gsub('standard','',license)
62     license = gsub('licen[sc]e','',license)
63     license = gsub('(gnu )?(gpl|general public)','gpl',license)
64     license = gsub('(mozilla )?(mpl|mozilla public)','mpl',license)
65     # remove any extra space introduced
66     license = chomp(gsub('[[:space:]]+',' ',license))
67     return(license)
68 }
69
70 license_text_extreme_reduce <- function(license) {
71     # remove everything that may or may not be a version specification
72     license = gsub('(ver?sion|v)? *[0-9.-]+ *(or *(higher|later|newer|greater|above))?',''
73                    ,license)
74     # remove any extra space introduced
75     license = chomp(gsub('[[:space:]]+',' ',license))
76     return(license)
77 }
78
79 license_text_hash_reduce <- function(text) {
80     # reduction of license text, suitable for hashing.
81     return(chomp(tolower(gsub('[[:space:]]+',' ',text))))
82 }
83
84 get_license <- function(pkg,license) {
85     license <- chomp(gsub('[[:space:]]+',' ',license))
86     if (length(grep('^file ',license))) {
87         if (length(grep('^file LICEN[CS]E$',license))) {
88             path = gsub('file ','',license)
89             path = file.path(pkg$path, path)
90             license <- license_text_reduce(readChar(path,file.info(path)$size))
91         } else {
92             error('invalid license file specification',license)
93             return(NA)
94         }
95     }
96     return(license)
97 }
98
99 get_license_hash <- function(pkg,license) {
100     return(digest(get_license(pkg,license),algo='sha1',serialize=FALSE))
101 }
102
103 is_acceptable_hash_license <- function(pkg,license) {
104     license_sha1 <- get_license_hash(pkg,license)
105     if (is.null(license_sha1)) {
106         return(FALSE)
107     }
108     action = db_license_override_hash(license_sha1)
109     if (is.null(action)) {
110         action = FALSE
111     }
112     if (action) {
113         warn('Wild license',license,'accepted via hash',license_sha1)
114     }
115     return(action)
116 }
117
118
119 accept_license <- function(pkg) {
120     # check the license
121     if (!('License' %in% names(pkg$description[1,]))) {
122         fail('package has no License: field in description!')
123     }
124     accept=NULL
125     for (license in strsplit(chomp(pkg$description[1,'License'])
126                             ,'[[:space:]]*\\|[[:space:]]*')[[1]]) {
127         if (is_acceptable_license(license)) {
128             accept=license
129             break
130         }
131         if (is_acceptable_hash_license(pkg,license)) {
132             accept=license
133             break
134         }
135     }
136     if (is.null(accept)) {
137         fail('No acceptable license:',pkg$description[1,'License'])
138     } else {
139         notice('Auto-accepted license',accept)
140     }
141     if (accept == 'Unlimited') {
142         # definition of Unlimited from ``Writing R extensions''
143         accept=paste('Unlimited (no restrictions on distribution or'
144                     ,'use other than those imposed by relevant laws)')
145     }
146     return(accept)
147 }