| 1 | package App::Repo; | ||
| 2 | use Data::Dumper; | ||
| 3 | use Digest::SHA qw< sha1_hex sha256_hex >; | ||
| 4 | use Digest::MD5 qw< md5_hex >; | ||
| 5 | use File::Path; | ||
| 6 | use File::Find; | ||
| 7 | use File::Copy; | ||
| 8 | |||
| 9 | use warnings; | ||
| 10 | use strict; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | App::Repo - create debian repository | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | require Exporter; | ||
| 19 | |||
| 20 | our @ISA = qw(Exporter); | ||
| 21 | our @EXPORT_OK = ( 'digest', 'packages' ); | ||
| 22 | our $VERSION = '0.11'; | ||
| 23 | |||
| 24 | |||
| 25 | my @deb_files = (); | ||
| 26 | my $url_base = 'https://fastapi.metacpan.org/source'; | ||
| 27 | my $stash = "$ENV{HOME}/.repo"; | ||
| 28 | |||
| 29 | my $digest = sub { | ||
| 30 | my $file = shift; | ||
| 31 | my( $data, %data ) = (); | ||
| 32 | open(my $fh,"<:raw :bytes",$file) || die "cant open $file: $!"; | ||
| 33 | while(<$fh>){ | ||
| 34 | $data .= $_; | ||
| 35 | } | ||
| 36 | |||
| 37 | %data = ( | ||
| 38 | sha1 => sha1_hex($data), | ||
| 39 | sha256 => sha256_hex($data), | ||
| 40 | md5 => md5_hex($data), | ||
| 41 | ); | ||
| 42 | |||
| 43 | return \%data; | ||
| 44 | }; | ||
| 45 | |||
| 46 | my $content = sub { | ||
| 47 | my( $dir, $file ) = @_; | ||
| 48 | my @control = (); | ||
| 49 | |||
| 50 | if($file =~ /\.deb/){ | ||
| 51 | my $file_size = -s "$dir/$file"; | ||
| 52 | system("mkdir -p $stash/tmp && cp $dir/$file $stash/tmp/ && cd $stash/tmp && ar -x $stash/tmp/$file && tar -xf $stash/tmp/control.tar.gz && cd -"); | ||
| 53 | open(my $fh,"<","$stash/tmp/control") || die "cant open $stash/tmp/control: $!"; | ||
| 54 | while(<$fh>){ | ||
| 55 | if(/^\n/){ next }; | ||
| 56 | chomp; | ||
| 57 | push @control, $_; | ||
| 58 | } | ||
| 59 | push @control, 'Size: ' . $file_size; | ||
| 60 | push @control, 'Filename: ' . "deb/$file"; | ||
| 61 | push @control, 'MD5sum: ' . $digest->("$dir/$file")->{'md5'}; | ||
| 62 | push @control, 'SHA1: ' . $digest->("$dir/$file")->{'sha1'}; | ||
| 63 | push @control, 'SHA256: ' . $digest->("$dir/$file")->{'sha256'}; | ||
| 64 | push @control, "\n"; | ||
| 65 | print "$file\n"; | ||
| 66 | return \@control; | ||
| 67 | } else { print "no deb file\n" } | ||
| 68 | }; | ||
| 69 | |||
| 70 | my $find_deb = sub { | ||
| 71 | my $deb_dir = shift; | ||
| 72 | my @packages = (); | ||
| 73 | find( sub{ | ||
| 74 | if(/\.deb$/){ | ||
| 75 | push @deb_files, $_; | ||
| 76 | }}, $deb_dir ); | ||
| 77 | |||
| 78 | for my $deb_file (@deb_files){ | ||
| 79 | push @packages, $content->($deb_dir, $deb_file); | ||
| 80 | } | ||
| 81 | return \@packages; | ||
| 82 | }; | ||
| 83 | |||
| 84 | |||
| 85 | sub digest { | ||
| 86 | $digest->(shift); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub packages { | ||
| 90 | $find_deb->(shift); | ||
| 91 | } | ||
| 92 | 1; |