push.pl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use File::Temp;
  5. use Getopt::Long;
  6. use Mojo::UserAgent;
  7. use Mojo::Util qw(slurp spurt trim);
  8. use Term::ReadKey;
  9. my $username;
  10. my $password;
  11. my $all;
  12. GetOptions(
  13. 'u|username=s' => \$username,
  14. 'p|password=s' => \$password,
  15. 'a|all' => \$all,
  16. ) or die 'bad args';
  17. die 'no repos specified' unless @ARGV;
  18. my $ua = Mojo::UserAgent->new->max_redirects(10);
  19. # Mojo::UserAgent::CookieJar::find is destructive...
  20. # this is a nondestructive version that makes the login succeed on the Hub
  21. Mojo::Util::monkey_patch 'Mojo::UserAgent::CookieJar', find => sub {
  22. my ($self, $url) = @_;
  23. return unless my $domain = my $host = $url->ihost;
  24. my $path = $url->path->to_abs_string;
  25. my @found;
  26. while ($domain =~ /[^.]+\.[^.]+|localhost$/) {
  27. next unless my $old = $self->{jar}{$domain};
  28. # Grab cookies
  29. #my $new = $self->{jar}{$domain} = [];
  30. for my $cookie (@$old) {
  31. next unless $cookie->domain || $host eq $cookie->origin;
  32. # Check if cookie has expired
  33. my $expires = $cookie->expires;
  34. next if $expires && time > ($expires->epoch || 0);
  35. #push @$new, $cookie;
  36. # Taste cookie
  37. next if $cookie->secure && $url->protocol ne 'https';
  38. next unless Mojo::UserAgent::CookieJar::_path($cookie->path, $path);
  39. my $name = $cookie->name;
  40. my $value = $cookie->value;
  41. push @found, Mojo::Cookie::Request->new(name => $name, value => $value);
  42. }
  43. }
  44. # Remove another part
  45. continue { $domain =~ s/^[^.]+\.?// }
  46. return @found;
  47. };
  48. sub get_form_bits {
  49. my $form = shift;
  50. my $ret = {};
  51. $form->find('input, select, textarea')->grep(sub {
  52. !$_->match('input[type=submit], input[type=reset], input[type=button]')
  53. && defined($_->attr('name'))
  54. })->each(sub {
  55. my $e = shift;
  56. my $name = $e->attr('name');
  57. $ret->{$name} = '' . $e->val;
  58. if ($e->type eq 'textarea') {
  59. $ret->{$name} = trim($ret->{$name});
  60. $ret->{$name} =~ s!\r\n|\r!\n!g;
  61. }
  62. });
  63. return $ret;
  64. }
  65. my $login = $ua->get('https://registry.hub.docker.com/account/login/');
  66. die 'login failed' unless $login->success;
  67. my $loginForm = $login->res->dom('#form-login')->first;
  68. my $loginBits = get_form_bits($loginForm);
  69. unless (defined $username) {
  70. print 'Hub Username: ';
  71. $username = ReadLine 0;
  72. chomp $username;
  73. }
  74. $loginBits->{username} = $username;
  75. unless (defined $password) {
  76. print 'Hub Password: ';
  77. ReadMode 2;
  78. $password = ReadLine 0;
  79. chomp $password;
  80. ReadMode 0;
  81. print "\n";
  82. }
  83. $loginBits->{password} = $password;
  84. $login = $ua->post($login->req->url->to_abs => {
  85. Referer => $login->req->url->to_abs->to_string,
  86. } => form => $loginBits);
  87. die 'login failed' unless $login->success;
  88. my $error = $login->res->dom('.alert-error');
  89. if ($error->size) {
  90. die $error->pluck('all_text')->join("\n") . "\n";
  91. }
  92. while (my $repo = shift) { # '/_/hylang', '/u/tianon/perl', etc
  93. $repo = '/_/' . $repo unless $repo =~ m!/!;
  94. $repo = '/' . $repo unless $repo =~ m!^/!;
  95. $repo =~ s!/+$!!;
  96. my $repoName = $repo;
  97. $repoName =~ s!^.*/!!; # 'hylang', 'perl', etc
  98. my $shortFile = $repoName . '/README-short.txt';
  99. my $short = slurp $shortFile or die 'missing ' . $shortFile;
  100. $short = trim $short;
  101. my $longFile = $repoName . '/README.md';
  102. my $long = slurp $longFile or die 'missing ' . $longFile;
  103. $long = trim $long;
  104. my $repoUrl = 'https://registry.hub.docker.com' . $repo . '/settings/';
  105. my $repoTx = $ua->get($repoUrl);
  106. die 'failed to get: ' . $repoUrl unless $repoTx->success;
  107. my $settingsForm = $repoTx->res->dom('form[name="repository_settings"]')->first;
  108. die 'failed to find form on ' . $repoUrl unless $settingsForm;
  109. my $settingsBits = get_form_bits($settingsForm);
  110. my $hubShort = $settingsBits->{description};
  111. my $hubLong = $settingsBits->{full_description};
  112. if ($hubShort ne $short) {
  113. my $file = File::Temp->new(SUFFIX => '.txt');
  114. my $filename = $file->filename;
  115. spurt $hubShort . "\n", $filename;
  116. system('vimdiff', $filename, $shortFile) == 0 or die "vimdiff on $filename and $shortFile failed";
  117. $hubShort = trim(slurp($filename));
  118. }
  119. if ($hubLong ne $long) {
  120. my $file = File::Temp->new(SUFFIX => '.md');
  121. my $filename = $file->filename;
  122. spurt $hubLong . "\n", $filename;
  123. system('vimdiff', $filename, $longFile) == 0 or die "vimdiff on $filename and $longFile failed";
  124. $hubLong = trim(slurp($filename));
  125. }
  126. say 'no change to ' . $repoName . '; skipping' and next if $settingsBits->{description} eq $hubShort and $settingsBits->{full_description} eq $hubLong;
  127. $settingsBits->{description} = $hubShort;
  128. $settingsBits->{full_description} = $hubLong;
  129. $repoTx = $ua->post($repoUrl => { Referer => $repoUrl } => form => $settingsBits);
  130. die 'post to ' . $repoUrl . ' failed' unless $repoTx->success;
  131. }