Commits

Burak Gürsoy committed c260a3f

Skip existing repos.

Comments (0)

Files changed (1)

-#!/usr/bin/perl -w
-
-package Net::BitBucket;
-use strict;
-use warnings;
-use constant UA_TIMEOUT => 10;
-use constant NBSP       => chr 160;
-use LWP::UserAgent;
-use Carp qw(croak);
-
-use constant BASE_URL => 'http://bitbucket.org/';
-
-our $VERSION = '0.10';
-
-my $UA = LWP::UserAgent->new;
-$UA->agent(sprintf '%s/%s', __PACKAGE__, $VERSION);
-$UA->env_proxy;
-$UA->timeout(UA_TIMEOUT);
-
-sub new { return bless {}, shift };
-
-sub agent { return $UA }
-
-sub get {
-    my $self = shift;
-    my $url  = shift;
-    my $r    = $self->agent->get($url);
-
-    if ( $r->is_success ) {
-        my $raw = $r->decoded_content;
-        return $raw;
-    }
-
-    croak( 'GET request failed: ' . $r->as_string );
-}
-
-sub repositories {
-    my $self = shift;
-    my $user = shift || croak 'No user name specified';
-    warn ">> Fetching the base URL ...\n";
-    my $raw  = eval { $self->get( BASE_URL . $user ) };
-    croak "$user is not a valid user. Error: $@" if $@;
-    warn ">> Parsing ...\n";
-    my(undef, $rest)  = split m{\Q<ol id="repositories" class="detailed">\E}xms, $raw, 2;
-    my $rid = 0;
-    my @repo;
-    foreach my $line ( split m{\n+}xms, $rest ) {
-	chomp $line;
-	my $e = $repo[$rid] ||= {};
-	if ( $line =~ m{\s+ \Q<h3><a href="\E (.+?)/overview">(.+?)</a></h3>}xms ) {
-            if ( @repo ) {
-		$rid++;
-                $e = $repo[$rid] = {};
-	    }
-	    my($url, $name) = ($1, $2);
-	    $url       =~ s{\A / }{}xms;
-	    $e->{url}  = join q{}, BASE_URL, $url;
-	    $e->{name} = $name;
-	    next;
-	}
-
-        if ( $line =~ m{\Q<p class="metadata">Created <time datetime="\E(.+?)">(.+?)ago</time>}xms ) {
-	    $e->{ago} = $2;
-	    next;
-	}
-
-        if ( $line =~ m{\Q<div class="description"><p>\E(.+?)</p></div>}xms ) {
-	    $e->{desc} = $1;
-	    next;
-	}
-    }
-    shift @repo if ! keys %{ $repo[0] };
-    return @repo;
-}
-
-sub trim {
-    my $self = shift;
-    my $s = shift;
-    $s =~ s{ \A \s+ }{}xms;
-    $s =~ s{ \s+ \z }{}xms;
-    return $s;
-}
-
-package main;
-use strict;
-use warnings;
-use File::Spec;
-use Data::Dumper;
-use File::Path;
-use Time::HiRes qw( time );
-use constant RE_CPAN   => qr{ \A (CPAN) \- (.+?) \z }xms;
-use constant SEPERATOR => join q{},  q{-} x 80, "\n";
-use Cwd;
-use Carp qw( croak );
-
-my $CWD   = getcwd;
-my $START = time;
-my $bit   = Net::BitBucket->new;
-
-_w( "CURRENT DIRECTORY: $CWD\n",
-    "STARTING TO CLONE REPOSITORIES FROM BURAK GURSOY...\n",
-    SEPERATOR );
-
-my $total = 0;
-foreach my $repo ( $bit->repositories( 'burak' ) ) {
-    chdir $CWD; # reset
-
-    my $name = $repo->{name};
-    my @path = $name =~ RE_CPAN ? ($1, $2) : ($name);
-    my $dir  = File::Spec->catdir( @path );
-
-    if ( @path > 1 ) {
-        mkpath $path[0];
-        chdir  $path[0];
-    }
-
-    _w( "PROCESSING $name ...\n" );
-    hg( clone => $repo->{url}, @path > 1 ? ($path[1]) : () );
-    _w( "... done!\n", SEPERATOR );
-
-    $total++;
-}
-
-_w( "ADDED %d REPOSITORIES IN %.4f SECONDS\n", $total, time - $START );
-
-sub _w {
-    my @args = @_;
-    printf {*STDERR} @args or croak "Unable to print to STDERR: $!";
-    return;
-}
-
-sub hg {
-    my @args = @_;
-    system( hg => @args ) && croak "FAILED(@args): $?";
-    return;
-}
-
-1;
-
-__END__
-
-=pod
-
-=head1 NAME
-
-bclone.pl - Get all stuff from Burak Gursoy
-
-=head1 SYNOPSIS
-
-   chdir projects
-   perl bclone.pl
-
-=head1 AUTHOR
-
-Burak Gursoy.
-
-=cut
+#!/usr/bin/perl -w
+
+package Net::BitBucket;
+use strict;
+use warnings;
+use constant UA_TIMEOUT => 10;
+use constant NBSP       => chr 160;
+use LWP::UserAgent;
+use Carp qw(croak);
+
+use constant BASE_URL => 'http://bitbucket.org/';
+
+our $VERSION = '0.10';
+
+my $UA = LWP::UserAgent->new;
+$UA->agent(sprintf '%s/%s', __PACKAGE__, $VERSION);
+$UA->env_proxy;
+$UA->timeout(UA_TIMEOUT);
+
+sub new { return bless {}, shift };
+
+sub agent { return $UA }
+
+sub get {
+    my $self = shift;
+    my $url  = shift;
+    my $r    = $self->agent->get($url);
+
+    if ( $r->is_success ) {
+        my $raw = $r->decoded_content;
+        return $raw;
+    }
+
+    croak( 'GET request failed: ' . $r->as_string );
+}
+
+sub repositories {
+    my $self = shift;
+    my $user = shift || croak 'No user name specified';
+    warn ">> Fetching the base URL ...\n";
+    my $raw  = eval { $self->get( BASE_URL . $user ) };
+    croak "$user is not a valid user. Error: $@" if $@;
+    warn ">> Parsing ...\n";
+    my(undef, $rest)  = split m{\Q<ol id="repositories" class="detailed">\E}xms, $raw, 2;
+    my $rid = 0;
+    my @repo;
+    foreach my $line ( split m{\n+}xms, $rest ) {
+	chomp $line;
+	my $e = $repo[$rid] ||= {};
+	if ( $line =~ m{\s+ \Q<h3><a href="\E (.+?)/overview">(.+?)</a></h3>}xms ) {
+            if ( @repo ) {
+		$rid++;
+                $e = $repo[$rid] = {};
+	    }
+	    my($url, $name) = ($1, $2);
+	    $url       =~ s{\A / }{}xms;
+	    $e->{url}  = join q{}, BASE_URL, $url;
+	    $e->{name} = $name;
+	    next;
+	}
+
+        if ( $line =~ m{\Q<p class="metadata">Created <time datetime="\E(.+?)">(.+?)ago</time>}xms ) {
+	    $e->{ago} = $2;
+	    next;
+	}
+
+        if ( $line =~ m{\Q<div class="description"><p>\E(.+?)</p></div>}xms ) {
+	    $e->{desc} = $1;
+	    next;
+	}
+    }
+    shift @repo if ! keys %{ $repo[0] };
+    return @repo;
+}
+
+sub trim {
+    my $self = shift;
+    my $s = shift;
+    $s =~ s{ \A \s+ }{}xms;
+    $s =~ s{ \s+ \z }{}xms;
+    return $s;
+}
+
+package main;
+use strict;
+use warnings;
+use File::Spec;
+use Data::Dumper;
+use File::Path;
+use Time::HiRes qw( time );
+use constant RE_CPAN   => qr{ \A (CPAN) \- (.+?) \z }xms;
+use constant SEPERATOR => join q{},  q{-} x 80, "\n";
+use Cwd;
+use Carp qw( croak );
+
+my $CWD   = getcwd;
+my $START = time;
+my $bit   = Net::BitBucket->new;
+
+_w( "CURRENT DIRECTORY: $CWD\n",
+    "STARTING TO CLONE REPOSITORIES FROM BURAK GURSOY...\n",
+    SEPERATOR );
+
+my $total = 0;
+foreach my $repo ( $bit->repositories( 'burak' ) ) {
+    chdir $CWD; # reset
+
+    my $name = $repo->{name};
+    my @path = $name =~ RE_CPAN ? ($1, $2) : ($name);
+    my $dir  = File::Spec->catdir( @path );
+
+    if ( @path > 1 ) {
+        mkpath $path[0];
+        chdir  $path[0];
+    }
+
+    _w( "PROCESSING $name ...\n" );
+    my $local_target = @path > 1 ? $path[1] : undef;
+
+    if ( $local_target && -d $local_target ) {
+        _w("$local_target exists. Skipping ...\n");
+        next;
+    }
+
+    eval {
+        hg( clone => $repo->{url}, $local_target ? ($local_target) : () );
+        _w( "... done!\n", SEPERATOR );
+        1;
+    } or do {
+        my $e = $@ || '[unknown error]';
+        die $@; # rollback && next?
+    };
+    $total++;
+}
+
+_w( "ADDED %d REPOSITORIES IN %.4f SECONDS\n", $total, time - $START );
+
+sub _w {
+    my @args = @_;
+    printf {*STDERR} @args or croak "Unable to print to STDERR: $!";
+    return;
+}
+
+sub hg {
+    my @args = @_;
+    system( hg => @args ) && croak "FAILED(@args): $?";
+    return;
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+bclone.pl - Get all stuff from Burak Gursoy
+
+=head1 SYNOPSIS
+
+   chdir projects
+   perl bclone.pl
+
+=head1 AUTHOR
+
+Burak Gursoy.
+
+=cut