Implemented converter AST to Prolog
[ebuild-toys.git] / ebuild-deps
blob91a3ac0d29010285498d64ebe97b6d252f6932b7
1 #! /usr/bin/perl -w
3 use strict;
5 use Parse::RecDescent;
6 use Term::ProgressBar;
7 use Term::ANSIColor;
8 use File::Basename;
9 use Data::Dumper;
10 use Getopt::Long;
12 use constant {
13 METADATA => '/usr/portage/metadata/cache'
16 open(GRAMMAR, 'grammar.rd')
17 or die "Can't open grammar file: $!";
19 my $grammar = do {
20 local $/;
21 <GRAMMAR>;
24 close(GRAMMAR);
26 sub usage {
27 print <<'END_OF_USAGE';
28 ebuild-deps [options]
30 General options:
31 --log <str> * Put garbage here
32 --[no-]meta * Parse metadata
34 Validation options:
35 --try-data | -d <str> * Validate a string
36 --try-file | -f <str> * Validate a file
38 (this is only for testing purpose)
40 Usage exmaples:
42 [1] ebuild-deps --log meta.log
44 clean-up the log file ;)
46 [2] ebuild-deps --log meta.log --meta
48 parse everyting in /usr/portage/metadata/cache
50 [3] ebuild-deps --try-data '>=app-some/pkg-1.0'
52 validate trivial and not so trivial dependencies
54 END_OF_USAGE
56 exit 0;
59 my $log = '/dev/null';
60 my $meta = 0;
62 my @todo_data = ();
63 my @todo_file = ();
65 my $result = GetOptions(
66 'log=s' => \$log,
67 'meta!' => \$meta,
68 'try-data|d=s' => \@todo_data,
69 'try-file|f=s' => \@todo_file,
70 'help|h' => sub { usage() },
73 open(LOG, '>', $log)
74 or die "Can't open ${log}: $! \n";
76 $Parse::RecDescent::skip = '';
78 my $parser = Parse::RecDescent->new($grammar)
79 or die "Invalid grammar specification";
81 my $index = 0;
83 for my $text (@todo_data) {
84 my $result = $parser->start($text);
86 print ">>> Input #$index ... ";
88 if (defined $result) {
89 print colored('OK', 'bold green'), "\n";
90 print Dumper($result);
91 } else {
92 print colored('FAILED', 'bold red'), "\n";
95 $index++;
98 for my $file (@todo_file) {
99 open(INPUT, $file)
100 or die "Can't open ${file}: $! \n";
102 my $text = do {
103 local $/;
104 <INPUT>;
107 close(INPUT);
109 my $result = $parser->start($text);
111 print ">>> File '$file' ... ";
113 if (defined $result) {
114 print colored('OK', 'bold green'), "\n";
115 print Dumper($result);
116 } else {
117 print colored('FAILED', 'bold red'), "\n";
121 if (not $meta) {
122 if (not @todo_data and not @todo_file) {
123 print "Seems nothing to be done. Bye.\n";
126 goto cleanup;
129 my @paths = glob(METADATA . '/*');
130 my $count = scalar @paths;
131 my $sofar = 0;
133 my $progress = Term::ProgressBar->new({
134 count => $count, ETA => 'linear'});
136 my ($pkg_ok, $pkg_failed) = (0, 0);
138 for my $path (@paths) {
139 if (-d $path) {
140 for my $file (glob($path . '/*')) {
141 my $category = basename($path);
142 my $package = basename($file);
144 print LOG $category, '/', $package, ' ... ';
146 open(INPUT, $file)
147 or die "Can't open ${file}: $! \n";
149 my $depend = <INPUT>;
151 my $result = $parser->start($depend);
153 if (defined $result) {
154 print LOG "OK\n";
155 $pkg_ok++;
156 } else {
157 print LOG "FAILED\n";
158 $pkg_failed++;
161 close(INPUT);
165 $progress->update(++$sofar);
168 if ($pkg_failed == 0) {
169 print "\n\nProcessed $pkg_ok versions. All OK. Bye.\n";
172 cleanup:
173 close(LOG);