1#************************************************************************* 2# 3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4# 5# Copyright 2000, 2010 Oracle and/or its affiliates. 6# 7# OpenOffice.org - a multi-platform office productivity suite 8# 9# This file is part of OpenOffice.org. 10# 11# OpenOffice.org is free software: you can redistribute it and/or modify 12# it under the terms of the GNU Lesser General Public License version 3 13# only, as published by the Free Software Foundation. 14# 15# OpenOffice.org is distributed in the hope that it will be useful, 16# but WITHOUT ANY WARRANTY; without even the implied warranty of 17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18# GNU Lesser General Public License version 3 for more details 19# (a copy is included in the LICENSE file that accompanied this code). 20# 21# You should have received a copy of the GNU Lesser General Public License 22# version 3 along with OpenOffice.org. If not, see 23# <http://www.openoffice.org/license.html> 24# for a copy of the LGPLv3 License. 25# 26#************************************************************************* 27 28#************************************************************************* 29# 30# SourceConfigHelper - Perl extension for parsing general info databases 31# 32# usage: see below 33# 34#************************************************************************* 35 36package SourceConfigHelper; 37 38use strict; 39 40use RepositoryHelper; 41use SourceConfig; 42use Cwd qw (cwd); 43use Carp; 44 45my $debug = 0; 46my @source_config_list; # array of sourceconfig objects 47 48#----------------------------------------------------------------------- 49# Constants 50#----------------------------------------------------------------------- 51 52use constant SOURCE_CONFIG_NONE => 0; 53use constant SOURCE_CONFIG_CURRENT_FIRST => 1; 54use constant SOURCE_CONFIG_ENVIRONMENT_FIRST => 2; 55use constant SOURCE_CONFIG_CURRENT_ONLY => 3; 56use constant SOURCE_CONFIG_ENVIRONMENT_ONLY => 4; 57 58use constant SOURCE_CONFIG_DEFAULT => SOURCE_CONFIG_CURRENT_FIRST; 59 60##### profiling ##### 61 62##### ctor ##### 63 64sub new { 65 my $proto = shift; 66 my $class = ref($proto) || $proto; 67 my $init_action = shift; 68 my $self = {}; 69 my $SourceConfigCurrent; 70 my $SourceConfigEnvironment; 71 72 $init_action = SOURCE_CONFIG_DEFAULT if (!defined ($init_action)); 73 if (!eval ($init_action) or ($init_action < SOURCE_CONFIG_NONE) or ($init_action > SOURCE_CONFIG_ENVIRONMENT_ONLY)) { 74 croak("wrong initial parameter: $init_action\n"); 75 } 76 77 if ($init_action != SOURCE_CONFIG_NONE) { 78 my $repositoryHash_ref = {}; 79 if ($init_action != SOURCE_CONFIG_ENVIRONMENT_ONLY) { 80 my $initial_directory = cwd(); 81 my $result = is_repository($initial_directory, $repositoryHash_ref); 82 if ($result) { 83 $SourceConfigCurrent = SourceConfig->new($repositoryHash_ref->{REPOSITORY_ROOT}); 84 } 85 } 86 if ($init_action != SOURCE_CONFIG_CURRENT_ONLY) { 87 my $source_config = $ENV{SOURCE_ROOT_DIR} . '/' . SourceConfig::SOURCE_CONFIG_FILE_NAME; 88 if (-f $source_config) { 89 $SourceConfigEnvironment = SourceConfig->new($source_config); 90 } 91 } 92 93 # fill array 94 95 if (($init_action == SOURCE_CONFIG_CURRENT_FIRST) or ($init_action == SOURCE_CONFIG_CURRENT_ONLY)) { 96 if (defined ($SourceConfigCurrent)) { 97 push (@source_config_list, $SourceConfigCurrent); 98 } 99 if ($init_action == SOURCE_CONFIG_CURRENT_FIRST) { 100 if (defined ($SourceConfigEnvironment)) { 101 push (@source_config_list, $SourceConfigEnvironment); 102 } 103 } 104 } 105 elsif (($init_action == SOURCE_CONFIG_ENVIRONMENT_FIRST) or ($init_action == SOURCE_CONFIG_ENVIRONMENT_ONLY)) { 106 if (defined ($SourceConfigEnvironment)) { 107 push (@source_config_list, $SourceConfigEnvironment); 108 } 109 if ($init_action == SOURCE_CONFIG_ENVIRONMENT_FIRST) { 110 if (defined ($SourceConfigCurrent)) { 111 push (@source_config_list, $SourceConfigCurrent); 112 } 113 } 114 } 115 } 116 117 $self->{SOURCE_CONFIG_LIST} = \@source_config_list; 118 119 bless($self, $class); 120 return $self; 121} 122 123##### methods ##### 124 125############################################################################################ 126 127sub add_SourceConfig { 128 my $self = shift; 129 my $source_config = shift; 130 push (@{$self->{SOURCE_CONFIG_LIST}}, $source_config); 131} 132 133############################################################################################ 134 135sub get_SourceConfigList { 136 my $self = shift; 137 return @{$self->{SOURCE_CONFIG_LIST}}; 138} 139 140############################################################################################ 141 142sub has_SourceConfig { 143 my $self = shift; 144 my $result = 0; 145 my $count = @{$self->{SOURCE_CONFIG_LIST}}; 146 $result = 1 if ($count > 0); 147 return $result; 148} 149 150############################################################################################ 151 152sub get_module_path { 153 my $self = shift; 154 my $module = shift; 155 my $function = \&SourceConfig::get_module_path; 156 my $result; 157 $result = $self->get_StringResult ($function, $module); 158 return $result; 159} 160 161############################################################################################ 162 163sub get_active_modules { 164 my $self = shift; 165 my $parameter; # empty 166 my $function = \&SourceConfig::get_active_modules; 167 my $array_ref; 168 $array_ref = $self->get_ArrayResult ($function, $parameter); 169 return @$array_ref; 170} 171 172############################################################################################ 173 174sub get_repositories { 175 my $self = shift; 176 my $parameter; # empty 177 my $function = \&SourceConfig::get_repositories; 178 my $array_ref; 179 $array_ref = $self->get_ArrayResult ($function, $parameter); 180 return @$array_ref; 181} 182 183############################################################################################ 184 185sub get_module_repository { 186 my $self = shift; 187 my $module = shift; 188 my $function = \&SourceConfig::get_module_repository; 189 my $result; 190 $result = $self->get_StringResult ($function, $module); 191 return $result; 192} 193 194############################################################################################ 195 196sub is_active { 197 my $self = shift; 198 my $module = shift; 199 my $function = \&SourceConfig::is_active; 200 my $result_ref; 201 my $is_active = 0; 202 $result_ref = $self->get_ResultOfList ($function, $module); 203 my $count = @$result_ref; 204 if ($count>0) { 205 foreach my $active (@$result_ref) { 206 if ($active) { 207 $is_active = $active; 208 } 209 } 210 } 211 return $is_active; 212} 213 214##### private methods ##### 215 216############################################################################################ 217# 218# is_repository () : check if the directory is a valid repository 219# 220# input: - directory 221# - hash reference, where the output will be stored 222# 223# output: 0 = FALSE, the directory is no valid repository 224# 1 = TRUE, the repository root can be found in $repositoryHash_ref->{REPOSITORY_ROOT} 225# 226############################################################################################ 227 228sub is_repository { 229 my $directory = shift; 230 my $repositoryHash_ref = shift; 231 $repositoryHash_ref->{INITIAL_DIRECTORY} = $directory; 232 $repositoryHash_ref->{REPOSITORY_ROOT} = undef; 233 $repositoryHash_ref->{REPOSITORY_NAME} = undef; 234 my $result = RepositoryHelper::search_via_build_lst($repositoryHash_ref); 235 chdir $repositoryHash_ref->{INITIAL_DIRECTORY}; 236 if (!$result) { 237 $result = RepositoryHelper::search_for_hg($repositoryHash_ref); 238 } 239 return $result; 240} 241 242############################################################################################ 243# 244# get_ResultOfList(): give back an array reference from all SourceConfig Objects results 245# 246# input: - function : reference to the called function of each SourceConfig Object 247# - parameter : parameter for the called function 248# 249# output: result : array of all results 250# 251############################################################################################ 252 253sub get_ResultOfList { 254 my $self = shift; 255 my $function = shift; 256 my $parameter = shift; 257 my @result; 258 foreach my $source_config (@{$self->{SOURCE_CONFIG_LIST}}) { 259 push (@result, &$function ($source_config, $parameter)); 260 } 261 return \@result; 262} 263 264############################################################################################ 265# 266# get_StringResult(): give back the first defined result from all SourceConfig Objects 267# 268# input: - function : reference to the called function of each SourceConfig Object 269# - parameter : parameter for the called function 270# 271# output: result : scalar variable (string), undef if no result 272# 273############################################################################################ 274 275sub get_StringResult { 276 my $self = shift; 277 my $function = shift; 278 my $parameter = shift; 279 my $result_ref; 280 $result_ref = $self->get_ResultOfList ($function, $parameter); 281 my $count = @$result_ref; 282 if ($count>0) { 283 my $value; 284 my $i = 0; 285 while (($i < $count) and !defined ($value)) { # search the first defined result 286 $value = $$result_ref[$i]; 287 $i++; 288 } 289 return $value; 290 } 291 return undef; 292} 293 294############################################################################################ 295# 296# get_StringResult(): give back a sorted and uniqe array reference of the results 297# from all SourceConfig Objects 298# 299# input: - function : reference to the called function of each SourceConfig Object 300# - parameter : parameter for the called function 301# 302# output: result : sorted and uniqe array reference 303# 304############################################################################################ 305 306sub get_ArrayResult { 307 my $self = shift; 308 my $function = shift; 309 my $parameter = shift; 310 my $result_ref; 311 my @modules; 312 $result_ref = $self->get_ResultOfList ($function, $parameter); 313 my $count = @$result_ref; 314 if ($count>0) { 315 my %moduleHash; 316 foreach my $module (@$result_ref) { 317 $moduleHash{$module}++; 318 } 319 @modules = sort keys %moduleHash; 320 } 321 return \@modules; 322} 323 324 ##### finish ##### 325 3261; # needed by use or require 327 328__END__ 329 330=head1 NAME 331 332SourceConfigHelper - Perl extension for handling with SourceConfigObjetcs 333 334=head1 SYNOPSIS 335 336 # example that will read source_config file and return the active repositories 337 338 use SourceConfigHelper; 339 340 # Create a new instance: 341 $a = SourceConfigHelper->new(); 342 343 # Get repositories for the actual workspace: 344 $a->get_repositories(); 345 346=head1 DESCRIPTION 347 348SourceConfigHelper is a perl extension to handle more than one objects of SourceConfig 349to set up a search order for modules. 350 351Methods: 352 353SourceConfigHelper::new() 354 355Creates a new instance of SourceConfigHelper. Can be initialized by: default - empty or with a constant of search order. default: the source_config will be taken first from the current repository and second from the environment 356Possible parameters are: 357SourceConfigHelper::SOURCE_CONFIG_NONE - no SourceConfig Object will be created 358SourceConfigHelper::SOURCE_CONFIG_CURRENT_FIRST - use the current repository first 359SourceConfigHelper::SOURCE_CONFIG_ENVIRONMENT_FIRST - use the repository of the environment first 360SourceConfigHelper::SOURCE_CONFIG_CURRENT_ONLY - use only the current repository 361SourceConfigHelper::SOURCE_CONFIG_ENVIRONMENT_ONLY - use only the repository of the environment 362 363SourceConfigHelper::get_repositories() 364 365Returns sorted list of active repositories for the actual workspace 366 367SourceConfigHelper::get_active_modules() 368 369Returns a sorted list of active modules 370 371SourceConfigHelper::get_all_modules() 372 373Returns sorted list of all modules in active repositories. 374 375SourceConfigHelper::get_module_path($module) 376 377Returns absolute module path. If the module is not active or don't exists, "undef" will be returned. 378 379SourceConfigHelper::get_module_repository($module) 380 381Returns the module's repository. If the module is not active or don't exists, "undef" will be returned. 382 383SourceConfigHelper::is_active() 384 385Returns 1 (TRUE) if a module is active 386Returns 0 (FALSE) if a module is not active 387 388SourceConfigHelper::add_SourceConfig($SourceConfigObject) 389 390Add the SourceConfigObject to the end of the list 391 392SourceConfigHelper::get_SourceConfigList() 393 394Return an array of SourceConfigObjects 395 396SourceConfigHelper::has_SourceConfig() 397 398Returns 1 (TRUE) if one or more SourceConfig Objects is in the list 399Returns 0 (FALSE) if no SourceConfig Object is in the list (can happen if there is no valid repository) 400 401=head2 EXPORT 402 403SourceConfigHelper::new() 404SourceConfigHelper::get_repositories() 405SourceConfigHelper::get_active_modules() 406SourceConfigHelper::get_all_modules() 407SourceConfigHelper::get_module_path($module) 408SourceConfigHelper::get_module_repository($module) 409SourceConfigHelper::is_active($module) 410SourceConfigHelper::add_SourceConfig($SourceConfigObject) 411SourceConfigHelper::get_SourceConfigList() 412SourceConfigHelper::has_SourceConfig() 413 414=head1 AUTHOR 415 416Kurt Zenker, kz@openoffice.org 417 418=head1 SEE ALSO 419 420perl(1). 421 422=cut 423