Commit 6c413fcc6857c15d46e4e8583dc5ed5724da13a8

Authored by Athos
1 parent 23707105
Exists in master

WIP: make asynchronous requests to update version

Now, git specs and copr json files are downloaded to the data directory
in arate defined in the config.yaml file and read on HTTP get requests.
Showing 3 changed files with 60 additions and 9 deletions   Show diff stats
1 *.swp 1 *.swp
2 *.swo 2 *.swo
  3 +data/
1 User: softwarepublico 1 User: softwarepublico
  2 +UpdateRate: 600
2 Repositories: 3 Repositories:
3 - v4 4 - v4
4 - v5 5 - v5
lib/CoprStatus.pm
@@ -27,24 +27,49 @@ sub git_url { @@ -27,24 +27,49 @@ sub git_url {
27 return "$domain/$spec_path"; 27 return "$domain/$spec_path";
28 } 28 }
29 29
30 -sub get_specs { 30 +sub download_specs {
  31 + my ( $branch, $user, $repo ) = @_;
  32 + return unless -e "data/copr/$user/$repo/monitor.json";
31 my $ua = LWP::UserAgent->new; 33 my $ua = LWP::UserAgent->new;
32 $ua->timeout(300); 34 $ua->timeout(300);
33 $ua->env_proxy; 35 $ua->env_proxy;
34 $ua->ssl_opts(SSL_verify_mode => 0x00); 36 $ua->ssl_opts(SSL_verify_mode => 0x00);
35 - my ( $branch ) = @_;  
36 - foreach my $package (keys %{$info}) { 37 + open(my $fh, "<", "data/copr/$user/$repo/monitor.json") or die;
  38 + my $result = do { local $/; <$fh> };
  39 + close($fh);
  40 + my $json = JSON->new->allow_nonref;
  41 + my $dec_result = $json->decode($result);
  42 + foreach(@{$dec_result->{'packages'}}) {
  43 + my $package = $_->{'pkg_name'};
37 my $git_url = git_url('http://softwarepublico.gov.br', 44 my $git_url = git_url('http://softwarepublico.gov.br',
38 'gitlab/softwarepublico/softwarepublico/raw/<branch>/src/pkg-rpm/<package>/<package>.spec', 45 'gitlab/softwarepublico/softwarepublico/raw/<branch>/src/pkg-rpm/<package>/<package>.spec',
39 $branch, $package); 46 $branch, $package);
40 - my $spec = $ua->get($git_url);  
41 - my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m; 47 + `mkdir -p data/git/$branch`;
  48 + my $spec_filename = 'data/git/'.$branch.'/'.$package.'.spec';
  49 + $ua->mirror( $git_url, $spec_filename );
  50 + }
  51 +}
  52 +
  53 +sub download_copr_versions {
  54 + my ( $user, $repo ) = @_;
  55 + `mkdir -p data/copr/$user/$repo`;
  56 + my $result = mirror(copr_monitor_url($user, $repo), "data/copr/$user/$repo/monitor.json");
  57 +}
  58 +
  59 +sub get_specs {
  60 + my ( $branch ) = @_;
  61 + foreach my $package (keys %{$info}) {
  62 + next unless -e "data/git/$branch/$package.spec";
  63 + open(my $fh, "<", "data/git/$branch/$package.spec") or die;
  64 + my $spec = do { local $/; <$fh> };
  65 + close($fh);
  66 + my $version = $1 if $spec =~ /^Version:\s*([^\s]+)\s*$/m;
42 if($version =~ /%\{version\}/) { 67 if($version =~ /%\{version\}/) {
43 - $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m; 68 + $version = $1 if $spec =~ /define version\s*([^\s]+)\s*$/m;
44 } 69 }
45 70
46 my $release = 'no_release'; 71 my $release = 'no_release';
47 - $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m; 72 + $release = $1 if $spec =~ /^Release:\s*([^\s]+)\s*$/m;
48 $version = "$version-$release"; 73 $version = "$version-$release";
49 $info->{$package}->{'git_version_'.$branch} = $version; 74 $info->{$package}->{'git_version_'.$branch} = $version;
50 } 75 }
@@ -52,7 +77,10 @@ sub get_specs { @@ -52,7 +77,10 @@ sub get_specs {
52 77
53 sub get_copr_versions { 78 sub get_copr_versions {
54 my ( $user, $repo ) = @_; 79 my ( $user, $repo ) = @_;
55 - my $result = get(copr_monitor_url($user, $repo)); 80 + return unless -e "data/copr/$user/$repo/monitor.json";
  81 + open(my $fh, "<", "data/copr/$user/$repo/monitor.json") or die;
  82 + my $result = do { local $/; <$fh> };
  83 + close($fh);
56 my $json = JSON->new->allow_nonref; 84 my $json = JSON->new->allow_nonref;
57 my $dec_result = $json->decode($result); 85 my $dec_result = $json->decode($result);
58 foreach(@{$dec_result->{'packages'}}) { 86 foreach(@{$dec_result->{'packages'}}) {
@@ -61,7 +89,6 @@ sub get_copr_versions { @@ -61,7 +89,6 @@ sub get_copr_versions {
61 my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'}; 89 my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
62 $info->{$package}->{$repo."_version"} = $version if $status eq "succeeded"; 90 $info->{$package}->{$repo."_version"} = $version if $status eq "succeeded";
63 } 91 }
64 -  
65 } 92 }
66 93
67 sub update_info { 94 sub update_info {
@@ -75,6 +102,23 @@ sub update_info { @@ -75,6 +102,23 @@ sub update_info {
75 } 102 }
76 } 103 }
77 104
  105 +sub update_files {
  106 + while(1) {
  107 + my $user = $config->{User};
  108 + foreach my $repo (@{$config->{Repositories}}) {
  109 + download_copr_versions($user, $repo);
  110 + }
  111 +
  112 + my $repo_index = 0;
  113 + foreach my $branch (@{$config->{Branches}}) {
  114 + download_specs($branch, $user, ${$config->{Repositories}}[$repo_index]);
  115 + $repo_index += 1;
  116 + }
  117 +
  118 + sleep $config->{UpdateRate};
  119 + }
  120 +}
  121 +
78 sub compare_versions { 122 sub compare_versions {
79 update_info(); 123 update_info();
80 my $match = {}; 124 my $match = {};
@@ -173,4 +217,9 @@ sub serve_json_status { @@ -173,4 +217,9 @@ sub serve_json_status {
173 ]; 217 ];
174 }; 218 };
175 219
  220 +my $child_pid = fork();
  221 +if($child_pid) {
  222 + update_files();
  223 +}
  224 +
176 1; 225 1;