Subversion Repositories DevTools

Rev

Rev 261 | Go to most recent revision | Details | 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
 
18
require 5.6.1;
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 Sys::Hostname;                          # For hostname
27
use JatsVersionUtils;
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
    #
230
    my ($USER) = $ENV{"USER"} || return "Need JATS 'USER' environment variable";
231
 
232
    my $MACHINENAME = hostname;
233
    return "Machine Name not determined" unless ( $MACHINENAME );
234
    chomp( $MACHINENAME );
235
 
236
    #
237
    #   Open files
238
    #
239
    open (DESCPKG, "<$src") || return "File not found [$src]";
240
    open (DESCPKGOUT, ">$dest")    || return "Failed to create file [$dest]";
241
 
242
    #
243
    #   Need to sniff the header of the file to determine which type of file
244
    #   it is. There are several types of file
245
    #
246
    my $line = <DESCPKG>;
247
    $line =~ s~\s+$~~;                              # Kill DOS and UNIX line endings
248
    return ("Empty descpkg file: $src") unless ( $line );
249
    print DESCPKGOUT $line, "\n";
250
 
251
    if ( $line =~ m/^Manifest-Version:/ )
252
    {
253
        ########################################################################
254
        #   Manifest format
255
        #
256
        my $active = 'h';
257
        my %attributes =
258
                (
259
                    'Built By:'         => $USER,
260
                    'Built On:'         => scalar( localtime()),
261
                    'Build Machine:'    => $MACHINENAME,
262
                );
263
 
264
        while ( $line = <DESCPKG> )
265
        {
266
            $line =~ s~\s+$~~;                              # Kill DOS and UNIX line endings
267
            if ( $active eq 'h' )
268
            {
269
                #
270
                #   Hunt for the Build Properties section
271
                #
272
                if ( $line =~ m/^Name: Build Properties/ )
273
                {
274
                    $active = 'p';
275
                }
276
            }
277
            elsif ($line)
278
            {
279
                #
280
                #   Process Build Properties
281
                #
282
 
283
                #
284
                #   Extract attribute name
285
                #   Pass on those we don't know
286
                #   Susbstitute those we do
287
                #
288
                $line =~ m/^(.*?:)\s+(.*)/;
289
                if ( exists $attributes{$1} )
290
                {
291
                    $line = "$1 $attributes{$1}";
292
                    delete $attributes{$1};
293
                }
294
            }
295
            else
296
            {
297
                $active = 'h';
298
 
299
                #
300
                #   End of the section
301
                #   Write out attributes not already processed
302
                #
303
                foreach  ( sort keys %attributes )
304
                {
305
                    print DESCPKGOUT "$_ $attributes{$_}\n";
306
                }
307
            }
308
        }
309
        continue
310
        {
311
            print DESCPKGOUT $line, "\n";
312
        }
313
    }
314
    elsif ( $line =~ m/^Package Name: / )
315
    {
316
        ########################################################################
317
        #   Original JATS format
318
        #
319
        while ( $line = <DESCPKG> )
320
        {
321
            $line =~ s~\s+$~~;                              # Kill DOS and UNIX line endings
322
            if ( $line =~ m/^(Released By:\s+)/ ) {
323
                $line = $1 . $USER;
324
 
325
            }
326
            elsif ( $line =~ m/^(Released On:\s+)/ ) {
327
                $line = $1 . localtime();
328
            }
329
        }
330
        continue
331
        {
332
            print DESCPKGOUT $line, "\n";
333
        }
334
    }
335
    else
336
    {
337
        ########################################################################
338
        #   Naughty format
339
        #   Possible a very old format
340
        #
341
        while ( $line = <DESCPKG> )
342
        {
343
            print DESCPKGOUT $line;
344
        }
345
    }
346
 
347
    close DESCPKG;
348
    close DESCPKGOUT;
349
    return undef;
350
}
351
 
352
1;