MetaPackager/Import.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 97 CC 3 SCAR 14.2
CC
3
Cov
0%
populate_packages_table
12.0
populate_all_tables
2.0
read_packages_file
2.0
CRAP 4.1
1 package CPAN::MetaPackager::Import;
2  
3 use boolean;
4 use parent 'CPAN::MetaPackager::Database';
5 use warnings qw(FATAL utf8); # Fatalize encoding glitches.
6  
7 use Data::Dumper::Concise; # For Dumper().
8 use DateTime::Tiny;
9  
10 use File::Spec;
11 use File::Slurper 'read_lines';
12  
13 use Moo;
14  
15 use Types::Standard qw/Str/;
16  
17 has packages_path =>
18 (
19 default => sub{return '/tmp/02packages.details.txt'},
20 is => 'rw',
21 isa => Str,
22 required => 0,
23 );
24  
25 our $VERSION = '1.05';
26  
27 # -----------------------------------------------
28  
29 sub populate_all_tables
30 {
31 my($self) = @_;
32  
33 $self -> init_config;
34 $self -> init_db;
35 $self -> logger -> info('Populating all tables');
36  
37 $self -> populate_packages_table;
38  
39 $self -> logger -> info('Populated all tables');
40 $self -> logger -> info('-' x 50);
41  
42 # Return 0 for OK and 1 for error.
43  
44 return 0;
45  
46 } # End of populate_all_tables.
47  
48 # -----------------------------------------------
49  
50 sub populate_packages_table
51 {
52 my($self) = @_;
53 my($path) = File::Spec -> catfile($self -> home_path, $self -> packages_path);
54 my($table_name) = 'packages';
55  
56 $self -> get_table_column_names(true, $table_name); # Populates $self -> column_names.
57  
58 my($packages) = $self -> read_packages_file;
59 my($start_time) = time;
60  
61 my($author);
62 my(%count);
63 my($distro);
64 my(@fields);
65 my($package);
66 my($record);
67 my($time_taken);
68 my($version);
69  
70 $count{package} = 0;
71 $count{total} = 0;
72  
73 for my $line (@$packages)
74 {
75 $count{total}++;
76  
77 next if ($count{total} <= 9);
78  
79 $count{package}++;
80  
81 ($package, $version, $distro) = split(/\s+/, $line);
82 @fields = split('/', $distro);
83 $author = $fields[2];
84  
85 $self -> insert_hashref
86 (
87 $table_name,
88 {
89 id => $count{package},
90 author => $author,
91 name => $package,
92 version => $version,
93 }
94 );
95  
96  
97 if ($count{package} % 10000 == 0)
98 {
99 $time_taken = (time - $start_time) / 60; # Convert seconds to minutes.
100 $time_taken = sprintf('%i mins', $time_taken);
101  
102 say "Stored $count{package} records into '$table_name'. Time taken: $time_taken";
103 }
104 }
105  
106 my($pad) = $self -> pad; # For temporary use, during import.
107 $$pad{$table_name} = $self -> read_table($table_name);
108 my($packages_count) = $#{$$pad{$table_name} } + 1;
109  
110 $self -> logger -> info("Finished populate_packages_table(). Stored $count{package} records into table '$table_name'");
111  
112 } # End of populate_packages_table.
113  
114 # --------------------------------------------------
115  
116 sub read_packages_file
117 {
118 my($self) = @_;
119  
120 $self -> init_config;
121 $self -> init_db;
122  
123 my($file_name) = $self -> packages_path;
124 my(@packages) = read_lines($file_name, 'UTF-8');
125  
126 $self -> logger -> info("$file_name: record count: @{[$#packages + 1]}. Includes 9 header records");
127  
128 return \@packages;
129  
130 } # End of read_packages_file.
131  
132 # --------------------------------------------------
133  
134 1;
135  
136 =pod
137  
138 =head1 NAME
139  
140 CPAN::MetaPackager::Import - Manage the cpan.metapackager.sqlite database
141  
142 =head1 Instructions
143  
144 See POD in MetaPackager.pm.
145  
146 ==head1 Author
147  
148 Ron Savage I<E<lt>ron@savage.net.auE<gt>>.
149  
150 My homepage: L<https://savage.net.au/>.
151  
152 =head1 License
153  
154 Perl 5.
155  
156 =head1 Copyright
157  
158 Copyright (c) 2026, Ron Savage.
159  
160 All Programs of mine are 'OSI Certified Open Source Software';
161 you can redistribute them and/or modify them under the terms of
162 The Perl License, a copy of which is available at:
163 http://dev.perl.org/licenses/
164  
165 =cut