[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);
|
---|
[6] | 10 | use 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 | }
|
---|
[49] | 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 | }
|
---|
[5] | 44 | die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
|
---|
[2] | 45 |
|
---|
[49] | 46 | $ENV{'PBROOT'} = $pbroot{$proj};
|
---|
| 47 |
|
---|
[8] | 48 | #
|
---|
[49] | 49 | # Check pb conf compliance
|
---|
[8] | 50 | #
|
---|
[49] | 51 | $ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
|
---|
| 52 | die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
|
---|
[2] | 53 |
|
---|
[49] | 54 | if (-f "$ENV{'PBCONF'}/$proj.pb") {
|
---|
| 55 | pb_conf_init("$ENV{'PBCONF'}/$proj.pb");
|
---|
[38] | 56 | } else {
|
---|
[49] | 57 | die "Unable to open $ENV{'PBCONF'}/$proj.pb";
|
---|
[38] | 58 | }
|
---|
| 59 |
|
---|
[8] | 60 | #
|
---|
[5] | 61 | # Check content
|
---|
[8] | 62 | #
|
---|
[18] | 63 | if (defined $confparam{"cvsroot"}) {
|
---|
| 64 | $ENV{'CVSROOT'} = $confparam{"cvsroot"};
|
---|
[17] | 65 | }
|
---|
| 66 |
|
---|
[13] | 67 | die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir));
|
---|
[5] | 68 |
|
---|
[8] | 69 | #
|
---|
[5] | 70 | # Set temp directory
|
---|
[8] | 71 | #
|
---|
[7] | 72 | if (not defined $ENV{'TMPDIR'}) {
|
---|
[2] | 73 | $ENV{'TMPDIR'}="/tmp";
|
---|
| 74 | }
|
---|
[5] | 75 | $ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
|
---|
[2] | 76 |
|
---|
[8] | 77 | #
|
---|
[5] | 78 | # Get global VERSION
|
---|
[8] | 79 | #
|
---|
[11] | 80 | open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
|
---|
[5] | 81 | $ver = <VER>;
|
---|
| 82 | chomp($ver);
|
---|
[8] | 83 | #print Dumper(%version);
|
---|
| 84 | die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
|
---|
[5] | 85 | $ENV{'PBVER'}=$ver;
|
---|
| 86 | close(VER);
|
---|
| 87 |
|
---|
[8] | 88 | #
|
---|
[5] | 89 | #Get global TAG
|
---|
[8] | 90 | #
|
---|
[11] | 91 | open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
|
---|
[5] | 92 | $tag = <TAG>;
|
---|
| 93 | chomp($tag);
|
---|
[8] | 94 | die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
|
---|
[5] | 95 | $ENV{'PBTAG'}=$tag;
|
---|
| 96 | close(TAG);
|
---|
| 97 |
|
---|
[8] | 98 | #
|
---|
[5] | 99 | # Adapt to your needs
|
---|
| 100 | # Set delivery directory
|
---|
[8] | 101 | #
|
---|
[25] | 102 | chdir "$ENV{'PBROOT'}/..";
|
---|
| 103 | my $path = `pwd`;
|
---|
| 104 | chomp($path);
|
---|
| 105 | $ENV{'PBTOPDIR'}=$path."/delivery";
|
---|
[9] | 106 | $ENV{'PBDESTDIR'}="$ENV{'PBTOPDIR'}/$ENV{'PBVER'}-$ENV{'PBTAG'}";
|
---|
[25] | 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 | }
|
---|
[5] | 118 |
|
---|
[25] | 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 |
|
---|
[8] | 126 | umask 0022;
|
---|
| 127 | return($proj);
|
---|
[2] | 128 | }
|
---|
[9] | 129 |
|
---|
| 130 | sub pbmkdir_p {
|
---|
[29] | 131 | my @dir = @_;
|
---|
| 132 | my $ret = mkpath(@dir, 0, 0755);
|
---|
| 133 | return($ret);
|
---|
[9] | 134 | }
|
---|
| 135 |
|
---|
| 136 | sub pbrm_rf {
|
---|
[29] | 137 | my @dir = @_;
|
---|
| 138 | my $ret = rmtree(@dir, 0, 0);
|
---|
| 139 | return($ret);
|
---|
[9] | 140 | }
|
---|
| 141 |
|
---|
[29] | 142 | sub pbsystem {
|
---|
| 143 |
|
---|
| 144 | my $cmd=shift;
|
---|
[30] | 145 | my $cmt=shift || $cmd;
|
---|
[29] | 146 |
|
---|
[30] | 147 | print $LOG "$cmt... ";
|
---|
[29] | 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 {
|
---|
[30] | 154 | print $LOG "OK\n" if ($debug >= 0);
|
---|
[29] | 155 | }
|
---|
[30] | 156 | }
|
---|
[2] | 157 | 1;
|
---|