openpkg-index.pl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. ##
  2. ## openpkg-index.pl -- create index from spec files
  3. ##
  4. ## Copyright (c) 2000-2003 Cable & Wireless Deutschland GmbH
  5. ## Copyright (c) 2000-2003 The OpenPKG Project <http://www.openpkg.org/>
  6. ## Copyright (c) 2000-2003 Ralf S. Engelschall <rse@engelschall.com>
  7. ##
  8. ## Permission to use, copy, modify, and distribute this software for
  9. ## any purpose with or without fee is hereby granted, provided that
  10. ## the above copyright notice and this permission notice appear in all
  11. ## copies.
  12. ##
  13. ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  14. ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
  17. ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18. ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  20. ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  23. ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. ## SUCH DAMAGE.
  25. ##
  26. require 5;
  27. use strict;
  28. use Getopt::Std;
  29. getopts('r:p:C:o:ci');
  30. use vars qw/$opt_r $opt_p $opt_C $opt_o $opt_c $opt_i/;
  31. use FileHandle;
  32. use DirHandle;
  33. my $l_prefix = '@l_prefix@';
  34. my $RPM = "$l_prefix/bin/rpm";
  35. my $R2C = "$l_prefix/bin/rpm2cpio";
  36. my $BZ = "$l_prefix/lib/openpkg/bzip2 -9";
  37. #########################################################################
  38. #
  39. # escape XML special characters for output in RDF file
  40. #
  41. # remove trailing whitespace
  42. # remove common leading whitespace
  43. #
  44. sub e ($) {
  45. my($s) = @_;
  46. my($i);
  47. $s =~ s/\n+$//sg;
  48. $s =~ s/[^\S\n]+$//mg;
  49. $i = undef;
  50. while ($s =~ /^([^\S\n]+)/mg) {
  51. $i = $1 if !defined $i || length($1) < length($i);
  52. }
  53. $s =~ s/^\Q$i\E//mg if defined $i;
  54. $s =~ s/&/&amp;/sg;
  55. $s =~ s/</&lt;/sg;
  56. $s =~ s/>/&gt;/sg;
  57. return $s;
  58. }
  59. sub commasep ($$) {
  60. my($k,$v) = @_;
  61. if ($k =~ /^(PreReq|BuildPreReq|Provides|Conflicts)$/) {
  62. return split(/\s*,\s*/, $v);
  63. }
  64. return $v;
  65. }
  66. sub optesc ($) {
  67. my($s) = @_;
  68. $s =~ s/([\x00-\x1f\x80-\xbf\s\%])/sprintf("%%%02x",ord($1))/eg;
  69. return $s;
  70. }
  71. sub vsub ($$) {
  72. my($var,$v) = @_;
  73. $v =~ s/\%\{([^}]+)\}/exists $var->{$1} ? $var->{$1} : '%{'.$1.'}'/emg;
  74. return $v;
  75. }
  76. sub upn ($) {
  77. my($t) = @_;
  78. my(@tok) = $t =~ /(\(|\)|\&\&|\|\||\!|\S+)/g;
  79. my(@out,$op,$o);
  80. my(@save);
  81. $op = [];
  82. foreach (@tok) {
  83. if ($_ eq '(') {
  84. push @save, $op;
  85. $op = [];
  86. } elsif ($_ eq ')') {
  87. die "FATAL: unresolved operators in: @tok\n" if @$op;
  88. $op = pop @save
  89. or die "FATAL: parenthesis stack underflow in: @tok\n";
  90. while ($o = pop @$op) {
  91. push @out, $o->[0];
  92. last if $o->[1];
  93. }
  94. } elsif ($_ eq '&&') {
  95. push @$op, [ '+', 1 ] ;
  96. } elsif ($_ eq '||') {
  97. push @$op, [ '|', 1 ] ;
  98. } elsif ($_ eq '!') {
  99. push @$op, [ '!', 0 ];
  100. } elsif (/^\%\{(\S*?)\}$/) {
  101. push @out, $1;
  102. while ($o = pop @$op) {
  103. push @out, $o->[0];
  104. last if $o->[1]; # binop
  105. }
  106. }
  107. }
  108. return join (' ',@out);
  109. }
  110. #
  111. # deduce external variables from description
  112. #
  113. # before openpkg-20021230
  114. #
  115. sub find_options ($) {
  116. my($descr) = @_;
  117. my $evar = {};
  118. $descr =~ s/--define\s*'(\S+)\s*\%\{\1\}'/$evar->{$1} = '%{'.$1.'}', ''/sge;
  119. return $evar;
  120. }
  121. #
  122. # translate default section from spec-file
  123. # into a hash
  124. # %if/%ifdef/%define... are translated to #if/#ifdef/#define
  125. #
  126. # #defines are interpolated (correct ?)
  127. #
  128. # #if/#ifdef/... sections are stripped
  129. # result is the same as if all conditions evaluate false (!)
  130. #
  131. # all attributes are of the form key: value
  132. # repeated attributes are coalesced into a list
  133. #
  134. sub package2data ($$) {
  135. my($s,$ovar) = @_;
  136. my(%evar,%var);
  137. my(@term, $term);
  138. my(%attr);
  139. my($l, $v, $cond, $d, $p);
  140. my($re,@defs);
  141. # combine multilines
  142. $s =~ s/\\\n/ /sg;
  143. #
  144. # map conditional variable macros
  145. #
  146. $s =~ s/^#\{\!\?([^:]*):\s*%(.*?)\s*\}\s*$/#ifndef $1\n#$2\n#endif/mg;
  147. $s =~ s/^#\{\!\?([^:]*):\s*(.*?)\s*\}\s*$/#ifndef $1\n$2\n#endif/mg;
  148. #
  149. # map option macro
  150. #
  151. $s =~ s/^#option\s+(\S+)\s*(.*?)\s*$/#ifndef $1\n#define $1 $2\n#endif\n#provides $1 $2/mg;
  152. #
  153. # use option variables for interpolation
  154. #
  155. %evar = %$ovar;
  156. #
  157. # guess more external parameters by scanning for "default" sections.
  158. #
  159. $re = '^\#ifndef\s+[\w\_]+\s*\n((?:\#define\s+[\w\_]+\s.*\n)+)\#endif\n';
  160. @defs = $s =~ /$re/gm;
  161. foreach (@defs) {
  162. while (/^\#define\s+([\w\_]+)\s(.*?)\s*$/mg) {
  163. $ovar->{$1} = $2;
  164. $evar{$1} = '%{'.$1.'}';
  165. }
  166. }
  167. $s =~ s/$re//gm;
  168. #
  169. # add everything looking like a with_ variable
  170. #
  171. $re = '%{(with\_[\w\_]+)}';
  172. @defs = $s =~ /$re/gm;
  173. foreach (@defs) {
  174. next if exists $ovar->{$1};
  175. $ovar->{$1} = '%{'.$1.'}';
  176. $evar{$1} = '%{'.$1.'}';
  177. }
  178. #
  179. # extract all conditional sections
  180. #
  181. @term = ();
  182. %var = ();
  183. $cond = '';
  184. foreach $l (split(/\n/, $s)) {
  185. $v = vsub(\%var,$l);
  186. if (($p) = $v =~ /^\#if\s+(.*?)\s*$/) {
  187. #
  188. # normalize #if expressions
  189. # "%{variable}" == "yes"
  190. # "%{variable}" == "no"
  191. # operators ! && ||
  192. #
  193. $term = '';
  194. while ($p =~ /(!=)|(\!|\|\||\&\&|\(|\))|"\%\{([^}]+)\}"\s*==\s*"(yes|no)"|(\S+)/g) {
  195. if (defined $1) {
  196. warn "WARNING: unknown token '$1':\n< $l\n> $v\n";
  197. } elsif (defined $5) {
  198. warn "WARNING: unknown token '$5':\n< $l\n> $v\n";
  199. } elsif (defined $2) {
  200. $term .= " $2 ";
  201. } elsif (exists $evar{$3}) {
  202. $term .= ($4 eq 'no' ? '! ' : '').vsub(\%evar,'%{'.$3.'}');
  203. } else {
  204. warn "WARNING: unknown conditional '$2':\n< $l\n> $v\n";
  205. }
  206. }
  207. #
  208. # join with previous conditions for this #if/#endif block
  209. #
  210. if ($term ne '') {
  211. push @term, "( $term )";
  212. $cond = join(' && ', grep { $_ ne '' } @term).'';
  213. } else {
  214. push @term, '';
  215. }
  216. } elsif ($v =~ /^\#else\s*$/) {
  217. #
  218. # reverse last condition
  219. #
  220. if (@term) {
  221. $term[-1] = ' ! '.$term[-1];
  222. $cond = join(' && ', grep { $_ ne '' } @term).'';
  223. } else {
  224. die "FATAL: else without if\n";
  225. }
  226. } elsif ($v =~ /^\#endif\s*$/) {
  227. #
  228. # unwind last #if expression
  229. #
  230. pop @term;
  231. $cond = join(' && ', grep { $_ ne '' } @term).'';
  232. } elsif ($v =~ /^\#(?:define)\s*(\S+)\s*(.*?)\s*$/) {
  233. #
  234. # define conditional variables
  235. # truth-value becomes current condition
  236. #
  237. # define internal variables
  238. # -> store for subsequent substitution
  239. #
  240. if (exists $evar{$1}) {
  241. if ($2 eq 'yes') {
  242. $evar{$1} = "( \%\{$1\} || ( $cond ) )";
  243. } elsif ($2 eq 'no') {
  244. $evar{$1} = "( %\{$1\} && ! ( $cond ) )";
  245. } else {
  246. warn "WARNING: logic too complex for '$1':\n< $l\n> $v\n";
  247. }
  248. } else {
  249. $var{$1} = $2;
  250. }
  251. } elsif ($v =~ /^\#(?:provides)\s*(\S+)\s*(.*?)\s*$/) {
  252. #
  253. # store option for current condition
  254. #
  255. if (exists $attr{'Name'}->{''}) {
  256. push @{$attr{'Provides'}->{$cond}},
  257. $attr{'Name'}->{''}->[0].'::'.$1.' = '.optesc($2);
  258. } else {
  259. warn "ERROR: no package name set for option $1 = $2\n";
  260. }
  261. } elsif ($v =~ /^\s*([^\#]\S*)\s*:\s*(.*?)\s*$/) {
  262. #
  263. # store attribute=value for current condition
  264. #
  265. push @{$attr{$1}->{$cond}}, commasep($1,$2);
  266. }
  267. }
  268. return \%attr;
  269. }
  270. #
  271. # split spec file into sections starting with a %word
  272. #
  273. # concatenate extended lines
  274. # strip comment lines
  275. # map %command to #command
  276. # split sections
  277. #
  278. # return package2data from default section.
  279. #
  280. sub spec2data ($) {
  281. my($s) = @_;
  282. my(%map);
  283. my($a,$o);
  284. my $spec = $s;
  285. # remove comments
  286. $s =~ s/^\s*#.*?\n//mg;
  287. # map commands
  288. $s =~ s/^%(ifdef|ifndef|if|option|define|else|endif|\{)/#$1/mg;
  289. # split sections
  290. foreach (split(/^(?=%\w+\s*\n)/m, $s)) {
  291. if (/^%(\w+)\s*\n/) {
  292. $map{$1} .= $';
  293. } else {
  294. $map{'*'} .= $_;
  295. }
  296. }
  297. if (exists $map{'description'}) {
  298. $o = find_options($map{'description'});
  299. $a = package2data($map{'*'}, $o );
  300. $a->{'Description'} = { '' => [ $map{'description'} ] };
  301. } else {
  302. $a = package2data($map{'*'}, {});
  303. }
  304. return $a;
  305. }
  306. ##########################################################################
  307. #
  308. # start of XML file
  309. #
  310. sub xml_head ($$) {
  311. my($fh,$res) = @_;
  312. print $fh <<EOFEOF;
  313. <?xml version="1.0" encoding="iso-8859-1"?>
  314. <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  315. xmlns="http://www.openpkg.org/xml-rdf-index/0.9">
  316. <Repository rdf:resource="$res">
  317. EOFEOF
  318. }
  319. #
  320. # end of XML file, corresponds with start tags
  321. #
  322. sub xml_foot ($) {
  323. my($fh) = @_;
  324. print $fh <<EOFEOF;
  325. </Repository>
  326. </rdf:RDF>
  327. EOFEOF
  328. }
  329. sub n($$) {
  330. my($a,$k) = @_;
  331. return unless $a->{$k};
  332. return unless $a->{$k}->{''};
  333. return $a->{$k}->{''}->[0];
  334. }
  335. #
  336. # send out $a->{$k} as text-style tag
  337. #
  338. sub xml_text ($$$;$) {
  339. my($i,$a,$k,$tag) = @_;
  340. my($out);
  341. return "" unless exists $a->{$k};
  342. $tag = $k unless defined $tag;
  343. $i = ' ' x $i;
  344. $out = e(n($a,$k));
  345. return if $out eq '';
  346. return "$i<$tag>\n$out\n$i</$tag>\n";
  347. }
  348. #
  349. # send out @{$a->{$k}} as body of an XML tag
  350. # $k is the name of the tag unless overridden by $tag
  351. # $i denotes the depth of indentation to form nicely
  352. # looking files.
  353. #
  354. # all data from the list is flattened into a single
  355. # body, separated by LF and escaped for XML metachars.
  356. #
  357. sub xml_tag ($$$;$) {
  358. my($i,$a,$k,$tag) = @_;
  359. my($out,$cond,$upn);
  360. return "" unless exists $a->{$k};
  361. $tag = $k unless defined $tag;
  362. $out = '';
  363. $i = ' ' x $i;
  364. foreach $cond (sort keys %{$a->{$k}}) {
  365. $upn = e(upn($cond));
  366. $out .= $i.
  367. ($cond ne '' ? "<$tag cond=\"$upn\">" : "<$tag>").
  368. join("\n", map { e($_) } @{$a->{$k}->{$cond}}).
  369. "</$tag>\n";
  370. }
  371. return $out;
  372. }
  373. #
  374. # send out @{$a->{$k}} as a rdf:bag
  375. # $k is the name of the outer tag unless overriden by $tag
  376. # $i denotes the depth of indentation, inner tags are indented
  377. # 2 or 4 more character positions.
  378. #
  379. # each element of the bag is listed
  380. #
  381. sub xml_bag ($$$;$) {
  382. my($i,$a,$k,$tag) = @_;
  383. my($out,$cond,$upn);
  384. return "" unless exists $a->{$k};
  385. $tag = $k unless defined $tag;
  386. $out = '';
  387. $i = ' ' x $i;
  388. foreach $cond (sort keys %{$a->{$k}}) {
  389. next unless @{$a->{$k}->{$cond}};
  390. $upn = e(upn($cond));
  391. $out .= $i.
  392. ($cond ne '' ? "<$tag cond=\"$upn\">\n" : "<$tag>\n").
  393. "$i <rdf:bag>\n".
  394. join("",
  395. map { "$i <rdf:li>".e($_)."</rdf:li>\n" }
  396. @{$a->{$k}->{$cond}}).
  397. "$i </rdf:bag>\n".
  398. "$i</$tag>\n";
  399. }
  400. return $out;
  401. }
  402. #
  403. # send out reference to another RDF
  404. #
  405. sub xml_reference ($$$) {
  406. my($fh, $res, $href) = @_;
  407. print $fh <<EOFEOF;
  408. <Repository rdf:resource="$res" href="$href"/>
  409. EOFEOF
  410. }
  411. #
  412. # translate attributes from %$a as generated by package2data
  413. # into XML and write to file $fh
  414. #
  415. sub xml_record ($$$) {
  416. my($fh, $a, $href) = @_;
  417. my($maj,$min,$rel,$about);
  418. $about =
  419. n($a,'Name').'-'.
  420. n($a,'Version').'-'.
  421. n($a,'Release');
  422. unless (defined $href) {
  423. # guess location from Information in Specfile
  424. $href = "$about.src.rpm";
  425. ($maj,$min,$rel) = n($a,'Release') =~ /^(\d+)\.(\d+)\.(\d+)/;
  426. if (defined $min) {
  427. if ($maj > 1 || ($maj == 1 && $min > 0)) {
  428. # 1.1 or later
  429. if (n($a,'Distribution') =~ /\[PLUS\]/) {
  430. $href = 'PLUS/'.$href;
  431. }
  432. }
  433. if ($maj > 1 || ($maj == 1 && $min >= 0)) {
  434. # 1.0 or later
  435. if ($rel > 0) {
  436. $href = 'UPD/'.$href;
  437. }
  438. }
  439. } else {
  440. # current
  441. }
  442. }
  443. print $fh <<EOFEOF;
  444. <rdf:Description about="$about" href="$href">
  445. EOFEOF
  446. # fake Source attribute from Source\d attribtutes
  447. # XXX only default conditional
  448. $a->{'Source'} = { '' => [
  449. map {
  450. s/\Q%{name}\E/n($a,'Name')/esg;
  451. s/\Q%{version}\E/n($a,'Version')/esg;
  452. s/\Q%{release}\E/n($a,'Release')/esg;
  453. s/.*\///;
  454. $_;
  455. }
  456. map {
  457. $a->{$_}->{''} ? @{$a->{$_}->{''}} : ()
  458. }
  459. sort {
  460. my($x) = $a =~ /^(\d*)$/;
  461. my($y) = $b =~ /^(\d*)$/;
  462. return $x <=> $y;
  463. }
  464. grep {
  465. /^Source\d*$/
  466. } keys %$a
  467. ]};
  468. delete $a->{'Source'} unless @{$a->{'Source'}->{''}};
  469. print $fh
  470. xml_tag(6, $a, 'Name'),
  471. xml_tag(6, $a, 'Version'),
  472. xml_tag(6, $a, 'Release'),
  473. xml_tag(6, $a, 'Distribution'),
  474. xml_tag(6, $a, 'Group'),
  475. xml_tag(6, $a, 'License'),
  476. xml_tag(6, $a, 'Packager'),
  477. xml_tag(6, $a, 'Summary'),
  478. xml_tag(6, $a, 'URL'),
  479. xml_tag(6, $a, 'Vendor'),
  480. xml_tag(6, $a, 'SourceRPM'),
  481. xml_tag(6, $a, 'Arch'),
  482. xml_tag(6, $a, 'Os'),
  483. xml_tag(6, $a, 'BuildRoot'),
  484. xml_tag(6, $a, 'BuildHost'),
  485. xml_tag(6, $a, 'BuildSystem'),
  486. xml_tag(6, $a, 'BuildTime'),
  487. xml_tag(6, $a, 'Relocations'),
  488. xml_tag(6, $a, 'Size'),
  489. xml_tag(6, $a, 'Prefixes'),
  490. xml_tag(6, $a, 'Platform'),
  491. xml_tag(6, $a, 'SigSize'),
  492. xml_tag(6, $a, 'SigMD5'),
  493. xml_tag(6, $a, 'SigPGP'),
  494. xml_tag(6, $a, 'SigGPG'),
  495. xml_bag(6, $a, 'BuildPreReq'),
  496. xml_bag(6, $a, 'PreReq'),
  497. xml_bag(6, $a, 'Provides'),
  498. xml_bag(6, $a, 'Conflicts'),
  499. xml_bag(6, $a, 'Source'),
  500. xml_bag(6, $a, 'Filenames'),
  501. xml_text(6, $a, 'Description');
  502. print $fh <<EOFEOF;
  503. </rdf:Description>
  504. EOFEOF
  505. }
  506. #####################################################################
  507. sub rpm2spec ($) {
  508. my($fn) = @_;
  509. my($pipe) = new FileHandle "$R2C '$fn' |"
  510. or die "FATAL: cannot read '$fn' ($!)\n";
  511. my($buf,@hdr,$n,$m,$name,$step);
  512. my($spec);
  513. while (read($pipe,$buf,110) == 110) {
  514. @hdr = unpack('a6a8a8a8a8a8a8a8a8a8a8a8a8a8',$buf);
  515. $n = hex($hdr[12]); # filename length
  516. $m = int(($n+5)/4)*4-2; # filename size (padded)
  517. last unless read($pipe,$buf,$m) == $m;
  518. $name = substr($buf,0,$n-1);
  519. $n = hex($hdr[7]); # file length
  520. $m = int(($n+3)/4)*4; # file size (padded)
  521. if ($name !~ /.spec$/) {
  522. while ($m > 0) {
  523. $step = $m > 8192 ? 8192 : $m;
  524. last unless read($pipe,$buf,$step);
  525. $m -= length($buf);
  526. }
  527. } else {
  528. if (read($pipe,$buf,$n) == $n) {
  529. $spec = $buf;
  530. }
  531. last;
  532. }
  533. }
  534. $pipe->close;
  535. return $spec;
  536. }
  537. #####################################################################
  538. sub rpm2data ($$) {
  539. my($fn,$platform) = @_;
  540. my($q,$pipe,%a);
  541. my($t,$v);
  542. $q = <<EOFEOF;
  543. Name %{Name}
  544. Version %{Version}
  545. Release %{Release}
  546. URL %{URL}
  547. Summary %{Summary}
  548. Copyright %{Copyright}
  549. License %{License}
  550. Distribution %{Distribution}
  551. Vendor %{Vendor}
  552. Group %{Group}
  553. Packager %{Packager}
  554. Prefixes %{Prefixes}
  555. BuildRoot %{BuildRoot}
  556. BuildHost %{BuildHost}
  557. BuildTime %{BuildTime}
  558. Arch %{Arch}
  559. Os %{Os}
  560. Size %{Size}
  561. SigSize %{SigSize}
  562. SigMD5 %{SigMD5}
  563. SigPGP %{SigPGP}
  564. SigGPG %{SigGPG}
  565. SourceRPM %{SourceRPM}
  566. [Patch %{Patch}
  567. ]
  568. [Source %{Source}
  569. ]
  570. [Filenames %{Filenames}
  571. ]
  572. [Conflicts %{CONFLICTNAME} %|CONFLICTFLAGS?{%{CONFLICTFLAGS:depflags} %{CONFLICTVERSION}}:{}|
  573. ]
  574. [PreReq %{REQUIRENAME} %|REQUIREFLAGS?{%{REQUIREFLAGS:depflags} %{REQUIREVERSION}}:{}|
  575. ]
  576. [Provides %{PROVIDENAME} %|PROVIDEFLAGS?{%{PROVIDEFLAGS:depflags} %{PROVIDEVERSION}}:{}|
  577. ]
  578. Description %{Description}
  579. EOFEOF
  580. $pipe = new FileHandle "$RPM -qp --qf '$q' '$fn' |"
  581. or die "FATAL: cannot read '$fn' ($!)\n";
  582. while (<$pipe>) {
  583. if (/^(\S+)\s+(.*?)\s*$/) {
  584. $t = $1;
  585. $v = $2;
  586. } elsif (/^(\s+.+?)\s*$/) {
  587. next unless defined $t;
  588. $v = $1;
  589. } else {
  590. $t = undef;
  591. next;
  592. }
  593. if (exists $a{$t}) {
  594. $a{$t} .= "\n$v";
  595. } else {
  596. $a{$t} = $v;
  597. }
  598. }
  599. $pipe->close;
  600. %a = map { $_ => $a{$_} }
  601. grep { $a{$_} ne '(none)' }
  602. keys %a;
  603. if ($a{'Relocations'} eq '(non relocatable)') {
  604. delete $a{'Relocations'};
  605. }
  606. if ($a{'SigMD5'} eq '(unknown type)') {
  607. delete $a{'SigMD5'};
  608. }
  609. $a{'Platform'} = "$a{'Arch'}-$platform-$a{'Os'}";
  610. $a{'PreReq'} =~ s/^rpmlib\(.*$//mg;
  611. $a{'Description'} = [ $a{'Description'} ];
  612. return { map {
  613. $_ => { '' => (ref $a{$_} ? $a{$_} : [ split(/\n+/, $a{$_}) ]) }
  614. } keys %a };
  615. }
  616. #####################################################################
  617. sub getindex ($) {
  618. my($dir) = @_;
  619. my(@idx) = sort { -M $a <=> -M $b; }
  620. grep { -f $_ }
  621. ( <$dir/00INDEX.rdf>, <$dir/00INDEX.rdf.*> );
  622. return unless @idx;
  623. return $idx[0];
  624. }
  625. sub list_specdir ($) {
  626. my($dir) = @_;
  627. my($dh,$d,$path);
  628. my(@list);
  629. $dh = new DirHandle($dir);
  630. while ($d = $dh->read) {
  631. next if $d =~ /^\./;
  632. $path = "$dir/$d/$d.spec";
  633. push @list, $path if -f $path;
  634. }
  635. return \@list;
  636. }
  637. sub list_rpmdir ($) {
  638. my($dir) = @_;
  639. my($dh,$d,$path);
  640. my(@list,$idx,$sub);
  641. $dh = new DirHandle($dir);
  642. while ($d = $dh->read) {
  643. next if $d =~ /^\./;
  644. $path = "$dir/$d";
  645. if (-d $path) {
  646. $idx = getindex($path);
  647. if (defined $idx) {
  648. push @list, $idx;
  649. } else {
  650. $sub = list_rpmdir($path);
  651. push @list, @$sub;
  652. undef $sub;
  653. }
  654. } else {
  655. next unless $d =~ /\.rpm$/ && -f $path;
  656. push @list, $path;
  657. }
  658. }
  659. return \@list;
  660. }
  661. #####################################################################
  662. sub readfile ($) {
  663. my($fn) = @_;
  664. my($fh) = new FileHandle "< $fn"
  665. or die "FATAL: cannot read '$fn' ($!)\n";
  666. my(@l) = <$fh>;
  667. $fh->close;
  668. return join('',@l);
  669. }
  670. sub relpath ($$) {
  671. my($prefix,$path) = @_;
  672. $path =~ s/^\Q$prefix\E\///s;
  673. return $path;
  674. }
  675. sub dirname ($) {
  676. my($path) = @_;
  677. $path =~ s/\/[^\/]*$//s;
  678. return $path.'/';
  679. }
  680. sub getresource ($) {
  681. my($fn) = @_;
  682. my($fh, $buf);
  683. if ($fn =~ /\.bz2$/) {
  684. $fh = new FileHandle "$BZ -dc $fn |"
  685. or die "FATAL: cannot read '$fn' ($!)\n";
  686. } else {
  687. $fh = new FileHandle "< $fn"
  688. or die "FATAL: cannot read '$fn' ($!)\n";
  689. }
  690. $fh->read($buf, 1024);
  691. $fh->close;
  692. if ($buf =~ /<Repository.*?rdf:resource="([^"]+)"/) {
  693. return $1;
  694. }
  695. return undef;
  696. }
  697. #####################################################################
  698. sub write_index ($$$$$$) {
  699. my($fh,$prefix,$resource,$platform,$list,$cache) = @_;
  700. my($a,$h,$r,$spec);
  701. my($mtime);
  702. foreach (@$list) {
  703. $a = undef;
  704. $h = undef;
  705. $r = undef;
  706. if (/\.spec$/) {
  707. $spec = readfile($_);
  708. $a = spec2data($spec);
  709. } elsif (/([^\/]+\.src\.rpm)$/) {
  710. $h = relpath($prefix, $_);
  711. if ($cache) {
  712. $mtime = (stat $_)[9];
  713. if (exists $cache->{"M$_"} &&
  714. $cache->{"M$_"} == $mtime) {
  715. $spec = $cache->{"S$_"};
  716. } else {
  717. $spec = rpm2spec($_);
  718. $cache->{"S$_"} = $spec;
  719. $cache->{"M$_"} = $mtime;
  720. }
  721. } else {
  722. $spec = rpm2spec($_);
  723. }
  724. $a = spec2data($spec);
  725. } elsif (/([^\/]+\.rpm)$/) {
  726. $h = relpath($prefix, $_);
  727. $a = rpm2data($_, $platform);
  728. } elsif (/([^\/]+\.rdf[^\/]*)$/) {
  729. $h = relpath($prefix, $_);
  730. $r = getresource($_) || $resource.dirname($h);
  731. }
  732. if ($a) {
  733. xml_record($fh, $a, $h);
  734. } elsif ($r) {
  735. xml_reference($fh, $r, $h);
  736. } else {
  737. warn "ERROR: cannot process $_\n";
  738. }
  739. }
  740. }
  741. #####################################################################
  742. my($prefix,$list,$fh,%cache,$tmpo);
  743. if ($#ARGV < 0) {
  744. print "openpkg:index:USAGE: $0 [-r resource] [-p platform] [-C cache.db] [-o index.rdf] [-c] [-i] dir ...\n";
  745. die "\n";
  746. }
  747. if ($opt_C) {
  748. require DB_File;
  749. tie %cache, 'DB_File', $opt_C, O_CREAT|O_RDWR, 0666, $DB_File::DB_HASH
  750. or die "FATAL: cannot tie cache '$opt_C' ($!)\n";
  751. }
  752. $opt_r = 'OpenPKG-CURRENT/Source/' unless defined $opt_r;
  753. $opt_p = 'unknown' unless defined $opt_p;
  754. if (defined $opt_o) {
  755. $tmpo = $opt_o . '.tmp';
  756. if ($opt_c) {
  757. $fh = new FileHandle "| $BZ -c > '$tmpo'"
  758. or die "FATAL: cannot write '$tmpo' ($!)\n";
  759. } else {
  760. $fh = new FileHandle "> $tmpo"
  761. or die "FATAL: cannot write '$tmpo' ($!)\n";
  762. }
  763. } else {
  764. if ($opt_c) {
  765. $fh = new FileHandle "| $BZ -c"
  766. or die "FATAL: cannot write to stdout ($!)\n";
  767. } else {
  768. $fh = new FileHandle ">&=1"
  769. or die "FATAL: cannot write to stdout ($!)\n";
  770. }
  771. }
  772. xml_head($fh, $opt_r);
  773. foreach $prefix (@ARGV) {
  774. die "FATAL: $prefix is not a directory\n" unless -d $prefix;
  775. if ($opt_i) {
  776. $list = list_rpmdir($prefix);
  777. } else {
  778. $list = list_specdir($prefix);
  779. }
  780. write_index($fh, $prefix, $opt_r, $opt_p, $list, $opt_C ? \%cache : undef);
  781. }
  782. xml_foot($fh);
  783. $fh->close
  784. or die "FATAL: write error on output ($!)\n";
  785. if (defined $tmpo) {
  786. rename $tmpo,$opt_o
  787. or die "FATAL: cannot rename $tmpo to $opt_o ($!)\n";
  788. }