dpkg-gencontrol.pl 8.3 KB

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