| 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 | # |
|---|
| 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 | # |
|---|
| 57 | # Use project configuration file if needed |
|---|
| 58 | # |
|---|
| 59 | if (not defined $ENV{'PBROOT'}) { |
|---|
| 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'})); |
|---|
| 65 | } |
|---|
| 66 | die "No pbroot defined - use env var PBROOT or -r pbroot " if (not defined $ENV{'PBROOT'}); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | # |
|---|
| 70 | # Check pb conf compliance |
|---|
| 71 | # |
|---|
| 72 | $ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf"; |
|---|
| 73 | die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}"); |
|---|
| 74 | |
|---|
| 75 | if (-f "$ENV{'PBCONF'}/$proj.pb") { |
|---|
| 76 | pb_conf_init("$ENV{'PBCONF'}/$proj.pb"); |
|---|
| 77 | } else { |
|---|
| 78 | die "Unable to open $ENV{'PBCONF'}/$proj.pb"; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | # |
|---|
| 82 | # Check content |
|---|
| 83 | # |
|---|
| 84 | if (defined $confparam{"cvsroot"}) { |
|---|
| 85 | $ENV{'CVSROOT'} = $confparam{"cvsroot"}; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir)); |
|---|
| 89 | |
|---|
| 90 | # |
|---|
| 91 | # Set temp directory |
|---|
| 92 | # |
|---|
| 93 | if (not defined $ENV{'TMPDIR'}) { |
|---|
| 94 | $ENV{'TMPDIR'}="/tmp"; |
|---|
| 95 | } |
|---|
| 96 | $ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 ); |
|---|
| 97 | |
|---|
| 98 | # |
|---|
| 99 | # Get global VERSION |
|---|
| 100 | # |
|---|
| 101 | open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?"; |
|---|
| 102 | $ver = <VER>; |
|---|
| 103 | chomp($ver); |
|---|
| 104 | #print Dumper(%version); |
|---|
| 105 | die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver}); |
|---|
| 106 | $ENV{'PBVER'}=$ver; |
|---|
| 107 | close(VER); |
|---|
| 108 | |
|---|
| 109 | # |
|---|
| 110 | #Get global TAG |
|---|
| 111 | # |
|---|
| 112 | open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?"; |
|---|
| 113 | $tag = <TAG>; |
|---|
| 114 | chomp($tag); |
|---|
| 115 | die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/); |
|---|
| 116 | $ENV{'PBTAG'}=$tag; |
|---|
| 117 | close(TAG); |
|---|
| 118 | |
|---|
| 119 | # |
|---|
| 120 | # Removes all directory existing below the delivery dir |
|---|
| 121 | # as they are temp dir only |
|---|
| 122 | # Files stay and have to be cleaned up manually |
|---|
| 123 | # |
|---|
| 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 =~ /^\./); |
|---|
| 128 | next if (-f "$ENV{'PBDESTDIR'}/$d"); |
|---|
| 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 | } |
|---|
| 136 | |
|---|
| 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 | |
|---|
| 144 | umask 0022; |
|---|
| 145 | return($proj); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | sub pbmkdir_p { |
|---|
| 149 | my @dir = @_; |
|---|
| 150 | my $ret = mkpath(@dir, 0, 0755); |
|---|
| 151 | return($ret); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | sub pbrm_rf { |
|---|
| 155 | my @dir = @_; |
|---|
| 156 | my $ret = rmtree(@dir, 0, 0); |
|---|
| 157 | return($ret); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | sub pbsystem { |
|---|
| 161 | |
|---|
| 162 | my $cmd=shift; |
|---|
| 163 | my $cmt=shift || $cmd; |
|---|
| 164 | |
|---|
| 165 | print $LOG "$cmt... "; |
|---|
| 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 { |
|---|
| 172 | print $LOG "OK\n" if ($debug >= 0); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | 1; |
|---|