Subversion Repositories DevTools

Rev

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