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

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

Still some fixes

  • Property svn:executable set to *
File size: 4.1 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=dirname($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 my $pbroot = pb_init($pbrc{$proj},"pbroot");
63 my %pbroot = %$pbroot;
64 # There is normaly only one line in it
65 $ENV{'PBROOT'} = (values %$pbroot)[0] if (defined $pbroot);
66 print $LOG "Using $ENV{'PBROOT'} as default pbroot from $pbrc{$proj}\n" if (($debug >= 0) and (defined $ENV{'PBROOT'}));
67 }
68 die "No pbroot defined - use env var PBROOT or -r pbroot " if (not defined $ENV{'PBROOT'});
69}
70
71#
72# Check pb conf compliance
73#
74$ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
75die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
76
77if (-f "$ENV{'PBCONF'}/$proj.pb") {
78 pb_conf_init("$ENV{'PBCONF'}/$proj.pb");
79} else {
80 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
81}
82
83#
84# Check content
85#
86if (defined $confparam{"cvsroot"}) {
87 $ENV{'CVSROOT'} = $confparam{"cvsroot"};
88}
89
90die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir));
91
92#
93# Set temp directory
94#
95if (not defined $ENV{'TMPDIR'}) {
96 $ENV{'TMPDIR'}="/tmp";
97}
98$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
99
100#
101# Get global VERSION
102#
103open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
104$ver = <VER>;
105chomp($ver);
106#print Dumper(%version);
107die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
108$ENV{'PBVER'}=$ver;
109close(VER);
110
111#
112#Get global TAG
113#
114open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
115$tag = <TAG>;
116chomp($tag);
117die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
118$ENV{'PBTAG'}=$tag;
119close(TAG);
120
121#
122# Removes all directory existing below the delivery dir
123# as they are temp dir only
124# Files stay and have to be cleaned up manually
125#
126if (-d $ENV{'PBDESTDIR'}) {
127 opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
128 foreach my $d (readdir(DIR)) {
129 next if ($d =~ /^\./);
130 next if (-f "$ENV{'PBDESTDIR'}/$d");
131 pbrm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
132 }
133 closedir(DIR);
134}
135if (! -d "$ENV{'PBDESTDIR'}") {
136 pbmkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
137}
138
139#
140# Set build directory
141#
142$ENV{'PBBUILDDIR'}=$topdir."/build";
143pbrm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
144pbmkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
145
146umask 0022;
147return($proj);
148}
149
150sub pbmkdir_p {
151my @dir = @_;
152my $ret = mkpath(@dir, 0, 0755);
153return($ret);
154}
155
156sub pbrm_rf {
157my @dir = @_;
158my $ret = rmtree(@dir, 0, 0);
159return($ret);
160}
161
162sub pbsystem {
163
164my $cmd=shift;
165my $cmt=shift || $cmd;
166
167print $LOG "$cmt... ";
168system("$cmd");
169if ($? == -1) {
170 print $LOG "failed to execute: $!\n" if ($debug >= 0);
171} elsif ($? & 127) {
172 printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
173} else {
174 print $LOG "OK\n" if ($debug >= 0);
175}
176}
1771;
Note: See TracBrowser for help on using the repository browser.