dpkg-gencontrol.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-gencontrol
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. use strict;
  18. use warnings;
  19. use POSIX;
  20. use POSIX qw(:errno_h);
  21. use Dpkg;
  22. use Dpkg::Gettext;
  23. use Dpkg::ErrorHandling;
  24. use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is);
  25. use Dpkg::Deps;
  26. use Dpkg::Control;
  27. use Dpkg::Control::Info;
  28. use Dpkg::Control::Fields;
  29. use Dpkg::Substvars;
  30. use Dpkg::Vars;
  31. use Dpkg::Changelog::Parse;
  32. textdomain("dpkg-dev");
  33. my $controlfile = 'debian/control';
  34. my $changelogfile = 'debian/changelog';
  35. my $changelogformat;
  36. my $fileslistfile = 'debian/files';
  37. my $packagebuilddir = 'debian/tmp';
  38. my $sourceversion;
  39. my $forceversion;
  40. my $forcefilename;
  41. my $stdout;
  42. my %remove;
  43. my %override;
  44. my $oppackage;
  45. my $substvars = Dpkg::Substvars->new();
  46. my $substvars_loaded = 0;
  47. sub version {
  48. printf _g("Debian %s version %s.\n"), $progname, $version;
  49. printf _g("
  50. Copyright (C) 1996 Ian Jackson.
  51. Copyright (C) 2000,2002 Wichert Akkerman.");
  52. printf _g("
  53. This is free software; see the GNU General Public License version 2 or
  54. later for copying conditions. There is NO warranty.
  55. ");
  56. }
  57. sub usage {
  58. printf _g(
  59. "Usage: %s [<option> ...]
  60. Options:
  61. -p<package> print control file for package.
  62. -c<controlfile> get control info from this file.
  63. -l<changelogfile> get per-version info from this file.
  64. -F<changelogformat> force change log format.
  65. -v<forceversion> set version of binary package.
  66. -f<fileslistfile> write files here instead of debian/files.
  67. -P<packagebuilddir> temporary build dir instead of debian/tmp.
  68. -n<filename> assume the package filename will be <filename>.
  69. -O write to stdout, not .../DEBIAN/control.
  70. -is, -ip, -isp, -ips deprecated, ignored for compatibility.
  71. -D<field>=<value> override or add a field and value.
  72. -U<field> remove a field.
  73. -V<name>=<value> set a substitution variable.
  74. -T<varlistfile> read variables here, not debian/substvars.
  75. -h, --help show this help message.
  76. --version show the version.
  77. "), $progname;
  78. }
  79. while (@ARGV) {
  80. $_=shift(@ARGV);
  81. if (m/^-p([-+0-9a-z.]+)$/) {
  82. $oppackage= $1;
  83. } elsif (m/^-p(.*)/) {
  84. error(_g("Illegal package name \`%s'"), $1);
  85. } elsif (m/^-c/) {
  86. $controlfile= $';
  87. } elsif (m/^-l/) {
  88. $changelogfile= $';
  89. } elsif (m/^-P/) {
  90. $packagebuilddir= $';
  91. } elsif (m/^-f/) {
  92. $fileslistfile= $';
  93. } elsif (m/^-v(.+)$/) {
  94. $forceversion= $1;
  95. } elsif (m/^-O$/) {
  96. $stdout= 1;
  97. } elsif (m/^-i[sp][sp]?$/) {
  98. # ignored for backwards compatibility
  99. } elsif (m/^-F([0-9a-z]+)$/) {
  100. $changelogformat=$1;
  101. } elsif (m/^-D([^\=:]+)[=:]/) {
  102. $override{$1}= $';
  103. } elsif (m/^-U([^\=:]+)$/) {
  104. $remove{$1}= 1;
  105. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  106. $substvars->set($1, $');
  107. $substvars->no_warn($1);
  108. } elsif (m/^-T(.*)$/) {
  109. $substvars->load($1) if -e $1;
  110. $substvars_loaded = 1;
  111. } elsif (m/^-n/) {
  112. $forcefilename= $';
  113. } elsif (m/^-(h|-help)$/) {
  114. usage();
  115. exit(0);
  116. } elsif (m/^--version$/) {
  117. version();
  118. exit(0);
  119. } else {
  120. usageerr(_g("unknown option \`%s'"), $_);
  121. }
  122. }
  123. umask 0022; # ensure sane default permissions for created files
  124. my %options = (file => $changelogfile);
  125. $options{"changelogformat"} = $changelogformat if $changelogformat;
  126. my $changelog = changelog_parse(%options);
  127. $substvars->set_version_substvars($changelog->{"Version"});
  128. $substvars->set_arch_substvars();
  129. $substvars->load("debian/substvars") if -e "debian/substvars" and not $substvars_loaded;
  130. $substvars->set("binary:Version", $forceversion) if defined $forceversion;
  131. my $control = Dpkg::Control::Info->new($controlfile);
  132. my $fields = Dpkg::Control->new(type => CTRL_PKG_DEB);
  133. my $pkg;
  134. if (defined($oppackage)) {
  135. $pkg = $control->get_pkg_by_name($oppackage);
  136. defined($pkg) || error(_g("package %s not in control info"), $oppackage);
  137. } else {
  138. my @packages = map { $_->{'Package'} } $control->get_packages();
  139. if (@packages == 0) {
  140. error(_g("no package stanza found in control info"));
  141. } elsif (@packages > 1) {
  142. error(_g("must specify package since control info has many (%s)"),
  143. "@packages");
  144. }
  145. $pkg = $control->get_pkg_by_idx(1);
  146. }
  147. $substvars->set_msg_prefix(sprintf(_g("package %s: "), $pkg->{Package}));
  148. # Scan source package
  149. my $src_fields = $control->get_source();
  150. foreach $_ (keys %{$src_fields}) {
  151. if (m/^Source$/) {
  152. set_source_package($src_fields->{$_});
  153. } else {
  154. field_transfer_single($src_fields, $fields);
  155. }
  156. }
  157. # Scan binary package
  158. foreach $_ (keys %{$pkg}) {
  159. my $v = $pkg->{$_};
  160. if (field_get_dep_type($_)) {
  161. # Delay the parsing until later
  162. } elsif (m/^Architecture$/) {
  163. my $host_arch = get_host_arch();
  164. if (debarch_eq('all', $v)) {
  165. $fields->{$_} = $v;
  166. } else {
  167. my @archlist = split(/\s+/, $v);
  168. my @invalid_archs = grep m/[^\w-]/, @archlist;
  169. warning(ngettext("`%s' is not a legal architecture string.",
  170. "`%s' are not legal architecture strings.",
  171. scalar(@invalid_archs)),
  172. join("' `", @invalid_archs))
  173. if @invalid_archs >= 1;
  174. grep(debarch_is($host_arch, $_), @archlist) ||
  175. error(_g("current host architecture '%s' does not " .
  176. "appear in package's architecture list (%s)"),
  177. $host_arch, "@archlist");
  178. $fields->{$_} = $host_arch;
  179. }
  180. } else {
  181. field_transfer_single($pkg, $fields);
  182. }
  183. }
  184. # Scan fields of dpkg-parsechangelog
  185. foreach $_ (keys %{$changelog}) {
  186. my $v = $changelog->{$_};
  187. if (m/^Source$/) {
  188. set_source_package($v);
  189. } elsif (m/^Version$/) {
  190. $sourceversion = $v;
  191. $fields->{$_} = $v unless defined($forceversion);
  192. } elsif (m/^Maintainer$/) {
  193. # That field must not be copied from changelog even if it's
  194. # allowed in the binary package control information
  195. } else {
  196. field_transfer_single($changelog, $fields);
  197. }
  198. }
  199. $fields->{'Version'} = $forceversion if defined($forceversion);
  200. # Process dependency fields in a second pass, now that substvars have been
  201. # initialized.
  202. my $facts = Dpkg::Deps::KnownFacts->new();
  203. $facts->add_installed_package($fields->{'Package'}, $fields->{'Version'});
  204. if (exists $pkg->{"Provides"}) {
  205. my $provides = deps_parse($substvars->substvars($pkg->{"Provides"}, no_warn => 1),
  206. reduce_arch => 1, union => 1);
  207. if (defined $provides) {
  208. foreach my $subdep ($provides->get_deps()) {
  209. if ($subdep->isa('Dpkg::Deps::Simple')) {
  210. $facts->add_provided_package($subdep->{package},
  211. $subdep->{relation}, $subdep->{version},
  212. $fields->{'Package'});
  213. }
  214. }
  215. }
  216. }
  217. my (@seen_deps);
  218. foreach my $field (field_list_pkg_dep()) {
  219. # Arch: all can't be simplified as the host architecture is not known
  220. my $reduce_arch = debarch_eq('all', $pkg->{Architecture} || "all") ? 0 : 1;
  221. if (exists $pkg->{$field}) {
  222. my $dep;
  223. my $field_value = $substvars->substvars($pkg->{$field},
  224. msg_prefix => sprintf(_g("%s field of package %s: "), $field, $pkg->{Package}));
  225. if (field_get_dep_type($field) eq 'normal') {
  226. $dep = deps_parse($field_value, use_arch => 1,
  227. reduce_arch => $reduce_arch);
  228. error(_g("error occurred while parsing %s field: %s"), $field,
  229. $field_value) unless defined $dep;
  230. $dep->simplify_deps($facts, @seen_deps);
  231. # Remember normal deps to simplify even further weaker deps
  232. push @seen_deps, $dep;
  233. } else {
  234. $dep = deps_parse($field_value, use_arch => 1,
  235. reduce_arch => $reduce_arch, union => 1);
  236. error(_g("error occurred while parsing %s field: %s"), $field,
  237. $field_value) unless defined $dep;
  238. $dep->simplify_deps($facts);
  239. $dep->sort();
  240. }
  241. error(_g("the %s field contains an arch-specific dependency but the " .
  242. "package is architecture all"), $field)
  243. if $dep->has_arch_restriction();
  244. $fields->{$field} = $dep->output();
  245. delete $fields->{$field} unless $fields->{$field}; # Delete empty field
  246. }
  247. }
  248. for my $f (qw(Package Version)) {
  249. defined($fields->{$f}) || error(_g("missing information for output field %s"), $f);
  250. }
  251. for my $f (qw(Maintainer Description Architecture)) {
  252. defined($fields->{$f}) || warning(_g("missing information for output field %s"), $f);
  253. }
  254. $oppackage = $fields->{'Package'};
  255. my $pkg_type = $pkg->{'Package-Type'} ||
  256. $pkg->get_custom_field('Package-Type') || 'deb';
  257. if ($pkg_type eq 'udeb') {
  258. delete $fields->{'Package-Type'};
  259. delete $fields->{'Homepage'};
  260. } else {
  261. for my $f (qw(Subarchitecture Kernel-Version Installer-Menu-Item)) {
  262. warning(_g("%s package with udeb specific field %s"), $pkg_type, $f)
  263. if defined($fields->{$f});
  264. }
  265. }
  266. my $verdiff = $fields->{'Version'} ne $substvars->get('source:Version') ||
  267. $fields->{'Version'} ne $sourceversion;
  268. if ($oppackage ne $sourcepackage || $verdiff) {
  269. $fields->{'Source'} = $sourcepackage;
  270. $fields->{'Source'} .= " (" . $substvars->get('source:Version') . ")" if $verdiff;
  271. }
  272. if (!defined($substvars->get('Installed-Size'))) {
  273. defined(my $c = open(DU, "-|")) || syserr(_g("cannot fork for %s"), "du");
  274. if (!$c) {
  275. chdir("$packagebuilddir") ||
  276. syserr(_g("chdir for du to \`%s'"), $packagebuilddir);
  277. exec("du", "-k", "-s", "--apparent-size", ".") or
  278. syserr(_g("unable to execute %s"), "du");
  279. }
  280. my $duo = '';
  281. while (<DU>) {
  282. $duo .= $_;
  283. }
  284. close(DU);
  285. $? && subprocerr(_g("du in \`%s'"), $packagebuilddir);
  286. $duo =~ m/^(\d+)\s+\.$/ ||
  287. error(_g("du gave unexpected output \`%s'"), $duo);
  288. $substvars->set('Installed-Size', $1);
  289. }
  290. if (defined($substvars->get('Extra-Size'))) {
  291. my $size = $substvars->get('Extra-Size') + $substvars->get('Installed-Size');
  292. $substvars->set('Installed-Size', $size);
  293. }
  294. if (defined($substvars->get('Installed-Size'))) {
  295. $fields->{'Installed-Size'} = $substvars->get('Installed-Size');
  296. }
  297. $substvars->no_warn('Installed-Size');
  298. for my $f (keys %override) {
  299. $fields->{$f} = $override{$f};
  300. }
  301. for my $f (keys %remove) {
  302. delete $fields->{$f};
  303. }
  304. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  305. open(Y, ">", "$fileslistfile.new") || syserr(_g("open new files list file"));
  306. binmode(Y);
  307. if (open(X, "<", $fileslistfile)) {
  308. binmode(X);
  309. while (<X>) {
  310. chomp;
  311. next if m/^([-+0-9a-z.]+)_[^_]+_([\w-]+)\.(a-z+) /
  312. && ($1 eq $oppackage)
  313. && ($3 eq $pkg_type)
  314. && (debarch_eq($2, $fields->{'Architecture'} || "")
  315. || debarch_eq($2, 'all'));
  316. print(Y "$_\n") || syserr(_g("copy old entry to new files list file"));
  317. }
  318. close(X) || syserr(_g("close old files list file"));
  319. } elsif ($! != ENOENT) {
  320. syserr(_g("read old files list file"));
  321. }
  322. my $sversion = $fields->{'Version'};
  323. $sversion =~ s/^\d+://;
  324. $forcefilename = sprintf("%s_%s_%s.%s", $oppackage, $sversion,
  325. $fields->{'Architecture'} || "", $pkg_type)
  326. unless ($forcefilename);
  327. print(Y $substvars->substvars(sprintf("%s %s %s\n", $forcefilename,
  328. $fields->{'Section'} || '-',
  329. $fields->{'Priority'} || '-')))
  330. || syserr(_g("write new entry to new files list file"));
  331. close(Y) || syserr(_g("close new files list file"));
  332. rename("$fileslistfile.new", $fileslistfile) || syserr(_g("install new files list file"));
  333. my $cf;
  334. my $fh_output;
  335. if (!$stdout) {
  336. $cf= "$packagebuilddir/DEBIAN/control";
  337. $cf= "./$cf" if $cf =~ m/^\s/;
  338. open($fh_output, ">", "$cf.new") ||
  339. syserr(_g("cannot open new output control file \`%s'"), "$cf.new");
  340. } else {
  341. $fh_output = \*STDOUT;
  342. }
  343. $fields->apply_substvars($substvars);
  344. $fields->output($fh_output);
  345. if (!$stdout) {
  346. close($fh_output) || syserr(_g("cannot close %s"), "$cf.new");
  347. rename("$cf.new", "$cf") ||
  348. syserr(_g("cannot install output control file \`%s'"), $cf);
  349. }
  350. $substvars->warn_about_unused();