/usr/lib/R/site-library/knitr/examples/upload-github.R is in r-cran-knitr 1.15.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | rm(list = ls(all.names = TRUE))
options(stringsAsFactors = FALSE)
FILES = c(
list.files('.', '.pdf$'),
'../../../knitr-book2/DDR-Yihui-Xie-Chap1-3.pdf'
)
i = file.exists(FILES)
if (!all(i)) stop('These files do not exist: ', paste(FILES[!i], collapse = ', '))
counts = data.frame(file = basename(FILES), count = 0)
if (file.exists('download_count.csv')) {
counts = rbind(counts, read.csv('download_count.csv'))
counts = counts[order(counts$file, counts$count, decreasing = TRUE), ]
counts = counts[!duplicated(counts$file), ]
}
rownames(counts) = FILES[match(basename(FILES), counts$file)]
library(httr)
token = Sys.getenv('GH_TOKEN', NA)
if (is.na(token)) stop('GH_TOKEN not found')
a = authenticate(token, "x-oauth-basic", "basic")
GET = function(...) {
r = httr::GET(..., a)
stop_for_status(r)
r
}
DELETE = function(...) {
r = httr::DELETE(..., a)
stop_for_status(r)
r
}
POST = function(...) {
r = httr::POST(..., a)
stop_for_status(r)
r
}
r = GET('https://api.github.com/repos/yihui/knitr/releases')
x = content(r, 'parsed')
for (i in x) {
if (i$tag_name == 'doc') {
r_url = i$url
r_upload = i$upload_url
break
}
}
r = GET(r_url)
x = content(r, 'parsed')
# delete assets that already exist
for (i in x$assets) {
j = match(i$name, counts$file)
if (is.na(j)) next
if (i$size == file.info(rownames(counts)[j])[, 'size']) {
FILES = setdiff(FILES, rownames(counts)[j])
message(i$name, ' probably not changed')
next
}
counts[j, 2] = counts[j, 2] + i$download_count
message('deleting ', i$name)
DELETE(i$url)
Sys.sleep(1)
}
# update download counts
counts = counts[order(counts$file), , drop = FALSE]
write.csv(counts, 'download_count.csv', row.names = FALSE)
# upload files
for (f in FILES) {
message('uploading ', f)
POST(
sub('{?name,label}', '', r_upload, fixed = TRUE),
query = list(name = basename(f)), body = upload_file(f, mime::guess_type(f))
)
Sys.sleep(1)
}
q('no')
|