dpkg-gencontrol.pl 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. sub usageversion {
  14. print STDERR
  15. "Debian dpkg-gencontrol $version.
  16. Copyright (C) 1996 Ian Jackson.
  17. Copyright (C) 2000 Wichert Akkerman.
  18. This is free software; see the GNU General Public Licence version 2 or later
  19. for copying conditions. There is NO warranty.
  20. Usage: dpkg-gencontrol [options ...]
  21. Options: -p<package> print control file for package
  22. -c<controlfile> get control info from this file
  23. -l<changelogfile> get per-version info from this file
  24. -F<changelogformat> force change log format
  25. -v<forceversion> set version of binary package
  26. -f<fileslistfile> write files here instead of debian/files
  27. -P<packagebuilddir> temporary build dir instead of debian/tmp
  28. -n<filename> assume the package filename will be <filename>
  29. -O write to stdout, not .../DEBIAN/control
  30. -is include section field
  31. -ip include priority field
  32. -isp|-ips include both section and priority
  33. -D<field>=<value> override or add a field and value
  34. -U<field> remove a field
  35. -V<name>=<value> set a substitution variable
  36. -T<varlistfile> read variables here, not debian/substvars
  37. -h print this message
  38. ";
  39. }
  40. $i=100;grep($fieldimps{$_}=$i--,
  41. qw(Package Version Section Priority Architecture Essential
  42. Pre-Depends Depends Recommends Suggests Enhances Optional
  43. Conflicts Replaces Provides Installed-Size Origin Maintainer
  44. Bugs Source Description Build-Depends Build-Depends-Indep
  45. Build-Conflicts Build-Conflicts-Indep ));
  46. while (@ARGV) {
  47. $_=shift(@ARGV);
  48. if (m/^-p([-+0-9a-z.]+)$/) {
  49. $oppackage= $1;
  50. } elsif (m/^-p([-+0-9a-zA-Z.]+)$/) {
  51. &usageerr("Illegal package name \`$1'");
  52. } elsif (m/^-c/) {
  53. $controlfile= $';
  54. } elsif (m/^-l/) {
  55. $changelogfile= $';
  56. } elsif (m/^-P/) {
  57. $packagebuilddir= $';
  58. } elsif (m/^-f/) {
  59. $fileslistfile= $';
  60. } elsif (m/^-v(.+)$/) {
  61. $forceversion= $1;
  62. } elsif (m/^-O$/) {
  63. $stdout= 1;
  64. } elsif (m/^-is$/) {
  65. $spinclude{'Section'}=1;
  66. } elsif (m/^-ip$/) {
  67. $spinclude{'Priority'}=1;
  68. } elsif (m/^-isp$/ || m/^-ips$/) {
  69. $spinclude{'Section'}=1;
  70. $spinclude{'Priority'}=1;
  71. } elsif (m/^-F([0-9a-z]+)$/) {
  72. $changelogformat=$1;
  73. } elsif (m/^-D([^\=:]+)[=:]/) {
  74. $override{$1}= $';
  75. } elsif (m/^-U([^\=:]+)$/) {
  76. $remove{$1}= 1;
  77. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  78. $substvar{$1}= $';
  79. } elsif (m/^-T/) {
  80. $varlistfile= $';
  81. } elsif (m/^-n/) {
  82. $forcefilename= $';
  83. } elsif (m/^-h$/) {
  84. &usageversion; exit(0);
  85. } else {
  86. &usageerr("unknown option \`$_'");
  87. }
  88. }
  89. &findarch;
  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/^Origin|Bugs|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-(Depends|Conflicts)(-Indep)?$/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|Enhances|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. $version= $f{'Version'};
  157. $origversion= $version; $origversion =~ s/-[^-]+$//;
  158. $substvar{"dpkg:UpstreamVersion"}=$origversion;
  159. $substvar{"dpkg:Version"}=$version;
  160. for $f (qw(Section Priority)) {
  161. $spvalue{$f}= $spdefault{$f} unless length($spvalue{$f});
  162. $f{$f}= $spvalue{$f} if $spinclude{$f} && length($spvalue{$f});
  163. }
  164. for $f (qw(Package Version)) {
  165. defined($f{$f}) || &error("missing information for output field $f");
  166. }
  167. for $f (qw(Maintainer Description Architecture)) {
  168. defined($f{$f}) || &warn("missing information for output field $f");
  169. }
  170. $oppackage= $f{'Package'};
  171. $verdiff= $f{'Version'} ne $sourceversion;
  172. if ($oppackage ne $sourcepackage || $verdiff) {
  173. $f{'Source'}= $sourcepackage;
  174. $f{'Source'}.= " ($sourceversion)" if $verdiff;
  175. }
  176. if (!defined($substvar{'Installed-Size'})) {
  177. defined($c= open(DU,"-|")) || &syserr("fork for du");
  178. if (!$c) {
  179. chdir("$packagebuilddir") || &syserr("chdir for du to \`$packagebuilddir'");
  180. exec("du","-k","-s","."); &syserr("exec du");
  181. }
  182. $duo=''; while (<DU>) { $duo.=$_; }
  183. close(DU); $? && &subprocerr("du in \`$packagebuilddir'");
  184. $duo =~ m/^(\d+)\s+\.$/ || &failure("du gave unexpected output \`$duo'");
  185. $substvar{'Installed-Size'}= $1;
  186. }
  187. if (defined($substvar{'Extra-Size'})) {
  188. $substvar{'Installed-Size'} += $substvar{'Extra-Size'};
  189. }
  190. if (length($substvar{'Installed-Size'})) {
  191. $f{'Installed-Size'}= $substvar{'Installed-Size'};
  192. }
  193. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  194. open(Y,"> $fileslistfile.new") || &syserr("open new files list file");
  195. chown(@fowner, "$fileslistfile.new")
  196. || &syserr("chown new files list file");
  197. if (open(X,"< $fileslistfile")) {
  198. while (<X>) {
  199. chomp;
  200. next if m/^([-+0-9a-z.]+)_[^_]+_(\w+)\.deb /
  201. && ($1 eq $oppackage) && ($2 eq $arch || $2 eq 'all');
  202. print(Y "$_\n") || &syserr("copy old entry to new files list file");
  203. }
  204. } elsif ($! != ENOENT) {
  205. &syserr("read old files list file");
  206. }
  207. $sversion=$f{'Version'};
  208. $sversion =~ s/^\d+://;
  209. $forcefilename=sprintf("%s_%s_%s.deb", $oppackage,$sversion,$f{'Architecture'})
  210. unless ($forcefilename);
  211. print(Y &substvars(sprintf("%s %s %s\n", $forcefilename,
  212. &spfileslistvalue('Section'), &spfileslistvalue('Priority'))))
  213. || &syserr("write new entry to new files list file");
  214. close(Y) || &syserr("close new files list file");
  215. rename("$fileslistfile.new",$fileslistfile) || &syserr("install new files list file");
  216. for $f (keys %override) { $f{&capit($f)}= $override{$f}; }
  217. for $f (keys %remove) { delete $f{&capit($f)}; }
  218. if (!$stdout) {
  219. $cf= "$packagebuilddir/DEBIAN/control";
  220. $cf= "./$cf" if $cf =~ m/^\s/;
  221. open(STDOUT,"> $cf.new") ||
  222. &syserr("cannot open new output control file \`$cf.new'");
  223. }
  224. &outputclose(1);
  225. if (!$stdout) {
  226. rename("$cf.new","$cf") || &syserr("cannot install output control file \`$cf'");
  227. }
  228. sub spfileslistvalue {
  229. $r= $spvalue{$_[0]};
  230. $r= '-' if !length($r);
  231. return $r;
  232. }