dpkg-gencontrol.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. @packages==1 ||
  140. error(_g("must specify package since control info has many (%s)"),
  141. "@packages");
  142. $pkg = $control->get_pkg_by_idx(1);
  143. }
  144. $substvars->set_msg_prefix(sprintf(_g("package %s: "), $pkg->{Package}));
  145. # Scan source package
  146. my $src_fields = $control->get_source();
  147. foreach $_ (keys %{$src_fields}) {
  148. if (m/^Source$/) {
  149. set_source_package($src_fields->{$_});
  150. } else {
  151. field_transfer_single($src_fields, $fields);
  152. }
  153. }
  154. # Scan binary package
  155. foreach $_ (keys %{$pkg}) {
  156. my $v = $pkg->{$_};
  157. if (field_get_dep_type($_)) {
  158. # Delay the parsing until later
  159. } elsif (m/^Architecture$/) {
  160. my $host_arch = get_host_arch();
  161. if (debarch_eq('all', $v)) {
  162. $fields->{$_} = $v;
  163. } else {
  164. my @archlist = split(/\s+/, $v);
  165. my @invalid_archs = grep m/[^\w-]/, @archlist;
  166. warning(ngettext("`%s' is not a legal architecture string.",
  167. "`%s' are not legal architecture strings.",
  168. scalar(@invalid_archs)),
  169. join("' `", @invalid_archs))
  170. if @invalid_archs >= 1;
  171. grep(debarch_is($host_arch, $_), @archlist) ||
  172. error(_g("current host architecture '%s' does not " .
  173. "appear in package's architecture list (%s)"),
  174. $host_arch, "@archlist");
  175. $fields->{$_} = $host_arch;
  176. }
  177. } else {
  178. field_transfer_single($pkg, $fields);
  179. }
  180. }
  181. # Scan fields of dpkg-parsechangelog
  182. foreach $_ (keys %{$changelog}) {
  183. my $v = $changelog->{$_};
  184. if (m/^Source$/) {
  185. set_source_package($v);
  186. } elsif (m/^Version$/) {
  187. $sourceversion = $v;
  188. $fields->{$_} = $v unless defined($forceversion);
  189. } elsif (m/^Maintainer$/) {
  190. # That field must not be copied from changelog even if it's
  191. # allowed in the binary package control information
  192. } else {
  193. field_transfer_single($changelog, $fields);
  194. }
  195. }
  196. $fields->{'Version'} = $forceversion if defined($forceversion);
  197. # Process dependency fields in a second pass, now that substvars have been
  198. # initialized.
  199. my $facts = Dpkg::Deps::KnownFacts->new();
  200. $facts->add_installed_package($fields->{'Package'}, $fields->{'Version'});
  201. if (exists $pkg->{"Provides"}) {
  202. my $provides = deps_parse($substvars->substvars($pkg->{"Provides"}, no_warn => 1),
  203. reduce_arch => 1, union => 1);
  204. if (defined $provides) {
  205. foreach my $subdep ($provides->get_deps()) {
  206. if ($subdep->isa('Dpkg::Deps::Simple')) {
  207. $facts->add_provided_package($subdep->{package},
  208. $subdep->{relation}, $subdep->{version},
  209. $fields->{'Package'});
  210. }
  211. }
  212. }
  213. }
  214. my (@seen_deps);
  215. foreach my $field (field_list_pkg_dep()) {
  216. # Arch: all can't be simplified as the host architecture is not known
  217. my $reduce_arch = debarch_eq('all', $pkg->{Architecture} || "all") ? 0 : 1;
  218. if (exists $pkg->{$field}) {
  219. my $dep;
  220. my $field_value = $substvars->substvars($pkg->{$field},
  221. msg_prefix => sprintf(_g("%s field of package %s: "), $field, $pkg->{Package}));
  222. if (field_get_dep_type($field) eq 'normal') {
  223. $dep = deps_parse($field_value, use_arch => 1,
  224. reduce_arch => $reduce_arch);
  225. error(_g("error occurred while parsing %s field: %s"), $field,
  226. $field_value) unless defined $dep;
  227. $dep->simplify_deps($facts, @seen_deps);
  228. # Remember normal deps to simplify even further weaker deps
  229. push @seen_deps, $dep;
  230. } else {
  231. $dep = deps_parse($field_value, use_arch => 1,
  232. reduce_arch => $reduce_arch, union => 1);
  233. error(_g("error occurred while parsing %s field: %s"), $field,
  234. $field_value) unless defined $dep;
  235. $dep->simplify_deps($facts);
  236. $dep->sort();
  237. }
  238. error(_g("the %s field contains an arch-specific dependency but the " .
  239. "package is architecture all"), $field)
  240. if $dep->has_arch_restriction();
  241. $fields->{$field} = $dep->output();
  242. delete $fields->{$field} unless $fields->{$field}; # Delete empty field
  243. }
  244. }
  245. for my $f (qw(Package Version)) {
  246. defined($fields->{$f}) || error(_g("missing information for output field %s"), $f);
  247. }
  248. for my $f (qw(Maintainer Description Architecture)) {
  249. defined($fields->{$f}) || warning(_g("missing information for output field %s"), $f);
  250. }
  251. $oppackage = $fields->{'Package'};
  252. my $pkg_type = $pkg->{'Package-Type'} ||
  253. $pkg->get_custom_field('Package-Type') || 'deb';
  254. if ($pkg_type eq 'udeb') {
  255. delete $fields->{'Package-Type'};
  256. delete $fields->{'Homepage'};
  257. } else {
  258. for my $f (qw(Subarchitecture Kernel-Version Installer-Menu-Item)) {
  259. warning(_g("%s package with udeb specific field %s"), $pkg_type, $f)
  260. if defined($fields->{$f});
  261. }
  262. }
  263. my $verdiff = $fields->{'Version'} ne $substvars->get('source:Version') ||
  264. $fields->{'Version'} ne $sourceversion;
  265. if ($oppackage ne $sourcepackage || $verdiff) {
  266. $fields->{'Source'} = $sourcepackage;
  267. $fields->{'Source'} .= " (" . $substvars->get('source:Version') . ")" if $verdiff;
  268. }
  269. if (!defined($substvars->get('Installed-Size'))) {
  270. defined(my $c = open(DU, "-|")) || syserr(_g("cannot fork for %s"), "du");
  271. if (!$c) {
  272. chdir("$packagebuilddir") ||
  273. syserr(_g("chdir for du to \`%s'"), $packagebuilddir);
  274. exec("du", "-k", "-s", ".") or syserr(_g("exec %s"), "du");
  275. }
  276. my $duo = '';
  277. while (<DU>) {
  278. $duo .= $_;
  279. }
  280. close(DU);
  281. $? && subprocerr(_g("du in \`%s'"), $packagebuilddir);
  282. $duo =~ m/^(\d+)\s+\.$/ ||
  283. error(_g("du gave unexpected output \`%s'"), $duo);
  284. $substvars->set('Installed-Size', $1);
  285. }
  286. if (defined($substvars->get('Extra-Size'))) {
  287. my $size = $substvars->get('Extra-Size') + $substvars->get('Installed-Size');
  288. $substvars->set('Installed-Size', $size);
  289. }
  290. if (defined($substvars->get('Installed-Size'))) {
  291. $fields->{'Installed-Size'} = $substvars->get('Installed-Size');
  292. }
  293. $substvars->no_warn('Installed-Size');
  294. for my $f (keys %override) {
  295. $fields->{$f} = $override{$f};
  296. }
  297. for my $f (keys %remove) {
  298. delete $fields->{$f};
  299. }
  300. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  301. open(Y, ">", "$fileslistfile.new") || syserr(_g("open new files list file"));
  302. binmode(Y);
  303. if (open(X, "<", $fileslistfile)) {
  304. binmode(X);
  305. while (<X>) {
  306. chomp;
  307. next if m/^([-+0-9a-z.]+)_[^_]+_([\w-]+)\.(a-z+) /
  308. && ($1 eq $oppackage)
  309. && ($3 eq $pkg_type)
  310. && (debarch_eq($2, $fields->{'Architecture'} || "")
  311. || debarch_eq($2, 'all'));
  312. print(Y "$_\n") || syserr(_g("copy old entry to new files list file"));
  313. }
  314. close(X) || syserr(_g("close old files list file"));
  315. } elsif ($! != ENOENT) {
  316. syserr(_g("read old files list file"));
  317. }
  318. my $sversion = $fields->{'Version'};
  319. $sversion =~ s/^\d+://;
  320. $forcefilename = sprintf("%s_%s_%s.%s", $oppackage, $sversion,
  321. $fields->{'Architecture'} || "", $pkg_type)
  322. unless ($forcefilename);
  323. print(Y $substvars->substvars(sprintf("%s %s %s\n", $forcefilename,
  324. $fields->{'Section'} || '-',
  325. $fields->{'Priority'} || '-')))
  326. || syserr(_g("write new entry to new files list file"));
  327. close(Y) || syserr(_g("close new files list file"));
  328. rename("$fileslistfile.new", $fileslistfile) || syserr(_g("install new files list file"));
  329. my $cf;
  330. my $fh_output;
  331. if (!$stdout) {
  332. $cf= "$packagebuilddir/DEBIAN/control";
  333. $cf= "./$cf" if $cf =~ m/^\s/;
  334. open($fh_output, ">", "$cf.new") ||
  335. syserr(_g("cannot open new output control file \`%s'"), "$cf.new");
  336. } else {
  337. $fh_output = \*STDOUT;
  338. }
  339. $fields->apply_substvars($substvars);
  340. $fields->output($fh_output);
  341. if (!$stdout) {
  342. close($fh_output);
  343. rename("$cf.new", "$cf") ||
  344. syserr(_g("cannot install output control file \`%s'"), $cf);
  345. }
  346. $substvars->warn_about_unused();