source: devel/pb/lib/common.pm @ 30

Last change on this file since 30 was 30, checked in by Bruno Cornec, 13 years ago

fix typos

  • Property svn:executable set to *
File size: 3.1 KB
Line 
1#!/usr/bin/perl -w
2#
3# Creates common environment
4#
5# $Id$
6#
7
8use strict;
9use lib qw (lib);
10use pb qw (pb_init);
11use File::Basename;
12use File::Path;
13use File::Temp qw /tempdir/;
14use Data::Dumper;
15
16$ENV{'PBETC'} = "/etc/pb";
17
18sub env_init {
19
20my $proj=shift;
21my $ver;
22my $tag;
23
24#
25# Check project name
26#
27if ((defined $ENV{'PBPROJ'}) &&
28    (not (defined $proj))) {
29    $proj = $ENV{'PBPROJ'};
30}
31die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
32
33#
34# Use project configuration file
35#
36pb_init("$ENV{'PBETC'}/$proj.pb");
37
38#
39# Check content
40#
41if (not defined $confparam{"pbroot"}) {
42    die "confparam pbroot doesn't exist in $ENV{'PBETC'}/$proj.pb";
43} else {
44    $ENV{'PBROOT'} = $confparam{"pbroot"}; 
45}
46if (defined $confparam{"cvsroot"}) {
47    $ENV{'CVSROOT'} = $confparam{"cvsroot"}; 
48}
49
50die "defpkgdir doesn't exist in $ENV{'PBETC'}/$proj.pb" if (not (defined %defpkgdir));
51
52#
53# Set temp directory
54#
55if (not defined $ENV{'TMPDIR'}) {
56    $ENV{'TMPDIR'}="/tmp";
57}
58$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
59
60#
61# Check pb conf compliance
62#
63$ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
64die "Project $ENV{'PBPROJ'} not ProjectBuild compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
65
66#
67# Get global VERSION
68#
69open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
70$ver = <VER>;
71chomp($ver);
72#print Dumper(%version);
73die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
74$ENV{'PBVER'}=$ver;
75close(VER);
76
77#
78#Get global TAG
79#
80open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
81$tag = <TAG>;
82chomp($tag);
83die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
84$ENV{'PBTAG'}=$tag;
85close(TAG);
86
87#
88# Adapt to your needs
89# Set delivery directory
90#
91chdir "$ENV{'PBROOT'}/..";
92my $path = `pwd`;
93chomp($path);
94$ENV{'PBTOPDIR'}=$path."/delivery";
95$ENV{'PBDESTDIR'}="$ENV{'PBTOPDIR'}/$ENV{'PBVER'}-$ENV{'PBTAG'}";
96if (-d $ENV{'PBDESTDIR'}) {
97    opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
98    foreach my $d (readdir(DIR)) {
99        next if ($d =~ /^\./);
100        pbrm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
101    }
102    closedir(DIR);
103}
104if (! -d "$ENV{'PBDESTDIR'}") {
105    pbmkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
106}
107
108#
109# Set build directory
110#
111$ENV{'PBBUILDDIR'}=$path."/build";
112pbrm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
113pbmkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
114
115umask 0022;
116return($proj);
117}
118
119sub pbmkdir_p {
120my @dir = @_;
121my $ret = mkpath(@dir, 0, 0755);
122return($ret);
123}
124
125sub pbrm_rf {
126my @dir = @_;
127my $ret = rmtree(@dir, 0, 0);
128return($ret);
129}
130
131sub pbsystem {
132
133my $cmd=shift;
134my $cmt=shift || $cmd;
135
136print $LOG "$cmt... ";
137system("$cmd");
138if ($? == -1) {
139    print $LOG "failed to execute: $!\n" if ($debug >= 0);
140} elsif ($? & 127) {
141    printf $LOG "child died with signal %d, %s coredump\n", ($? & 127),  ($? & 128) ? 'with' : 'without' if ($debug >= 0);
142} else {
143    print $LOG "OK\n" if ($debug >= 0);
144}
145}
1461;
Note: See TracBrowser for help on using the repository browser.