dpkg-gencontrol.pl 8.4 KB

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