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

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

pbrc is now global and no hardcoded conf file used anymore

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