]> git.donarmstrong.com Git - cran2deb.git/blob - trunk/exec/license
74e01a57ea63ece49babfb568f87604b7f2bb6f6
[cran2deb.git] / trunk / 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             license_sha1 = digest(readChar(path,file.info(path)$size)
45                                  ,algo='sha1', serialize=FALSE)
46         } else if (length(grep('^[0-9a-f]{40}$',path))) {
47             license_sha1 = path
48         } else {
49             error(path,'does not exist and does not look like an SHA1 hash')
50             return()
51         }
52         db_add_license_hash(license,license_sha1)
53     } else if (cmd == 'pkg') {
54         if (argc != 3) {
55             usage()
56             return()
57         }
58         license <- argv[2]
59         pkg_name <- argv[3]
60         current_action <- db_license_override_name(license)
61         if (is.null(current_action)) {
62             notice('license',license,'is not known; add it')
63             return()
64         }
65         action = 'accept'
66         if (!current_action) {
67             action = 'reject'
68         }
69         notice('in future, will',action,'the package',pkg_name,'under license',license)
70         tmp <- setup()
71         success <- try((function() {
72             pkg <- prepare_pkg(tmp,pkg_name)
73             if (!('License' %in% names(pkg$description[1,]))) {
74                 error('package',pkg$name,'has no License: field in DESCRIPTION')
75                 return()
76             }
77             first_license = (strsplit(chomp(pkg$description[1,'License'])
78                                      ,'[[:space:]]*\\|[[:space:]]*')[[1]])[1]
79             license_sha1 <- get_license_hash(pkg,first_license)
80             db_add_license_hash(license,license_sha1)
81         })())
82         cleanup(tmp)
83         if (inherits(success,'try-error')) {
84             return()
85         }
86     } else if (cmd == 'view') {
87         if (argc != 2) {
88             usage()
89             return()
90         }
91         pkg_name <- argv[2]
92         tmp <- setup()
93         success <- try((function() {
94             pkg <- prepare_pkg(tmp,pkg_name)
95             if (!('License' %in% names(pkg$description[1,]))) {
96                 error('package',pkg$name,'has no License: field in DESCRIPTION')
97                 return()
98             }
99             first_license = (strsplit(chomp(pkg$description[1,'License'])
100                                      ,'[[:space:]]*\\|[[:space:]]*')[[1]])[1]
101             first_license = get_license(pkg,first_license)
102             cat(strwrap(first_license),file='|less')
103         })())
104         cleanup(tmp)
105         if (inherits(success,'try-error')) {
106             return()
107         }
108     } else if (cmd == 'ls') {
109         for (x in db_license_overrides()) print(x)
110     } else if (cmd == 'help') {
111         usage()
112         return()
113     } else if (cmd == 'quit') {
114         exit()
115     }
116 }
117
118 argc <- length(argv)
119 if (argc > 1) {
120     exec_cmd(argc-1,argv[c(2:argc)])
121 } else {
122     while(T) {
123         argv <- strsplit(readline('license> '),'[[:space:]]+')[[1]]
124         exec_cmd(length(argv),argv)
125     }
126 }