source: ProjectBuilder/devel/pb/lib/ProjectBuilder/common.pm@ 69

Last change on this file since 69 was 69, checked in by Bruno Cornec, 17 years ago

Rework interfaces of pb_init, conf files content and management to ease usage

  • Property svn:executable set to *
File size: 4.0 KB
RevLine 
[2]1#!/usr/bin/perl -w
2#
3# Creates common environment
4#
5# $Id$
6#
7
[18]8use strict;
[5]9use lib qw (lib);
[52]10use ProjectBuilder::pb qw (pb_init);
[2]11use File::Basename;
[9]12use File::Path;
[2]13use File::Temp qw /tempdir/;
[8]14use Data::Dumper;
[2]15
[49]16$ENV{'PBETC'} = "$ENV{'HOME'}/.pbrc";
[5]17
[9]18sub env_init {
[2]19
[5]20my $proj=shift;
21my $ver;
22my $tag;
[2]23
[8]24#
[5]25# Check project name
[49]26# Could be with env var PBPROJ
27# or option -p
28# if not define take the first in conf file
[8]29#
[5]30if ((defined $ENV{'PBPROJ'}) &&
31 (not (defined $proj))) {
32 $proj = $ENV{'PBPROJ'};
33}
[69]34
[49]35#
[69]36# We get the pbrc file for that project
37# and use its content
38#
39$pbrc = pb_init("$ENV{'PBETC'}","pbrc");
40
41my %pbrc = %$pbrc;
42if (not defined $proj) {
43 # Take the first as the default project
44 $proj = (keys %pbrc)[0];
45 print $LOG "Using $proj as default project as none has been specified\n" if (($debug >= 0) and (defined $proj));
46}
47die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
48
49#
50# Set delivery directory
51#
52my $topdir=basename($pbrc{$proj});
53chdir $topdir || die "Unable to change directory to $topdir";
54$ENV{'PBDESTDIR'}=$topdir."/delivery";
55
56#
[67]57# Use project configuration file if needed
[49]58#
[67]59if (not defined $ENV{'PBROOT'}) {
[69]60 if (-f $pbrc{$proj}) {
61 $pbroot = pb_init($pbrc{$proj},"pbroot");
62 # There is normaly only one line in it
63 $ENV{'PBROOT'} = (values %$pbroot)[0] if (defined $pbroot);
64 print $LOG "Using $ENV{'PBROOT'} as default pbroot from $pbrc{$proj}\n" if (($debug >= 0) and (defined $ENV{'PBROOT'}));
[67]65 }
[69]66 die "No pbroot defined - use env var PBROOT or -r pbroot " if (not defined $ENV{'PBROOT'});
[49]67}
[2]68
[8]69#
[49]70# Check pb conf compliance
[8]71#
[49]72$ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
73die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
[2]74
[49]75if (-f "$ENV{'PBCONF'}/$proj.pb") {
76 pb_conf_init("$ENV{'PBCONF'}/$proj.pb");
[38]77} else {
[49]78 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
[38]79}
80
[8]81#
[5]82# Check content
[8]83#
[18]84if (defined $confparam{"cvsroot"}) {
85 $ENV{'CVSROOT'} = $confparam{"cvsroot"};
[17]86}
87
[13]88die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir));
[5]89
[8]90#
[5]91# Set temp directory
[8]92#
[7]93if (not defined $ENV{'TMPDIR'}) {
[2]94 $ENV{'TMPDIR'}="/tmp";
95}
[5]96$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
[2]97
[8]98#
[5]99# Get global VERSION
[8]100#
[11]101open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
[5]102$ver = <VER>;
103chomp($ver);
[8]104#print Dumper(%version);
105die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
[5]106$ENV{'PBVER'}=$ver;
107close(VER);
108
[8]109#
[5]110#Get global TAG
[8]111#
[11]112open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
[5]113$tag = <TAG>;
114chomp($tag);
[8]115die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
[5]116$ENV{'PBTAG'}=$tag;
117close(TAG);
118
[8]119#
[69]120# Removes all directory existing below the delivery dir
121# as they are temp dir only
[68]122# Files stay and have to be cleaned up manually
[8]123#
[25]124if (-d $ENV{'PBDESTDIR'}) {
125 opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
126 foreach my $d (readdir(DIR)) {
127 next if ($d =~ /^\./);
[68]128 next if (-f "$ENV{'PBDESTDIR'}/$d");
[25]129 pbrm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
130 }
131 closedir(DIR);
132}
133if (! -d "$ENV{'PBDESTDIR'}") {
134 pbmkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
135}
[5]136
[25]137#
138# Set build directory
139#
140$ENV{'PBBUILDDIR'}=$path."/build";
141pbrm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
142pbmkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
143
[8]144umask 0022;
145return($proj);
[2]146}
[9]147
148sub pbmkdir_p {
[29]149my @dir = @_;
150my $ret = mkpath(@dir, 0, 0755);
151return($ret);
[9]152}
153
154sub pbrm_rf {
[29]155my @dir = @_;
156my $ret = rmtree(@dir, 0, 0);
157return($ret);
[9]158}
159
[29]160sub pbsystem {
161
162my $cmd=shift;
[30]163my $cmt=shift || $cmd;
[29]164
[30]165print $LOG "$cmt... ";
[29]166system("$cmd");
167if ($? == -1) {
168 print $LOG "failed to execute: $!\n" if ($debug >= 0);
169} elsif ($? & 127) {
170 printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
171} else {
[30]172 print $LOG "OK\n" if ($debug >= 0);
[29]173}
[30]174}
[2]1751;
Note: See TracBrowser for help on using the repository browser.