Repo/Agent.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 139 CC 5 SCAR 21.2
CC
5
Cov
0%
parse_control
30.0
get_packages
2.0
read_json
2.0
CRAP 8.4
1 package App::Repo::Agent;
2  
3 use 5.010;
4 use LWP::Curl;
5 use Data::Dumper;
6 use Digest::SHA qw< sha1_hex sha256_hex >;
7 use Digest::MD5 qw< md5_hex >;
8 use Term::ANSIColor;
9 use JSON::PP;
10 use File::Path;
11 use File::Find;
12 use File::Copy;
13  
14 use warnings;
15 use strict;
16  
17 =head1 NAME
18
19 App::Repo - creates Packages list and starts APT repository
20
21 =cut
22  
23 require Exporter;
24  
25 our @ISA = qw(Exporter);
26 our @EXPORT_OK = ( 'printer' );
27 our $VERSION = '0.01';
28  
29 my $base_path = "$ENV{HOME}/.repo/stash";
30  
31 my $curl = LWP::Curl->new(
32 user_agent => 'Telesphoreo APT-HTTP/1.0.592',
33 #agent => 'Cydia/0.9 CFNetwork/711.4.6 Darwin/14.0.0',
34 timeout => 10,
35 maxredirs => 10,
36 );
37  
38 sub get_packages {
39 my $repo_url = shift; $repo_url =~ s/\/$//;
40 my @packages_list = qw( Packages Packages.bz2 Packages.gz );
41  
42 mkpath($base_path);
43 for(@packages_list){
44 my $packages_tmp_file = "$base_path/$_";
45 say "packages_tmp_file: $packages_tmp_file";
46 say "trying $repo_url/$_";
47 my $system = system("curl --user-agent \"Telesphoreo APT-HTTP/1.0.592\" -kLo $packages_tmp_file $repo_url/$_");
48 say "system: $system";
49  
50 #my $res = $curl->get("$repo_url/$_");
51 #if($res->is_success){
52 #open(my $fh,"> :raw :bytes",$packages_tmp_file);
53 # print $fh $res->content;
54 # close $fh;
55 # }
56 }
57 return parse_control($repo_url, "Packages.gz");
58 }
59  
60 sub parse_control {
61 my( $repo_url, $packages_tmp_file ) = @_;
62 my( @packages, %packages, $i ) = ();
63  
64 if( -f "$base_path/Packages.gz"){
65 say "gz exist";
66 system("gunzip -f $base_path/Packages.gz");
67 }
68 if( -f "$base_path/Packages.bz2"){
69 say "bz2 exist";
70 system("bzcat $base_path/Packages.bz2 > $base_path/Packages");
71 }
72  
73 if( -f "$base_path/Packages"){
74 open(my $fh, '<', "$base_path/Packages") || die "cant open $base_path/Packages: $!";
75 while(<$fh>){
76 if( /\:\ /){
77 s/(.*?)(\:\ )(.*)/$1$2$3/;
78 my($key, $value) = ($1, $3); chomp $value;
79 $packages{$key} = $value;
80 } else {
81 $packages{url} = "$repo_url/$packages{Filename}";
82 $packages{number} = $i++;
83 $packages{repository} = $repo_url;
84 $packages{t} = $repo_url;
85 push @packages, { %packages };
86 }
87 }
88 }
89 return \@packages;
90 }
91  
92 #print Dumper(get_packages("$ARGV[0]"));
93  
94 sub read_json {
95 open(my $fh,"<", "$base_path/packages.json") || die "cant open: $base_path/packages.json: $!";
96 my $json = <$fh>;
97 my $p = decode_json $json;
98 }
99  
100 sub write_json {
101 my @p = @{get_packages(shift)};
102 my $json = encode_json \@p;
103 open(my $fh,">", "$base_path/packages.json") || die "cant open: $base_path/packages.json: $!";
104 print $fh $json;
105 }
106  
107 sub printer {
108 my @p = @{get_packages(shift)};
109 my %lenght = ();
110 for(@p){
111 print colored(['white on_cyan'],"$_->{number}") . " $_->{Name}" . colored(['blue']," - ");
112 }
113 }
114  
115  
116 #my @url = grep { $_->{Name} } @{get_packages("$ARGV[0]")};
117 #for(@url){
118 # say $_->{url};
119 #}
120  
121  
122  
123  
124  
125 printer("$ARGV[0]");
126 #for(@p){ say $_->{Name} };
127  
128 __DATA__