This commit was generated by cvs2svn to compensate for changes in r10640,
[freeside.git] / torrus / bin / compilexml.in
1 #!@PERL@ -w
2 #  Copyright (C) 2002  Stanislav Sinyagin
3 #
4 #  This program is free software; you can redistribute it and/or modify
5 #  it under the terms of the GNU General Public License as published by
6 #  the Free Software Foundation; either version 2 of the License, or
7 #  (at your option) any later version.
8 #
9 #  This program is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #  GNU General Public License for more details.
13 #
14 #  You should have received a copy of the GNU General Public License
15 #  along with this program; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
18 # $Id: compilexml.in,v 1.1 2010-12-27 00:04:00 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21 BEGIN { require '@torrus_config_pl@'; }
22
23 use Getopt::Long;
24 use strict;
25
26 use Torrus::ConfigTree::XMLCompiler;
27 use Torrus::SiteConfig;
28 use Torrus::Log;
29
30 exit(1) if not Torrus::SiteConfig::verify();
31
32 our @trees;
33 our $all_trees;
34 our $no_ds;
35 our $no_validation;
36 our $force;
37
38 our $debug;
39 our $verbose;
40 our $help_needed;
41
42 my $ok = GetOptions ('tree=s'   => \@trees,
43                      'all'      => \$all_trees,
44                      'nods'     => \$no_ds,
45                      'noval'    => \$no_validation,
46                      'force'    => \$force,
47                      'debug'    => \$debug,
48                      'verbose'  => \$verbose,
49                      'help'     => \$help_needed);
50
51 if( not $ok or not (scalar(@trees) or $all_trees) or
52     $help_needed or scalar(@ARGV) > 0 )
53 {
54     print STDERR "Usage: $0 --tree=NAME [options...]\n",
55     "Options:\n",
56     "  --tree=NAME     tree name(s) to compile\n",
57     "  --all           compile all trees\n",
58     "  --nods          compile non-datasource configuration only\n",
59     "  --noval         disable parameter validation\n",
60     "  --force         force the compiler even if anoother " .
61         "compiler process is probably running\n",
62     "  --debug         set the log level to debug\n",
63     "  --verbose       set the log level to info\n",
64     "  --help          this help message\n";
65     exit 1;
66 }
67
68 if( $all_trees )
69 {
70     @trees = Torrus::SiteConfig::listTreeNames();
71 }
72
73 if( $debug )
74 {
75     Torrus::Log::setLevel('debug');
76 }
77 elsif( $verbose )
78 {
79     Torrus::Log::setLevel('verbose');
80 }
81
82
83 &Torrus::DB::setSafeSignalHandlers();
84
85 Verbose(sprintf('Torrus version %s', '@VERSION@'));
86
87 our $global_ok = 1;
88
89 foreach my $tree ( @trees )
90 {
91     if( not Torrus::SiteConfig::treeExists( $tree ) )
92     {
93         Error("Tree named \"" . $tree . "\" does not exist");
94         exit(1);
95     }
96
97     &Torrus::DB::checkInterrupted();
98     
99     Verbose("Compiling tree: $tree");
100
101     my $ok = 1;
102     my $compiler =
103         new Torrus::ConfigTree::XMLCompiler( -TreeName => $tree,
104                                              -NoDSRebuild => $no_ds,
105                                              -ForceWriter => $force );
106     if( not defined( $compiler ) )
107     {
108         Error('Cannot initialize compiler for tree ' . $tree . '. Exiting');
109         Error('If you are sure there are no other compiler processes ' .
110               'running, use the --force option');
111         $global_ok = 0;
112         last;
113     }     
114     
115     my @xmlFiles = @Torrus::Global::xmlAlwaysIncludeFirst;
116     push( @xmlFiles, Torrus::SiteConfig::listXmlFiles( $tree ) );
117     push( @xmlFiles, @Torrus::Global::xmlAlwaysIncludeLast );
118
119     foreach my $xmlfile ( @xmlFiles )
120     {
121         if( not $compiler->compile( $xmlfile ) )
122         {
123             Error($xmlfile . ' compiled with errors'); $ok = 0;
124         }
125     }
126
127     if( not $ok )
128     {
129         Error("Errors found during XML compilation in the tree named \"" .
130               $tree . "\"");
131         $global_ok = 0;
132         last;
133     }
134
135     Verbose('Data post-processing...');
136     if( not $compiler->postProcess() )
137     {
138         Error('Errors found during post-processing');
139         $ok = 0;
140     }
141
142     if( $no_validation )
143     {
144         Verbose('Skipping data validation...');
145     }
146     else
147     {
148         Verbose('Data validation...');
149         if( not $compiler->validate() )
150         {
151             Error('Errors found during validation process');
152             $ok = 0;
153         }
154     }
155
156     &Torrus::DB::checkInterrupted();
157     
158     # Preserve the dynamic tokenset members
159     if( not $compiler->{'first_time_created'} )
160     {
161         my $oldConfig = new Torrus::ConfigTree( -TreeName => $tree );
162         if( defined( $oldConfig ) )
163         {
164             foreach my $ts ( $oldConfig->getTsets() )
165             {
166                 if( $compiler->tsetExists( $ts ) )
167                 {
168                     foreach my $member ( $oldConfig->tsetMembers( $ts ) )
169                     {
170                         my $origin = $oldConfig->tsetMembers( $ts, $member );
171                         if( defined( $origin ) and $origin ne 'static' )
172                         {
173                             my $path = $oldConfig->path($member);
174                             if( $compiler->nodeExists( $path ) )
175                             {
176                                 my $token = $compiler->token( $path );
177                                 $compiler->tsetAddMember
178                                     ( $ts, $token, $origin );
179                                 Verbose('Preserved dynamic tokenset member: ' .
180                                         $path . ' in ' . $ts);
181                             }
182                         }
183                     }
184                 }
185             }
186         }
187
188         undef $oldConfig;
189     }
190                     
191     &Torrus::DB::checkInterrupted();
192                              
193     $compiler->finalize( $ok );
194     undef $compiler;
195     &Torrus::DB::cleanupEnvironment();
196
197     $global_ok = $ok ? $global_ok:0;
198 }
199
200 exit($global_ok ? 0:1);
201
202
203 # Local Variables:
204 # mode: perl
205 # indent-tabs-mode: nil
206 # perl-indent-level: 4
207 # End: