Subversion Repositories DevTools

Rev

Rev 261 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 dpurdie 1
#! perl
2
########################################################################
3
# Copyright ( C ) 2005 ERG Limited, All rights reserved
4
#
5
# Module name   : jats.sh
6
# Module type   : Perl Package
7
# Compiler(s)   : n/a
8
# Environment(s): jats
9
#
10
# Description   : This package contains functions to manipulate desckpkg files
11
#
12
# Usage:
13
#
14
# Version   Who      Date        Description
15
#
16
#......................................................................#
17
 
261 dpurdie 18
require 5.006_001;
227 dpurdie 19
use strict;
20
use warnings;
21
 
22
package DescPkg;
23
 
24
our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION);
25
use Exporter;
26
use JatsVersionUtils;
279 dpurdie 27
use JatsEnv;
227 dpurdie 28
 
29
$VERSION = 1.00;
30
@ISA = qw(Exporter);
31
 
32
# Symbols to autoexport (:DEFAULT tag)
33
@EXPORT = qw( ReadDescpkg
34
              CopyDescpkg
35
            );
36
 
37
#-------------------------------------------------------------------------------
38
# Function        : ReadDescpkg
39
#
40
# Description     : Read in a descpkg file
41
#                   Support both old and new formats of the file
42
#
43
# Inputs          : path    - path of the file to process
44
#                   mode    - 1 == process dependancies
45
#
46
# Returns         : undef if the file was not found
47
#                   Pointer to a hash of useful information
48
#
49
sub ReadDescpkg
50
{
51
    my ($path, $mode) = @_;
52
    my $line;
53
    my $rec;
54
    my $ver_string;
55
 
56
    open (DESCPKG, "$path") || return undef;
57
 
58
    #
59
    #  Slurp the first line and determine the type of the file
60
    #  If the descpkg file is empty then this is an error
61
    #
62
    $line = <DESCPKG>;
63
    if ( ! $line )
64
    {
65
        close DESCPKG;
66
        return undef;
67
    }
68
    elsif ( $line =~ m/^Manifest-Version:/ )
69
    {
70
        #
71
        #   Manifest form
72
        #
73
        my $section;
74
        while ( defined( $line = <DESCPKG> ) )
75
        {
76
            $line =~ s~\s+$~~;                              # Kill DOS and UNIX line endings
77
 
78
            #
79
            #   Detect section break;
80
            #
81
            if ( $line =~ m/^Name:\s*(.*)/ )
82
            {
83
                $section = $1;
84
                next;
85
            }
86
            next unless ( $section );
87
 
88
            #
89
            #   Extract Build Properties
90
            #
91
            if ( $section eq "Build Properties" )
92
            {
93
                if ( $line =~ m/^Package Name:\s*(.*)/ )
94
                {
95
                    $rec->{'NAME'} = $1;
96
                }
97
                elsif ( $line =~ m/^Package Version:\s*(.*)$/ )
98
                {
99
                    $ver_string = $1;
100
                }
101
            }
102
            elsif ( $mode && $section eq "Build Dependencies" )
103
            {
104
                my %data;
105
                if ( $line =~ m/(.*):\s*(.*)/ )
106
                {
107
                    $data{name} = $1;
108
                    $data{version} = $2;
109
                    push @{$rec->{'PACKAGES'}}, \%data;
110
                }
111
            }
112
        }
113
    }
114
    elsif ( $line =~ m/^Package Name:\s/ )
115
    {
116
        #
117
        #   New form
118
        #
119
        while ( 1 )
120
        {
121
            $line =~ s~\s+$~~;                              # Kill DOS and UNIX line endings
122
            if ( $line =~ m/^Package Name:\s*(.*)/ )
123
            {
124
                $rec->{'NAME'} = $1;
125
            }
126
            elsif ( $line =~ m/^Version:\s*(.*)$/ )
127
            {
128
                    $ver_string = $1;
129
            }
130
            elsif ( $line =~ m/^Build Dependencies:/ )
131
            {
132
                last;
133
            }
134
            last unless ( defined ($line = <DESCPKG>) )
135
        }
136
 
137
        #
138
        #   Extract dependancies
139
        #   Keep the order of the dependancies as this may be important
140
        #   These are stored in an array of hashes.
141
        #
142
        #   Locate lines of the form:
143
        #       <sandbox .... />
144
        #   and extract all attributes. These are of the form
145
        #       attribute_name="attribute_value"
146
        #   The values are stored in a hash for later use
147
        #
148
        if ( $mode )
149
        {
150
            while ( defined( $line = <DESCPKG> ) )
151
            {
152
                $line =~ s~\s+$~~;                              # Kill DOS and UNIX line endings
153
                if ( $line =~ m~<sandbox\s+(.*)/>~ )
154
                {
155
                    my $raw = $1;
156
                    my $data;
157
                    while ( $raw =~ m/(\w*?)="(.*?)"/g )
158
                    {
159
                        $data->{$1} = $2;
160
                    }
161
                    push @{$rec->{'PACKAGES'}}, $data;
162
                }
163
            }
164
        }
165
    }
166
    else
167
    {
168
        #
169
        #   Old form
170
        #   Cleanup various bad habits
171
        #       1) Remove trailing comments ie: space-space
172
        #       2) Replace , with a space
173
        #
174
        $line =~ s~\s+-\s+.*~~;
175
        $line =~ s~,~ ~g;
176
 
177
        my $proj;
178
        ($rec->{'NAME'}, $ver_string, $proj) = split( ' ', $line );
179
        #
180
        #   Attempt to correct for a common error in old packages
181
        #   where the project is attached to to the version
182
        #   ie: name 1.2.3.cr instead of name 1.2.3 cr
183
        #
184
        $ver_string .= '.' . $proj if ( $proj );
185
    }
186
 
187
    close DESCPKG;
188
 
189
    #
190
    #   Ensure the package Name has been found
191
    #
192
    return undef
193
        unless ( exists ($rec->{'NAME'}) && $rec->{'NAME'} && $ver_string );
194
 
195
    #
196
    #   Split the version string into bits and save the results
197
    #
198
    (
199
     $rec->{'NAME'},
200
     $rec->{'VERSION'},
201
     $rec->{'PROJ'},
202
     $rec->{'VERSION_FULL'} ) = SplitPackage( $rec->{'NAME'} ,$ver_string);
203
 
204
    return $rec;
205
}
206
 
207
#-------------------------------------------------------------------------------
208
# Function        : CopyDescpkg
209
#
210
# Description     : Copy a descpkg file and update various fields
211
#                   Several fields will be re-written or modified
212
#                   Used when creating a package to maintain package contents
213
#                   Supports all the formats of descpkg
214
#
215
# Inputs          : $src       - Source Path
216
#                   $dest      - Destination path
217
#
218
#
219
# Returns         : 0    - All is well
220
#                   Else - Error string
221
#
222
#
223
sub CopyDescpkg
224
{
225
    my ($src,$dest) = @_;
226
 
227
    #
228
    #   Ensure that we have user and machine name
229
    #
279 dpurdie 230
    EnvImport( "USER");
231
    EnvImport( "GBE_HOSTNAME");
232
 
227 dpurdie 233
    #
234
    #   Open files
235
    #
236
    open (DESCPKG, "<$src") || return "File not found [$src]";
237
    open (DESCPKGOUT, ">$dest")    || return "Failed to create file [$dest]";
238
 
239
    #
240
    #   Need to sniff the header of the file to determine which type of file
241
    #   it is. There are several types of file
242
    #
243
    my $line = <DESCPKG>;
244
    $line =~ s~\s+$~~;                              # Kill DOS and UNIX line endings
245
    return ("Empty descpkg file: $src") unless ( $line );
246
    print DESCPKGOUT $line, "\n";
247
 
248
    if ( $line =~ m/^Manifest-Version:/ )
249
    {
250
        ########################################################################
251
        #   Manifest format
252
        #
253
        my $active = 'h';
254
        my %attributes =
255
                (
279 dpurdie 256
                    'Built By:'         => $::USER,
227 dpurdie 257
                    'Built On:'         => scalar( localtime()),
279 dpurdie 258
                    'Build Machine:'    => $::GBE_HOSTNAME
227 dpurdie 259
                );
260
 
261
        while ( $line = <DESCPKG> )
262
        {
263
            $line =~ s~\s+$~~;                              # Kill DOS and UNIX line endings
264
            if ( $active eq 'h' )
265
            {
266
                #
267
                #   Hunt for the Build Properties section
268
                #
269
                if ( $line =~ m/^Name: Build Properties/ )
270
                {
271
                    $active = 'p';
272
                }
273
            }
274
            elsif ($line)
275
            {
276
                #
277
                #   Process Build Properties
278
                #
279
 
280
                #
281
                #   Extract attribute name
282
                #   Pass on those we don't know
283
                #   Susbstitute those we do
284
                #
285
                $line =~ m/^(.*?:)\s+(.*)/;
286
                if ( exists $attributes{$1} )
287
                {
288
                    $line = "$1 $attributes{$1}";
289
                    delete $attributes{$1};
290
                }
291
            }
292
            else
293
            {
294
                $active = 'h';
295
 
296
                #
297
                #   End of the section
298
                #   Write out attributes not already processed
299
                #
300
                foreach  ( sort keys %attributes )
301
                {
302
                    print DESCPKGOUT "$_ $attributes{$_}\n";
303
                }
304
            }
305
        }
306
        continue
307
        {
308
            print DESCPKGOUT $line, "\n";
309
        }
310
    }
311
    elsif ( $line =~ m/^Package Name: / )
312
    {
313
        ########################################################################
314
        #   Original JATS format
315
        #
316
        while ( $line = <DESCPKG> )
317
        {
318
            $line =~ s~\s+$~~;                              # Kill DOS and UNIX line endings
319
            if ( $line =~ m/^(Released By:\s+)/ ) {
279 dpurdie 320
                $line = $1 . $::USER;
227 dpurdie 321
 
322
            }
323
            elsif ( $line =~ m/^(Released On:\s+)/ ) {
324
                $line = $1 . localtime();
325
            }
326
        }
327
        continue
328
        {
329
            print DESCPKGOUT $line, "\n";
330
        }
331
    }
332
    else
333
    {
334
        ########################################################################
335
        #   Naughty format
336
        #   Possible a very old format
337
        #
338
        while ( $line = <DESCPKG> )
339
        {
340
            print DESCPKGOUT $line;
341
        }
342
    }
343
 
344
    close DESCPKG;
345
    close DESCPKGOUT;
346
    return undef;
347
}
348
 
349
1;