[2] | 1 | #!/usr/bin/perl -w
|
---|
| 2 | #
|
---|
| 3 | # Creates common environment
|
---|
| 4 | #
|
---|
| 5 | # $Id$
|
---|
| 6 | #
|
---|
| 7 |
|
---|
[18] | 8 | use strict;
|
---|
[5] | 9 | use lib qw (lib);
|
---|
[52] | 10 | use ProjectBuilder::pb qw (pb_init);
|
---|
[2] | 11 | use File::Basename;
|
---|
[9] | 12 | use File::Path;
|
---|
[2] | 13 | use File::Temp qw /tempdir/;
|
---|
[8] | 14 | use Data::Dumper;
|
---|
[70] | 15 | use vars qw (%pbrc);
|
---|
[2] | 16 |
|
---|
[49] | 17 | $ENV{'PBETC'} = "$ENV{'HOME'}/.pbrc";
|
---|
[5] | 18 |
|
---|
[9] | 19 | sub env_init {
|
---|
[2] | 20 |
|
---|
[5] | 21 | my $proj=shift;
|
---|
| 22 | my $ver;
|
---|
| 23 | my $tag;
|
---|
[2] | 24 |
|
---|
[8] | 25 | #
|
---|
[5] | 26 | # Check project name
|
---|
[49] | 27 | # Could be with env var PBPROJ
|
---|
| 28 | # or option -p
|
---|
| 29 | # if not define take the first in conf file
|
---|
[8] | 30 | #
|
---|
[5] | 31 | if ((defined $ENV{'PBPROJ'}) &&
|
---|
| 32 | (not (defined $proj))) {
|
---|
| 33 | $proj = $ENV{'PBPROJ'};
|
---|
| 34 | }
|
---|
[69] | 35 |
|
---|
[49] | 36 | #
|
---|
[69] | 37 | # We get the pbrc file for that project
|
---|
| 38 | # and use its content
|
---|
| 39 | #
|
---|
[70] | 40 | my $pbrc = pb_init("$ENV{'PBETC'}","pbrc");
|
---|
[69] | 41 |
|
---|
[70] | 42 | %pbrc = %$pbrc;
|
---|
[69] | 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=basename($pbrc{$proj});
|
---|
| 54 | chdir $topdir || die "Unable to change directory to $topdir";
|
---|
| 55 | $ENV{'PBDESTDIR'}=$topdir."/delivery";
|
---|
| 56 |
|
---|
| 57 | #
|
---|
[67] | 58 | # Use project configuration file if needed
|
---|
[49] | 59 | #
|
---|
[67] | 60 | if (not defined $ENV{'PBROOT'}) {
|
---|
[69] | 61 | if (-f $pbrc{$proj}) {
|
---|
| 62 | $pbroot = pb_init($pbrc{$proj},"pbroot");
|
---|
| 63 | # There is normaly only one line in it
|
---|
| 64 | $ENV{'PBROOT'} = (values %$pbroot)[0] if (defined $pbroot);
|
---|
| 65 | print $LOG "Using $ENV{'PBROOT'} as default pbroot from $pbrc{$proj}\n" if (($debug >= 0) and (defined $ENV{'PBROOT'}));
|
---|
[67] | 66 | }
|
---|
[69] | 67 | die "No pbroot defined - use env var PBROOT or -r pbroot " if (not defined $ENV{'PBROOT'});
|
---|
[49] | 68 | }
|
---|
[2] | 69 |
|
---|
[8] | 70 | #
|
---|
[49] | 71 | # Check pb conf compliance
|
---|
[8] | 72 | #
|
---|
[49] | 73 | $ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
|
---|
| 74 | die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
|
---|
[2] | 75 |
|
---|
[49] | 76 | if (-f "$ENV{'PBCONF'}/$proj.pb") {
|
---|
| 77 | pb_conf_init("$ENV{'PBCONF'}/$proj.pb");
|
---|
[38] | 78 | } else {
|
---|
[49] | 79 | die "Unable to open $ENV{'PBCONF'}/$proj.pb";
|
---|
[38] | 80 | }
|
---|
| 81 |
|
---|
[8] | 82 | #
|
---|
[5] | 83 | # Check content
|
---|
[8] | 84 | #
|
---|
[18] | 85 | if (defined $confparam{"cvsroot"}) {
|
---|
| 86 | $ENV{'CVSROOT'} = $confparam{"cvsroot"};
|
---|
[17] | 87 | }
|
---|
| 88 |
|
---|
[13] | 89 | die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir));
|
---|
[5] | 90 |
|
---|
[8] | 91 | #
|
---|
[5] | 92 | # Set temp directory
|
---|
[8] | 93 | #
|
---|
[7] | 94 | if (not defined $ENV{'TMPDIR'}) {
|
---|
[2] | 95 | $ENV{'TMPDIR'}="/tmp";
|
---|
| 96 | }
|
---|
[5] | 97 | $ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
|
---|
[2] | 98 |
|
---|
[8] | 99 | #
|
---|
[5] | 100 | # Get global VERSION
|
---|
[8] | 101 | #
|
---|
[11] | 102 | open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
|
---|
[5] | 103 | $ver = <VER>;
|
---|
| 104 | chomp($ver);
|
---|
[8] | 105 | #print Dumper(%version);
|
---|
| 106 | die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
|
---|
[5] | 107 | $ENV{'PBVER'}=$ver;
|
---|
| 108 | close(VER);
|
---|
| 109 |
|
---|
[8] | 110 | #
|
---|
[5] | 111 | #Get global TAG
|
---|
[8] | 112 | #
|
---|
[11] | 113 | open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
|
---|
[5] | 114 | $tag = <TAG>;
|
---|
| 115 | chomp($tag);
|
---|
[8] | 116 | die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
|
---|
[5] | 117 | $ENV{'PBTAG'}=$tag;
|
---|
| 118 | close(TAG);
|
---|
| 119 |
|
---|
[8] | 120 | #
|
---|
[69] | 121 | # Removes all directory existing below the delivery dir
|
---|
| 122 | # as they are temp dir only
|
---|
[68] | 123 | # Files stay and have to be cleaned up manually
|
---|
[8] | 124 | #
|
---|
[25] | 125 | if (-d $ENV{'PBDESTDIR'}) {
|
---|
| 126 | opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
|
---|
| 127 | foreach my $d (readdir(DIR)) {
|
---|
| 128 | next if ($d =~ /^\./);
|
---|
[68] | 129 | next if (-f "$ENV{'PBDESTDIR'}/$d");
|
---|
[25] | 130 | pbrm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
|
---|
| 131 | }
|
---|
| 132 | closedir(DIR);
|
---|
| 133 | }
|
---|
| 134 | if (! -d "$ENV{'PBDESTDIR'}") {
|
---|
| 135 | pbmkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
|
---|
| 136 | }
|
---|
[5] | 137 |
|
---|
[25] | 138 | #
|
---|
| 139 | # Set build directory
|
---|
| 140 | #
|
---|
| 141 | $ENV{'PBBUILDDIR'}=$path."/build";
|
---|
| 142 | pbrm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
|
---|
| 143 | pbmkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
|
---|
| 144 |
|
---|
[8] | 145 | umask 0022;
|
---|
| 146 | return($proj);
|
---|
[2] | 147 | }
|
---|
[9] | 148 |
|
---|
| 149 | sub pbmkdir_p {
|
---|
[29] | 150 | my @dir = @_;
|
---|
| 151 | my $ret = mkpath(@dir, 0, 0755);
|
---|
| 152 | return($ret);
|
---|
[9] | 153 | }
|
---|
| 154 |
|
---|
| 155 | sub pbrm_rf {
|
---|
[29] | 156 | my @dir = @_;
|
---|
| 157 | my $ret = rmtree(@dir, 0, 0);
|
---|
| 158 | return($ret);
|
---|
[9] | 159 | }
|
---|
| 160 |
|
---|
[29] | 161 | sub pbsystem {
|
---|
| 162 |
|
---|
| 163 | my $cmd=shift;
|
---|
[30] | 164 | my $cmt=shift || $cmd;
|
---|
[29] | 165 |
|
---|
[30] | 166 | print $LOG "$cmt... ";
|
---|
[29] | 167 | system("$cmd");
|
---|
| 168 | if ($? == -1) {
|
---|
| 169 | print $LOG "failed to execute: $!\n" if ($debug >= 0);
|
---|
| 170 | } elsif ($? & 127) {
|
---|
| 171 | printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
|
---|
| 172 | } else {
|
---|
[30] | 173 | print $LOG "OK\n" if ($debug >= 0);
|
---|
[29] | 174 | }
|
---|
[30] | 175 | }
|
---|
[2] | 176 | 1;
|
---|