dpkg-gencontrol.pl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/perl
  2. $dpkglibdir= ".";
  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. sub usageversion {
  14. print STDERR
  15. "Debian GNU/Linux dpkg-gencontrol $version. Copyright (C) 1996
  16. Ian Jackson. This is free software; see the GNU General Public Licence
  17. version 2 or later for copying conditions. There is NO warranty.
  18. Usage: dpkg-gencontrol [options ...]
  19. Options: -p<package> print control file for package
  20. -c<controlfile> get control info from this file
  21. -l<changelogfile> get per-version info from this file
  22. -F<changelogformat> force change log format
  23. -v<forceversion> set version of binary package
  24. -f<fileslistfile> write files here instead of debian/files
  25. -P<packagebuilddir> temporary build dir instead of debian/tmp
  26. -O write to stdout, not .../DEBIAN/control
  27. -is include section field
  28. -ip include priority field
  29. -isp|-ips include both section and priority
  30. -D<field>=<value> override or add a field and value
  31. -U<field> remove a field
  32. -V<name>=<value> set a substitution variable
  33. -T<varlistfile> read variables here, not debian/substvars
  34. -h print this message
  35. ";
  36. }
  37. $i=100;grep($fieldimps{$_}=$i--,
  38. qw(Package Version Section Priority Architecture Essential
  39. Pre-Depends Depends Recommends Suggests Optional Conflicts Replaces
  40. Provides Installed-Size Maintainer Source Description Build-Depends
  41. Build-Indep-Depends Build-Conflicts Build-Indep-Conflicts));
  42. while (@ARGV) {
  43. $_=shift(@ARGV);
  44. if (m/^-p([-+0-9a-z.]+)$/) {
  45. $oppackage= $1;
  46. } elsif (m/^-c/) {
  47. $controlfile= $';
  48. } elsif (m/^-l/) {
  49. $changelogfile= $';
  50. } elsif (m/^-P/) {
  51. $packagebuilddir= $';
  52. } elsif (m/^-f/) {
  53. $fileslistfile= $';
  54. } elsif (m/^-v(.+)$/) {
  55. $forceversion= $1;
  56. } elsif (m/^-O$/) {
  57. $stdout= 1;
  58. } elsif (m/^-is$/) {
  59. $spinclude{'Section'}=1;
  60. } elsif (m/^-ip$/) {
  61. $spinclude{'Priority'}=1;
  62. } elsif (m/^-isp$/ || m/^-ips$/) {
  63. $spinclude{'Section'}=1;
  64. $spinclude{'Priority'}=1;
  65. } elsif (m/^-F([0-9a-z]+)$/) {
  66. $changelogformat=$1;
  67. } elsif (m/^-D([^\=:]+)[=:]/) {
  68. $override{$1}= $';
  69. } elsif (m/^-U([^\=:]+)$/) {
  70. $remove{$1}= 1;
  71. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  72. $substvar{$1}= $';
  73. } elsif (m/^-T/) {
  74. $varlistfile= $';
  75. } elsif (m/^-h$/) {
  76. &usageversion; exit(0);
  77. } else {
  78. &usageerr("unknown option \`$_'");
  79. }
  80. }
  81. $arch = $override{Architecture} or do {
  82. if (exists $ENV{DEB_HOST_ARCH}) {
  83. $arch=$ENV{DEB_HOST_ARCH};
  84. } else {
  85. $arch=`dpkg --print-architecture`;
  86. $? && &subprocerr("dpkg --print-architecture");
  87. }
  88. };
  89. chomp $arch;
  90. &parsechangelog;
  91. &parsecontrolfile;
  92. if (length($oppackage)) {
  93. defined($p2i{"C $oppackage"}) || &error("package $oppackage not in control info");
  94. $myindex= $p2i{"C $oppackage"};
  95. } else {
  96. @packages= grep(m/^C /,keys %p2i);
  97. @packages==1 ||
  98. &error("must specify package since control info has many (@packages)");
  99. $myindex=1;
  100. }
  101. #print STDERR "myindex $myindex\n";
  102. for $_ (keys %fi) {
  103. $v= $fi{$_};
  104. if (s/^C //) {
  105. #print STDERR "G key >$_< value >$v<\n";
  106. if (m/^Maintainer$/) { $f{$_}=$v; }
  107. elsif (m/^Source$/) { &setsourcepackage; }
  108. elsif (s/^X[CS]*B[CS]*-//i) { $f{$_}= $v; }
  109. elsif (m/^X[CS]+-|^Standards-Version$|^Build-(Indep-)?(Depends|Conflicts)$/i) { }
  110. elsif (m/^Section$|^Priority$/) { $spdefault{$_}= $v; }
  111. else { &unknown('general section of control info file'); }
  112. } elsif (s/^C$myindex //) {
  113. #print STDERR "P key >$_< value >$v<\n";
  114. if (m/^(Package|Description|Essential|Pre-Depends|Depends)$/ ||
  115. m/^(Recommends|Suggests|Optional|Conflicts|Provides|Replaces)$/) {
  116. $f{$_}= $v;
  117. } elsif (m/^Section$|^Priority$/) {
  118. $spvalue{$_}= $v;
  119. } elsif (m/^Architecture$/) {
  120. if ($v eq 'all') {
  121. $f{$_}= $v;
  122. } elsif ($v eq 'any') {
  123. $f{$_}= $arch;
  124. } else {
  125. @archlist= split(/\s+/,$v);
  126. grep($arch eq $_, @archlist) ||
  127. &error("current build architecture $arch does not".
  128. " appear in package's list (@archlist)");
  129. $f{$_}= $arch;
  130. }
  131. } elsif (s/^X[CS]*B[CS]*-//i) {
  132. $f{$_}= $v;
  133. } elsif (!m/^X[CS]+-/i) {
  134. &unknown("package's section of control info file");
  135. }
  136. } elsif (m/^C\d+ /) {
  137. #print STDERR "X key >$_< value not shown<\n";
  138. } elsif (s/^L //) {
  139. #print STDERR "L key >$_< value >$v<\n";
  140. if (m/^Source$/) {
  141. &setsourcepackage;
  142. } elsif (m/^Version$/) {
  143. $sourceversion= $v;
  144. $f{$_}= $v unless length($forceversion);
  145. } elsif (m/^(Maintainer|Changes|Urgency|Distribution|Date|Closes)$/) {
  146. } elsif (s/^X[CS]*B[CS]*-//i) {
  147. $f{$_}= $v;
  148. } elsif (!m/^X[CS]+-/i) {
  149. &unknown("parsed version of changelog");
  150. }
  151. } else {
  152. &internerr("value from nowhere, with key >$_< and value >$v<");
  153. }
  154. }
  155. $f{'Version'}= $forceversion if length($forceversion);
  156. for $f (qw(Section Priority)) {
  157. $spvalue{$f}= $spdefault{$f} unless length($spvalue{$f});
  158. $f{$f}= $spvalue{$f} if $spinclude{$f} && length($spvalue{$f});
  159. }
  160. for $f (qw(Package Version)) {
  161. defined($f{$f}) || &error("missing information for output field $f");
  162. }
  163. for $f (qw(Maintainer Description Architecture)) {
  164. defined($f{$f}) || &warn("missing information for output field $f");
  165. }
  166. $oppackage= $f{'Package'};
  167. $verdiff= $f{'Version'} ne $sourceversion;
  168. if ($oppackage ne $sourcepackage || $verdiff) {
  169. $f{'Source'}= $sourcepackage;
  170. $f{'Source'}.= " ($sourceversion)" if $verdiff;
  171. }
  172. if (!defined($substvar{'Installed-Size'})) {
  173. defined($c= open(DU,"-|")) || &syserr("fork for du");
  174. if (!$c) {
  175. chdir("$packagebuilddir") || &syserr("chdir for du to \`$packagebuilddir'");
  176. exec("du","-k","-s","."); &syserr("exec du");
  177. }
  178. $duo=''; while (<DU>) { $duo.=$_; }
  179. close(DU); $? && &subprocerr("du in \`$packagebuilddir'");
  180. $duo =~ m/^(\d+)\s+\.$/ || &failure("du gave unexpected output \`$duo'");
  181. $substvar{'Installed-Size'}= $1;
  182. }
  183. if (defined($substvar{'Extra-Size'})) {
  184. $substvar{'Installed-Size'} += $substvar{'Extra-Size'};
  185. }
  186. if (length($substvar{'Installed-Size'})) {
  187. $f{'Installed-Size'}= $substvar{'Installed-Size'};
  188. }
  189. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  190. open(Y,"> $fileslistfile.new") || &syserr("open new files list file");
  191. chown(@fowner, "$fileslistfile.new")
  192. || &syserr("chown new files list file");
  193. if (open(X,"< $fileslistfile")) {
  194. while (<X>) {
  195. chomp;
  196. next if m/^([-+0-9a-z.]+)_[^_]+_(\w+)\.deb /
  197. && ($1 eq $oppackage) && ($2 eq $arch || $2 eq 'all');
  198. print(Y "$_\n") || &syserr("copy old entry to new files list file");
  199. }
  200. } elsif ($! != ENOENT) {
  201. &syserr("read old files list file");
  202. }
  203. $sversion=$f{'Version'};
  204. $sversion =~ s/^\d+://;
  205. print(Y &substvars(sprintf("%s_%s_%s.deb %s %s\n",
  206. $oppackage,$sversion,$f{'Architecture'},
  207. &spfileslistvalue('Section'), &spfileslistvalue('Priority'))))
  208. || &syserr("write new entry to new files list file");
  209. close(Y) || &syserr("close new files list file");
  210. rename("$fileslistfile.new",$fileslistfile) || &syserr("install new files list file");
  211. for $f (keys %override) { $f{&capit($f)}= $override{$f}; }
  212. for $f (keys %remove) { delete $f{&capit($f)}; }
  213. if (!$stdout) {
  214. $cf= "$packagebuilddir/DEBIAN/control";
  215. $cf= "./$cf" if $cf =~ m/^\s/;
  216. open(STDOUT,"> $cf.new") ||
  217. &syserr("cannot open new output control file \`$cf.new'");
  218. }
  219. &outputclose(1);
  220. if (!$stdout) {
  221. rename("$cf.new","$cf") || &syserr("cannot install output control file \`$cf'");
  222. }
  223. sub spfileslistvalue {
  224. $r= $spvalue{$_[0]};
  225. $r= '-' if !length($r);
  226. return $r;
  227. }