2 ## DOC: cran2deb license
3 ## DOC: add licenses and change acceptance/rejection of licenses
6 suppressPackageStartupMessages(library(cran2deb))
7 suppressPackageStartupMessages(library(digest))
9 exec_cmd <- function(argc, argv) {
11 message(paste('usage: accept <license>'
13 ,' hash <license> (<path>|<hash>)'
14 ,' pkg <license> <pkg>'
25 if (cmd == 'accept' || cmd == 'reject') {
30 action = (cmd == 'accept')
31 db_add_license_override(argv[2],action)
32 } else if (cmd == 'hash') {
39 if (is.null(db_license_override_name(license))) {
40 error('license',license,'is not known; add it first')
43 if (!file.exists(path)) {
44 error(path,'does not exist')
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') {
56 license_sha1 = argv[3]
57 if (is.null(db_license_override_name(license))) {
58 error('license',license,'is not known; add it first')
61 db_add_license_hash(license,license_sha1)
62 } else if (cmd == 'pkg') {
69 current_action <- db_license_override_name(license)
70 if (is.null(current_action)) {
71 notice('license',license,'is not known; add it')
75 if (!current_action) {
78 notice('in future, will',action,'the package',pkg_name,'under license',license)
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')
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)
92 if (inherits(success,'try-error')) {
95 } else if (cmd == 'view') {
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')
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')
114 if (inherits(success,'try-error')) {
117 } else if (cmd == 'ls') {
118 licenses <- db_license_overrides()
119 for (i in rownames(licenses$overrides)) {
121 if (licenses$overrides[i,'accept']==0) {
124 cat(paste(mode,licenses$overrides[i,'name'],'\n'))
126 for (i in rownames(licenses$hashes)) {
127 cat(paste('hash_sha1',licenses$hashes[i,'name'],licenses$hashes[i,'sha1'],'\n'))
129 } else if (cmd == 'help') {
132 } else if (cmd == 'quit') {
139 exec_cmd(argc-1,argv[c(2:argc)])
142 argv <- strsplit(readline('license> '),'[[:space:]]+')[[1]]
143 exec_cmd(length(argv),argv)