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
Line 
1#!/usr/bin/perl -w
2#
3# Creates common environment
4#
5# $Id$
6#
7
8use strict;
9use lib qw (lib);
10use ProjectBuilder::pb qw (pb_init);
11use File::Basename;
12use File::Path;
13use File::Temp qw /tempdir/;
14use Data::Dumper;
15use vars qw (%pbrc);
16
17$ENV{'PBETC'} = "$ENV{'HOME'}/.pbrc";
18
19sub env_init {
20
21my $proj=shift;
22my $ver;
23my $tag;
24
25#
26# Check project name
27# Could be with env var PBPROJ
28# or option -p
29# if not define take the first in conf file
30#
31if ((defined $ENV{'PBPROJ'}) &&
32 (not (defined $proj))) {
33 $proj = $ENV{'PBPROJ'};
34}
35
36#
37# We get the pbrc file for that project
38# and use its content
39#
40my $pbrc = pb_init("$ENV{'PBETC'}","pbrc");
41
42%pbrc = %$pbrc;
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#
58# Use project configuration file if needed
59#
60if (not defined $ENV{'PBROOT'}) {
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'}));
66 }
67 die "No pbroot defined - use env var PBROOT or -r pbroot " if (not defined $ENV{'PBROOT'});
68}
69
70#
71# Check pb conf compliance
72#
73$ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
74die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
75
76if (-f "$ENV{'PBCONF'}/$proj.pb") {
77 pb_conf_init("$ENV{'PBCONF'}/$proj.pb");
78} else {
79 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
80}
81
82#
83# Check content
84#
85if (defined $confparam{"cvsroot"}) {
86 $ENV{'CVSROOT'} = $confparam{"cvsroot"};
87}
88
89die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir));
90
91#
92# Set temp directory
93#
94if (not defined $ENV{'TMPDIR'}) {
95 $ENV{'TMPDIR'}="/tmp";
96}
97$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
98
99#
100# Get global VERSION
101#
102open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
103$ver = <VER>;
104chomp($ver);
105#print Dumper(%version);
106die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
107$ENV{'PBVER'}=$ver;
108close(VER);
109
110#
111#Get global TAG
112#
113open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
114$tag = <TAG>;
115chomp($tag);
116die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
117$ENV{'PBTAG'}=$tag;
118close(TAG);
119
120#
121# Removes all directory existing below the delivery dir
122# as they are temp dir only
123# Files stay and have to be cleaned up manually
124#
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 =~ /^\./);
129 next if (-f "$ENV{'PBDESTDIR'}/$d");
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}
137
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
145umask 0022;
146return($proj);
147}
148
149sub pbmkdir_p {
150my @dir = @_;
151my $ret = mkpath(@dir, 0, 0755);
152return($ret);
153}
154
155sub pbrm_rf {
156my @dir = @_;
157my $ret = rmtree(@dir, 0, 0);
158return($ret);
159}
160
161sub pbsystem {
162
163my $cmd=shift;
164my $cmt=shift || $cmd;
165
166print $LOG "$cmt... ";
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 {
173 print $LOG "OK\n" if ($debug >= 0);
174}
175}
1761;
Note: See TracBrowser for help on using the repository browser.