dpkg-gencontrol.pl 12 KB

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