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
.gitignore
1 1 *.swp
2 2 *.swo
  3 +data/
... ...
config.yaml
1 1 User: softwarepublico
  2 +UpdateRate: 600
2 3 Repositories:
3 4 - v4
4 5 - v5
... ...
lib/CoprStatus.pm
... ... @@ -27,24 +27,49 @@ sub git_url {
27 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 33 my $ua = LWP::UserAgent->new;
32 34 $ua->timeout(300);
33 35 $ua->env_proxy;
34 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 44 my $git_url = git_url('http://softwarepublico.gov.br',
38 45 'gitlab/softwarepublico/softwarepublico/raw/<branch>/src/pkg-rpm/<package>/<package>.spec',
39 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 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 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 73 $version = "$version-$release";
49 74 $info->{$package}->{'git_version_'.$branch} = $version;
50 75 }
... ... @@ -52,7 +77,10 @@ sub get_specs {
52 77  
53 78 sub get_copr_versions {
54 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 84 my $json = JSON->new->allow_nonref;
57 85 my $dec_result = $json->decode($result);
58 86 foreach(@{$dec_result->{'packages'}}) {
... ... @@ -61,7 +89,6 @@ sub get_copr_versions {
61 89 my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
62 90 $info->{$package}->{$repo."_version"} = $version if $status eq "succeeded";
63 91 }
64   -
65 92 }
66 93  
67 94 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 122 sub compare_versions {
79 123 update_info();
80 124 my $match = {};
... ... @@ -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 225 1;
... ...