summaryrefslogtreecommitdiffabout
path: root/lib/SlackBuild/Info.pm
blob: 4f1c1f1002c5dc7356a073947fa793bebb43bbd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package SlackBuild::Info;
use strict;
use warnings;
use Carp;
use Text::ParseWords;
use JSON;

=head1 NAME

SlackBuild::Info - a slackbuilds.org info object

=head1 DESCRIPTION

A representation of the B<slackbuilds.org> "info" file. See
the L<https://slackbuilds.org/guidelines/>, section B<$PRGNAM.info file>.

=cut    
    
my %attributes = (
    PRGNAM   => 1,
    VERSION  => 1,
    HOMEPAGE => 1,
    DOWNLOAD => 'ARRAY',
    MD5SUM   => 'ARRAY',
    DOWNLOAD_x86_64 => 'ARRAY',
    MD5SUM_x86_64   => 'ARRAY',
    REQUIRES => 'ARRAY',
    MAINTAINER => 1,
    EMAIL => 1
);

=head1 CONSTRUCTOR

    new SlackBuild::Info(FH)

Reads the attributes from the file handle B<FH>.

    new SlackBuild::Info(\$string)

Reads the attributes from the string B<$string>, which must contain the text
of an info file.    

    new SlackBuild::Info($filename)

Reads the attributes from the file named B<$filename>.

    new SlackBuild::Info

Returns an empty info object.

=cut    

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    if (my $arg = shift) {
	my $fd;
	if (ref($arg) eq 'GLOB') {
	    open($fd, '<&', $arg)
		or croak "can't dup filehandle: $!";
	} else {
	    open($fd, '<', $arg)
		or croak "can't open $arg: $!";
	}
	while (<$fd>) {
	    chomp;
	    s/\s+$//;
	    if (/\\$/) {
		chop;
		$_ .= <$fd>;
		redo;
	    }
	    s/^\s+//;
	    next if /^(#.*)?$/;
	    croak "bad input line: $_"
		unless /^(?<kw>[A-Za-z_][A-Za-z_0-9]*)="(?<val>.*)"$/;
	    if (exists($attributes{$+{kw}})) {
		$self->${\$+{kw}}($+{val});
	    } else {
		carp "ignoring unrecognized line: $_";
	    }
	}
	close $fd;
    }
    return $self;
}

=head2 for_arch

    $clone = $info->for_arch($arch)

Returns a clone of B<$info> with B<DOWNOAD> and B<MD5SUM> attributes adjusted
for architecture B<$arch>.

=cut    

sub for_arch {
    my ($self, $arch) = @_;
    my $clone = new SlackBuild::Info;
    my @attrs = keys %attributes;
    
    if (my $d = $self->{'DOWNLOAD_'.$arch}) {
	if ($d ne 'UNSUPPORTED') {
	    $clone->{DOWNLOAD} = $d;
	    $clone->{MD5SUM} = $self->{'MD5SUM_'.$arch};
	    @attrs = grep { !/^(DOWNLOAD|MD5SUM)/ } @attrs;
	}
    }

    foreach my $attr (@attrs) {
	if ($self->{attr} && $attributes{$attr} eq 'ARRAY') {
	    $clone->{$attr} = [ @{$self->{$attr}} ];
	} else {
	    $clone->{$attr} = $self->{$attr};
	}
    }
    return $clone;
}

=head1 STRING REPRESENTATION

When used is string context, objects of this class are represented as
JSON objects with attribute names sorted in lexical order. The same
representation is returned by the B<str> method.    

=cut

sub as_string {
    my $self = shift;
    my %h;
    my @k = keys %attributes;
    @h{@k} = map { $self->${\$_} } @k;
    JSON->new->canonical(1)->encode(\%h);
}

use overload '""' => sub { shift->as_string };

{
    no strict 'refs';
    use feature 'state';
    while (my ($attr,$type) = each %attributes) {
	*{ __PACKAGE__ . '::' . $attr } = sub {
	    my $self = shift;
	    croak "too many arguments" if @_ > 1;
	    if (my $v = shift) {
		if ($type eq 'ARRAY') {
		    $self->{$attr} = [parse_line('\s+', 0, $v)];
		} else {
		    $self->{$attr} = $v;
		}
	    }
	    return $self->{$attr};
	}
    }
}

1;

Return to:

Send suggestions and report system problems to the System administrator.