File Coverage

blib/lib/App/PAUSE/TimeMachine.pm
Criterion Covered Total %
statement 18 63 28.5
branch 0 12 0.0
condition 0 8 0.0
subroutine 6 19 31.5
pod 0 12 0.0
total 24 114 21.0


line stmt bran cond sub pod time code
1             package App::PAUSE::TimeMachine;
2              
3 1     1   12141 use strict;
  1         1  
  1         23  
4 1     1   14 use 5.008_005;
  1         3  
  1         28  
5             our $VERSION = '0.02';
6              
7 1     1   5033 use Pod::Usage ();
  1         32817  
  1         18  
8 1     1   743 use IPC::Run;
  1         27553  
  1         36  
9 1     1   391 use Plack::Runner;
  1         5108  
  1         9  
10 1     1   28125 use IO::Compress::Gzip ();
  1         24382  
  1         499  
11              
12             our $GIT_REPO = "git://github.com/batchpause/PAUSE-git";
13              
14             sub new {
15 0     0 0   bless {}, shift;
16             }
17              
18             sub psgi_app {
19 0     0 0   my $self = shift;
20              
21             return sub {
22 0     0     my $env = shift;
23              
24 0 0         if ($env->{PATH_INFO} eq '/') {
25 0           return [200, ['Content-Type' => 'text/plain'], ["Access $env->{SCRIPT_NAME}/yyyy-mm-dd"]];
26             }
27              
28 0           my $date = 'today';
29 0 0         if ($env->{PATH_INFO} =~ s!^/([^/]+)!!) {
30 0           $date = $1;
31             }
32              
33 0 0         if ($env->{PATH_INFO} eq '/modules/02packages.details.txt.gz') {
34 0           chomp(my $rev = $self->git_capture('rev-list', '-1', "--before=$date", 'master'));
35 0           my $text = $self->git_capture('show', "$rev:02packages.details.txt");
36              
37 0           my($lastmod) = $text =~ m!^Last-Updated:\s*(.*)$!m;
38              
39 0 0 0       if ($env->{HTTP_IF_MODIFIED_SINCE} && $env->{HTTP_IF_MODIFIED_SINCE} eq $lastmod) {
40 0           return [304, [], []];
41             }
42              
43 0           IO::Compress::Gzip::gzip \$text => \my $gztext;
44             return [
45 0           200,
46             [
47             'Content-Type' => 'application/x-gzip',
48             'Content-Length' => length($gztext),
49             'Last-Modified' => $lastmod,
50             ],
51             [$gztext],
52             ];
53             }
54              
55 0 0         if ($env->{PATH_INFO} =~ m!authors/id/!) {
56 0           return [301, ['Location' => "http://backpan.perl.org/$env->{PATH_INFO}"], []];
57             }
58              
59 0           return [404, ['Content-Type' => 'text/plain'], ["Not Found"]];
60 0           };
61             }
62              
63             sub git_dir {
64 0 0   0 0   $ENV{"PAUSETM_GIT_DIR"} || "$ENV{HOME}/.pausetm/PAUSE-git";
65             }
66              
67             sub run {
68 0     0 0   my($self, @args) = @_;
69              
70 0   0       my $cmd = shift @args || "help";
71 0   0       my $can = $self->can("cmd_$cmd") || $self->can("cmd_help");
72 0           $self->$can(@args);
73             }
74              
75             sub git_raw {
76 0     0 0   my($self, @commands) = @_;
77 0           IPC::Run::run ["git", @commands], \my $in, \*STDOUT;
78             }
79              
80             sub git {
81 0     0 0   my($self, @commands) = @_;
82 0           IPC::Run::run ["git", "--git-dir", $self->git_dir, @commands];
83             }
84              
85             sub git_capture {
86 0     0 0   my($self, @commands) = @_;
87 0           IPC::Run::run ["git", "--git-dir", $self->git_dir, @commands], \my $in, \my $out;
88 0           $out;
89             }
90              
91             sub cmd_help {
92 0     0 0   my $self = shift;
93 0           Pod::Usage::pod2usage(1);
94             }
95              
96             sub cmd_init {
97 0     0 0   my $self = shift;
98 0           $self->git_raw('clone', $GIT_REPO, $self->git_dir);
99             }
100              
101             sub cmd_sync {
102 0     0 0   my $self = shift;
103 0           $self->git('pull', 'origin', 'master');
104             }
105              
106             sub cmd_cat {
107 0     0 0   my($self, $date) = @_;
108 0           chomp(my $rev = $self->git_capture('rev-list', '-1', "--before=$date", 'master'));
109 0           $self->git('show', "$rev:02packages.details.txt");
110             }
111              
112             sub cmd_server {
113 0     0 0   my($self, @args) = @_;
114              
115 0           my $runner = Plack::Runner->new;
116 0           $runner->parse_options(@args);
117 0           $runner->run($self->psgi_app);
118             }
119              
120             1;
121             __END__