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 |
|
---|
16 | $ENV{'PBETC'} = "$ENV{'HOME'}/.pbrc";
|
---|
17 |
|
---|
18 | sub env_init {
|
---|
19 |
|
---|
20 | my $proj=shift;
|
---|
21 | my $ver;
|
---|
22 | my $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 | #
|
---|
30 | if ((defined $ENV{'PBPROJ'}) &&
|
---|
31 | (not (defined $proj))) {
|
---|
32 | $proj = $ENV{'PBPROJ'};
|
---|
33 | }
|
---|
34 | #
|
---|
35 | # Use project configuration file
|
---|
36 | #
|
---|
37 | pb_init("$ENV{'PBETC'}");
|
---|
38 |
|
---|
39 | if (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 | }
|
---|
44 | die "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";
|
---|
52 | die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
|
---|
53 |
|
---|
54 | if (-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 | #
|
---|
63 | if (defined $confparam{"cvsroot"}) {
|
---|
64 | $ENV{'CVSROOT'} = $confparam{"cvsroot"};
|
---|
65 | }
|
---|
66 |
|
---|
67 | die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir));
|
---|
68 |
|
---|
69 | #
|
---|
70 | # Set temp directory
|
---|
71 | #
|
---|
72 | if (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 | #
|
---|
80 | open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
|
---|
81 | $ver = <VER>;
|
---|
82 | chomp($ver);
|
---|
83 | #print Dumper(%version);
|
---|
84 | die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
|
---|
85 | $ENV{'PBVER'}=$ver;
|
---|
86 | close(VER);
|
---|
87 |
|
---|
88 | #
|
---|
89 | #Get global TAG
|
---|
90 | #
|
---|
91 | open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
|
---|
92 | $tag = <TAG>;
|
---|
93 | chomp($tag);
|
---|
94 | die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
|
---|
95 | $ENV{'PBTAG'}=$tag;
|
---|
96 | close(TAG);
|
---|
97 |
|
---|
98 | #
|
---|
99 | # Adapt to your needs
|
---|
100 | # Set delivery directory
|
---|
101 | #
|
---|
102 | chdir "$ENV{'PBROOT'}/..";
|
---|
103 | my $path = `pwd`;
|
---|
104 | chomp($path);
|
---|
105 | $ENV{'PBTOPDIR'}=$path."/delivery";
|
---|
106 | $ENV{'PBDESTDIR'}="$ENV{'PBTOPDIR'}/$ENV{'PBVER'}-$ENV{'PBTAG'}";
|
---|
107 | if (-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 | }
|
---|
115 | if (! -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";
|
---|
123 | pbrm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
|
---|
124 | pbmkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
|
---|
125 |
|
---|
126 | umask 0022;
|
---|
127 | return($proj);
|
---|
128 | }
|
---|
129 |
|
---|
130 | sub pbmkdir_p {
|
---|
131 | my @dir = @_;
|
---|
132 | my $ret = mkpath(@dir, 0, 0755);
|
---|
133 | return($ret);
|
---|
134 | }
|
---|
135 |
|
---|
136 | sub pbrm_rf {
|
---|
137 | my @dir = @_;
|
---|
138 | my $ret = rmtree(@dir, 0, 0);
|
---|
139 | return($ret);
|
---|
140 | }
|
---|
141 |
|
---|
142 | sub pbsystem {
|
---|
143 |
|
---|
144 | my $cmd=shift;
|
---|
145 | my $cmt=shift || $cmd;
|
---|
146 |
|
---|
147 | print $LOG "$cmt... ";
|
---|
148 | system("$cmd");
|
---|
149 | if ($? == -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 | }
|
---|
157 | 1;
|
---|