CycloneDX/Util.pm

filter: Statementstmt 61.1% 33 / 54 Branchbran 25.0% 2 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 53.3% 8 / 15 Podpod 100% 7 / 7 totaltotal 57.4% 50 / 87 CC 6 SCAR 13.3
CC
6
Cov
55%
file_write
12.0
cpan_meta_to_spdx_license
6.0
file_read
3.5
CRAP 3.8
« CycloneDX/Declarations/Contents.pm ↑ Summary CycloneDX/Component/ConfidenceInterval.pm »
1 package SBOM::CycloneDX::Util;
2  
3 16 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 our @EXPORT = qw(
14 urn_uuid urn_cdx
15 cpan_meta_to_spdx_license
16 cyclonedx_tool cyclonedx_component
17 file_read file_write
18 );
19  
20 16 require SBOM::CycloneDX::Component;
21 16 require SBOM::CycloneDX::ExternalReference;
22 16 require SBOM::CycloneDX::License;
23 16 require SBOM::CycloneDX::Tool;
24  
25  
26 16 my %CPAN_META_SPEC_LICENSE_MAPPING = (
27  
28 # CPAN::Meta::Spec licenses
29  
30 # license | SPDX | Description
31 # -----------------|-------------------------------------------|--------------------------------
32  
33 agpl_3 => 'AGPL-3.0', # GNU Affero General Public License, Version 3
34 apache_1_1 => 'Apache-1.1', # Apache Software License, Version 1.1
35 apache_2_0 => 'Apache-2.0', # Apache License, Version 2.0
36 artistic_1 => 'Artistic-1.0', # Artistic License, (Version 1)
37 artistic_2 => 'Artistic-2.0', # Artistic License, Version 2.0
38 bsd => 'BSD-3-Clause', # BSD License (three-clause)
39 freebsd => 'BSD-2-Clause-FreeBSD', # FreeBSD License (two-clause)
40 gfdl_1_2 => 'GFDL-1.2-or-later', # GNU Free Documentation License, Version 1.2
41 gfdl_1_3 => 'GFDL-1.3-or-later', # GNU Free Documentation License, Version 1.3
42 gpl_1 => 'GPL-1.0-only', # GNU General Public License, Version 1
43 gpl_2 => 'GPL-2.0-only', # GNU General Public License, Version 2
44 gpl_3 => 'GPL-3.0-only', # GNU General Public License, Version 3
45 lgpl_2_1 => 'LGPL-2.1', # GNU Lesser General Public License, Version 2.1
46 lgpl_3_0 => 'LGPL-3.0', # GNU Lesser General Public License, Version 3.0
47 mit => 'MIT', # MIT (aka X11) License
48 mozilla_1_0 => 'MPL-1.0', # Mozilla Public License, Version 1.0
49 mozilla_1_1 => 'MPL-1.1', # Mozilla Public License, Version 1.1
50 openssl => 'OpenSSL', # OpenSSL License
51 perl_5 => 'Artistic-1.0-Perl OR GPL-1.0-or-later', # The Perl 5 License (Artistic 1 & GPL 1 or later)
52 qpl_1_0 => 'QPL-1.0', # Q Public License, Version 1.0
53 ssleay => 'SSLeay-standalone', # Original SSLeay License
54 sun => 'SISSL', # Sun Internet Standards Source License (SISSL)
55 zlib => 'Zlib', # zlib License
56  
57  
58 # Additional license (from CPAN::Meta::Spec)
59 #
60 # The following license strings are also valid and indicate other licensing not described above:
61 #
62 # license | SPDX | Description
63 # -----------------|-------------------------------------------|--------------------------------
64  
65 open_source => 'NOASSERTION', # Other Open Source Initiative (OSI) approved license
66 restricted => 'NOASSERTION', # Requires special permission from copyright holder
67 unrestricted => 'CC0-1.0', # Not an OSI approved license, but not restricted
68 unknown => 'NONE', # License not provided in metadata
69 );
70  
71 0 ▶sub urn_uuid { sprintf 'urn:uuid:%s', create_uuid_as_string(UUID_V4) }
Subroutine urn_uuid: not called CC=1 SCAR=6.9
Pod documented
72 0 ▶sub urn_cdx { sprintf 'urn:cdx:%s', create_uuid_as_string(UUID_V4) }
Subroutine urn_cdx: not called CC=1 SCAR=6.9
Pod documented
73  
74 0 ▶sub cyclonedx_component {
Subroutine cyclonedx_component: not called CC=1 SCAR=6.9
Pod documented
75  
76 0 my $component = SBOM::CycloneDX::Component->new(
77 type => 'library',
78 group => 'CPAN',
79 name => 'SBOM-CycloneDX',
80 version => sprintf('%s', $SBOM::CycloneDX::VERSION),
81 description => 'Perl distribution for CycloneDX',
82 licenses => [SBOM::CycloneDX::License->new(id => cpan_meta_to_spdx_license('artistic_2'))],
83 bom_ref => sprintf('%s@%s', 'SBOM-CycloneDX', $SBOM::CycloneDX::VERSION),
84 external_references => _cyclonedx_external_references()
85 );
86  
87 0 return $component;
88  
89 }
90  
91 0 ▶sub cyclonedx_tool {
Subroutine cyclonedx_tool: not called CC=1 SCAR=6.9
Pod documented
92  
93 0 SBOM::CycloneDX::Tool->new(
94 vendor => 'CPAN',
95 name => 'SBOM-CycloneDX',
96 version => sprintf('%s', $SBOM::CycloneDX::VERSION),
97 external_references => _cyclonedx_external_references()
98 );
99  
100 }
101  
102 0 ▶sub _cyclonedx_external_references {
Subroutine _cyclonedx_external_references: not called CC=1 SCAR=6.9
103  
104 0 my @references = (
105 {type => 'website', url => 'https://metacpan.org/pod/SBOM::CycloneDX'},
106 {type => 'documentation', url => 'https://metacpan.org/dist/SBOM-CycloneDX'},
107 {type => 'vcs', url => 'https://github.com/giterlizzi/perl-SBOM-CycloneDX'},
108 {type => 'issue-tracker', url => 'https://github.com/giterlizzi/perl-SBOM-CycloneDX/issues'},
109 {type => 'license', url => 'https://github.com/giterlizzi/perl-SBOM-CycloneDX/blob/main/LICENSE'},
110 {type => 'release-notes', url => 'https://github.com/giterlizzi/perl-SBOM-CycloneDX/blob/main/Changes'},
111 {type => 'distribution', url => 'https://metacpan.org/dist/SBOM-CycloneDX'}
112 );
113  
114 0 return [map { SBOM::CycloneDX::ExternalReference->new(%{$_}) } @references];
115  
116 }
117  
118 0 ▶sub cpan_meta_to_spdx_license {
Subroutine cpan_meta_to_spdx_license: not called CC=2 SCAR=17.9
Pod documented
119 0 ▶ return $CPAN_META_SPEC_LICENSE_MAPPING{$_[0]} || undef;
Condition0%0/2
$CPAN_META_SPEC_LICENSE_MAPPING{$_[0]} || undef
l!l
00
MC/DC0%0/1
$CPAN_META_SPEC_LICENSE_MAPPING{$_[0]} || undef
$CPAN_META_SPEC_LICENSE_MAPPING{$_[0]}
Truth table0%0/2
A
A= $CPAN_META_SPEC_LICENSE_MAPPING{$_[0]}
Aresult
11
00
120 }
121  
122 73 ▶sub file_read {
Subroutine file_read: called CC=3 SCAR=12.6
Pod documented
123  
124 73 my $file = shift;
125  
126 73 ▶ if (ref($file) eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq "GLOB") 0 73 73
127 0 return do { local $/; <$file> };
128 }
129  
130 73 return do {
131 73 ▶ open(my $fh, '<', $file) or Carp::croak qq{Failed to read file: $!};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or Carp::croak("Failed to read file: $!") 73 0 73
132 73 local $/ = undef;
133 73 <$fh>;
134 };
135  
136 }
137  
138 0 ▶sub file_write {
Subroutine file_write: not called CC=3 SCAR=24.8
Pod documented
139  
140 0 my ($file, $content) = @_;
141  
142 0 my $fh = undef;
143  
144 0 ▶ if (ref($file) eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'GLOB') { } 0 0 0
145 0 $fh = $file;
146 }
147 else {
148 0 ▶ open($fh, '>', $file) or Carp::croak "Can't open file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $file or Carp::croak("Can't open file: $!") 0 0 0
149 }
150  
151 0 $fh->autoflush(1);
152  
153 0 print $fh $content;
154 0 close($fh);
155  
156 }
157  
158  
159 16 1;
160  
161 =encoding utf-8
162  
163 =head1 NAME
164  
165 SBOM::CycloneDX::Util - Utility for CycloneDX
166  
167 =head1 SYNOPSIS
168  
169 use SBOM::CycloneDX::Util qw(cpan_meta_to_spdx_license);
170  
171 say cpan_meta_to_spdx_license('artistic_2'); # Artistic-2.0
172  
173  
174 =head1 DESCRIPTION
175  
176 L<SBOM::CycloneDX::Util> provides a set of utility for L<SBOM::CycloneDX>.
177  
178 =head2 FUNCTIONS
179  
180 =over
181  
182 =item urn_uuid
183  
184 Return a random URN UUID
185  
186 =item urn_cdx
187  
188 Return a random CDX UUID
189  
190 =item cyclonedx_component
191  
192 Return the representation of L<SBOM::CycloneDX> component using
193 L<SBOM::CycloneDX::Component> object.
194  
195 =item cyclonedx_tool
196  
197 Return the representation of L<SBOM::CycloneDX> tool using
198 L<SBOM::CycloneDX::Tool> object.
199  
200 =item cpan_meta_to_spdx_license
201  
202 Convert the L<CPAN::Meta> license to SPDX license identifier.
203  
204 =item file_read
205  
206 Read a file.
207  
208 =item file_write
209  
210 Write a content to file.
211  
212 =back
213  
214  
215 =head1 SUPPORT
216  
217 =head2 Bugs / Feature Requests
218  
219 Please report any bugs or feature requests through the issue tracker
220 at L<https://github.com/giterlizzi/perl-SBOM-CycloneDX/issues>.
221 You will be notified automatically of any progress on your issue.
222  
223 =head2 Source Code
224  
225 This is open source software. The code repository is available for
226 public review and contribution under the terms of the license.
227  
228 L<https://github.com/giterlizzi/perl-SBOM-CycloneDX>
229  
230 git clone https://github.com/giterlizzi/perl-SBOM-CycloneDX.git
231  
232  
233 =head1 AUTHOR
234  
235 =over 4
236  
237 =item * Giuseppe Di Terlizzi <gdt@cpan.org>
238  
239 =back
240  
241  
242 =head1 LICENSE AND COPYRIGHT
243  
244 This software is copyright (c) 2025-2026 by Giuseppe Di Terlizzi.
245  
246 This is free software; you can redistribute it and/or modify it under
247 the same terms as the Perl 5 programming language system itself.
248  
249 =cut
« CycloneDX/Declarations/Contents.pm ↑ Summary CycloneDX/Component/ConfidenceInterval.pm »