dpkg-gencontrol.pl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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, -ip, -isp, -ips deprecated, ignored for compatibility
  31. -D<field>=<value> override or add a field and value
  32. -U<field> remove a field
  33. -V<name>=<value> set a substitution variable
  34. -T<varlistfile> read variables here, not debian/substvars
  35. -h print this message
  36. ";
  37. }
  38. $i=100;grep($fieldimps{$_}=$i--,
  39. qw(Package Version Section Priority Architecture Essential
  40. Pre-Depends Depends Recommends Suggests Enhances Optional
  41. Conflicts Replaces Provides Installed-Size Origin Maintainer
  42. Bugs Source Description Build-Depends Build-Depends-Indep
  43. Build-Conflicts Build-Conflicts-Indep ));
  44. while (@ARGV) {
  45. $_=shift(@ARGV);
  46. if (m/^-p([-+0-9a-z.]+)$/) {
  47. $oppackage= $1;
  48. } elsif (m/^-p(.*)/) {
  49. &error("Illegal package name \`$1'");
  50. } elsif (m/^-c/) {
  51. $controlfile= $';
  52. } elsif (m/^-l/) {
  53. $changelogfile= $';
  54. } elsif (m/^-P/) {
  55. $packagebuilddir= $';
  56. } elsif (m/^-f/) {
  57. $fileslistfile= $';
  58. } elsif (m/^-v(.+)$/) {
  59. $forceversion= $1;
  60. } elsif (m/^-O$/) {
  61. $stdout= 1;
  62. } elsif (m/^-i[sp][sp]?$/) {
  63. # ignored for backwards compatibility
  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/^-n/) {
  75. $forcefilename= $';
  76. } elsif (m/^-h$/) {
  77. &usageversion; exit(0);
  78. } else {
  79. &usageerr("unknown option \`$_'");
  80. }
  81. }
  82. &findarch;
  83. &parsechangelog;
  84. &parsesubstvars;
  85. &parsecontrolfile;
  86. if (length($oppackage)) {
  87. defined($p2i{"C $oppackage"}) || &error("package $oppackage not in control info");
  88. $myindex= $p2i{"C $oppackage"};
  89. } else {
  90. @packages= grep(m/^C /,keys %p2i);
  91. @packages==1 ||
  92. &error("must specify package since control info has many (@packages)");
  93. $myindex=1;
  94. }
  95. #print STDERR "myindex $myindex\n";
  96. my %pkg_dep_fields = map { $_ => 1 } @pkg_dep_fields;
  97. for $_ (keys %fi) {
  98. $v= $fi{$_};
  99. if (s/^C //) {
  100. #print STDERR "G key >$_< value >$v<\n";
  101. if (m/^Origin|Bugs|Maintainer$/) { $f{$_}=$v; }
  102. elsif (m/^Source$/) { &setsourcepackage; }
  103. elsif (s/^X[CS]*B[CS]*-//i) { $f{$_}= $v; }
  104. elsif (m/^X[CS]+-|^(Standards-Version|Uploaders)$|^Build-(Depends|Conflicts)(-Indep)?$/i) { }
  105. elsif (m/^Section$|^Priority$/) { $spdefault{$_}= $v; }
  106. else { $_ = "C $_"; &unknown('general section of control info file'); }
  107. } elsif (s/^C$myindex //) {
  108. #print STDERR "P key >$_< value >$v<\n";
  109. if (m/^(Package|Description|Essential|Optional)$/) {
  110. $f{$_}= $v;
  111. } elsif (exists($pkg_dep_fields{$_})) {
  112. } elsif (m/^Section$|^Priority$/) {
  113. $spvalue{$_}= $v;
  114. } elsif (m/^Architecture$/) {
  115. if (debian_arch_eq('all', $v)) {
  116. $f{$_}= $v;
  117. } elsif (debian_arch_is($arch, $v)) {
  118. $f{$_}= $arch;
  119. } else {
  120. @archlist= split(/\s+/,$v);
  121. my @invalid_archs = grep m/[^\w-]/, @archlist;
  122. &warn("`".join("' `", @invalid_archs)."' are not legal ".
  123. "architecture strings.") if @invalid_archs > 1;
  124. &warn("`@invalid_archs' is not a legal ".
  125. "architecture string.") if @invalid_archs == 1;
  126. grep(debian_arch_is($arch, $_), @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. $_ = "C$myindex $_"; &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. $_ = "L $_"; &unknown("parsed version of changelog");
  150. }
  151. } elsif (m/o:/) {
  152. } else {
  153. &internerr("value from nowhere, with key >$_< and value >$v<");
  154. }
  155. }
  156. $f{'Version'}= $forceversion if length($forceversion);
  157. $version= $f{'Version'};
  158. $origversion= $version; $origversion =~ s/-[^-]+$//;
  159. $substvar{"dpkg:UpstreamVersion"}=$origversion;
  160. $substvar{"dpkg:Version"}=$version;
  161. for $_ (keys %fi) {
  162. $v= $fi{$_};
  163. if (s/^C //) {
  164. } elsif (s/^C$myindex //) {
  165. if (m/^(Package|Description|Essential|Optional)$/) {
  166. } elsif (exists($pkg_dep_fields{$_})) {
  167. my $dep = parsedep(substvars($v), 1, 1);
  168. &error("error occurred while parsing $_") unless defined $dep;
  169. $f{$_}= showdep($dep, 0);
  170. } elsif (m/^Section$|^Priority$/) {
  171. } elsif (m/^Architecture$/) {
  172. } elsif (s/^X[CS]*B[CS]*-//i) {
  173. } elsif (!m/^X[CS]+-/i) {
  174. }
  175. } elsif (m/^C\d+ /) {
  176. } elsif (s/^L //) {
  177. } elsif (m/o:/) {
  178. } else {
  179. }
  180. }
  181. for $f (qw(Section Priority)) {
  182. $spvalue{$f}= $spdefault{$f} unless length($spvalue{$f});
  183. $f{$f}= $spvalue{$f} if length($spvalue{$f});
  184. }
  185. for $f (qw(Package Version)) {
  186. defined($f{$f}) || &error("missing information for output field $f");
  187. }
  188. for $f (qw(Maintainer Description Architecture)) {
  189. defined($f{$f}) || &warn("missing information for output field $f");
  190. }
  191. $oppackage= $f{'Package'};
  192. $verdiff= $f{'Version'} ne $sourceversion;
  193. if ($oppackage ne $sourcepackage || $verdiff) {
  194. $f{'Source'}= $sourcepackage;
  195. $f{'Source'}.= " ($sourceversion)" if $verdiff;
  196. }
  197. if (!defined($substvar{'Installed-Size'})) {
  198. defined($c= open(DU,"-|")) || &syserr("fork for du");
  199. if (!$c) {
  200. chdir("$packagebuilddir") || &syserr("chdir for du to \`$packagebuilddir'");
  201. exec("du","-k","-s","."); &syserr("exec du");
  202. }
  203. $duo=''; while (<DU>) { $duo.=$_; }
  204. close(DU); $? && &subprocerr("du in \`$packagebuilddir'");
  205. $duo =~ m/^(\d+)\s+\.$/ || &failure("du gave unexpected output \`$duo'");
  206. $substvar{'Installed-Size'}= $1;
  207. }
  208. if (defined($substvar{'Extra-Size'})) {
  209. $substvar{'Installed-Size'} += $substvar{'Extra-Size'};
  210. }
  211. if (length($substvar{'Installed-Size'})) {
  212. $f{'Installed-Size'}= $substvar{'Installed-Size'};
  213. }
  214. for $f (keys %override) { $f{&capit($f)}= $override{$f}; }
  215. for $f (keys %remove) { delete $f{&capit($f)}; }
  216. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  217. open(Y,"> $fileslistfile.new") || &syserr("open new files list file");
  218. binmode(Y);
  219. chown(@fowner, "$fileslistfile.new")
  220. || &syserr("chown new files list file");
  221. if (open(X,"< $fileslistfile")) {
  222. binmode(X);
  223. while (<X>) {
  224. chomp;
  225. next if m/^([-+0-9a-z.]+)_[^_]+_([\w-]+)\.deb /
  226. && ($1 eq $oppackage)
  227. && (debian_arch_eq($2, $f{'Architecture'})
  228. || debian_arch_eq($2, 'all'));
  229. print(Y "$_\n") || &syserr("copy old entry to new files list file");
  230. }
  231. close(X) || &syserr("close old files list file");
  232. } elsif ($! != ENOENT) {
  233. &syserr("read old files list file");
  234. }
  235. $sversion=$f{'Version'};
  236. $sversion =~ s/^\d+://;
  237. $forcefilename=sprintf("%s_%s_%s.deb", $oppackage,$sversion,$f{'Architecture'})
  238. unless ($forcefilename);
  239. print(Y &substvars(sprintf("%s %s %s\n", $forcefilename,
  240. &spfileslistvalue('Section'), &spfileslistvalue('Priority'))))
  241. || &syserr("write new entry to new files list file");
  242. close(Y) || &syserr("close new files list file");
  243. rename("$fileslistfile.new",$fileslistfile) || &syserr("install new files list file");
  244. if (!$stdout) {
  245. $cf= "$packagebuilddir/DEBIAN/control";
  246. $cf= "./$cf" if $cf =~ m/^\s/;
  247. open(STDOUT,"> $cf.new") ||
  248. &syserr("cannot open new output control file \`$cf.new'");
  249. binmode(STDOUT);
  250. }
  251. &outputclose(1);
  252. if (!$stdout) {
  253. rename("$cf.new","$cf") || &syserr("cannot install output control file \`$cf'");
  254. }
  255. sub spfileslistvalue {
  256. $r= $spvalue{$_[0]};
  257. $r= '-' if !length($r);
  258. return $r;
  259. }