dpkg-genchanges.pl 15 KB

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