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