PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/bclone.pl

https://bitbucket.org/burak/cpan-tools
Perl | 140 lines | 98 code | 25 blank | 17 comment | 10 complexity | 83324a9df50036a5b1c92806ba4d2645 MD5 | raw file
  1. #!/usr/bin/perl
  2. package Net::BitBucket;
  3. use strict;
  4. use warnings;
  5. use constant UA_TIMEOUT => 10;
  6. use LWP::UserAgent;
  7. use JSON;
  8. use Carp qw(croak);
  9. use constant URL => 'https://api.bitbucket.org/1.0/users/%s/';
  10. use constant BASE_URL => 'https://bitbucket.org/%s/%s/';
  11. our $VERSION = '0.10';
  12. my $UA = LWP::UserAgent->new;
  13. $UA->agent(sprintf '%s/%s', __PACKAGE__, $VERSION);
  14. $UA->env_proxy;
  15. $UA->timeout(UA_TIMEOUT);
  16. sub new { return bless {}, shift };
  17. sub agent { return $UA }
  18. sub get {
  19. my $self = shift;
  20. my $url = shift;
  21. my $r = $self->agent->get($url);
  22. if ( $r->is_success ) {
  23. my $raw = $r->decoded_content;
  24. return JSON::from_json( $raw );
  25. }
  26. croak( 'GET request failed: ' . $r->as_string );
  27. }
  28. sub repositories {
  29. my $self = shift;
  30. my $user = shift || croak 'No user name specified';
  31. warn ">> Fetching the base URL ...\n";
  32. my $raw = eval { $self->get( sprintf URL, $user ) };
  33. croak "$user is not a valid user. Error: $@" if $@;
  34. croak "Data set is not a hash but $raw" if ref $raw ne 'HASH';
  35. my $r = $raw->{repositories} || die "No 'repositories' key in resultset";
  36. my @repos = sort { $a->{name} cmp $b->{name} }
  37. map { {
  38. name => $_->{name},
  39. url => sprintf( BASE_URL, $user, $_->{slug} ),
  40. }}
  41. @{ $r };
  42. return @repos;
  43. }
  44. package main;
  45. use strict;
  46. use warnings;
  47. use File::Spec;
  48. use Data::Dumper;
  49. use File::Path;
  50. use Time::HiRes qw( time );
  51. use constant RE_CPAN => qr{ \A (CPAN) \- (.+?) \z }xms;
  52. use constant SEPERATOR => join q{}, q{-} x 80, "\n";
  53. use Cwd;
  54. use Carp qw( croak );
  55. my $CWD = getcwd;
  56. my $START = time;
  57. my $bit = Net::BitBucket->new;
  58. _w( "CURRENT DIRECTORY: $CWD\n",
  59. "STARTING TO CLONE REPOSITORIES FROM BURAK GURSOY...\n",
  60. SEPERATOR );
  61. my $total = 0;
  62. foreach my $repo ( $bit->repositories( 'burak' ) ) {
  63. chdir $CWD; # reset
  64. my $name = $repo->{name};
  65. my @path = $name =~ RE_CPAN ? ($1, $2) : ($name);
  66. my $dir = File::Spec->catdir( @path );
  67. if ( @path > 1 ) {
  68. mkpath $path[0];
  69. chdir $path[0];
  70. }
  71. _w( "PROCESSING $name ...\n" );
  72. my $local_target = @path > 1 ? $path[1] : undef;
  73. if ( $local_target && -d $local_target ) {
  74. _w("$local_target exists. Skipping ...\n");
  75. next;
  76. }
  77. eval {
  78. hg( clone => $repo->{url}, $local_target ? ($local_target) : () );
  79. _w( "... done!\n", SEPERATOR );
  80. 1;
  81. } or do {
  82. my $e = $@ || '[unknown error]';
  83. die $@; # rollback && next?
  84. };
  85. $total++;
  86. }
  87. _w( "ADDED %d REPOSITORIES IN %.4f SECONDS\n", $total, time - $START );
  88. sub _w {
  89. my @args = @_;
  90. printf {*STDERR} @args or croak "Unable to print to STDERR: $!";
  91. return;
  92. }
  93. sub hg {
  94. my @args = @_;
  95. system( hg => @args ) && croak "FAILED(@args): $?";
  96. return;
  97. }
  98. 1;
  99. __END__
  100. =pod
  101. =head1 NAME
  102. bclone.pl - Get all stuff from Burak Gursoy
  103. =head1 SYNOPSIS
  104. chdir projects
  105. perl bclone.pl
  106. =head1 AUTHOR
  107. Burak Gursoy.
  108. =cut