File Coverage

blib/lib/App/PAUSE/TimeMachine.pm
Criterion Covered Total %
statement 18 65 27.6
branch 0 12 0.0
condition 0 8 0.0
subroutine 6 20 30.0
pod 0 13 0.0
total 24 118 20.3


line stmt bran cond sub pod time code
1             package App::PAUSE::TimeMachine;
2              
3 1     1   12398 use strict;
  1         2  
  1         24  
4 1     1   14 use 5.008_005;
  1         2  
  1         29  
5             our $VERSION = '0.03';
6              
7 1     1   395 use Pod::Usage ();
  1         33154  
  1         19  
8 1     1   662 use IPC::Run;
  1         27957  
  1         34  
9 1     1   338 use Plack::Runner;
  1         5077  
  1         9  
10 1     1   443 use IO::Compress::Gzip ();
  1         23419  
  1         526  
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_opts {
81 0     0 0   my $self = shift;
82 0           ("--git-dir=" . $self->git_dir . "/.git", "--work-tree=.");
83             }
84              
85             sub git {
86 0     0 0   my($self, @commands) = @_;
87 0           IPC::Run::run ["git", $self->git_opts, @commands];
88             }
89              
90             sub git_capture {
91 0     0 0   my($self, @commands) = @_;
92 0           IPC::Run::run ["git", $self->git_opts, @commands], \my $in, \my $out;
93 0           $out;
94             }
95              
96             sub cmd_help {
97 0     0 0   my $self = shift;
98 0           Pod::Usage::pod2usage(1);
99             }
100              
101             sub cmd_init {
102 0     0 0   my $self = shift;
103 0           $self->git_raw('clone', $GIT_REPO, $self->git_dir);
104             }
105              
106             sub cmd_sync {
107 0     0 0   my $self = shift;
108 0           $self->git('pull', 'origin', 'master');
109             }
110              
111             sub cmd_cat {
112 0     0 0   my($self, $date) = @_;
113 0           chomp(my $rev = $self->git_capture('rev-list', '-1', "--before=$date", 'master'));
114 0           $self->git('show', "$rev:02packages.details.txt");
115             }
116              
117             sub cmd_server {
118 0     0 0   my($self, @args) = @_;
119              
120 0           my $runner = Plack::Runner->new;
121 0           $runner->parse_options(@args);
122 0           $runner->run($self->psgi_app);
123             }
124              
125             1;
126             __END__