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