-
Notifications
You must be signed in to change notification settings - Fork 8
/
Dark.pm
214 lines (152 loc) · 5.4 KB
/
Dark.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package CPAN::Dark;
# ABSTRACT: manages a DarkPAN installation
use strict;
use warnings;
use Carp;
use YAML 0.73;
use File::Path 2.08;
use Path::Class 0.24;
use List::Util 1.20, 'first';
use Archive::Tar 1.76;
use Compress::Zlib 2.035;
use CPAN::Mini::Inject 0.30; # removes old entries of injected modules
sub new
{
bless { cpmi => CPAN::Mini::Inject->new->parsecfg }, shift;
}
sub create_darkpan
{
my $self = shift;
my $cpmi = $self->{cpmi};
return unless my $repo = $self->should_create_repository;
File::Path::mkpath( [ dir( $repo, 'authors' ), dir( $repo, 'modules' ) ] );
$self->write_gz( file( $repo, 'authors', '01mailrc.txt.gz' ), '' );
$self->write_gz( file( $repo, 'modules', '02packages.details.txt.gz' ),
<<END_PACKAGE_HEADER );
File: 02packages.details.txt
URL: http://cpan.perl.org/modules/02packages.details.txt.gz
Description: Package names found in directory \$CPAN/authors/id/
Columns: package name, version, path
Intended-For: Automated fetch routines, namespace documentation.
Written-By: CPAN::Dark $CPAN::Dark::VERSION
Line-Count: 0
Last-Updated: ${ \localtime() }
END_PACKAGE_HEADER
}
sub should_create_repository
{
my $self = shift;
my $cpmi = $self->{cpmi};
my $repo = $cpmi->config->get( 'local' );
Carp::croak( "No DarkPAN configured!" ) unless $repo;
return $repo unless -d $repo;
return $repo unless -f file( $repo, 'authors', '01mailrc.txt.gz' );
return $repo unless -f
file( $repo, 'modules', '02packages.details.txt.gz' );
return;
}
sub write_gz
{
my ($self, $file, $contents) = @_;
Carp::croak( "Cannot write to '$file': $!" )
unless my $fh = $file->open( '>' );
my $gz = Compress::Zlib::gzopen( $fh, 'wb' );
$gz->gzwrite( $contents );
$gz->gzclose;
}
sub inject_files
{
my $self = shift;
$self->inject_file_as($self->{cpmi}->config->{author},$_) for (@_);
}
sub inject_files_as
{
my $self = shift;
my $author = shift;
$self->inject_file_as($author,$_) for (@_);
}
sub inject_file_as
{
my ($self, $author, $file) = @_;
$self->create_darkpan;
Carp::croak( "Cannot find '$file'" ) unless $file and -e $file;
my $meta = $self->load_metayaml( $file );
(my $module = $meta->{name}) =~ s/-/::/g;
$self->{cpmi}->add(
file => $file,
module => $module,
version => $meta->{version},
authorid => $author,
);
$self->{cpmi}->writelist->inject;
}
sub load_metayaml
{
my ($self, $file) = @_;
my $tar = Archive::Tar->new;
$tar->read( $file );
return unless my $meta_file = first { /META\.yml$/ } $tar->list_files;
return YAML::Load( $tar->get_content( $meta_file ) );
}
1;
__END__
=pod
=head1 NAME
CPAN::Dark - manage a DarkPAN installation
=head1 SYNOPSIS
use CPAN::Dark;
CPAN::Dark->new->inject_files( @list_of_dist_tarballs );
CPAN::Dark->new->inject_files_as( $author, @list_of_dist_tarballs );
=head1 DESCRIPTION
A DarkPAN is like the public CPAN except that it's not public. Otherwise it
resembles the public CPAN sufficiently that CPAN installation tools such as
CPAN.pm, CPANPLUS, and cpanminus can install distributions from it.
While existing CPAN tools such as L<CPAN::Mini> allow you to I<mirror> the
public CPAN and L<CPAN::Mini::Inject> allows you to install your own private
distributions into such a mirror, C<CPAN::Dark> takes the opposite approach. A
DarkPAN I<only> contains the distributions you have explicitly installed into
it.
C<CPAN::Dark> relies on the existence of an appropriate
L<CPAN::Mini::Inject::Config>-style configuration file:
=over 4
=item * pointed to by the C<MCPANI_CONFIG> environment variable
=item * present at F<$HOME/.mcpani/config>
=item * present at F</usr/local/etc/mcpani>
=item * present at F</etc/mcpani>
=back
The contents of this file must conform to the described file format, with one
additional parameter. Provide the C<author> configuration to set a default
author for all injected DarkPAN distributions.
Example:
local: /home/larry/darkpan
remote: http://localhost/
author: LWALL
repository: /home/larry/tmp
=head1 METHODS
This module provides three public methods:
=head2 C<new()>
This constructor creates and returns a new C<CPAN::Dark> object. There is no
user-servicable configuration. This will throw an exception if the
configuration file is missing or invalid.
=head2 C<inject_files( @tarballs )>
Given a list of paths to CPAN-compatible tarballs, this method will inject them
into the configured DarkPAN installation. If the DarkPAN has not yet been
created and initialized, this method will attempt to create it. This method
will also throw an exception if any of the given files do not exist or are not
readable.
=head2 C<inject_files_as( $author, @tarballs )>
The same function as I<inject_files>, but you can use a different author, then
given via L<CPAN::Mini::Inject> configuration.
=head2 C<create_darkpan()>
This method will create the DarkPAN represented by the current configuration,
if necessary. It will throw an exception if this is not possible. Check your
file permissions if this happens.
=head1 SEE ALSO
L<darkpan-inject>
L<CPAN::Mini>
L<CPAN::Mini::Inject>
=head1 AUTHOR
chromatic C<< chromatic at wgz dot org >>
=head1 COPYRIGHT & LICENSE
Copyright (c) 2011, chromatic. Redistribution and modification permitted under
the terms of the Artistic License 2.0.