Last active
December 19, 2015 17:09
-
-
Save larryv/5989459 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function s:CalculateChecksum(file, hash) | |
let [f, h] = [shellescape(a:file), shellescape(a:hash)] | |
let cmd = printf('openssl dgst -%s %s', h, f) | |
return matchstr(system(cmd), '\X\zs\x\+\ze\n$') | |
endfunction | |
function s:ChecksumDistfile(name, path, urls, hashes) | |
let tmp = tempname() | |
if filereadable(a:path) | |
call system(printf('ln -s %s %s', shellescape(a:path), tmp)) | |
else | |
" TODO Fetch distfiles on the fly | |
"for url in map(a:urls, 'shellescape(v:val)') | |
" system(printf("curl -#fL %s > %s", url, tmp)) | |
" if !v:shell_error | |
" break | |
" endif | |
"endfor | |
return [] | |
endif | |
return map(copy(a:hashes), 's:CalculateChecksum(tmp, v:val)') | |
endfunction | |
function s:InsertChecksums(portfile) | |
let digests = ['rmd160', 'sha256'] | |
let portdir = shellescape(fnamemodify(a:portfile, ':h')) | |
let output = system(printf('port -qD %s distfiles', portdir)) | |
" Sanitize | |
let output = substitute(output, '\[\(.\{-1,}\)\]', '\1', 'g') | |
let output = substitute(output, | |
\ '\%(md5\|sha1\|rmd160\|sha256\): \x\+\n', | |
\ '', | |
\ 'g') | |
let checksums = map(map(split(output, "\n \n"), 'split(v:val)'), | |
\ 's:ChecksumDistfile(v:val[0], v:val[1],' . | |
\ 'v:val[2:], digests)') | |
echo checksums | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment