dpkg-gencontrol.pl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 1996 Ian Jackson.
  17. Copyright 2000,2002 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(.*)/) {
  51. &error("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. my %pkg_dep_fields = map { $_ => 1 } @pkg_dep_fields;
  103. for $_ (keys %fi) {
  104. $v= $fi{$_};
  105. if (s/^C //) {
  106. #print STDERR "G key >$_< value >$v<\n";
  107. if (m/^Origin|Bugs|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|Uploaders)$|^Build-(Depends|Conflicts)(-Indep)?$/i) { }
  111. elsif (m/^Section$|^Priority$/) { $spdefault{$_}= $v; }
  112. else { $_ = "C $_"; &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|Optional)$/) {
  116. $f{$_}= $v;
  117. } elsif (exists($pkg_dep_fields{$_})) {
  118. $f{$_}= showdep($v, 0);
  119. } elsif (m/^Section$|^Priority$/) {
  120. $spvalue{$_}= $v;
  121. } elsif (m/^Architecture$/) {
  122. if ($v eq 'all') {
  123. $f{$_}= $v;
  124. } elsif ($v eq 'any') {
  125. $f{$_}= $arch;
  126. } else {
  127. @archlist= split(/\s+/,$v);
  128. grep($arch eq $_, @archlist) ||
  129. &error("current build architecture $arch does not".
  130. " appear in package's list (@archlist)");
  131. $f{$_}= $arch;
  132. }
  133. } elsif (s/^X[CS]*B[CS]*-//i) {
  134. $f{$_}= $v;
  135. } elsif (!m/^X[CS]+-/i) {
  136. $_ = "C$myindex $_"; &unknown("package's section of control info file");
  137. }
  138. } elsif (m/^C\d+ /) {
  139. #print STDERR "X key >$_< value not shown<\n";
  140. } elsif (s/^L //) {
  141. #print STDERR "L key >$_< value >$v<\n";
  142. if (m/^Source$/) {
  143. &setsourcepackage;
  144. } elsif (m/^Version$/) {
  145. $sourceversion= $v;
  146. $f{$_}= $v unless length($forceversion);
  147. } elsif (m/^(Maintainer|Changes|Urgency|Distribution|Date|Closes)$/) {
  148. } elsif (s/^X[CS]*B[CS]*-//i) {
  149. $f{$_}= $v;
  150. } elsif (!m/^X[CS]+-/i) {
  151. $_ = "L $_"; &unknown("parsed version of changelog");
  152. }
  153. } elsif (m/o:/) {
  154. } else {
  155. &internerr("value from nowhere, with key >$_< and value >$v<");
  156. }
  157. }
  158. $f{'Version'}= $forceversion if length($forceversion);
  159. $version= $f{'Version'};
  160. $origversion= $version; $origversion =~ s/-[^-]+$//;
  161. $substvar{"dpkg:UpstreamVersion"}=$origversion;
  162. $substvar{"dpkg:Version"}=$version;
  163. for $f (qw(Section Priority)) {
  164. $spvalue{$f}= $spdefault{$f} unless length($spvalue{$f});
  165. $f{$f}= $spvalue{$f} if $spinclude{$f} && length($spvalue{$f});
  166. }
  167. for $f (qw(Package Version)) {
  168. defined($f{$f}) || &error("missing information for output field $f");
  169. }
  170. for $f (qw(Maintainer Description Architecture)) {
  171. defined($f{$f}) || &warn("missing information for output field $f");
  172. }
  173. $oppackage= $f{'Package'};
  174. $verdiff= $f{'Version'} ne $sourceversion;
  175. if ($oppackage ne $sourcepackage || $verdiff) {
  176. $f{'Source'}= $sourcepackage;
  177. $f{'Source'}.= " ($sourceversion)" if $verdiff;
  178. }
  179. if (!defined($substvar{'Installed-Size'})) {
  180. defined($c= open(DU,"-|")) || &syserr("fork for du");
  181. if (!$c) {
  182. chdir("$packagebuilddir") || &syserr("chdir for du to \`$packagebuilddir'");
  183. exec("du","-k","-s","."); &syserr("exec du");
  184. }
  185. $duo=''; while (<DU>) { $duo.=$_; }
  186. close(DU); $? && &subprocerr("du in \`$packagebuilddir'");
  187. $duo =~ m/^(\d+)\s+\.$/ || &failure("du gave unexpected output \`$duo'");
  188. $substvar{'Installed-Size'}= $1;
  189. }
  190. if (defined($substvar{'Extra-Size'})) {
  191. $substvar{'Installed-Size'} += $substvar{'Extra-Size'};
  192. }
  193. if (length($substvar{'Installed-Size'})) {
  194. $f{'Installed-Size'}= $substvar{'Installed-Size'};
  195. }
  196. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  197. open(Y,"> $fileslistfile.new") || &syserr("open new files list file");
  198. binmode(Y);
  199. chown(@fowner, "$fileslistfile.new")
  200. || &syserr("chown new files list file");
  201. if (open(X,"< $fileslistfile")) {
  202. binmode(X);
  203. while (<X>) {
  204. chomp;
  205. next if m/^([-+0-9a-z.]+)_[^_]+_(\w+)\.deb /
  206. && ($1 eq $oppackage) && ($2 eq $arch || $2 eq 'all');
  207. print(Y "$_\n") || &syserr("copy old entry to new files list file");
  208. }
  209. close(X) || &syserr("close old files list file");
  210. } elsif ($! != ENOENT) {
  211. &syserr("read old files list file");
  212. }
  213. $sversion=$f{'Version'};
  214. $sversion =~ s/^\d+://;
  215. $forcefilename=sprintf("%s_%s_%s.deb", $oppackage,$sversion,$f{'Architecture'})
  216. unless ($forcefilename);
  217. print(Y &substvars(sprintf("%s %s %s\n", $forcefilename,
  218. &spfileslistvalue('Section'), &spfileslistvalue('Priority'))))
  219. || &syserr("write new entry to new files list file");
  220. close(Y) || &syserr("close new files list file");
  221. rename("$fileslistfile.new",$fileslistfile) || &syserr("install new files list file");
  222. for $f (keys %override) { $f{&capit($f)}= $override{$f}; }
  223. for $f (keys %remove) { delete $f{&capit($f)}; }
  224. if (!$stdout) {
  225. $cf= "$packagebuilddir/DEBIAN/control";
  226. $cf= "./$cf" if $cf =~ m/^\s/;
  227. open(STDOUT,"> $cf.new") ||
  228. &syserr("cannot open new output control file \`$cf.new'");
  229. binmode(STDOUT);
  230. }
  231. &outputclose(1);
  232. if (!$stdout) {
  233. rename("$cf.new","$cf") || &syserr("cannot install output control file \`$cf'");
  234. }
  235. sub spfileslistvalue {
  236. $r= $spvalue{$_[0]};
  237. $r= '-' if !length($r);
  238. return $r;
  239. }