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