gnupload 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #!/bin/sh
  2. # Sign files and upload them.
  3. scriptversion=2012-12-11.16; # UTC
  4. # Copyright (C) 2004-2013 Free Software Foundation, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # Originally written by Alexandre Duret-Lutz <adl@gnu.org>.
  19. # The master copy of this file is maintained in the gnulib Git repository.
  20. # Please send bug reports and feature requests to bug-gnulib@gnu.org.
  21. set -e
  22. GPG='gpg --batch --no-tty'
  23. conffile=.gnuploadrc
  24. to=
  25. dry_run=false
  26. replace=
  27. symlink_files=
  28. delete_files=
  29. delete_symlinks=
  30. collect_var=
  31. dbg=
  32. nl='
  33. '
  34. usage="Usage: $0 [OPTION]... [CMD] FILE... [[CMD] FILE...]
  35. Sign all FILES, and process them at selected destinations according to CMD.
  36. <http://www.gnu.org/prep/maintain/html_node/Automated-FTP-Uploads.html>
  37. explains further.
  38. Commands:
  39. --delete delete FILES from destination
  40. --symlink create symbolic links
  41. --rmsymlink remove symbolic links
  42. -- treat the remaining arguments as files to upload
  43. Options:
  44. --help print this help text and exit
  45. --to DEST specify one destination for FILES
  46. (multiple --to options are allowed)
  47. --user NAME sign with key NAME
  48. --replace allow replacements of existing files
  49. --symlink-regex[=EXPR] use sed script EXPR to compute symbolic link names
  50. --dry-run do nothing, show what would have been done
  51. (including the constructed directive file)
  52. --version output version information and exit
  53. If --symlink-regex is given without EXPR, then the link target name
  54. is created by replacing the version information with '-latest', e.g.:
  55. foo-1.3.4.tar.gz -> foo-latest.tar.gz
  56. Recognized destinations are:
  57. alpha.gnu.org:DIRECTORY
  58. savannah.gnu.org:DIRECTORY
  59. savannah.nongnu.org:DIRECTORY
  60. ftp.gnu.org:DIRECTORY
  61. build directive files and upload files by FTP
  62. download.gnu.org.ua:{alpha|ftp}/DIRECTORY
  63. build directive files and upload files by SFTP
  64. [user@]host:DIRECTORY upload files with scp
  65. Options and commands are applied in order. If the file $conffile exists
  66. in the current working directory, its contents are prepended to the
  67. actual command line options. Use this to keep your defaults. Comments
  68. (#) and empty lines in $conffile are allowed.
  69. Examples:
  70. 1. Upload foobar-1.0.tar.gz to ftp.gnu.org:
  71. gnupload --to ftp.gnu.org:foobar foobar-1.0.tar.gz
  72. 2. Upload foobar-1.0.tar.gz and foobar-1.0.tar.xz to ftp.gnu.org:
  73. gnupload --to ftp.gnu.org:foobar foobar-1.0.tar.gz foobar-1.0.tar.xz
  74. 3. Same as above, and also create symbolic links to foobar-latest.tar.*:
  75. gnupload --to ftp.gnu.org:foobar \\
  76. --symlink-regex \\
  77. foobar-1.0.tar.gz foobar-1.0.tar.xz
  78. 4. Upload foobar-0.9.90.tar.gz to two sites:
  79. gnupload --to alpha.gnu.org:foobar \\
  80. --to sources.redhat.com:~ftp/pub/foobar \\
  81. foobar-0.9.90.tar.gz
  82. 5. Delete oopsbar-0.9.91.tar.gz and upload foobar-0.9.91.tar.gz
  83. (the -- terminates the list of files to delete):
  84. gnupload --to alpha.gnu.org:foobar \\
  85. --to sources.redhat.com:~ftp/pub/foobar \\
  86. --delete oopsbar-0.9.91.tar.gz \\
  87. -- foobar-0.9.91.tar.gz
  88. gnupload uses the ncftpput program to do the transfers; if you don't
  89. happen to have an ncftp package installed, the ncftpput-ftp script in
  90. the build-aux/ directory of the gnulib package
  91. (http://savannah.gnu.org/projects/gnulib) may serve as a replacement.
  92. Send patches and bug reports to <bug-gnulib@gnu.org>."
  93. # Read local configuration file
  94. if test -r "$conffile"; then
  95. echo "$0: Reading configuration file $conffile"
  96. conf=`sed 's/#.*$//;/^$/d' "$conffile" | tr "\015$nl" ' '`
  97. eval set x "$conf \"\$@\""
  98. shift
  99. fi
  100. while test -n "$1"; do
  101. case $1 in
  102. -*)
  103. collect_var=
  104. case $1 in
  105. --help)
  106. echo "$usage"
  107. exit $?
  108. ;;
  109. --to)
  110. if test -z "$2"; then
  111. echo "$0: Missing argument for --to" 1>&2
  112. exit 1
  113. else
  114. to="$to $2"
  115. shift
  116. fi
  117. ;;
  118. --user)
  119. if test -z "$2"; then
  120. echo "$0: Missing argument for --user" 1>&2
  121. exit 1
  122. else
  123. GPG="$GPG --local-user $2"
  124. shift
  125. fi
  126. ;;
  127. --delete)
  128. collect_var=delete_files
  129. ;;
  130. --replace)
  131. replace="replace: true"
  132. ;;
  133. --rmsymlink)
  134. collect_var=delete_symlinks
  135. ;;
  136. --symlink-regex=*)
  137. symlink_expr=`expr "$1" : '[^=]*=\(.*\)'`
  138. ;;
  139. --symlink-regex)
  140. symlink_expr='s|-[0-9][0-9\.]*\(-[0-9][0-9]*\)\{0,1\}\.|-latest.|'
  141. ;;
  142. --symlink)
  143. collect_var=symlink_files
  144. ;;
  145. --dry-run|-n)
  146. dry_run=:
  147. ;;
  148. --version)
  149. echo "gnupload $scriptversion"
  150. exit $?
  151. ;;
  152. --)
  153. shift
  154. break
  155. ;;
  156. -*)
  157. echo "$0: Unknown option '$1', try '$0 --help'" 1>&2
  158. exit 1
  159. ;;
  160. esac
  161. ;;
  162. *)
  163. if test -z "$collect_var"; then
  164. break
  165. else
  166. eval "$collect_var=\"\$$collect_var $1\""
  167. fi
  168. ;;
  169. esac
  170. shift
  171. done
  172. dprint()
  173. {
  174. echo "Running $* ..."
  175. }
  176. if $dry_run; then
  177. dbg=dprint
  178. fi
  179. if test -z "$to"; then
  180. echo "$0: Missing destination sites" >&2
  181. exit 1
  182. fi
  183. if test -n "$symlink_files"; then
  184. x=`echo "$symlink_files" | sed 's/[^ ]//g;s/ //g'`
  185. if test -n "$x"; then
  186. echo "$0: Odd number of symlink arguments" >&2
  187. exit 1
  188. fi
  189. fi
  190. if test $# = 0; then
  191. if test -z "${symlink_files}${delete_files}${delete_symlinks}"; then
  192. echo "$0: No file to upload" 1>&2
  193. exit 1
  194. fi
  195. else
  196. # Make sure all files exist. We don't want to ask
  197. # for the passphrase if the script will fail.
  198. for file
  199. do
  200. if test ! -f $file; then
  201. echo "$0: Cannot find '$file'" 1>&2
  202. exit 1
  203. elif test -n "$symlink_expr"; then
  204. linkname=`echo $file | sed "$symlink_expr"`
  205. if test -z "$linkname"; then
  206. echo "$0: symlink expression produces empty results" >&2
  207. exit 1
  208. elif test "$linkname" = $file; then
  209. echo "$0: symlink expression does not alter file name" >&2
  210. exit 1
  211. fi
  212. fi
  213. done
  214. fi
  215. # Make sure passphrase is not exported in the environment.
  216. unset passphrase
  217. unset passphrase_fd_0
  218. GNUPGHOME=${GNUPGHOME:-$HOME/.gnupg}
  219. # Reset PATH to be sure that echo is a built-in. We will later use
  220. # 'echo $passphrase' to output the passphrase, so it is important that
  221. # it is a built-in (third-party programs tend to appear in 'ps'
  222. # listings with their arguments...).
  223. # Remember this script runs with 'set -e', so if echo is not built-in
  224. # it will exit now.
  225. if $dry_run || grep -q "^use-agent" $GNUPGHOME/gpg.conf; then :; else
  226. PATH=/empty echo -n "Enter GPG passphrase: "
  227. stty -echo
  228. read -r passphrase
  229. stty echo
  230. echo
  231. passphrase_fd_0="--passphrase-fd 0"
  232. fi
  233. if test $# -ne 0; then
  234. for file
  235. do
  236. echo "Signing $file ..."
  237. rm -f $file.sig
  238. echo "$passphrase" | $dbg $GPG $passphrase_fd_0 -ba -o $file.sig $file
  239. done
  240. fi
  241. # mkdirective DESTDIR BASE FILE STMT
  242. # Arguments: See upload, below
  243. mkdirective ()
  244. {
  245. stmt="$4"
  246. if test -n "$3"; then
  247. stmt="
  248. filename: $3$stmt"
  249. fi
  250. cat >${2}.directive<<EOF
  251. version: 1.2
  252. directory: $1
  253. comment: gnupload v. $scriptversion$stmt
  254. EOF
  255. if $dry_run; then
  256. echo "File ${2}.directive:"
  257. cat ${2}.directive
  258. echo "File ${2}.directive:" | sed 's/./-/g'
  259. fi
  260. }
  261. mksymlink ()
  262. {
  263. while test $# -ne 0
  264. do
  265. echo "symlink: $1 $2"
  266. shift
  267. shift
  268. done
  269. }
  270. # upload DEST DESTDIR BASE FILE STMT FILES
  271. # Arguments:
  272. # DEST Destination site;
  273. # DESTDIR Destination directory;
  274. # BASE Base name for the directive file;
  275. # FILE Name of the file to distribute (may be empty);
  276. # STMT Additional statements for the directive file;
  277. # FILES List of files to upload.
  278. upload ()
  279. {
  280. dest=$1
  281. destdir=$2
  282. base=$3
  283. file=$4
  284. stmt=$5
  285. files=$6
  286. rm -f $base.directive $base.directive.asc
  287. case $dest in
  288. alpha.gnu.org:*)
  289. mkdirective "$destdir" "$base" "$file" "$stmt"
  290. echo "$passphrase" | $dbg $GPG $passphrase_fd_0 --clearsign $base.directive
  291. $dbg ncftpput ftp-upload.gnu.org /incoming/alpha $files $base.directive.asc
  292. ;;
  293. ftp.gnu.org:*)
  294. mkdirective "$destdir" "$base" "$file" "$stmt"
  295. echo "$passphrase" | $dbg $GPG $passphrase_fd_0 --clearsign $base.directive
  296. $dbg ncftpput ftp-upload.gnu.org /incoming/ftp $files $base.directive.asc
  297. ;;
  298. savannah.gnu.org:*)
  299. if test -z "$files"; then
  300. echo "$0: warning: standalone directives not applicable for $dest" >&2
  301. fi
  302. $dbg ncftpput savannah.gnu.org /incoming/savannah/$destdir $files
  303. ;;
  304. savannah.nongnu.org:*)
  305. if test -z "$files"; then
  306. echo "$0: warning: standalone directives not applicable for $dest" >&2
  307. fi
  308. $dbg ncftpput savannah.nongnu.org /incoming/savannah/$destdir $files
  309. ;;
  310. download.gnu.org.ua:alpha/*|download.gnu.org.ua:ftp/*)
  311. destdir_p1=`echo "$destdir" | sed 's,^[^/]*/,,'`
  312. destdir_topdir=`echo "$destdir" | sed 's,/.*,,'`
  313. mkdirective "$destdir_p1" "$base" "$file" "$stmt"
  314. echo "$passphrase" | $dbg $GPG $passphrase_fd_0 --clearsign $base.directive
  315. for f in $files $base.directive.asc
  316. do
  317. echo put $f
  318. done | $dbg sftp -b - puszcza.gnu.org.ua:/incoming/$destdir_topdir
  319. ;;
  320. /*)
  321. dest_host=`echo "$dest" | sed 's,:.*,,'`
  322. mkdirective "$destdir" "$base" "$file" "$stmt"
  323. echo "$passphrase" | $dbg $GPG $passphrase_fd_0 --clearsign $base.directive
  324. $dbg cp $files $base.directive.asc $dest_host
  325. ;;
  326. *)
  327. if test -z "$files"; then
  328. echo "$0: warning: standalone directives not applicable for $dest" >&2
  329. fi
  330. $dbg scp $files $dest
  331. ;;
  332. esac
  333. rm -f $base.directive $base.directive.asc
  334. }
  335. #####
  336. # Process any standalone directives
  337. stmt=
  338. if test -n "$symlink_files"; then
  339. stmt="$stmt
  340. `mksymlink $symlink_files`"
  341. fi
  342. for file in $delete_files
  343. do
  344. stmt="$stmt
  345. archive: $file"
  346. done
  347. for file in $delete_symlinks
  348. do
  349. stmt="$stmt
  350. rmsymlink: $file"
  351. done
  352. if test -n "$stmt"; then
  353. for dest in $to
  354. do
  355. destdir=`echo $dest | sed 's/[^:]*://'`
  356. upload "$dest" "$destdir" "`hostname`-$$" "" "$stmt"
  357. done
  358. fi
  359. # Process actual uploads
  360. for dest in $to
  361. do
  362. for file
  363. do
  364. echo "Uploading $file to $dest ..."
  365. stmt=
  366. #
  367. # allowing file replacement is all or nothing.
  368. if test -n "$replace"; then stmt="$stmt
  369. $replace"
  370. fi
  371. #
  372. files="$file $file.sig"
  373. destdir=`echo $dest | sed 's/[^:]*://'`
  374. if test -n "$symlink_expr"; then
  375. linkname=`echo $file | sed "$symlink_expr"`
  376. stmt="$stmt
  377. symlink: $file $linkname
  378. symlink: $file.sig $linkname.sig"
  379. fi
  380. upload "$dest" "$destdir" "$file" "$file" "$stmt" "$files"
  381. done
  382. done
  383. exit 0
  384. # Local variables:
  385. # eval: (add-hook 'write-file-hooks 'time-stamp)
  386. # time-stamp-start: "scriptversion="
  387. # time-stamp-format: "%:y-%02m-%02d.%02H"
  388. # time-stamp-time-zone: "UTC"
  389. # time-stamp-end: "; # UTC"
  390. # End: