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

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

Try to better organize the tree to be perl compliant

  • Property svn:executable set to *
File size: 3.4 KB
Line 
1#!/usr/bin/perl -w
2#
3# Creates common environment
4#
5# $Id$
6#
7
8use strict;
9use lib qw (lib);
10use 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# Use project configuration file
36#
37pb_init("$ENV{'PBETC'}");
38
39if (not defined $proj) {
40 # Take the first as the default project
41 $proj = (keys %pbroot)[0];
42 print $LOG "Using $proj as default project as none has been specified\n" if (($debug >= 0) and (defined $proj));
43}
44die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
45
46$ENV{'PBROOT'} = $pbroot{$proj};
47
48#
49# Check pb conf compliance
50#
51$ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
52die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
53
54if (-f "$ENV{'PBCONF'}/$proj.pb") {
55 pb_conf_init("$ENV{'PBCONF'}/$proj.pb");
56} else {
57 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
58}
59
60#
61# Check content
62#
63if (defined $confparam{"cvsroot"}) {
64 $ENV{'CVSROOT'} = $confparam{"cvsroot"};
65}
66
67die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir));
68
69#
70# Set temp directory
71#
72if (not defined $ENV{'TMPDIR'}) {
73 $ENV{'TMPDIR'}="/tmp";
74}
75$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
76
77#
78# Get global VERSION
79#
80open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
81$ver = <VER>;
82chomp($ver);
83#print Dumper(%version);
84die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
85$ENV{'PBVER'}=$ver;
86close(VER);
87
88#
89#Get global TAG
90#
91open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
92$tag = <TAG>;
93chomp($tag);
94die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
95$ENV{'PBTAG'}=$tag;
96close(TAG);
97
98#
99# Adapt to your needs
100# Set delivery directory
101#
102chdir "$ENV{'PBROOT'}/..";
103my $path = `pwd`;
104chomp($path);
105$ENV{'PBTOPDIR'}=$path."/delivery";
106$ENV{'PBDESTDIR'}="$ENV{'PBTOPDIR'}/$ENV{'PBVER'}-$ENV{'PBTAG'}";
107if (-d $ENV{'PBDESTDIR'}) {
108 opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
109 foreach my $d (readdir(DIR)) {
110 next if ($d =~ /^\./);
111 pbrm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
112 }
113 closedir(DIR);
114}
115if (! -d "$ENV{'PBDESTDIR'}") {
116 pbmkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
117}
118
119#
120# Set build directory
121#
122$ENV{'PBBUILDDIR'}=$path."/build";
123pbrm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
124pbmkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
125
126umask 0022;
127return($proj);
128}
129
130sub pbmkdir_p {
131my @dir = @_;
132my $ret = mkpath(@dir, 0, 0755);
133return($ret);
134}
135
136sub pbrm_rf {
137my @dir = @_;
138my $ret = rmtree(@dir, 0, 0);
139return($ret);
140}
141
142sub pbsystem {
143
144my $cmd=shift;
145my $cmt=shift || $cmd;
146
147print $LOG "$cmt... ";
148system("$cmd");
149if ($? == -1) {
150 print $LOG "failed to execute: $!\n" if ($debug >= 0);
151} elsif ($? & 127) {
152 printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
153} else {
154 print $LOG "OK\n" if ($debug >= 0);
155}
156}
1571;
Note: See TracBrowser for help on using the repository browser.