File Coverage

blib/lib/Debian/Copyright.pm
Criterion Covered Total %
statement 59 64 92.1
branch 12 20 60.0
condition 1 6 16.6
subroutine 11 11 100.0
pod 3 3 100.0
total 86 104 82.6


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Debian::Copyright - manage Debian copyright files
4              
5             =head1 VERSION
6              
7             This document describes Debian::Copyright version 0.2 .
8              
9             =head1 SYNOPSIS
10              
11             my $c = Debian::Copyright->new(); # construct a new
12             $c->read($file1); # parse debian copyright file
13             $c->read($file2); # parse a second
14             $c->write($ofile); # write to file
15              
16             =head1 DESCRIPTION
17              
18             Debian::Copyright can be used for the representation, manipulation and
19             merging of Debian copyright files in an object-oriented way. It provides easy
20             reading and writing of the F file found in Debian source
21             packages. Debian has recently started standardising its copyright files
22             around the machine-readable
23             L
24             format.
25              
26             =head2 note on terminology
27              
28             The term "Stanza" derives from the
29             L tool. The official
30             term would now be "Paragraph". For the purposes of discussing the DEP-5 format the terms are used interchangeably in this documentation.
31              
32             =head1 FIELDS
33              
34             =head2 header
35              
36             An instance of L class. Contains the
37             the first stanza of the copyright file. If multiple files were parsed only the
38             first will be retained.
39              
40             =head2 files
41              
42             A hash reference (actually L instance) with keys being the values
43             of the C clause and values instances of
44             L class.
45              
46             =head2 licenses
47              
48             A hash reference (actually L instance) with keys being the values
49             of the C clause and values instances of
50             L class.
51              
52             =cut
53              
54             package Debian::Copyright;
55             require v5.10.1;
56 3     3   98256 use base 'Class::Accessor';
  3         7  
  3         3686  
57 3     3   8236 use strict;
  3         8  
  3         98  
58 3     3   17 use Carp;
  3         12  
  3         366  
59              
60             our $VERSION = '0.2';
61              
62             __PACKAGE__->mk_accessors(qw( _parser header files licenses ));
63              
64 3     3   3463 use Parse::DebControl;
  3         593539  
  3         128  
65 3     3   2467 use Debian::Copyright::Stanza::Header;
  3         85  
  3         33  
66 3     3   3122 use Debian::Copyright::Stanza::Files;
  3         8  
  3         125  
67 3     3   2628 use Debian::Copyright::Stanza::License;
  3         7  
  3         22  
68 3     3   368 use Tie::IxHash;
  3         6  
  3         2101  
69              
70             =head1 CONSTRUCTOR
71              
72             =head2 new
73              
74             Constructs a new L instance.
75              
76             The C
field is initialised with an empty string.
77             The C and C fields are initialised with an
78             empty instance of L.
79              
80             =cut
81              
82             sub new {
83 5     5 1 2445 my $class = shift;
84              
85 5         45 my $self = $class->SUPER::new();
86              
87 5         95 $self->_parser( Parse::DebControl->new );
88              
89 5         148 $self->header(undef);
90 5         72 $self->files( Tie::IxHash->new );
91 5         124 $self->licenses( Tie::IxHash->new );
92              
93 5         98 return $self;
94             }
95              
96             =head1 METHODS
97              
98             =head2 read I
99              
100             Parse L and accessors.
101              
102             I can be either a file name, an opened file handle or a string scalar
103             reference.
104              
105             =cut
106              
107             sub read {
108 6     6 1 52058 my ( $self, $file ) = @_;
109              
110 6         15 my $parser_method = 'parse_file';
111              
112 6 100       21 if ( ref($file) ) {
113 1         3 $file = $$file;
114 1         2 $parser_method = 'parse_mem';
115             }
116              
117 6         25 my $stanzas = $self->_parser->$parser_method( $file,
118             { useTieIxHash => 1, verbMultiLine => 1 } );
119              
120 6 50       35388 if (exists $stanzas->[0]->{Format}) {
121 6         49 my $header = shift @$stanzas;
122 6 100       28 if (! $self->header) {
123 5         117 $self->header( Debian::Copyright::Stanza::Header->new($header) );
124             }
125             }
126              
127 5         87 for (@$stanzas) {
128 57 50       222 next if $_->{Format};
129 57 100       486 if ( $_->{Files} ) {
130 47         364 $self->files->Push(
131             $_->{Files} => Debian::Copyright::Stanza::Files->new($_) );
132 47         1015 next;
133             }
134 10 100       83 if ( $_->{License} ) {
135 9         92 my $license = $_->{License};
136 9 50       95 if ($license =~ m{\A([^\n]+)$}xms) {
137 9         27 $license = $1;
138             }
139             else {
140 0         0 croak "License stanza does not make sense";
141             }
142 9         30 $self->licenses->Push(
143             $license => Debian::Copyright::Stanza::License->new($_) );
144 9         147 next;
145             }
146 1         15 die "Got copyright stanza with unrecognised field\n";
147             }
148 4         192 return;
149             }
150              
151             =head2 write I
152              
153             Writes a debian/copyright-like file in I with the contents defined in the
154             accessor fields.
155              
156             I can be either a file name, an opened file handle or a string scalar
157             reference.
158              
159             =cut
160              
161             sub write {
162 2     2 1 687 my ( $self, $file ) = @_;
163              
164 2         10 my @stanzas = (
165             $self->header,
166             $self->files->Values,
167             $self->licenses->Values
168             );
169 2         75 my $string = join "\n", @stanzas;
170              
171 2 50 33     19 if ( ref($file) and ref($file) eq 'SCALAR' ) {
    0 0        
172 2         8 $$file = $string;
173             }
174             elsif ( ref($file) and ref($file) eq 'GLOB' ) {
175 0           $file->print($string);
176             }
177             else {
178 0           my $fh;
179 0 0         open $fh, '>', $file or die "Unable to open '$file' for writing: $!";
180              
181 0           print $fh $string;
182             }
183             }
184              
185             =head1 LIMITATIONS
186              
187             =over
188              
189             =item This module is written with one particular version of
190             L
191             in mind. Furthermore version 0.1 of this software was for a draft
192             version the standard. The changes in going from draft to standard
193             were such that it was not worth attempting to maintain backwards
194             compatibility.
195              
196             =item Test coverage is not yet complete.
197              
198             =back
199              
200             =head1 INCOMPATIBILITIES
201              
202             This version is not backwards compatible with version 0.1.
203              
204             =head1 ACKNOWLEDGEMENTS
205              
206             Thanks to Charles Plessy for various comments regarding the documentation.
207              
208             =head1 COPYRIGHT & LICENSE
209              
210             Copyright (C) 2011-2012 Nicholas Bamber L
211              
212             This module was adapted from L.
213             Copyright (C) 2009 Damyan Ivanov L [Portions]
214              
215             This program is free software; you can redistribute it and/or modify it under
216             the terms of the GNU General Public License version 2 as published by the Free
217             Software Foundation.
218              
219             This program is distributed in the hope that it will be useful, but WITHOUT ANY
220             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
221             PARTICULAR PURPOSE.
222              
223             =cut
224              
225             1;