dpkg-gencontrol.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/usr/bin/perl
  2. $dpkglibdir= "."; # This line modified by Makefile
  3. $version= '1.3.0'; # This line modified by Makefile
  4. $controlfile= 'debian/control';
  5. $changelogfile= 'debian/changelog';
  6. $fileslistfile= 'debian/files';
  7. $varlistfile= 'debian/substvars';
  8. $packagebuilddir= 'debian/tmp';
  9. use POSIX;
  10. use POSIX qw(:errno_h);
  11. push(@INC,$dpkglibdir);
  12. require 'controllib.pl';
  13. require 'dpkg-gettext.pl';
  14. textdomain("dpkg-dev");
  15. sub version {
  16. printf _g("Debian %s version %s.\n"), $progname, $version;
  17. printf _g("
  18. Copyright (C) 1996 Ian Jackson.
  19. Copyright (C) 2000,2002 Wichert Akkerman.");
  20. printf _g("
  21. This is free software; see the GNU General Public Licence version 2 or
  22. later for copying conditions. There is NO warranty.
  23. ");
  24. }
  25. sub usage {
  26. printf _g(
  27. "Usage: %s [<option> ...]
  28. Options:
  29. -p<package> print control file for package.
  30. -c<controlfile> get control info from this file.
  31. -l<changelogfile> get per-version info from this file.
  32. -F<changelogformat> force change log format.
  33. -v<forceversion> set version of binary package.
  34. -f<fileslistfile> write files here instead of debian/files.
  35. -P<packagebuilddir> temporary build dir instead of debian/tmp.
  36. -n<filename> assume the package filename will be <filename>.
  37. -O write to stdout, not .../DEBIAN/control.
  38. -is, -ip, -isp, -ips deprecated, ignored for compatibility.
  39. -D<field>=<value> override or add a field and value.
  40. -U<field> remove a field.
  41. -V<name>=<value> set a substitution variable.
  42. -T<varlistfile> read variables here, not debian/substvars.
  43. -h, --help show this help message.
  44. --version show the version.
  45. "), $progname;
  46. }
  47. $i=100;grep($fieldimps{$_}=$i--,
  48. qw(Package Version Section Priority Architecture Essential
  49. Pre-Depends Depends Recommends Suggests Enhances Optional
  50. Conflicts Replaces Provides Installed-Size Origin Maintainer
  51. Bugs Source Description Build-Depends Build-Depends-Indep
  52. Build-Conflicts Build-Conflicts-Indep ));
  53. while (@ARGV) {
  54. $_=shift(@ARGV);
  55. if (m/^-p([-+0-9a-z.]+)$/) {
  56. $oppackage= $1;
  57. } elsif (m/^-p(.*)/) {
  58. &error(sprintf(_g("Illegal package name \`%s'"), $1));
  59. } elsif (m/^-c/) {
  60. $controlfile= $';
  61. } elsif (m/^-l/) {
  62. $changelogfile= $';
  63. } elsif (m/^-P/) {
  64. $packagebuilddir= $';
  65. } elsif (m/^-f/) {
  66. $fileslistfile= $';
  67. } elsif (m/^-v(.+)$/) {
  68. $forceversion= $1;
  69. } elsif (m/^-O$/) {
  70. $stdout= 1;
  71. } elsif (m/^-i[sp][sp]?$/) {
  72. # ignored for backwards compatibility
  73. } elsif (m/^-F([0-9a-z]+)$/) {
  74. $changelogformat=$1;
  75. } elsif (m/^-D([^\=:]+)[=:]/) {
  76. $override{$1}= $';
  77. } elsif (m/^-U([^\=:]+)$/) {
  78. $remove{$1}= 1;
  79. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  80. $substvar{$1}= $';
  81. } elsif (m/^-T/) {
  82. $varlistfile= $';
  83. } elsif (m/^-n/) {
  84. $forcefilename= $';
  85. } elsif (m/^-(h|-help)$/) {
  86. &usage; exit(0);
  87. } elsif (m/^--version$/) {
  88. &version; exit(0);
  89. } else {
  90. &usageerr(sprintf(_g("unknown option \`%s'"), $_));
  91. }
  92. }
  93. parsechangelog($changelogfile, $changelogformat);
  94. parsesubstvars($varlistfile);
  95. parsecontrolfile($controlfile);
  96. if (defined($oppackage)) {
  97. defined($p2i{"C $oppackage"}) || &error(sprintf(_g("package %s not in control info"), $oppackage));
  98. $myindex= $p2i{"C $oppackage"};
  99. } else {
  100. @packages= grep(m/^C /,keys %p2i);
  101. @packages==1 ||
  102. &error(sprintf(_g("must specify package since control info has many (%s)"), "@packages"));
  103. $myindex=1;
  104. }
  105. #print STDERR "myindex $myindex\n";
  106. my %pkg_dep_fields = map { $_ => 1 } @pkg_dep_fields;
  107. for $_ (keys %fi) {
  108. $v= $fi{$_};
  109. if (s/^C //) {
  110. #print STDERR "G key >$_< value >$v<\n";
  111. if (m/^(Origin|Bugs|Maintainer)$/) {
  112. $f{$_} = $v;
  113. } elsif (m/^Source$/) {
  114. setsourcepackage($v);
  115. }
  116. elsif (s/^X[CS]*B[CS]*-//i) { $f{$_}= $v; }
  117. elsif (m/^X[CS]+-|^(Standards-Version|Uploaders)$|^Build-(Depends|Conflicts)(-Indep)?$/i) { }
  118. elsif (m/^Section$|^Priority$/) { $spdefault{$_}= $v; }
  119. else { $_ = "C $_"; &unknown(_g('general section of control info file')); }
  120. } elsif (s/^C$myindex //) {
  121. #print STDERR "P key >$_< value >$v<\n";
  122. if (m/^(Package|Description|Essential|Optional)$/) {
  123. $f{$_}= $v;
  124. } elsif (exists($pkg_dep_fields{$_})) {
  125. } elsif (m/^Section$|^Priority$/) {
  126. $spvalue{$_}= $v;
  127. } elsif (m/^Architecture$/) {
  128. my $host_arch = get_host_arch();
  129. if (debian_arch_eq('all', $v)) {
  130. $f{$_}= $v;
  131. } elsif (debian_arch_is($host_arch, $v)) {
  132. $f{$_} = $host_arch;
  133. } else {
  134. @archlist= split(/\s+/,$v);
  135. my @invalid_archs = grep m/[^\w-]/, @archlist;
  136. warning(sprintf(ngettext(
  137. "`%s' is not a legal architecture string.",
  138. "`%s' are not legal architecture strings.",
  139. scalar(@invalid_archs)),
  140. join("' `", @invalid_archs)))
  141. if @invalid_archs >= 1;
  142. grep(debian_arch_is($host_arch, $_), @archlist) ||
  143. &error(sprintf(_g("current build architecture %s does not".
  144. " appear in package's list (%s)"),
  145. $host_arch, "@archlist"));
  146. $f{$_} = $host_arch;
  147. }
  148. } elsif (s/^X[CS]*B[CS]*-//i) {
  149. $f{$_}= $v;
  150. } elsif (!m/^X[CS]+-/i) {
  151. $_ = "C$myindex $_"; &unknown(_g("package's section of control info file"));
  152. }
  153. } elsif (m/^C\d+ /) {
  154. #print STDERR "X key >$_< value not shown<\n";
  155. } elsif (s/^L //) {
  156. #print STDERR "L key >$_< value >$v<\n";
  157. if (m/^Source$/) {
  158. setsourcepackage($v);
  159. } elsif (m/^Version$/) {
  160. $sourceversion= $v;
  161. $f{$_} = $v unless defined($forceversion);
  162. } elsif (m/^(Maintainer|Changes|Urgency|Distribution|Date|Closes)$/) {
  163. } elsif (s/^X[CS]*B[CS]*-//i) {
  164. $f{$_}= $v;
  165. } elsif (!m/^X[CS]+-/i) {
  166. $_ = "L $_"; &unknown(_g("parsed version of changelog"));
  167. }
  168. } elsif (m/o:/) {
  169. } else {
  170. &internerr(sprintf(_g("value from nowhere, with key >%s< and value >%s<"), $_, $v));
  171. }
  172. }
  173. $f{'Version'} = $forceversion if defined($forceversion);
  174. &init_substvars;
  175. init_substvar_arch();
  176. for $_ (keys %fi) {
  177. $v= $fi{$_};
  178. if (s/^C //) {
  179. } elsif (s/^C$myindex //) {
  180. if (m/^(Package|Description|Essential|Optional)$/) {
  181. } elsif (exists($pkg_dep_fields{$_})) {
  182. my $dep = parsedep(substvars($v), 1, 1);
  183. &error(sprintf(_g("error occurred while parsing %s"), $_)) unless defined $dep;
  184. $f{$_}= showdep($dep, 0);
  185. } elsif (m/^Section$|^Priority$/) {
  186. } elsif (m/^Architecture$/) {
  187. } elsif (s/^X[CS]*B[CS]*-//i) {
  188. } elsif (!m/^X[CS]+-/i) {
  189. }
  190. } elsif (m/^C\d+ /) {
  191. } elsif (s/^L //) {
  192. } elsif (m/o:/) {
  193. } else {
  194. }
  195. }
  196. for $f (qw(Section Priority)) {
  197. $spvalue{$f} = $spdefault{$f} unless defined($spvalue{$f});
  198. $f{$f} = $spvalue{$f} if defined($spvalue{$f});
  199. }
  200. for $f (qw(Package Version)) {
  201. defined($f{$f}) || &error(sprintf(_g("missing information for output field %s"), $f));
  202. }
  203. for $f (qw(Maintainer Description Architecture)) {
  204. defined($f{$f}) || warning(sprintf(_g("missing information for output field %s"), $f));
  205. }
  206. $oppackage= $f{'Package'};
  207. $verdiff = $f{'Version'} ne $substvar{'source:Version'} or
  208. $f{'Version'} ne $sourceversion;
  209. if ($oppackage ne $sourcepackage || $verdiff) {
  210. $f{'Source'}= $sourcepackage;
  211. $f{'Source'}.= " ($substvar{'source:Version'})" if $verdiff;
  212. }
  213. if (!defined($substvar{'Installed-Size'})) {
  214. defined($c= open(DU,"-|")) || &syserr(_g("fork for du"));
  215. if (!$c) {
  216. chdir("$packagebuilddir") || &syserr(sprintf(_g("chdir for du to \`%s'"), $packagebuilddir));
  217. exec("du","-k","-s",".") or &syserr(_g("exec du"));
  218. }
  219. $duo=''; while (<DU>) { $duo.=$_; }
  220. close(DU); $? && &subprocerr(sprintf(_g("du in \`%s'"), $packagebuilddir));
  221. $duo =~ m/^(\d+)\s+\.$/ || &failure(sprintf(_g("du gave unexpected output \`%s'"), $duo));
  222. $substvar{'Installed-Size'}= $1;
  223. }
  224. if (defined($substvar{'Extra-Size'})) {
  225. $substvar{'Installed-Size'} += $substvar{'Extra-Size'};
  226. }
  227. if (defined($substvar{'Installed-Size'})) {
  228. $f{'Installed-Size'}= $substvar{'Installed-Size'};
  229. }
  230. for $f (keys %override) { $f{&capit($f)}= $override{$f}; }
  231. for $f (keys %remove) { delete $f{&capit($f)}; }
  232. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  233. open(Y,"> $fileslistfile.new") || &syserr(_g("open new files list file"));
  234. binmode(Y);
  235. chown(getfowner(), "$fileslistfile.new")
  236. || &syserr(_g("chown new files list file"));
  237. if (open(X,"< $fileslistfile")) {
  238. binmode(X);
  239. while (<X>) {
  240. chomp;
  241. next if m/^([-+0-9a-z.]+)_[^_]+_([\w-]+)\.deb /
  242. && ($1 eq $oppackage)
  243. && (debian_arch_eq($2, $f{'Architecture'})
  244. || debian_arch_eq($2, 'all'));
  245. print(Y "$_\n") || &syserr(_g("copy old entry to new files list file"));
  246. }
  247. close(X) || &syserr(_g("close old files list file"));
  248. } elsif ($! != ENOENT) {
  249. &syserr(_g("read old files list file"));
  250. }
  251. $sversion=$f{'Version'};
  252. $sversion =~ s/^\d+://;
  253. $forcefilename=sprintf("%s_%s_%s.deb", $oppackage,$sversion,$f{'Architecture'})
  254. unless ($forcefilename);
  255. print(Y &substvars(sprintf("%s %s %s\n", $forcefilename,
  256. &spfileslistvalue('Section'), &spfileslistvalue('Priority'))))
  257. || &syserr(_g("write new entry to new files list file"));
  258. close(Y) || &syserr(_g("close new files list file"));
  259. rename("$fileslistfile.new",$fileslistfile) || &syserr(_g("install new files list file"));
  260. if (!$stdout) {
  261. $cf= "$packagebuilddir/DEBIAN/control";
  262. $cf= "./$cf" if $cf =~ m/^\s/;
  263. open(STDOUT,"> $cf.new") ||
  264. &syserr(sprintf(_g("cannot open new output control file \`%s'"), "$cf.new"));
  265. binmode(STDOUT);
  266. }
  267. outputclose($varlistfile);
  268. if (!$stdout) {
  269. rename("$cf.new","$cf") || &syserr(sprintf(_g("cannot install output control file \`%s'"), $cf));
  270. }
  271. sub spfileslistvalue {
  272. $r= $spvalue{$_[0]};
  273. $r = '-' if !defined($r);
  274. return $r;
  275. }