]> git.donarmstrong.com Git - cran2deb.git/blob - branch/split_build/exec/license
rename double_build -> split_build
[cran2deb.git] / branch / split_build / exec / license
1 #!/usr/bin/env r
2 ## DOC: cran2deb license
3 ## DOC:     add licenses and change acceptance/rejection of licenses
4 ## DOC:
5
6 suppressPackageStartupMessages(library(cran2deb))
7 suppressPackageStartupMessages(library(digest))
8
9 exec_cmd <- function(argc, argv) {
10     usage <- function()
11         message(paste('usage: accept <license>'
12                      ,'       reject <license>'
13                      ,'       hash <license> (<path>|<hash>)'
14                      ,'       pkg <license> <pkg>'
15                      ,'       view <pkg>'
16                      ,'       ls'
17                      ,'       quit'
18                      ,sep='\n'))
19
20     if (argc < 1) {
21         exit()
22     }
23     cmd = argv[1]
24
25     if (cmd == 'accept' || cmd == 'reject') {
26         if (argc != 2) {
27             usage()
28             return()
29         }
30         action = (cmd == 'accept')
31         db_add_license_override(argv[2],action)
32     } else if (cmd == 'hash') {
33         if (argc != 3) {
34             usage()
35             return()
36         }
37         license = argv[2]
38         path = argv[3]
39         if (is.null(db_license_override_name(license))) {
40             error('license',license,'is not known; add it first')
41             return()
42         }
43         if (!file.exists(path)) {
44             error(path,'does not exist')
45             return()
46         }
47         license_sha1 = digest(readChar(path,file.info(path)$size)
48                              ,algo='sha1', serialize=FALSE)
49         db_add_license_hash(license,license_sha1)
50     } else if (cmd == 'hash_sha1') {
51         if (argc != 3) {
52             usage()
53             return()
54         }
55         license = argv[2]
56         license_sha1 = argv[3]
57         if (is.null(db_license_override_name(license))) {
58             error('license',license,'is not known; add it first')
59             return()
60         }
61         db_add_license_hash(license,license_sha1)
62     } else if (cmd == 'pkg') {
63         if (argc != 3) {
64             usage()
65             return()
66         }
67         license <- argv[2]
68         pkg_name <- argv[3]
69         current_action <- db_license_override_name(license)
70         if (is.null(current_action)) {
71             notice('license',license,'is not known; add it')
72             return()
73         }
74         action = 'accept'
75         if (!current_action) {
76             action = 'reject'
77         }
78         notice('in future, will',action,'the package',pkg_name,'under license',license)
79         tmp <- setup()
80         success <- try((function() {
81             pkg <- prepare_pkg(tmp,pkg_name)
82             if (!('License' %in% names(pkg$description[1,]))) {
83                 error('package',pkg$name,'has no License: field in DESCRIPTION')
84                 return()
85             }
86             first_license = (strsplit(chomp(pkg$description[1,'License'])
87                                      ,'[[:space:]]*\\|[[:space:]]*')[[1]])[1]
88             license_sha1 <- get_license_hash(pkg,first_license)
89             db_add_license_hash(license,license_sha1)
90         })())
91         cleanup(tmp)
92         if (inherits(success,'try-error')) {
93             return()
94         }
95     } else if (cmd == 'view') {
96         if (argc != 2) {
97             usage()
98             return()
99         }
100         pkg_name <- argv[2]
101         tmp <- setup()
102         success <- try((function() {
103             pkg <- prepare_pkg(tmp,pkg_name)
104             if (!('License' %in% names(pkg$description[1,]))) {
105                 error('package',pkg$name,'has no License: field in DESCRIPTION')
106                 return()
107             }
108             first_license = (strsplit(chomp(pkg$description[1,'License'])
109                                      ,'[[:space:]]*\\|[[:space:]]*')[[1]])[1]
110             first_license = get_license(pkg,first_license)
111             cat(strwrap(first_license),file='|less')
112         })())
113         cleanup(tmp)
114         if (inherits(success,'try-error')) {
115             return()
116         }
117     } else if (cmd == 'ls') {
118         licenses <- db_license_overrides()
119         for (i in rownames(licenses$overrides)) {
120             mode='accept'
121             if (licenses$overrides[i,'accept']==0) {
122                 mode='reject'
123             }
124             cat(paste(mode,licenses$overrides[i,'name'],'\n'))
125         }
126         for (i in rownames(licenses$hashes)) {
127             cat(paste('hash_sha1',licenses$hashes[i,'name'],licenses$hashes[i,'sha1'],'\n'))
128         }
129     } else if (cmd == 'help') {
130         usage()
131         return()
132     } else if (cmd == 'quit') {
133         exit()
134     }
135 }
136
137 argc <- length(argv)
138 if (argc > 1) {
139     exec_cmd(argc-1,argv[c(2:argc)])
140 } else {
141     while(T) {
142         argv <- strsplit(readline('license> '),'[[:space:]]+')[[1]]
143         exec_cmd(length(argv),argv)
144     }
145 }