xref: /aoo41x/main/solenv/bin/modules/SourceConfig.pm (revision cdf0e10c)
1*cdf0e10cSrcweir#*************************************************************************
2*cdf0e10cSrcweir#
3*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir#
5*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir#
7*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir#
9*cdf0e10cSrcweir# This file is part of OpenOffice.org.
10*cdf0e10cSrcweir#
11*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir#
15*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir#
21*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir#
26*cdf0e10cSrcweir#*************************************************************************
27*cdf0e10cSrcweir
28*cdf0e10cSrcweir#*************************************************************************
29*cdf0e10cSrcweir#
30*cdf0e10cSrcweir# SourceConfig - Perl extension for parsing general info databases
31*cdf0e10cSrcweir#
32*cdf0e10cSrcweir# usage: see below
33*cdf0e10cSrcweir#
34*cdf0e10cSrcweir#*************************************************************************
35*cdf0e10cSrcweir
36*cdf0e10cSrcweirpackage SourceConfig;
37*cdf0e10cSrcweir
38*cdf0e10cSrcweiruse strict;
39*cdf0e10cSrcweir
40*cdf0e10cSrcweiruse constant SOURCE_CONFIG_FILE_NAME => 'source_config';
41*cdf0e10cSrcweiruse constant SOURCE_CONFIG_VERSION => 3;
42*cdf0e10cSrcweir
43*cdf0e10cSrcweiruse Carp;
44*cdf0e10cSrcweiruse Cwd;
45*cdf0e10cSrcweiruse RepositoryHelper;
46*cdf0e10cSrcweiruse File::Basename;
47*cdf0e10cSrcweiruse File::Temp qw(tmpnam);
48*cdf0e10cSrcweir
49*cdf0e10cSrcweirmy $debug = 0;
50*cdf0e10cSrcweir
51*cdf0e10cSrcweir#####  profiling #####
52*cdf0e10cSrcweir
53*cdf0e10cSrcweir##### ctor #####
54*cdf0e10cSrcweir
55*cdf0e10cSrcweirsub new {
56*cdf0e10cSrcweir    my $proto = shift;
57*cdf0e10cSrcweir    my $class = ref($proto) || $proto;
58*cdf0e10cSrcweir    my $source_root = shift;
59*cdf0e10cSrcweir    my $self = {};
60*cdf0e10cSrcweir    $self->{USER_SOURCE_ROOT} = undef;
61*cdf0e10cSrcweir    $self->{SOURCE_CONFIG_FILE} = undef;
62*cdf0e10cSrcweir    if (defined $source_root) {
63*cdf0e10cSrcweir        $source_root = Cwd::realpath($source_root);
64*cdf0e10cSrcweir        $source_root =~ s/\\|\/$//;
65*cdf0e10cSrcweir        if (-f $source_root) {
66*cdf0e10cSrcweir            # We have path to source_config
67*cdf0e10cSrcweir            if (File::Basename::basename($source_root) eq 'source_config') {
68*cdf0e10cSrcweir                # We have path to source_config
69*cdf0e10cSrcweir                $self->{SOURCE_CONFIG_FILE} = $source_root;
70*cdf0e10cSrcweir                $source_root = File::Basename::dirname($source_root);
71*cdf0e10cSrcweir            } else {
72*cdf0e10cSrcweir                croak("$source_root is not a source_config file");
73*cdf0e10cSrcweir            };
74*cdf0e10cSrcweir        } else {
75*cdf0e10cSrcweir            $self->{USER_SOURCE_ROOT} = $source_root;
76*cdf0e10cSrcweir            $source_root .= '/..';
77*cdf0e10cSrcweir        }
78*cdf0e10cSrcweir    } else {
79*cdf0e10cSrcweir        $source_root = $ENV{SOURCE_ROOT_DIR};
80*cdf0e10cSrcweir    };
81*cdf0e10cSrcweir    $source_root = Cwd::realpath($source_root);
82*cdf0e10cSrcweir    $self->{SOURCE_ROOT} = $source_root;
83*cdf0e10cSrcweir    $self->{DEBUG} = 0;
84*cdf0e10cSrcweir    $self->{VERBOSE} = 0;
85*cdf0e10cSrcweir    $self->{REPOSITORIES} = {};
86*cdf0e10cSrcweir    $self->{ACTIVATED_REPOSITORIES} = {};
87*cdf0e10cSrcweir    $self->{MODULE_PATHS} = {};
88*cdf0e10cSrcweir    $self->{MODULE_BUILD_LIST_PATHS} = {};
89*cdf0e10cSrcweir    $self->{ACTIVATED_MODULES} = {};
90*cdf0e10cSrcweir    $self->{MODULE_REPOSITORY} = {};
91*cdf0e10cSrcweir    $self->{REAL_MODULES} = {};
92*cdf0e10cSrcweir    $self->{NEW_MODULES} = [];
93*cdf0e10cSrcweir    $self->{REMOVE_MODULES} = {};
94*cdf0e10cSrcweir    $self->{REMOVE_REPOSITORIES} = {};
95*cdf0e10cSrcweir    $self->{NEW_REPOSITORIES} = [];
96*cdf0e10cSrcweir    $self->{WARNINGS} = [];
97*cdf0e10cSrcweir    $self->{REPORT_MESSAGES} = [];
98*cdf0e10cSrcweir    $self->{CONFIG_FILE_CONTENT} = [];
99*cdf0e10cSrcweir    if (defined $self->{USER_SOURCE_ROOT}) {
100*cdf0e10cSrcweir        ${$self->{REPOSITORIES}}{File::Basename::basename($self->{USER_SOURCE_ROOT})} = $self->{USER_SOURCE_ROOT};
101*cdf0e10cSrcweir    };
102*cdf0e10cSrcweir    $self->{SOURCE_CONFIG_FILE} = get_config_file($self->{SOURCE_ROOT}) if (!defined $self->{SOURCE_CONFIG_FILE});
103*cdf0e10cSrcweir    $self->{SOURCE_CONFIG_DEFAULT} = $self->{SOURCE_ROOT} .'/'.SOURCE_CONFIG_FILE_NAME;
104*cdf0e10cSrcweir    if (defined $self->{USER_SOURCE_ROOT}) {
105*cdf0e10cSrcweir        ${$self->{REPOSITORIES}}{File::Basename::basename($self->{USER_SOURCE_ROOT})} = $self->{USER_SOURCE_ROOT};
106*cdf0e10cSrcweir    };
107*cdf0e10cSrcweir    read_config_file($self);
108*cdf0e10cSrcweir   	get_module_paths($self);
109*cdf0e10cSrcweir    bless($self, $class);
110*cdf0e10cSrcweir    return $self;
111*cdf0e10cSrcweir}
112*cdf0e10cSrcweir
113*cdf0e10cSrcweir##### methods #####
114*cdf0e10cSrcweir
115*cdf0e10cSrcweirsub get_version {
116*cdf0e10cSrcweir    return SOURCE_CONFIG_VERSION;
117*cdf0e10cSrcweir};
118*cdf0e10cSrcweir
119*cdf0e10cSrcweirsub get_repositories
120*cdf0e10cSrcweir{
121*cdf0e10cSrcweir    my $self        = shift;
122*cdf0e10cSrcweir    return sort keys %{$self->{REPOSITORIES}};
123*cdf0e10cSrcweir}
124*cdf0e10cSrcweir
125*cdf0e10cSrcweirsub add_repository
126*cdf0e10cSrcweir{
127*cdf0e10cSrcweir    my $self        = shift;
128*cdf0e10cSrcweir    my $new_rep_path = shift;
129*cdf0e10cSrcweir    $new_rep_path = Cwd::realpath($new_rep_path);
130*cdf0e10cSrcweir    my $new_rep_name = File::Basename::basename($new_rep_path);
131*cdf0e10cSrcweir    if (defined ${$self->{REPOSITORIES}}{$new_rep_name}) {
132*cdf0e10cSrcweir        croak("Repository $new_rep_name is already defined!!");
133*cdf0e10cSrcweir    };
134*cdf0e10cSrcweir    ${$self->{REPOSITORIES}}{$new_rep_name} = $new_rep_path;
135*cdf0e10cSrcweir    $self -> get_repository_module_paths($new_rep_name);
136*cdf0e10cSrcweir}
137*cdf0e10cSrcweir
138*cdf0e10cSrcweirsub get_config_file_default_path {
139*cdf0e10cSrcweir    my $self        = shift;
140*cdf0e10cSrcweir    return $self->{SOURCE_CONFIG_DEFAULT};
141*cdf0e10cSrcweir}
142*cdf0e10cSrcweir
143*cdf0e10cSrcweirsub get_config_file_path {
144*cdf0e10cSrcweir    my $self = shift;
145*cdf0e10cSrcweir    return $self->{SOURCE_CONFIG_FILE};
146*cdf0e10cSrcweir}
147*cdf0e10cSrcweir
148*cdf0e10cSrcweirsub get_module_repository {
149*cdf0e10cSrcweir    my $self = shift;
150*cdf0e10cSrcweir    my $module = shift;
151*cdf0e10cSrcweir    if (defined ${$self->{MODULE_REPOSITORY}}{$module}) {
152*cdf0e10cSrcweir        return ${$self->{MODULE_REPOSITORY}}{$module};
153*cdf0e10cSrcweir    } else {
154*cdf0e10cSrcweir        Carp::cluck("No such module $module in active repositories!!\n");
155*cdf0e10cSrcweir        return undef;
156*cdf0e10cSrcweir    };
157*cdf0e10cSrcweir}
158*cdf0e10cSrcweir
159*cdf0e10cSrcweirsub get_module_path {
160*cdf0e10cSrcweir    my $self = shift;
161*cdf0e10cSrcweir    my $module = shift;
162*cdf0e10cSrcweir    if (defined ${$self->{MODULE_PATHS}}{$module}) {
163*cdf0e10cSrcweir        return ${$self->{MODULE_PATHS}}{$module};
164*cdf0e10cSrcweir    } else {
165*cdf0e10cSrcweir        Carp::cluck("No path for module $module in active repositories!!\n") if ($debug);
166*cdf0e10cSrcweir        return undef;
167*cdf0e10cSrcweir    };
168*cdf0e10cSrcweir}
169*cdf0e10cSrcweir
170*cdf0e10cSrcweirsub get_module_build_list {
171*cdf0e10cSrcweir    my $self = shift;
172*cdf0e10cSrcweir    my $module = shift;
173*cdf0e10cSrcweir    if (defined ${$self->{MODULE_BUILD_LIST_PATHS}}{$module}) {
174*cdf0e10cSrcweir        return ${$self->{MODULE_BUILD_LIST_PATHS}}{$module};
175*cdf0e10cSrcweir    } else {
176*cdf0e10cSrcweir        my @possible_build_lists = ('build.lst', 'build.xlist'); # build lists names
177*cdf0e10cSrcweir        foreach (@possible_build_lists) {
178*cdf0e10cSrcweir            my $possible_path = ${$self->{MODULE_PATHS}}{$module} . "/prj/$_";
179*cdf0e10cSrcweir            if (-e $possible_path) {
180*cdf0e10cSrcweir                ${$self->{MODULE_BUILD_LIST_PATHS}}{$module} = $possible_path;
181*cdf0e10cSrcweir                return $possible_path;
182*cdf0e10cSrcweir            };
183*cdf0e10cSrcweir        };
184*cdf0e10cSrcweir        Carp::cluck("No build list in module $module found!!\n") if ($self->{DEBUG});
185*cdf0e10cSrcweir        return undef;
186*cdf0e10cSrcweir    };
187*cdf0e10cSrcweir}
188*cdf0e10cSrcweir
189*cdf0e10cSrcweirsub get_all_modules
190*cdf0e10cSrcweir{
191*cdf0e10cSrcweir    my $self = shift;
192*cdf0e10cSrcweir    my $module = shift;
193*cdf0e10cSrcweir    return sort keys %{$self->{MODULE_PATHS}};
194*cdf0e10cSrcweir};
195*cdf0e10cSrcweir
196*cdf0e10cSrcweirsub get_active_modules
197*cdf0e10cSrcweir{
198*cdf0e10cSrcweir    my $self        = shift;
199*cdf0e10cSrcweir    if (scalar keys %{$self->{ACTIVATED_MODULES}}) {
200*cdf0e10cSrcweir        return sort keys %{$self->{ACTIVATED_MODULES}};
201*cdf0e10cSrcweir	}
202*cdf0e10cSrcweir   	return sort keys %{$self->{REAL_MODULES}};
203*cdf0e10cSrcweir}
204*cdf0e10cSrcweir
205*cdf0e10cSrcweirsub is_active
206*cdf0e10cSrcweir{
207*cdf0e10cSrcweir    my $self        = shift;
208*cdf0e10cSrcweir    my $module		= shift;
209*cdf0e10cSrcweir    if (scalar keys %{$self->{ACTIVATED_MODULES}}) {
210*cdf0e10cSrcweir        return exists ($self->{ACTIVATED_MODULES}{$module});
211*cdf0e10cSrcweir	}
212*cdf0e10cSrcweir    return exists ($self->{REAL_MODULES}{$module});
213*cdf0e10cSrcweir}
214*cdf0e10cSrcweir
215*cdf0e10cSrcweir##### private methods #####
216*cdf0e10cSrcweir
217*cdf0e10cSrcweirsub get_repository_module_paths {
218*cdf0e10cSrcweir    my $self        = shift;
219*cdf0e10cSrcweir    my $repository        = shift;
220*cdf0e10cSrcweir    my $repository_path = ${$self->{REPOSITORIES}}{$repository};
221*cdf0e10cSrcweir    if (opendir DIRHANDLE, $repository_path) {
222*cdf0e10cSrcweir        foreach my $module (readdir(DIRHANDLE)) {
223*cdf0e10cSrcweir            next if (($module =~ /^\.+/) || (!-d "$repository_path/$module"));
224*cdf0e10cSrcweir            my $module_entry = $module;
225*cdf0e10cSrcweir            if (($module !~ s/\.lnk$//) && ($module !~ s/\.link$//)) {
226*cdf0e10cSrcweir                $self->{REAL_MODULES}{$module}++;
227*cdf0e10cSrcweir            }
228*cdf0e10cSrcweir            my $possible_path = "$repository_path/$module_entry";
229*cdf0e10cSrcweir            if (-d $possible_path) {
230*cdf0e10cSrcweir                if (defined ${$self->{MODULE_PATHS}}{$module}) {
231*cdf0e10cSrcweir                    close DIRHANDLE;
232*cdf0e10cSrcweir                    croak("Ambiguous paths for module $module: $possible_path and " . ${$self->{MODULE_PATHS}}{$module});
233*cdf0e10cSrcweir                };
234*cdf0e10cSrcweir                ${$self->{MODULE_PATHS}}{$module} = $possible_path;
235*cdf0e10cSrcweir                ${$self->{MODULE_REPOSITORY}}{$module} = $repository;
236*cdf0e10cSrcweir            }
237*cdf0e10cSrcweir        };
238*cdf0e10cSrcweir        close DIRHANDLE;
239*cdf0e10cSrcweir    } else {
240*cdf0e10cSrcweir        croak("Cannot read $repository_path repository content");
241*cdf0e10cSrcweir    };
242*cdf0e10cSrcweir};
243*cdf0e10cSrcweir
244*cdf0e10cSrcweirsub get_module_paths {
245*cdf0e10cSrcweir    my $self        = shift;
246*cdf0e10cSrcweir    foreach my $repository (keys %{$self->{REPOSITORIES}}) {
247*cdf0e10cSrcweir        get_repository_module_paths($self, $repository);
248*cdf0e10cSrcweir    };
249*cdf0e10cSrcweir    my @false_actives = ();
250*cdf0e10cSrcweir    foreach (keys %{$self->{ACTIVATED_MODULES}}) {
251*cdf0e10cSrcweir        push(@false_actives, $_) if (!defined  ${$self->{MODULE_PATHS}}{$_});
252*cdf0e10cSrcweir    };
253*cdf0e10cSrcweir    croak("Error!! Activated module(s): @false_actives\nnot found in the active repositories!! Please check your " . $self->{SOURCE_CONFIG_FILE} . "\n") if (scalar @false_actives);
254*cdf0e10cSrcweir    croak("No modules found!") if (!scalar keys %{$self->{MODULE_PATHS}});
255*cdf0e10cSrcweir};
256*cdf0e10cSrcweir
257*cdf0e10cSrcweirsub get_config_file {
258*cdf0e10cSrcweir    my $source_root = shift;
259*cdf0e10cSrcweir    my $possible_path = $source_root . '/' . SOURCE_CONFIG_FILE_NAME;
260*cdf0e10cSrcweir    return $possible_path if (-f $possible_path);
261*cdf0e10cSrcweir    return '';
262*cdf0e10cSrcweir};
263*cdf0e10cSrcweir
264*cdf0e10cSrcweir#
265*cdf0e10cSrcweir# Fallback - fallback repository is based on RepositoryHelper educated guess
266*cdf0e10cSrcweir#
267*cdf0e10cSrcweirsub get_fallback_repository {
268*cdf0e10cSrcweir    my $self = shift;
269*cdf0e10cSrcweir    my $repository_root = RepositoryHelper->new()->get_repository_root();
270*cdf0e10cSrcweir    ${$self->{REPOSITORIES}}{File::Basename::basename($repository_root)} = $repository_root;
271*cdf0e10cSrcweir};
272*cdf0e10cSrcweir
273*cdf0e10cSrcweirsub read_config_file {
274*cdf0e10cSrcweir    my $self = shift;
275*cdf0e10cSrcweir    if (!$self->{SOURCE_CONFIG_FILE}) {
276*cdf0e10cSrcweir        if (!defined $self->{USER_SOURCE_ROOT}) {
277*cdf0e10cSrcweir            get_fallback_repository($self);
278*cdf0e10cSrcweir        };
279*cdf0e10cSrcweir        return;
280*cdf0e10cSrcweir    };
281*cdf0e10cSrcweir    my $repository_section = 0;
282*cdf0e10cSrcweir    my $module_section = 0;
283*cdf0e10cSrcweir    my $line = 0;
284*cdf0e10cSrcweir    my @file_content = ();
285*cdf0e10cSrcweir
286*cdf0e10cSrcweir    if (open(SOURCE_CONFIG_FILE, $self->{SOURCE_CONFIG_FILE})) {
287*cdf0e10cSrcweir        foreach (<SOURCE_CONFIG_FILE>) {
288*cdf0e10cSrcweir            push (@{$self->{CONFIG_FILE_CONTENT}}, $_);
289*cdf0e10cSrcweir            $line++;
290*cdf0e10cSrcweir            chomp;
291*cdf0e10cSrcweir            next if (!/^\S+/);
292*cdf0e10cSrcweir            next if (/^\s*#+/);
293*cdf0e10cSrcweir            s/\r\n//;
294*cdf0e10cSrcweir            if (/^\[repositories\]\s*(\s+#)*/) {
295*cdf0e10cSrcweir                $module_section = 0;
296*cdf0e10cSrcweir                $repository_section = 1;
297*cdf0e10cSrcweir                next;
298*cdf0e10cSrcweir            };
299*cdf0e10cSrcweir            if (/^\[modules\]\s*(\s+#)*/) {
300*cdf0e10cSrcweir                $module_section = 1;
301*cdf0e10cSrcweir                $repository_section = 0;
302*cdf0e10cSrcweir                next;
303*cdf0e10cSrcweir            };
304*cdf0e10cSrcweir            next if (!$repository_section && !$module_section);
305*cdf0e10cSrcweir            if (/\s*(\S+)=active\s*(\s+#)*/) {
306*cdf0e10cSrcweir                if ($repository_section) {
307*cdf0e10cSrcweir                    my $repository_source_path = $self->{SOURCE_ROOT} . "/$1";
308*cdf0e10cSrcweir                    if (defined $ENV{UPDMINOREXT}) {
309*cdf0e10cSrcweir                        $repository_source_path .= $ENV{UPDMINOREXT};
310*cdf0e10cSrcweir                        if (defined ${$self->{REPOSITORIES}}{$1.$ENV{UPDMINOREXT}}) {
311*cdf0e10cSrcweir                            delete ${$self->{REPOSITORIES}}{$1.$ENV{UPDMINOREXT}};
312*cdf0e10cSrcweir                        };
313*cdf0e10cSrcweir                    };
314*cdf0e10cSrcweir                    ${$self->{REPOSITORIES}}{$1} = $repository_source_path;
315*cdf0e10cSrcweir                    ${$self->{ACTIVATED_REPOSITORIES}}{$1}++;
316*cdf0e10cSrcweir                    next;
317*cdf0e10cSrcweir                }
318*cdf0e10cSrcweir                if ($module_section) {
319*cdf0e10cSrcweir                    ${$self->{ACTIVATED_MODULES}}{$1}++;
320*cdf0e10cSrcweir                    next;
321*cdf0e10cSrcweir                };
322*cdf0e10cSrcweir            };
323*cdf0e10cSrcweir            croak("Line $line in " . $self->{SOURCE_CONFIG_FILE} . ' violates format. Please make your checks!');
324*cdf0e10cSrcweir        };
325*cdf0e10cSrcweir        close SOURCE_CONFIG_FILE;
326*cdf0e10cSrcweir        if (!scalar keys %{$self->{REPOSITORIES}}) {
327*cdf0e10cSrcweir            get_fallback_repository($self);
328*cdf0e10cSrcweir        };
329*cdf0e10cSrcweir    } else {
330*cdf0e10cSrcweir        croak('Cannot open ' . $self->{SOURCE_CONFIG_FILE} . ' for reading');
331*cdf0e10cSrcweir    };
332*cdf0e10cSrcweir};
333*cdf0e10cSrcweir
334*cdf0e10cSrcweirsub remove_all_activated_repositories {
335*cdf0e10cSrcweir    my $self = shift;
336*cdf0e10cSrcweir    $self->remove_activated_repositories([keys %{$self->{ACTIVATED_REPOSITORIES}}]);
337*cdf0e10cSrcweir};
338*cdf0e10cSrcweir
339*cdf0e10cSrcweirsub remove_activated_repositories {
340*cdf0e10cSrcweir    my $self = shift;
341*cdf0e10cSrcweir    my $new_repositories_ref = shift;
342*cdf0e10cSrcweir    push(@{$self->{WARNINGS}}, "\nWARNING: Empty repository list passed for removing from source_config\n") if (!scalar @$new_repositories_ref);
343*cdf0e10cSrcweir    $self->{VERBOSE} = shift;
344*cdf0e10cSrcweir    $self->{REMOVE_REPOSITORIES} = {};
345*cdf0e10cSrcweir    foreach (@$new_repositories_ref) {
346*cdf0e10cSrcweir        if (!defined ${$self->{ACTIVATED_REPOSITORIES}}{$_}) {
347*cdf0e10cSrcweir            push (@{$self->{WARNINGS}}, "\nWARNING: repository $_ is not activated in ". $self->get_config_file_default_path()."\n");
348*cdf0e10cSrcweir        } else {
349*cdf0e10cSrcweir            ${$self->{REMOVE_REPOSITORIES}}{$_}++;
350*cdf0e10cSrcweir            delete ${$self->{ACTIVATED_REPOSITORIES}}{$_};
351*cdf0e10cSrcweir        };
352*cdf0e10cSrcweir    };
353*cdf0e10cSrcweir    generate_config_file($self);
354*cdf0e10cSrcweir};
355*cdf0e10cSrcweir
356*cdf0e10cSrcweirsub remove_all_activated_modules {
357*cdf0e10cSrcweir    my $self = shift;
358*cdf0e10cSrcweir    $self->remove_activated_modules([keys %{$self->{ACTIVATED_MODULES}}]);
359*cdf0e10cSrcweir};
360*cdf0e10cSrcweir
361*cdf0e10cSrcweirsub remove_activated_modules {
362*cdf0e10cSrcweir    my $self = shift;
363*cdf0e10cSrcweir    my $new_modules_ref = shift;
364*cdf0e10cSrcweir    push(@{$self->{WARNINGS}}, "\nWARNING: Empty module list passed for removing from source_config\n") if (!scalar @$new_modules_ref);
365*cdf0e10cSrcweir    $self->{VERBOSE} = shift;
366*cdf0e10cSrcweir    $self->{REMOVE_MODULES} = {};
367*cdf0e10cSrcweir    foreach (@$new_modules_ref) {
368*cdf0e10cSrcweir        if (!defined ${$self->{ACTIVATED_MODULES}}{$_}) {
369*cdf0e10cSrcweir            push (@{$self->{WARNINGS}}, "\nWARNING: module $_ is not activated in ". $self->get_config_file_default_path()."\n");
370*cdf0e10cSrcweir        } else {
371*cdf0e10cSrcweir            ${$self->{REMOVE_MODULES}}{$_}++;
372*cdf0e10cSrcweir            delete ${$self->{ACTIVATED_MODULES}}{$_};
373*cdf0e10cSrcweir        };
374*cdf0e10cSrcweir    };
375*cdf0e10cSrcweir    generate_config_file($self);
376*cdf0e10cSrcweir};
377*cdf0e10cSrcweir
378*cdf0e10cSrcweirsub add_active_repositories {
379*cdf0e10cSrcweir    my $self = shift;
380*cdf0e10cSrcweir    $self->{NEW_REPOSITORIES} = shift;
381*cdf0e10cSrcweir    croak('Empty repository list passed for addition to source_config') if (!scalar @{$self->{NEW_REPOSITORIES}});
382*cdf0e10cSrcweir    $self->{VERBOSE} = shift;
383*cdf0e10cSrcweir    foreach (@{$self->{NEW_REPOSITORIES}}) {
384*cdf0e10cSrcweir        $self->add_repository($_);
385*cdf0e10cSrcweir    };
386*cdf0e10cSrcweir    generate_config_file($self);
387*cdf0e10cSrcweir};
388*cdf0e10cSrcweir
389*cdf0e10cSrcweirsub add_active_modules {
390*cdf0e10cSrcweir    my $self = shift;
391*cdf0e10cSrcweir    my $module_list_ref = shift;
392*cdf0e10cSrcweir    my $ignored_modules_string = '';
393*cdf0e10cSrcweir    my @real_modules = ();
394*cdf0e10cSrcweir    foreach my $module (sort @$module_list_ref) {
395*cdf0e10cSrcweir        if ($self->get_module_path($module)) {
396*cdf0e10cSrcweir            push(@real_modules, $module);
397*cdf0e10cSrcweir        } else {
398*cdf0e10cSrcweir            $ignored_modules_string .= " $module";
399*cdf0e10cSrcweir        };
400*cdf0e10cSrcweir    };
401*cdf0e10cSrcweir    push (@{$self->{WARNINGS}}, "\nWARNING: following modules are not found in active repositories, and have not been added to the " . $self->get_config_file_default_path() . ":$ignored_modules_string\n") if ($ignored_modules_string);
402*cdf0e10cSrcweir    $self->{NEW_MODULES} = \@real_modules;
403*cdf0e10cSrcweir    croak('Empty module list passed for addition to source_config') if (!scalar @{$self->{NEW_MODULES}});
404*cdf0e10cSrcweir    $self->{VERBOSE} = shift;
405*cdf0e10cSrcweir    generate_config_file($self);
406*cdf0e10cSrcweir};
407*cdf0e10cSrcweir
408*cdf0e10cSrcweirsub add_content {
409*cdf0e10cSrcweir    my $self = shift;
410*cdf0e10cSrcweir    my $content = shift;
411*cdf0e10cSrcweir    my $entries_to_add = shift;
412*cdf0e10cSrcweir    return if (!scalar @$entries_to_add);
413*cdf0e10cSrcweir    my $message;
414*cdf0e10cSrcweir    my $message_part1;
415*cdf0e10cSrcweir    my $warning_message;
416*cdf0e10cSrcweir    my $activated_entries;
417*cdf0e10cSrcweir
418*cdf0e10cSrcweir    if ($entries_to_add == $self->{NEW_MODULES}) {
419*cdf0e10cSrcweir        $self->{NEW_MODULES} = [];
420*cdf0e10cSrcweir        $message_part1 = "Module(s):\n";
421*cdf0e10cSrcweir        $activated_entries = $self->{ACTIVATED_MODULES};
422*cdf0e10cSrcweir    } elsif ($entries_to_add == $self->{NEW_REPOSITORIES}) {
423*cdf0e10cSrcweir        $self->{NEW_REPOSITORIES} = [];
424*cdf0e10cSrcweir        $message_part1 = "Repositories:\n";
425*cdf0e10cSrcweir        $activated_entries = $self->{ACTIVATED_REPOSITORIES};
426*cdf0e10cSrcweir    };
427*cdf0e10cSrcweir    foreach my $entry (@$entries_to_add) {
428*cdf0e10cSrcweir        if (defined $$activated_entries{$entry}) {
429*cdf0e10cSrcweir            $warning_message .= "$entry "
430*cdf0e10cSrcweir        } else {
431*cdf0e10cSrcweir            push(@$content, "$entry=active\n");
432*cdf0e10cSrcweir            ${$activated_entries}{$entry}++;
433*cdf0e10cSrcweir            $message .= "$entry "
434*cdf0e10cSrcweir        };
435*cdf0e10cSrcweir    };
436*cdf0e10cSrcweir
437*cdf0e10cSrcweir    push(@{$self->{REPORT_MESSAGES}}, "\n$message_part1 $message\nhave been added to the ". $self->get_config_file_default_path()."\n") if ($message);
438*cdf0e10cSrcweir    push (@{$self->{WARNINGS}}, "\nWARNING: $message_part1 $warning_message\nare already added to the ". $self->get_config_file_default_path()."\n") if ($warning_message);
439*cdf0e10cSrcweir};
440*cdf0e10cSrcweir
441*cdf0e10cSrcweirsub generate_config_file {
442*cdf0e10cSrcweir    my $self = shift;
443*cdf0e10cSrcweir    my @config_content_new = ();
444*cdf0e10cSrcweir    my ($module_section, $repository_section);
445*cdf0e10cSrcweir    my %removed_modules = ();
446*cdf0e10cSrcweir    my %removed_repositories = ();
447*cdf0e10cSrcweir    foreach (@{$self->{CONFIG_FILE_CONTENT}}) {
448*cdf0e10cSrcweir        if (/^\[repositories\]\s*(\s+#)*/) {
449*cdf0e10cSrcweir            if ($module_section) {
450*cdf0e10cSrcweir                $self->add_content(\@config_content_new, $self->{NEW_MODULES});
451*cdf0e10cSrcweir            };
452*cdf0e10cSrcweir            $module_section = 0;
453*cdf0e10cSrcweir            $repository_section = 1;
454*cdf0e10cSrcweir        };
455*cdf0e10cSrcweir        if (/^\[modules\]\s*(\s+#)*/) {
456*cdf0e10cSrcweir            if ($repository_section) {
457*cdf0e10cSrcweir                $self->add_content(\@config_content_new, $self->{NEW_REPOSITORIES});
458*cdf0e10cSrcweir            };
459*cdf0e10cSrcweir            $module_section = 1;
460*cdf0e10cSrcweir            $repository_section = 0;
461*cdf0e10cSrcweir        };
462*cdf0e10cSrcweir        if ($module_section && /\s*(\S+)=active\s*(\s+#)*/) {
463*cdf0e10cSrcweir            if (defined ${$self->{REMOVE_MODULES}}{$1}) {
464*cdf0e10cSrcweir                $removed_modules{$1}++;
465*cdf0e10cSrcweir                next;
466*cdf0e10cSrcweir            };
467*cdf0e10cSrcweir        }
468*cdf0e10cSrcweir        if ($repository_section && /\s*(\S+)=active\s*(\s+#)*/) {
469*cdf0e10cSrcweir            if (defined ${$self->{REMOVE_REPOSITORIES}}{$1}) {
470*cdf0e10cSrcweir                $removed_repositories{$1}++;
471*cdf0e10cSrcweir                next;
472*cdf0e10cSrcweir            };
473*cdf0e10cSrcweir        }
474*cdf0e10cSrcweir        push(@config_content_new, $_);
475*cdf0e10cSrcweir    };
476*cdf0e10cSrcweir    if (scalar @{$self->{NEW_MODULES}}) {
477*cdf0e10cSrcweir        push(@config_content_new, "[modules]\n") if (!$module_section);
478*cdf0e10cSrcweir        $self->add_content(\@config_content_new, $self->{NEW_MODULES});
479*cdf0e10cSrcweir    };
480*cdf0e10cSrcweir    if (scalar @{$self->{NEW_REPOSITORIES}}) {
481*cdf0e10cSrcweir        push(@config_content_new, "[repositories]\n") if (!$repository_section);
482*cdf0e10cSrcweir        $self->add_content(\@config_content_new, $self->{NEW_REPOSITORIES});
483*cdf0e10cSrcweir    };
484*cdf0e10cSrcweir    if (scalar keys %removed_modules) {
485*cdf0e10cSrcweir        my @deleted_modules = keys %removed_modules;
486*cdf0e10cSrcweir        push(@{$self->{REPORT_MESSAGES}}, "\nModules: @deleted_modules\nhave been removed from the ". $self->get_config_file_default_path()."\n");
487*cdf0e10cSrcweir
488*cdf0e10cSrcweir    };
489*cdf0e10cSrcweir    if (scalar keys %removed_repositories) {
490*cdf0e10cSrcweir        my @deleted_repositories = keys %removed_repositories;
491*cdf0e10cSrcweir        push(@{$self->{REPORT_MESSAGES}}, "\nRepositories: @deleted_repositories\nhave been removed from the ". $self->get_config_file_default_path()."\n");
492*cdf0e10cSrcweir
493*cdf0e10cSrcweir    };
494*cdf0e10cSrcweir
495*cdf0e10cSrcweir    # Writing file, printing warnings and reports
496*cdf0e10cSrcweir
497*cdf0e10cSrcweir    #check if we need to write a new file
498*cdf0e10cSrcweir    my $write_needed = 0;
499*cdf0e10cSrcweir    if ((scalar @{$self->{CONFIG_FILE_CONTENT}}) != (scalar @config_content_new)) {
500*cdf0e10cSrcweir        $write_needed++;
501*cdf0e10cSrcweir    } else {
502*cdf0e10cSrcweir        foreach my $i (0 .. $#{$self->{CONFIG_FILE_CONTENT}}) {
503*cdf0e10cSrcweir            if (${$self->{CONFIG_FILE_CONTENT}}[$i] ne $config_content_new[$i]) {
504*cdf0e10cSrcweir                $write_needed++;
505*cdf0e10cSrcweir                last;
506*cdf0e10cSrcweir            };
507*cdf0e10cSrcweir        };
508*cdf0e10cSrcweir    };
509*cdf0e10cSrcweir    if ($write_needed) {
510*cdf0e10cSrcweir        my $temp_config_file = File::Temp::tmpnam($ENV{TMP});
511*cdf0e10cSrcweir        die("Cannot open $temp_config_file") if (!open(NEW_CONFIG, ">$temp_config_file"));
512*cdf0e10cSrcweir        print NEW_CONFIG $_ foreach (@config_content_new);
513*cdf0e10cSrcweir        close NEW_CONFIG;
514*cdf0e10cSrcweir        rename($temp_config_file, $self->get_config_file_default_path()) or  system("mv", $temp_config_file, $self->get_config_file_default_path());
515*cdf0e10cSrcweir        if (-e $temp_config_file) {
516*cdf0e10cSrcweir            system("rm -rf $temp_config_file") if (!unlink $temp_config_file);
517*cdf0e10cSrcweir        };
518*cdf0e10cSrcweir        $self->{CONFIG_FILE_CONTENT} = \@config_content_new;
519*cdf0e10cSrcweir    };
520*cdf0e10cSrcweir    if ($self->{VERBOSE}) {
521*cdf0e10cSrcweir        print $_ foreach (@{$self->{WARNINGS}});
522*cdf0e10cSrcweir        $self->{VERBOSE} = 0;
523*cdf0e10cSrcweir    };
524*cdf0e10cSrcweir    $self->{WARNINGS} = [];
525*cdf0e10cSrcweir    print $_ foreach (@{$self->{REPORT_MESSAGES}});
526*cdf0e10cSrcweir    $self->{REPORT_MESSAGES} = [];
527*cdf0e10cSrcweir};
528*cdf0e10cSrcweir
529*cdf0e10cSrcweir##### finish #####
530*cdf0e10cSrcweir
531*cdf0e10cSrcweir1; # needed by use or require
532*cdf0e10cSrcweir
533*cdf0e10cSrcweir__END__
534*cdf0e10cSrcweir
535*cdf0e10cSrcweir=head1 NAME
536*cdf0e10cSrcweir
537*cdf0e10cSrcweirSourceConfig - Perl extension for parsing general info databases
538*cdf0e10cSrcweir
539*cdf0e10cSrcweir=head1 SYNOPSIS
540*cdf0e10cSrcweir
541*cdf0e10cSrcweir    # example that will read source_config file and return the active repositories
542*cdf0e10cSrcweir
543*cdf0e10cSrcweir    use SourceConfig;
544*cdf0e10cSrcweir
545*cdf0e10cSrcweir    # Create a new instance of the parser:
546*cdf0e10cSrcweir    $a = SourceConfig->new();
547*cdf0e10cSrcweir
548*cdf0e10cSrcweir    # Get repositories for the actual workspace:
549*cdf0e10cSrcweir    $a->get_repositories();
550*cdf0e10cSrcweir
551*cdf0e10cSrcweir    # Add a repository new_repository for the actual workspace (via full path):
552*cdf0e10cSrcweir    $a->add_repository(/DEV300/new_repository);
553*cdf0e10cSrcweir
554*cdf0e10cSrcweir=head1 DESCRIPTION
555*cdf0e10cSrcweir
556*cdf0e10cSrcweirSourceConfig is a perl extension to load and parse General Info Databses.
557*cdf0e10cSrcweirIt uses a simple object oriented interface to retrieve the information stored
558*cdf0e10cSrcweirin the database.
559*cdf0e10cSrcweir
560*cdf0e10cSrcweirMethods:
561*cdf0e10cSrcweir
562*cdf0e10cSrcweirSourceConfig::new()
563*cdf0e10cSrcweir
564*cdf0e10cSrcweirCreates a new instance of SourceConfig. Can be initialized by: path to the default repository, path to the source_config, default - empty, the source_config will be taken from the environment
565*cdf0e10cSrcweir
566*cdf0e10cSrcweir
567*cdf0e10cSrcweirSourceConfig::get_version()
568*cdf0e10cSrcweir
569*cdf0e10cSrcweirReturns version number of the module. Can't fail.
570*cdf0e10cSrcweir
571*cdf0e10cSrcweir
572*cdf0e10cSrcweirSourceConfig::get_repositories()
573*cdf0e10cSrcweir
574*cdf0e10cSrcweirReturns sorted list of active repositories for the actual workspace
575*cdf0e10cSrcweir
576*cdf0e10cSrcweir
577*cdf0e10cSrcweirSourceConfig::add_repository(REPOSITORY_PATH)
578*cdf0e10cSrcweir
579*cdf0e10cSrcweirAdds a repository to the list of active repositories
580*cdf0e10cSrcweir
581*cdf0e10cSrcweir
582*cdf0e10cSrcweirSourceConfig::get_active_modules()
583*cdf0e10cSrcweir
584*cdf0e10cSrcweirReturns a sorted list of active modules
585*cdf0e10cSrcweir
586*cdf0e10cSrcweirSourceConfig::get_all_modules()
587*cdf0e10cSrcweir
588*cdf0e10cSrcweirReturns sorted list of all modules in active repositories.
589*cdf0e10cSrcweir
590*cdf0e10cSrcweirSourceConfig::get_module_path($module)
591*cdf0e10cSrcweir
592*cdf0e10cSrcweirReturns absolute module path
593*cdf0e10cSrcweir
594*cdf0e10cSrcweirSourceConfig::get_module_build_list($module)
595*cdf0e10cSrcweir
596*cdf0e10cSrcweirReturns absolute module build list path
597*cdf0e10cSrcweir
598*cdf0e10cSrcweirSourceConfig::get_module_repository($module)
599*cdf0e10cSrcweir
600*cdf0e10cSrcweirReturns the module's repository
601*cdf0e10cSrcweir
602*cdf0e10cSrcweirSourceConfig::get_config_file_path()
603*cdf0e10cSrcweir
604*cdf0e10cSrcweirReturns absolute module to the source configuration file
605*cdf0e10cSrcweir
606*cdf0e10cSrcweirSourceConfig::get_config_file_default_path()
607*cdf0e10cSrcweir
608*cdf0e10cSrcweirReturns default path for source configuration file
609*cdf0e10cSrcweir
610*cdf0e10cSrcweirSourceConfig::is_active()
611*cdf0e10cSrcweir
612*cdf0e10cSrcweirReturns 1 (TRUE) if a module is active
613*cdf0e10cSrcweirReturns 0 (FALSE) if a module is not active
614*cdf0e10cSrcweir
615*cdf0e10cSrcweirSourceConfig::add_active_modules($module_array_ref)
616*cdf0e10cSrcweir
617*cdf0e10cSrcweirAdds modules from the @$module_array_ref as active to the source_config file
618*cdf0e10cSrcweir
619*cdf0e10cSrcweirSourceConfig::add_active_repositories($repository_array_ref)
620*cdf0e10cSrcweir
621*cdf0e10cSrcweirAdds repositories from the @$repository_array_ref as active to the source_config file
622*cdf0e10cSrcweir
623*cdf0e10cSrcweirSourceConfig::remove_activated_modules($module_array_ref)
624*cdf0e10cSrcweir
625*cdf0e10cSrcweirRemoves modules from the @$module_array_ref from the source_config file
626*cdf0e10cSrcweir
627*cdf0e10cSrcweirSourceConfig::remove_all_activated_modules()
628*cdf0e10cSrcweir
629*cdf0e10cSrcweirRemoves all activated modules from the source_config file
630*cdf0e10cSrcweir
631*cdf0e10cSrcweirSourceConfig::remove_activated_repositories($repository_array_ref)
632*cdf0e10cSrcweir
633*cdf0e10cSrcweirRemoves repositories from the @$repository_array_ref from the source_config file
634*cdf0e10cSrcweir
635*cdf0e10cSrcweirSourceConfig::remove_all_activated_repositories()
636*cdf0e10cSrcweir
637*cdf0e10cSrcweirRemoves all activated repositories from the source_config file
638*cdf0e10cSrcweir
639*cdf0e10cSrcweir
640*cdf0e10cSrcweir=head2 EXPORT
641*cdf0e10cSrcweir
642*cdf0e10cSrcweirSourceConfig::new()
643*cdf0e10cSrcweirSourceConfig::get_version()
644*cdf0e10cSrcweirSourceConfig::get_repositories()
645*cdf0e10cSrcweirSourceConfig::add_repository()
646*cdf0e10cSrcweirSourceConfig::get_active_modules()
647*cdf0e10cSrcweirSourceConfig::get_all_modules()
648*cdf0e10cSrcweirSourceConfig::get_module_path($module)
649*cdf0e10cSrcweirSourceConfig::get_module_build_list($module)
650*cdf0e10cSrcweirSourceConfig::get_module_repository($module)
651*cdf0e10cSrcweirSourceConfig::get_config_file_path()
652*cdf0e10cSrcweirSourceConfig::get_config_file_default_path()
653*cdf0e10cSrcweirSourceConfig::is_active($module)
654*cdf0e10cSrcweirSourceConfig::add_active_modules($module_array_ref)
655*cdf0e10cSrcweirSourceConfig::add_active_repositories($repository_array_ref)
656*cdf0e10cSrcweirSourceConfig::remove_activated_modules($module_array_ref)
657*cdf0e10cSrcweirSourceConfig::remove_all_activated_modules()
658*cdf0e10cSrcweirSourceConfig::remove_activated_repositories($repository_array_ref)
659*cdf0e10cSrcweirSourceConfig::remove_all_activated_repositories()
660*cdf0e10cSrcweir
661*cdf0e10cSrcweir=head1 AUTHOR
662*cdf0e10cSrcweir
663*cdf0e10cSrcweirVladimir Glazunov, vg@openoffice.org
664*cdf0e10cSrcweir
665*cdf0e10cSrcweir=head1 SEE ALSO
666*cdf0e10cSrcweir
667*cdf0e10cSrcweirperl(1).
668*cdf0e10cSrcweir
669*cdf0e10cSrcweir=cut
670