Permalink
Browse files

merge

  • Loading branch information...
2 parents 49fc08d + 3df8b3b commit 6bb958c810511abfd93c34d56db1fa103880bea3 @kazeburo committed Feb 4, 2011
Showing with 61 additions and 2 deletions.
  1. +2 −0 Makefile.PL
  2. +2 −1 lib/OrePAN/Archive.pm
  3. +13 −1 lib/OrePAN/Package/Index.pm
  4. +44 −0 t/01_make_index.t
  5. BIN t/dummy-cpan/Foo-Bar-0.01.tar.gz
View
@@ -22,6 +22,8 @@ requires 'Cwd';
requires 'File::Basename';
requires 'File::Find';
requires 'CPAN::DistnameInfo';
+requires 'File::Sync';
+requires 'File::Temp';
requires_external_bin 'tar';
requires_external_bin 'unzip';
View
@@ -134,11 +134,12 @@ sub get_packages {
for my $file (@files) {
my $quote = quotemeta($archive);
next if any { $file =~ m{^$quote/$_/} } @ignore_dirs;
- next if $file !~ /\.pm(?:\.PL)?$/; #for common::sense
+ next if $file !~ /\.pm(?:\.PL)?$/;
infof("parsing: $file");
my $module = Module::Metadata->new_from_file( $file );
my $pkg = $module->name;
my $ver = $module->version;
+ # Module::Metadata cannot parse 'common::sense' package.
if ( !$pkg || $pkg eq 'main' ) {
($pkg, $ver) = _parse_version($file->slurp);
}
@@ -8,6 +8,9 @@ use IO::Zlib;
use CPAN::DistnameInfo;
use version;
use Log::Minimal;
+use File::Temp qw(:mktemp);
+use File::Sync ();
+use Carp ();
has filename => (
is => 'ro',
@@ -73,6 +76,7 @@ sub add {
}
}
+# TODO need flock?
sub save {
my ($self, ) = @_;
@@ -86,12 +90,20 @@ sub save {
}
infof( "Save %s", $self->filename);
- my $fh = IO::Zlib->new($self->filename, 'wb') or die $!;
+ # Because we do rename(2) atomically, temporary file must be in same
+ # partion with target file.
+ my $tmp = mktemp($self->filename . '.XXXXXX');
+
+ my $fh = IO::Zlib->new($tmp, 'wb') or die $!;
print {$fh} "File: 02packages.details.txt\n\n";
for my $key ( sort keys %modules ) {
print {$fh} sprintf("%s\t%s\t%s\n", $key, $modules{$key}->[0] || 'undef', $modules{$key}->[1]);
}
+ File::Sync::fsync($fh);
close $fh;
+
+ rename( $tmp, $self->filename )
+ or Carp::croak("Cannot rename temporary file '$tmp' to @{[ $self->filename ]}: $!");
}
no Mouse; __PACKAGE__->meta->make_immutable;
View
@@ -0,0 +1,44 @@
+use strict;
+use warnings;
+use utf8;
+use Test::More;
+use OrePAN::Package::Index;
+use File::Temp;
+use OrePAN::Archive;
+
+$Log::Minimal::PRINT = sub {
+ my ( $time, $type, $message, $trace) = @_;
+ note "$time [$type] $message at $trace";
+};
+
+my $tmp = File::Temp->new();
+
+# make index
+{
+ my $index = OrePAN::Package::Index->new(filename => $tmp->filename);
+ my $archive = OrePAN::Archive->new(filename => "t/dummy-cpan/Foo-Bar-0.01.tar.gz");
+ my %packages = $archive->get_packages;
+ is_deeply \%packages, { 'Foo::Bar' => '0.01' };
+ my $pauseid = "DUMMY";
+
+ $index->add(
+ File::Spec->catfile(
+ substr( $pauseid, 0, 1 ), substr( $pauseid, 0, 2 ),
+ $pauseid, "Foo-Bar-0.01.tar.gz"
+ ),
+ \%packages
+ );
+ $index->save();
+}
+
+# and read it.
+my $fh = IO::Zlib->new($tmp->filename, 'rb') or die $!;
+my $got = join('', <$fh>); # Note: IO::Zlib does not handle $/
+is $got, <<"...";
+File: 02packages.details.txt
+
+Foo::Bar\t0.01\tD/DU/DUMMY/Foo-Bar-0.01.tar.gz
+...
+
+done_testing;
+
Binary file not shown.

0 comments on commit 6bb958c

Please sign in to comment.