Subversion Repositories DevTools

Rev

Rev 7301 | Details | Compare with Previous | Last modification | View Log | RSS feed

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