dpkg-genchanges.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use POSIX;
  5. use POSIX qw(:errno_h :signal_h);
  6. use Dpkg;
  7. use Dpkg::Gettext;
  8. use Dpkg::ErrorHandling qw(warning error failure unknown internerr syserr
  9. subprocerr usageerr);
  10. use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is);
  11. push(@INC,$dpkglibdir);
  12. require 'controllib.pl';
  13. our (%f, %fi);
  14. our %p2i;
  15. our %substvar;
  16. our $sourcepackage;
  17. textdomain("dpkg-dev");
  18. my @changes_fields = qw(Format Date Source Binary Architecture Version
  19. Distribution Urgency Maintainer Changed-By
  20. Description Closes Changes Files);
  21. my $controlfile = 'debian/control';
  22. my $changelogfile = 'debian/changelog';
  23. my $changelogformat;
  24. my $fileslistfile = 'debian/files';
  25. my $varlistfile = 'debian/substvars';
  26. my $uploadfilesdir = '..';
  27. my $sourcestyle = 'i';
  28. my $quiet = 0;
  29. my %f2p; # - file to package map
  30. my %p2f; # - package to file map, has entries for both "packagename"
  31. # and "packagename architecture"
  32. my %p2ver; # - package to version map
  33. my %p2arch;
  34. my %f2sec; # - file to section map
  35. my %f2seccf;
  36. my %f2pri; # - file to priority map
  37. my %f2pricf;
  38. my %sourcedefault; # - default values as taken from source (used for Section,
  39. # Priority and Maintainer)
  40. my @descriptions;
  41. my @sourcefiles;
  42. my @fileslistfiles;
  43. my %md5sum; # - md5sum to file map
  44. my %remove; # - fields to remove
  45. my %override;
  46. my %archadded;
  47. my @archvalues;
  48. my $dsc;
  49. my $changesdescription;
  50. my $sourceonly;
  51. my $binaryonly;
  52. my $archspecific;
  53. my $forcemaint;
  54. my $forcechangedby;
  55. my $since;
  56. sub version {
  57. printf _g("Debian %s version %s.\n"), $progname, $version;
  58. printf _g("
  59. Copyright (C) 1996 Ian Jackson.
  60. Copyright (C) 2000,2001 Wichert Akkerman.");
  61. printf _g("
  62. This is free software; see the GNU General Public Licence version 2 or
  63. later for copying conditions. There is NO warranty.
  64. ");
  65. }
  66. sub usage {
  67. printf _g(
  68. "Usage: %s [<option> ...]
  69. Options:
  70. -b binary-only build - no source files.
  71. -B arch-specific - no source or arch-indep files.
  72. -S source-only upload.
  73. -c<controlfile> get control info from this file.
  74. -l<changelogfile> get per-version info from this file.
  75. -f<fileslistfile> get .deb files list from this file.
  76. -v<sinceversion> include all changes later than version.
  77. -C<changesdescription> use change description from this file.
  78. -m<maintainer> override control's maintainer value.
  79. -e<maintainer> override changelog's maintainer value.
  80. -u<uploadfilesdir> directory with files (default is \`..').
  81. -si (default) src includes orig for debian-revision 0 or 1.
  82. -sa source includes orig src.
  83. -sd source is diff and .dsc only.
  84. -q quiet - no informational messages on stderr.
  85. -F<changelogformat> force change log format.
  86. -V<name>=<value> set a substitution variable.
  87. -T<varlistfile> read variables here, not debian/substvars.
  88. -D<field>=<value> override or add a field and value.
  89. -U<field> remove a field.
  90. -h, --help show this help message.
  91. --version show the version.
  92. "), $progname;
  93. }
  94. while (@ARGV) {
  95. $_=shift(@ARGV);
  96. if (m/^-b$/) {
  97. $sourceonly && &usageerr(_g("cannot combine -b or -B and -S"));
  98. $binaryonly= 1;
  99. } elsif (m/^-B$/) {
  100. $sourceonly && &usageerr(_g("cannot combine -b or -B and -S"));
  101. $archspecific=1;
  102. $binaryonly= 1;
  103. printf STDERR _g("%s: arch-specific upload - not including arch-independent packages")."\n", $progname;
  104. } elsif (m/^-S$/) {
  105. $binaryonly && &usageerr(_g("cannot combine -b or -B and -S"));
  106. $sourceonly= 1;
  107. } elsif (m/^-s([iad])$/) {
  108. $sourcestyle= $1;
  109. } elsif (m/^-q$/) {
  110. $quiet= 1;
  111. } elsif (m/^-c/) {
  112. $controlfile= $';
  113. } elsif (m/^-l/) {
  114. $changelogfile= $';
  115. } elsif (m/^-C/) {
  116. $changesdescription= $';
  117. } elsif (m/^-f/) {
  118. $fileslistfile= $';
  119. } elsif (m/^-v/) {
  120. $since= $';
  121. } elsif (m/^-T/) {
  122. $varlistfile= $';
  123. } elsif (m/^-m/) {
  124. $forcemaint= $';
  125. } elsif (m/^-e/) {
  126. $forcechangedby= $';
  127. } elsif (m/^-F([0-9a-z]+)$/) {
  128. $changelogformat=$1;
  129. } elsif (m/^-D([^\=:]+)[=:]/) {
  130. $override{$1}= $';
  131. } elsif (m/^-u/) {
  132. $uploadfilesdir= $';
  133. } elsif (m/^-U([^\=:]+)$/) {
  134. $remove{$1}= 1;
  135. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  136. $substvar{$1}= $';
  137. } elsif (m/^-(h|-help)$/) {
  138. &usage; exit(0);
  139. } elsif (m/^--version$/) {
  140. &version; exit(0);
  141. } else {
  142. &usageerr(sprintf(_g("unknown option \`%s'"), $_));
  143. }
  144. }
  145. parsechangelog($changelogfile, $changelogformat, $since);
  146. parsecontrolfile($controlfile);
  147. if (not $sourceonly) {
  148. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  149. open(FL,"< $fileslistfile") || &syserr(_g("cannot read files list file"));
  150. while(<FL>) {
  151. if (m/^(([-+.0-9a-z]+)_([^_]+)_([-\w]+)\.u?deb) (\S+) (\S+)$/) {
  152. defined($p2f{"$2 $4"}) &&
  153. warning(sprintf(_g("duplicate files list entry for package %s (line %d)"), $2, $.));
  154. $f2p{$1}= $2;
  155. $p2f{"$2 $4"}= $1;
  156. $p2f{$2}= $1;
  157. $p2ver{$2}= $3;
  158. defined($f2sec{$1}) &&
  159. warning(sprintf(_g("duplicate files list entry for file %s (line %d)"), $1, $.));
  160. $f2sec{$1}= $5;
  161. $f2pri{$1}= $6;
  162. push(@fileslistfiles,$1);
  163. } elsif (m/^([-+.0-9a-z]+_[^_]+_([-\w]+)\.[a-z0-9.]+) (\S+) (\S+)$/) {
  164. # A non-deb package
  165. $f2sec{$1}= $3;
  166. $f2pri{$1}= $4;
  167. push(@archvalues,$2) unless !$2 || $archadded{$2}++;
  168. push(@fileslistfiles,$1);
  169. } elsif (m/^([-+.,_0-9a-zA-Z]+) (\S+) (\S+)$/) {
  170. defined($f2sec{$1}) &&
  171. warning(sprintf(_g("duplicate files list entry for file %s (line %d)"), $1, $.));
  172. $f2sec{$1}= $2;
  173. $f2pri{$1}= $3;
  174. push(@fileslistfiles,$1);
  175. } else {
  176. &error(sprintf(_g("badly formed line in files list file, line %d"), $.));
  177. }
  178. }
  179. close(FL);
  180. }
  181. for $_ (keys %fi) {
  182. my $v = $fi{$_};
  183. if (s/^C //) {
  184. if (m/^Source$/) {
  185. setsourcepackage($v);
  186. }
  187. elsif (m/^Section$|^Priority$/i) { $sourcedefault{$_}= $v; }
  188. elsif (m/^Maintainer$/i) { $f{$_}= $v; }
  189. elsif (s/^X[BS]*C[BS]*-//i) { $f{$_}= $v; }
  190. elsif (m/^X[BS]+-/i ||
  191. m/^Build-(Depends|Conflicts)(-Indep)?$/i ||
  192. m/^(Standards-Version|Uploaders|Homepage|Origin|Bugs)$/i ||
  193. m/^Vcs-(Browser|Arch|Bzr|Cvs|Darcs|Git|Hg|Mtn|Svn)$/i) {
  194. }
  195. else { &unknown(_g('general section of control info file')); }
  196. } elsif (s/^C(\d+) //) {
  197. my $i = $1;
  198. my $p = $fi{"C$i Package"};
  199. my $a = $fi{"C$i Architecture"};
  200. my $host_arch = get_host_arch();
  201. if (!defined($p2f{$p}) && not $sourceonly) {
  202. if ((debarch_eq('all', $a) && !$archspecific) ||
  203. grep(debarch_is($host_arch, $_), split(/\s+/, $a))) {
  204. warning(sprintf(_g("package %s in control file but not in files list"), $p));
  205. next;
  206. }
  207. } else {
  208. my $f = $p2f{$p};
  209. $p2arch{$p}=$a;
  210. if (m/^Description$/) {
  211. $v=$` if $v =~ m/\n/;
  212. if (defined($f) && $f =~ m/\.udeb$/) {
  213. push(@descriptions,sprintf("%-10s - %-.65s (udeb)",$p,$v));
  214. } else {
  215. push(@descriptions,sprintf("%-10s - %-.65s",$p,$v));
  216. }
  217. } elsif (m/^Section$/) {
  218. $f2seccf{$f} = $v if defined($f);
  219. } elsif (m/^Priority$/) {
  220. $f2pricf{$f} = $v if defined($f);
  221. } elsif (s/^X[BS]*C[BS]*-//i) {
  222. $f{$_}= $v;
  223. } elsif (m/^Architecture$/) {
  224. if (not $sourceonly) {
  225. if (grep(debarch_is($host_arch, $_), split(/\s+/, $v))) {
  226. $v = $host_arch;
  227. } elsif (!debarch_eq('all', $v)) {
  228. $v= '';
  229. }
  230. } else {
  231. $v = '';
  232. }
  233. push(@archvalues,$v) unless !$v || $archadded{$v}++;
  234. } elsif (m/^(Package|Essential|Pre-Depends|Depends|Provides)$/ ||
  235. m/^(Recommends|Suggests|Enhances|Conflicts|Breaks|Replaces)$/ ||
  236. m/^Tag$/i ||
  237. m/^X[BS]+-/i) {
  238. } else {
  239. &unknown(_g("package's section of control info file"));
  240. }
  241. }
  242. } elsif (s/^L //) {
  243. if (m/^Source$/i) {
  244. setsourcepackage($v);
  245. } elsif (m/^Maintainer$/i) {
  246. $f{"Changed-By"}=$v;
  247. } elsif (m/^(Version|Changes|Urgency|Distribution|Date|Closes)$/i) {
  248. $f{$_}= $v;
  249. } elsif (s/^X[BS]*C[BS]*-//i) {
  250. $f{$_}= $v;
  251. } elsif (!m/^X[BS]+-/i) {
  252. &unknown(_g("parsed version of changelog"));
  253. }
  254. } elsif (m/^o:.*/) {
  255. } else {
  256. &internerr(sprintf(_g("value from nowhere, with key >%s< and value >%s<"), $_, $v));
  257. }
  258. }
  259. if ($changesdescription) {
  260. $changesdescription="./$changesdescription" if $changesdescription =~ m/^\s/;
  261. $f{'Changes'}= '';
  262. open(X,"< $changesdescription") || &syserr(_g("read changesdescription"));
  263. while(<X>) {
  264. s/\s*\n$//;
  265. $_= '.' unless m/\S/;
  266. $f{'Changes'}.= "\n $_";
  267. }
  268. }
  269. for my $p (keys %p2f) {
  270. my ($pp, $aa) = (split / /, $p);
  271. defined($p2i{"C $pp"}) ||
  272. warning(sprintf(_g("package %s listed in files list but not in control info"), $pp));
  273. }
  274. for my $p (keys %p2f) {
  275. my $f = $p2f{$p};
  276. my $sec = $f2seccf{$f};
  277. $sec = $sourcedefault{'Section'} if !defined($sec);
  278. if (!defined($sec)) {
  279. $sec = '-';
  280. warning(sprintf(_g("missing Section for binary package %s; using '-'"), $p));
  281. }
  282. $sec eq $f2sec{$f} || &error(sprintf(_g("package %s has section %s in ".
  283. "control file but %s in files list"),
  284. $p, $sec, $f2sec{$f}));
  285. my $pri = $f2pricf{$f};
  286. $pri = $sourcedefault{'Priority'} if !defined($pri);
  287. if (!defined($pri)) {
  288. $pri = '-';
  289. warning(sprintf(_g("missing Priority for binary package %s; using '-'"), $p));
  290. }
  291. $pri eq $f2pri{$f} || &error(sprintf(_g("package %s has priority %s in ".
  292. "control file but %s in files list"),
  293. $p, $pri, $f2pri{$f}));
  294. }
  295. &init_substvars;
  296. init_substvar_arch();
  297. my $origsrcmsg;
  298. if (!$binaryonly) {
  299. my $sec = $sourcedefault{'Section'};
  300. if (!defined($sec)) {
  301. $sec = '-';
  302. warning(_g("missing Section for source files"));
  303. }
  304. my $pri = $sourcedefault{'Priority'};
  305. if (!defined($pri)) {
  306. $pri = '-';
  307. warning(_g("missing Priority for source files"));
  308. }
  309. (my $sversion = $substvar{'source:Version'}) =~ s/^\d+://;
  310. $dsc= "$uploadfilesdir/${sourcepackage}_${sversion}.dsc";
  311. open(CDATA,"< $dsc") || &error(sprintf(_g("cannot open .dsc file %s: %s"), $dsc, $!));
  312. push(@sourcefiles,"${sourcepackage}_${sversion}.dsc");
  313. parsecdata(\*CDATA, 'S', -1, sprintf(_g("source control file %s"), $dsc));
  314. my $files = $fi{'S Files'};
  315. for my $file (split(/\n /, $files)) {
  316. next if $file eq '';
  317. $file =~ m/^([0-9a-f]{32})[ \t]+\d+[ \t]+([0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+)$/
  318. || &error(sprintf(_g("Files field contains bad line \`%s'"), $file));
  319. ($md5sum{$2},$file) = ($1,$2);
  320. push(@sourcefiles,$file);
  321. }
  322. for my $f (@sourcefiles) {
  323. $f2sec{$f} = $sec;
  324. $f2pri{$f} = $pri;
  325. }
  326. if (($sourcestyle =~ m/i/ && $sversion !~ m/-(0|1|0\.1)$/ ||
  327. $sourcestyle =~ m/d/) &&
  328. grep(m/\.diff\.gz$/,@sourcefiles)) {
  329. $origsrcmsg= _g("not including original source code in upload");
  330. @sourcefiles= grep(!m/\.orig\.tar\.gz$/,@sourcefiles);
  331. } else {
  332. if ($sourcestyle =~ m/d/ && !grep(m/\.diff\.gz$/,@sourcefiles)) {
  333. warning(_g("ignoring -sd option for native Debian package"));
  334. }
  335. $origsrcmsg= _g("including full source code in upload");
  336. }
  337. } else {
  338. $origsrcmsg= _g("binary-only upload - not including any source code");
  339. }
  340. print(STDERR "$progname: $origsrcmsg\n") ||
  341. &syserr(_g("write original source message")) unless $quiet;
  342. $f{'Format'}= $substvar{'Format'};
  343. if (!defined($f{'Date'})) {
  344. chop(my $date822 = `date -R`);
  345. $? && subprocerr("date -R");
  346. $f{'Date'}= $date822;
  347. }
  348. $f{'Binary'}= join(' ',grep(s/C //,keys %p2i));
  349. unshift(@archvalues,'source') unless $binaryonly;
  350. $f{'Architecture'}= join(' ',@archvalues);
  351. $f{'Description'}= "\n ".join("\n ",sort @descriptions);
  352. $f{'Files'}= '';
  353. my %filedone;
  354. for my $f (@sourcefiles, @fileslistfiles) {
  355. next if ($archspecific && debarch_eq('all', $p2arch{$f2p{$f}}));
  356. next if $filedone{$f}++;
  357. my $uf = "$uploadfilesdir/$f";
  358. open(STDIN,"< $uf") || &syserr(sprintf(_g("cannot open upload file %s for reading"), $uf));
  359. (my @s = stat(STDIN)) || syserr(sprintf(_g("cannot fstat upload file %s"), $uf));
  360. my $size = $s[7];
  361. $size || warn(sprintf(_g("upload file %s is empty"), $uf));
  362. my $md5sum = `md5sum`;
  363. $? && subprocerr(sprintf(_g("md5sum upload file %s"), $uf));
  364. $md5sum =~ m/^([0-9a-f]{32})\s*-?\s*$/i ||
  365. &failure(sprintf(_g("md5sum upload file %s gave strange output \`%s'"), $uf, $md5sum));
  366. $md5sum= $1;
  367. defined($md5sum{$f}) && $md5sum{$f} ne $md5sum &&
  368. &error(sprintf(_g("md5sum of source file %s (%s) is different ".
  369. "from md5sum in %s (%s)"),
  370. $uf, $md5sum, $dsc, $md5sum{$f}));
  371. $f{'Files'}.= "\n $md5sum $size $f2sec{$f} $f2pri{$f} $f";
  372. }
  373. $f{'Source'}= $sourcepackage;
  374. if ($f{'Version'} ne $substvar{'source:Version'}) {
  375. $f{'Source'} .= " ($substvar{'source:Version'})";
  376. }
  377. $f{'Maintainer'} = $forcemaint if defined($forcemaint);
  378. $f{'Changed-By'} = $forcechangedby if defined($forcechangedby);
  379. for my $f (qw(Version Distribution Maintainer Changes)) {
  380. defined($f{$f}) || &error(sprintf(_g("missing information for critical output field %s"), $f));
  381. }
  382. for my $f (qw(Urgency)) {
  383. defined($f{$f}) || warning(sprintf(_g("missing information for output field %s"), $f));
  384. }
  385. for my $f (keys %override) {
  386. $f{capit($f)} = $override{$f};
  387. }
  388. for my $f (keys %remove) {
  389. delete $f{capit($f)};
  390. }
  391. set_field_importance(@changes_fields);
  392. outputclose();