Subversion Repositories DevTools

Rev

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

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