1 #*************************************************************************
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 # Copyright 2008 by Sun Microsystems, Inc.
7 # OpenOffice.org - a multi-platform office productivity suite
9 # $RCSfile: SourceConfig.pm,v $
13 # This file is part of OpenOffice.org.
15 # OpenOffice.org is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU Lesser General Public License version 3
17 # only, as published by the Free Software Foundation.
19 # OpenOffice.org is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU Lesser General Public License version 3 for more details
23 # (a copy is included in the LICENSE file that accompanied this code).
25 # You should have received a copy of the GNU Lesser General Public License
26 # version 3 along with OpenOffice.org. If not, see
27 # <http://www.openoffice.org/license.html>
28 # for a copy of the LGPLv3 License.
30 #*************************************************************************
32 #*************************************************************************
34 # SourceConfig - Perl extension for parsing general info databases
38 #*************************************************************************
44 use constant SOURCE_CONFIG_FILE_NAME
=> 'source_config';
54 my $class = ref($proto) || $proto;
55 my $source_root = shift;
56 if (defined $source_root) {
57 $source_root =~ s/\\|\/$//;
59 $source_root = $ENV{SOLARSRC
};
63 $self->{SOURCE_ROOT
} = $source_root;
64 $self->{REPOSITORIES
} = {};
65 $self->{MODULE_PATHS
} = {};
66 $self->{MODULE_BUILD_LIST_PATHS
} = {};
67 $self->{ACTIVE_MODULES
} = {};
68 $self->{MODULE_REPOSITORY
} = {};
69 $self->{SOURCE_CONFIG_FILE
} = get_config_file
($source_root);
70 $self->{SOURCE_CONFIG_DEFAULT
} = Cwd
::realpath
($source_root) .'/'.SOURCE_CONFIG_FILE_NAME
;
71 read_config_file
($self);
81 return sort keys %{$self->{REPOSITORIES
}};
84 sub get_config_file_default_path
{
86 return $self->{SOURCE_CONFIG_DEFAULT
};
89 sub get_config_file_path
{
91 return $self->{SOURCE_CONFIG_FILE
};
94 sub get_module_repository
{
97 $self -> get_module_paths
() if (!scalar keys %{$self->{MODULE_PATHS
}});
98 if (defined ${$self->{MODULE_REPOSITORY
}}{$module}) {
99 return ${$self->{MODULE_REPOSITORY
}}{$module};
101 Carp
::cluck
("No such module $module in active repositories!!\n");
106 sub get_module_path
{
109 $self -> get_module_paths
() if (!scalar keys %{$self->{MODULE_PATHS
}});
110 if (defined ${$self->{MODULE_PATHS
}}{$module}) {
111 return ${$self->{MODULE_PATHS
}}{$module};
113 Carp
::cluck
("No path for module $module in active repositories!!\n");
118 sub get_module_build_list
{
121 $self -> get_buildlist_paths
() if (!scalar keys %{$self->{MODULE_BUILD_LIST_PATHS
}});
122 if (defined ${$self->{MODULE_BUILD_LIST_PATHS
}}{$module}) {
123 return ${$self->{MODULE_BUILD_LIST_PATHS
}}{$module};
125 Carp
::cluck
("No build list in module $module found!!\n") if ($self->{DEBUG
});
134 $self -> get_module_paths
() if (!scalar keys %{$self->{MODULE_PATHS
}});
135 return sort keys %{$self->{MODULE_PATHS
}};
139 sub get_active_modules
142 return sort keys %{$self->{ACTIVE_MODULES
}};
146 ##### private methods #####
148 sub get_buildlist_paths
{
150 $self -> get_module_paths
() if (!scalar keys %{$self->{MODULE_PATHS
}});
151 my @possible_build_lists = ('build.lst', 'build.xlist'); # build lists names
152 foreach my $module (keys %{$self->{MODULE_PATHS
}}) {
153 foreach (@possible_build_lists) {
154 my $possible_path = ${$self->{MODULE_PATHS
}}{$module} . "/prj/$_";
155 ${$self->{MODULE_BUILD_LIST_PATHS
}}{$module} = $possible_path if (-e
$possible_path);
160 sub get_module_paths
{
162 foreach my $repository (keys %{$self->{REPOSITORIES
}}) {
163 my $repository_path = ${$self->{REPOSITORIES
}}{$repository};
164 if (opendir DIRHANDLE
, $repository_path) {
165 foreach my $module (readdir(DIRHANDLE
)) {
166 next if ($module =~ /^\.+/);
167 my $module_entry = $module;
168 $module =~ s/\.lnk$//;
169 $module =~ s/\.link$//;
170 my $possible_path = "$repository_path/$module_entry";
171 if (-d
$possible_path) {
172 if (defined ${$self->{MODULE_PATHS
}}{$module}) {
174 croak
("Ambiguous paths for module $module: $possible_path and " . ${$self->{MODULE_PATHS
}}{$module});
176 ${$self->{MODULE_PATHS
}}{$module} = $possible_path;
177 ${$self->{MODULE_REPOSITORY
}}{$module} = $repository;
182 croak
("Cannot read $_ repository content");
187 sub get_config_file
{
188 my $source_root = shift;
189 foreach ($source_root, $source_root . '/..') {
190 if (-f
$_ . '/' . SOURCE_CONFIG_FILE_NAME
) {
191 return Cwd
::realpath
($_) .'/'.SOURCE_CONFIG_FILE_NAME
;
197 sub read_config_file
{
199 if (!$self->{SOURCE_CONFIG_FILE
}) {
200 ${$self->{REPOSITORIES
}}{File
::Basename
::basename
($self->{SOURCE_ROOT
})} = $self->{SOURCE_ROOT
};
203 my $repository_section = 0;
204 my $module_section = 0;
206 if (open(SOURCE_CONFIG_FILE
, $self->{SOURCE_CONFIG_FILE
})) {
207 foreach (<SOURCE_CONFIG_FILE
>) {
213 if (/^\[repositories\]\s*(\s+#)*/) {
215 $repository_section = 1;
218 if (/^\[modules\]\s*(\s+#)*/) {
220 $repository_section = 0;
223 next if (!$repository_section && !$module_section);
224 if (/\s*(\S+)=active\s*(\s+#)*/) {
225 if ($repository_section) {
226 ${$self->{REPOSITORIES
}}{$1} = File
::Basename
::dirname
($self->{SOURCE_ROOT
}) . "/$1";
229 if ($module_section) {
230 ${$self->{ACTIVE_MODULES
}}{$1}++;
234 croak
("Line $line in " . $self->{SOURCE_CONFIG_FILE
} . 'violates format. Please make your checks!!');
236 close SOURCE_CONFIG_FILE
;
238 croak
('Cannot open ' . $self->{SOURCE_CONFIG_FILE
} . 'for reading');
244 1; # needed by use or require
250 SourceConfig - Perl extension for parsing general info databases
254 # example that will read source_config file and return the active repositories
258 # Create a new instance of the parser:
259 $a = SourceConfig->new();
261 # Get repositories for the actual workspace:
262 $a->get_repositories();
267 SourceConfig is a perl extension to load and parse General Info Databses.
268 It uses a simple object oriented interface to retrieve the information stored
275 Creates a new instance of SourceConfig. Can't fail.
278 SourceConfig::get_repositories()
280 Returns sorted list of active repositories for the actual workspace
283 SourceConfig::get_active_modules()
285 Returns a sorted list of active modules
287 SourceConfig::get_all_modules()
289 Returns sorted list of all modules in active repositories.
291 SourceConfig::get_module_path($module)
293 Returns absolute module path
295 SourceConfig::get_module_build_list($module)
297 Returns absolute module build list path
299 SourceConfig::get_module_repository($module)
301 Returns the module's repository
303 SourceConfig::get_config_file_path()
305 Returns absolute module to the source configuration file
307 SourceConfig::get_config_file_default_path()
309 Returns default path for source configuration file
314 SourceConfig::get_repositories()
315 SourceConfig::get_active_modules()
316 SourceConfig::get_all_modules()
317 SourceConfig::get_module_path($module)
318 SourceConfig::get_module_build_list($module)
319 SourceConfig::get_module_repository($module)
320 SourceConfig::get_config_file_path()
321 SourceConfig::get_config_file_default_path()
325 Vladimir Glazunov, vg@openoffice.com