[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 | }
|
---|
[49] | 34 | #
|
---|
[67] | 35 | # Use project configuration file if needed
|
---|
[49] | 36 | #
|
---|
[67] | 37 | if (not defined $ENV{'PBROOT'}) {
|
---|
| 38 | pb_init("$ENV{'PBETC'}");
|
---|
[49] | 39 |
|
---|
[67] | 40 | if (not defined $proj) {
|
---|
| 41 | # Take the first as the default project
|
---|
| 42 | $proj = (keys %pbroot)[0];
|
---|
[49] | 43 | print $LOG "Using $proj as default project as none has been specified\n" if (($debug >= 0) and (defined $proj));
|
---|
[67] | 44 | }
|
---|
| 45 | die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
|
---|
| 46 |
|
---|
| 47 | $ENV{'PBROOT'} = $pbroot{$proj};
|
---|
| 48 | } else {
|
---|
| 49 | die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
|
---|
[49] | 50 | }
|
---|
[2] | 51 |
|
---|
[8] | 52 | #
|
---|
[49] | 53 | # Check pb conf compliance
|
---|
[8] | 54 | #
|
---|
[49] | 55 | $ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
|
---|
| 56 | die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
|
---|
[2] | 57 |
|
---|
[49] | 58 | if (-f "$ENV{'PBCONF'}/$proj.pb") {
|
---|
| 59 | pb_conf_init("$ENV{'PBCONF'}/$proj.pb");
|
---|
[38] | 60 | } else {
|
---|
[49] | 61 | die "Unable to open $ENV{'PBCONF'}/$proj.pb";
|
---|
[38] | 62 | }
|
---|
| 63 |
|
---|
[8] | 64 | #
|
---|
[5] | 65 | # Check content
|
---|
[8] | 66 | #
|
---|
[18] | 67 | if (defined $confparam{"cvsroot"}) {
|
---|
| 68 | $ENV{'CVSROOT'} = $confparam{"cvsroot"};
|
---|
[17] | 69 | }
|
---|
| 70 |
|
---|
[13] | 71 | die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir));
|
---|
[5] | 72 |
|
---|
[8] | 73 | #
|
---|
[5] | 74 | # Set temp directory
|
---|
[8] | 75 | #
|
---|
[7] | 76 | if (not defined $ENV{'TMPDIR'}) {
|
---|
[2] | 77 | $ENV{'TMPDIR'}="/tmp";
|
---|
| 78 | }
|
---|
[5] | 79 | $ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
|
---|
[2] | 80 |
|
---|
[8] | 81 | #
|
---|
[5] | 82 | # Get global VERSION
|
---|
[8] | 83 | #
|
---|
[11] | 84 | open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
|
---|
[5] | 85 | $ver = <VER>;
|
---|
| 86 | chomp($ver);
|
---|
[8] | 87 | #print Dumper(%version);
|
---|
| 88 | die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
|
---|
[5] | 89 | $ENV{'PBVER'}=$ver;
|
---|
| 90 | close(VER);
|
---|
| 91 |
|
---|
[8] | 92 | #
|
---|
[5] | 93 | #Get global TAG
|
---|
[8] | 94 | #
|
---|
[11] | 95 | open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
|
---|
[5] | 96 | $tag = <TAG>;
|
---|
| 97 | chomp($tag);
|
---|
[8] | 98 | die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
|
---|
[5] | 99 | $ENV{'PBTAG'}=$tag;
|
---|
| 100 | close(TAG);
|
---|
| 101 |
|
---|
[8] | 102 | #
|
---|
[5] | 103 | # Adapt to your needs
|
---|
| 104 | # Set delivery directory
|
---|
[68] | 105 | # Removes all directory existing below as they are temp dir only
|
---|
| 106 | # Files stay and have to be cleaned up manually
|
---|
[8] | 107 | #
|
---|
[25] | 108 | chdir "$ENV{'PBROOT'}/..";
|
---|
| 109 | my $path = `pwd`;
|
---|
| 110 | chomp($path);
|
---|
[68] | 111 | $ENV{'PBDESTDIR'}=$path."/delivery";
|
---|
[25] | 112 | if (-d $ENV{'PBDESTDIR'}) {
|
---|
| 113 | opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
|
---|
| 114 | foreach my $d (readdir(DIR)) {
|
---|
| 115 | next if ($d =~ /^\./);
|
---|
[68] | 116 | next if (-f "$ENV{'PBDESTDIR'}/$d");
|
---|
[25] | 117 | pbrm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
|
---|
| 118 | }
|
---|
| 119 | closedir(DIR);
|
---|
| 120 | }
|
---|
| 121 | if (! -d "$ENV{'PBDESTDIR'}") {
|
---|
| 122 | pbmkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
|
---|
| 123 | }
|
---|
[5] | 124 |
|
---|
[25] | 125 | #
|
---|
| 126 | # Set build directory
|
---|
| 127 | #
|
---|
| 128 | $ENV{'PBBUILDDIR'}=$path."/build";
|
---|
| 129 | pbrm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
|
---|
| 130 | pbmkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
|
---|
| 131 |
|
---|
[8] | 132 | umask 0022;
|
---|
| 133 | return($proj);
|
---|
[2] | 134 | }
|
---|
[9] | 135 |
|
---|
| 136 | sub pbmkdir_p {
|
---|
[29] | 137 | my @dir = @_;
|
---|
| 138 | my $ret = mkpath(@dir, 0, 0755);
|
---|
| 139 | return($ret);
|
---|
[9] | 140 | }
|
---|
| 141 |
|
---|
| 142 | sub pbrm_rf {
|
---|
[29] | 143 | my @dir = @_;
|
---|
| 144 | my $ret = rmtree(@dir, 0, 0);
|
---|
| 145 | return($ret);
|
---|
[9] | 146 | }
|
---|
| 147 |
|
---|
[29] | 148 | sub pbsystem {
|
---|
| 149 |
|
---|
| 150 | my $cmd=shift;
|
---|
[30] | 151 | my $cmt=shift || $cmd;
|
---|
[29] | 152 |
|
---|
[30] | 153 | print $LOG "$cmt... ";
|
---|
[29] | 154 | system("$cmd");
|
---|
| 155 | if ($? == -1) {
|
---|
| 156 | print $LOG "failed to execute: $!\n" if ($debug >= 0);
|
---|
| 157 | } elsif ($? & 127) {
|
---|
| 158 | printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
|
---|
| 159 | } else {
|
---|
[30] | 160 | print $LOG "OK\n" if ($debug >= 0);
|
---|
[29] | 161 | }
|
---|
[30] | 162 | }
|
---|
[2] | 163 | 1;
|
---|