gnupload 11 KB

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