602
|
1 |
# Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
# All rights reserved.
|
|
3 |
# This component and the accompanying materials are made available
|
|
4 |
# under the terms of the License "Eclipse Public License v1.0"
|
|
5 |
# which accompanies this distribution, and is available
|
|
6 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
#
|
|
8 |
# Initial Contributors:
|
|
9 |
# Nokia Corporation - initial contribution.
|
|
10 |
#
|
|
11 |
# Contributors:
|
|
12 |
#
|
|
13 |
# Description:
|
|
14 |
#
|
|
15 |
#
|
|
16 |
|
|
17 |
package Symbian::DistributionPolicy::Reader;
|
|
18 |
use strict;
|
|
19 |
|
|
20 |
use File::Basename;
|
|
21 |
use Symbian::DistributionPolicy;
|
|
22 |
|
|
23 |
our $cache = {}; # Persistent (private) cache
|
|
24 |
|
|
25 |
sub new {
|
|
26 |
return bless {};
|
|
27 |
}
|
|
28 |
|
|
29 |
# public.
|
|
30 |
|
|
31 |
sub ReadPolicyFile {
|
|
32 |
my $self = shift;
|
|
33 |
my $path = shift;
|
|
34 |
my $dir = -d $path ? $path : dirname($path);
|
|
35 |
# look in cache first, retrieve if not already defined
|
|
36 |
return $cache->{$dir} ||= $self->readfile($dir.'\\DISTRIBUTION.POLICY');
|
|
37 |
}
|
|
38 |
|
|
39 |
# private.
|
|
40 |
|
|
41 |
sub readfile {
|
|
42 |
my $self = shift;
|
|
43 |
my $file = shift;
|
|
44 |
my $hasCategory;
|
|
45 |
my $policy = Symbian::DistributionPolicy->new();
|
|
46 |
# default policy applies when the file does not exist
|
|
47 |
return $policy if !-e $file;
|
|
48 |
# attempt to open the policy file
|
|
49 |
open(POLICY, $file) or die "Couldn't open $file: $!\n";
|
|
50 |
# read policy data
|
|
51 |
while (<POLICY>) {
|
|
52 |
s/(?<!\\)#.*$//; # ignore comments
|
|
53 |
s/^\s+|\s+$//g; # trim whitespace
|
|
54 |
next unless /\S/; # skip blank line
|
|
55 |
# parse line
|
|
56 |
if (/^authori[sz]ed\s+(.+?)(?:\s+until\s+(.+?))?$/i) {
|
|
57 |
# licensee specific authorisation
|
|
58 |
if (!$policy->SetAuthorizedUntil($1, $2)) {
|
|
59 |
warn "Invalid Authorized directive in $file\n";
|
|
60 |
}
|
|
61 |
} elsif (/^category\s+([a-z])$/i) {
|
|
62 |
# ipr category
|
|
63 |
if (!$policy->SetCategory($1)) {
|
|
64 |
warn "Invalid Category directive in $file\n";
|
|
65 |
}
|
|
66 |
$hasCategory = 1;
|
|
67 |
} elsif (/^description\s+(.*)$/i) {
|
|
68 |
# free text description
|
|
69 |
$policy->SetDescription($1);
|
|
70 |
} elsif (/^expires\s+(.*)$/i) {
|
|
71 |
# best before date
|
|
72 |
if (!$policy->SetExpires($1)) {
|
|
73 |
warn "Invalid Expires directive in $file\n";
|
|
74 |
}
|
|
75 |
} elsif (/^export\s+(un)?restricted$/i) {
|
|
76 |
# exportable/embargoed?
|
|
77 |
$policy->SetExportRestricted(!defined($1));
|
|
78 |
} elsif (/^osd:\s*(.+?)$/i) {
|
|
79 |
# parse osd info
|
|
80 |
$self->handle_osd($1, $policy);
|
|
81 |
}
|
|
82 |
}
|
|
83 |
close(POLICY);
|
|
84 |
|
|
85 |
if (!$hasCategory) {
|
|
86 |
warn "Warning: \'$file\' does not contain an IPR category\n";
|
|
87 |
}
|
|
88 |
|
|
89 |
return $policy;
|
|
90 |
}
|
|
91 |
|
|
92 |
sub handle_osd {
|
|
93 |
my $self = shift;
|
|
94 |
local $_ = shift;
|
|
95 |
my $policy = shift;
|
|
96 |
# SGL.PPS246.201DistributionPolicyFileContents.doc
|
|
97 |
if (/^(common|optional)\s+(symbian|replaceable)\s+(.+)$/i) {
|
|
98 |
# set common/optional
|
|
99 |
if (lc($1) eq 'common') {
|
|
100 |
$policy->SetCommon(1);
|
|
101 |
} else {
|
|
102 |
$policy->SetOptional(1);
|
|
103 |
}
|
|
104 |
# set symbian/replaceable
|
|
105 |
if (lc($2) eq 'symbian') {
|
|
106 |
$policy->SetSymbian(1);
|
|
107 |
} else {
|
|
108 |
$policy->SetReplaceable(1);
|
|
109 |
}
|
|
110 |
} elsif (/^(?:reference\/test)\s+(.+)$/i) {
|
|
111 |
# set test
|
|
112 |
$policy->SetTest(1);
|
|
113 |
} elsif (/^(?:test\/reference)\s+(.+)$/i) {
|
|
114 |
# synonym for reference/test
|
|
115 |
$policy->SetTest(1);
|
|
116 |
} else {
|
|
117 |
warn "Invalid OSD directive: '$_' (see SGL.PPS246.201)\n";
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
1;
|
|
122 |
|
|
123 |
=pod
|
|
124 |
|
|
125 |
=head1 NAME
|
|
126 |
|
|
127 |
Symbian::DistributionPolicy::Reader - Caching DISTRIBUTION.POLICY file reader.
|
|
128 |
|
|
129 |
=head1 SYNOPSIS
|
|
130 |
|
|
131 |
use Symbian::DistributionPolicy::Reader;
|
|
132 |
|
|
133 |
my $dpr = Symbian::DistributionPolicy::Reader->new();
|
|
134 |
|
|
135 |
my $policy = $dpr->ReadPolicyFile($path);
|
|
136 |
|
|
137 |
=head1 DESCRIPTION
|
|
138 |
|
|
139 |
This module parses and caches policy data from DISTRIBUTION.POLICY files.
|
|
140 |
|
|
141 |
=head1 METHODS
|
|
142 |
|
|
143 |
=head2 new()
|
|
144 |
|
|
145 |
Creates the reader object.
|
|
146 |
|
|
147 |
=head2 ReadPolicyFile($path)
|
|
148 |
|
|
149 |
Read the DISTRIBUTION.POLICY file in $path (which can be e.g. a source file, a
|
|
150 |
directory or the DISTRIBUTION.POLICY file itself) and return a
|
|
151 |
Symbian::DistributionPolicy object containing the policy data. The policy is
|
|
152 |
cached to prevent unnecessary re-reading of .POLICY files in subsequent calls.
|
|
153 |
|
|
154 |
=head1 SEE ALSO
|
|
155 |
|
|
156 |
L<Symbian::DistributionPolicy> to find out what you can do with your $policy
|
|
157 |
object.
|
|
158 |
|
|
159 |
=head1 COPYRIGHT
|
|
160 |
|
|
161 |
Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
162 |
All rights reserved.
|
|
163 |
This component and the accompanying materials are made available
|
|
164 |
under the terms of the License "Eclipse Public License v1.0"
|
|
165 |
which accompanies this distribution, and is available
|
|
166 |
at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
167 |
|
|
168 |
Initial Contributors:
|
|
169 |
Nokia Corporation - initial contribution.
|
|
170 |
|
|
171 |
Contributors:
|
|
172 |
|
|
173 |
Description:
|
|
174 |
|
|
175 |
|
|
176 |
=cut
|