1 | #!/usr/bin/perl -w
|
---|
2 | #
|
---|
3 | # Creates common environment
|
---|
4 | #
|
---|
5 | # $Id$
|
---|
6 | #
|
---|
7 |
|
---|
8 | use strict;
|
---|
9 | use lib qw (lib);
|
---|
10 | use ProjectBuilder::pb qw (pb_init);
|
---|
11 | use File::Basename;
|
---|
12 | use File::Path;
|
---|
13 | use File::Temp qw /tempdir/;
|
---|
14 | use Data::Dumper;
|
---|
15 | use vars qw (%pbrc);
|
---|
16 |
|
---|
17 | $ENV{'PBETC'} = "$ENV{'HOME'}/.pbrc";
|
---|
18 |
|
---|
19 | sub env_init {
|
---|
20 |
|
---|
21 | my $proj=shift;
|
---|
22 | my $ver;
|
---|
23 | my $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 | #
|
---|
31 | if ((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 | #
|
---|
40 | my $pbrc = pb_init("$ENV{'PBETC'}","pbrc");
|
---|
41 |
|
---|
42 | %pbrc = %$pbrc;
|
---|
43 | if (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 | }
|
---|
48 | die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
|
---|
49 |
|
---|
50 | #
|
---|
51 | # Set delivery directory
|
---|
52 | #
|
---|
53 | my $topdir=dirname($pbrc{$proj});
|
---|
54 | chdir $topdir || die "Unable to change directory to $topdir";
|
---|
55 | $ENV{'PBDESTDIR'}=$topdir."/delivery";
|
---|
56 |
|
---|
57 | #
|
---|
58 | # Use project configuration file if needed
|
---|
59 | #
|
---|
60 | if (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";
|
---|
75 | die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
|
---|
76 |
|
---|
77 | if (-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 | #
|
---|
86 | if (defined $confparam{"cvsroot"}) {
|
---|
87 | $ENV{'CVSROOT'} = $confparam{"cvsroot"};
|
---|
88 | }
|
---|
89 |
|
---|
90 | die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir));
|
---|
91 |
|
---|
92 | #
|
---|
93 | # Set temp directory
|
---|
94 | #
|
---|
95 | if (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 | #
|
---|
103 | open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
|
---|
104 | $ver = <VER>;
|
---|
105 | chomp($ver);
|
---|
106 | #print Dumper(%version);
|
---|
107 | die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
|
---|
108 | $ENV{'PBVER'}=$ver;
|
---|
109 | close(VER);
|
---|
110 |
|
---|
111 | #
|
---|
112 | #Get global TAG
|
---|
113 | #
|
---|
114 | open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
|
---|
115 | $tag = <TAG>;
|
---|
116 | chomp($tag);
|
---|
117 | die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
|
---|
118 | $ENV{'PBTAG'}=$tag;
|
---|
119 | close(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 | #
|
---|
126 | if (-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 | }
|
---|
135 | if (! -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";
|
---|
143 | pbrm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
|
---|
144 | pbmkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
|
---|
145 |
|
---|
146 | umask 0022;
|
---|
147 | return($proj);
|
---|
148 | }
|
---|
149 |
|
---|
150 | sub pbmkdir_p {
|
---|
151 | my @dir = @_;
|
---|
152 | my $ret = mkpath(@dir, 0, 0755);
|
---|
153 | return($ret);
|
---|
154 | }
|
---|
155 |
|
---|
156 | sub pbrm_rf {
|
---|
157 | my @dir = @_;
|
---|
158 | my $ret = rmtree(@dir, 0, 0);
|
---|
159 | return($ret);
|
---|
160 | }
|
---|
161 |
|
---|
162 | sub pbsystem {
|
---|
163 |
|
---|
164 | my $cmd=shift;
|
---|
165 | my $cmt=shift || $cmd;
|
---|
166 |
|
---|
167 | print $LOG "$cmt... ";
|
---|
168 | system("$cmd");
|
---|
169 | if ($? == -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 | }
|
---|
177 | 1;
|
---|