openpkg-index.pl 24 KB

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