-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.pm
177 lines (151 loc) · 5.13 KB
/
Util.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package Util;
use strict;
use File::Basename;
use XML::Simple;
use WWW::Mechanize;
use Data::Dumper;
use File::Slurp;
# -------------------------------------------------------------------
sub download_feed {
my $param = shift;
my $content;
my $cachefile = dirname($0) . "/feed.xml";
if (-r $cachefile) {
$content = read_file( $cachefile );
} else {
my $netmech = WWW::Mechanize->new();
$netmech->get($param->{url});
$content = $netmech->content();
open (my $cache, '>', $cachefile);
print $cache $content;
close $cache;
}
my $xmlproc = new XML::Simple;
return $xmlproc->XMLin($content);
}
## Return a list of releases
## Params:
## $feed hashref of the feed
## $basename Releases of which package? If empty, get all releases of all packages
## returns arrayref of hashref
sub get_releases {
my $feed = shift;
my $basename = shift || '';
my @releases;
foreach my $url (keys %{$feed->{entry}}) {
next if $basename && $url !~ /$basename-/;
push @releases, Util::release_to_version_hash($url);
}
return \@releases;
}
sub filter_releases {
my $releases = shift;
my $filter = shift || {};
$filter->{stable} = 1 unless exists($filter->{stable});
$filter->{rc} = 0 unless exists($filter->{rc});
$filter->{beta} = 0 unless exists($filter->{beta});
$filter->{alpha} = 0 unless exists($filter->{alpha});
$filter->{major} = 0 unless exists($filter->{major});
$filter->{minor} = 0 unless exists($filter->{minor});
$filter->{patch} = 0 unless exists($filter->{patch});
$filter->{pkg} = '' unless exists($filter->{pkg});
my @legit;
foreach my $pkg (@$releases) {
## filters
next if ($filter->{pkg} && $pkg->{pkg} ne $filter->{pkg});
next if ($pkg->{dev} =~ /RC/ && $filter->{'rc'} == 0);
next if ($pkg->{dev} =~ /alpha/ && $filter->{'alpha'} == 0);
next if ($pkg->{dev} =~ /beta/ && $filter->{'beta'} == 0);
next if ($filter->{major} && $filter->{major} != $pkg->{major});
next if ($filter->{minor} && $filter->{minor} != $pkg->{minor});
next if ($filter->{patch} && $filter->{minor} != $pkg->{patch});
push @legit, $pkg;
}
return \@legit;
}
## Return the best release identifier string or null
## This is generally the highest version which fits criteria.
## params:
## $releases ArrayRef of Release URL Strings
## filter: A HashRef of options: {stable => 1, rc => 0, beta => 0, alpha => 0 }
sub get_best_release {
my $releases = shift;
my $filter = shift || { stable => 1, rc => 0, beta => 0, alpha => 0, major => 0, minor => 0, patch => 0, pkg => '' };
my @legit = Util::filter_releases($releases, $filter);
die "No apropiate version found" unless @legit;
return pop @{Util::sort_releases(@legit)};
}
# params:
## $releases ArrayRef of release hashrefs
sub sort_releases {
my $releases = shift;
return [sort Util::compare_versions @$releases];
}
## Return a version hash for comparison
## Params:
## $release STRING a release string
sub release_to_version_hash {
my $release = shift;
my ($package, $major, $minor, $patch, $dev) = $release =~ /\/(\w+)-(\d+)\.(\d+)\.(\d+)(\w*)/;
return { major => $major,
minor => $minor,
patch => $patch,
dev => $dev,
pkg => $package,
url => $release,
string => sprintf("%d.%d.%d%s", $major, $minor, $patch, $dev)
};
}
## A compare function callback for version hashes, suitable for sort
## returns -1, 0 or 1
sub compare_versions {
$a = shift if @_;
$b = shift if @_;
return ($a->{major} <=> $b->{major}
or $a->{minor} <=> $b->{minor}
or $a->{patch} <=> $b->{patch}
##
or ( !$a->{dev} && $b->{dev} ? 1 : 0 )
or ( !$b->{dev} && $a->{dev} ? -1 : 0 )
or lc($a->{dev}) cmp lc($b->{dev}))
}
sub get_specfile_version {
my $params = shift || {};
my $specfile_name = defined ($params->{specfilename}) ? $params->{specfilename} : shift @{glob '*.spec'};
my $version = '';
# # Read it from the spec file
my $spec_fh = IO::File->new($specfile_name, 'r');
foreach my $line (<$spec_fh>) {
last if ($version) = $line =~ /^\s*Version:\s*([\w\.]*)[\s#]*/;
}
$spec_fh->close();
return $version;
}
## parse full and partial version strings
sub version_string_to_version_hash {
my $version_string = shift;
my $major = ($version_string =~ s/^(\d+)\.*//) ? $1 : '';
my $minor = ($version_string =~ s/^(\d+)\.*//) ? $1 : '';
my $patch = ($version_string =~ s/^(\d+)\.*//) ? $1 : '';
my $dev = $version_string || '';
my $hash = { major => $major,
minor => $minor,
patch => $patch,
dev => $dev
};
$hash->{string} = sprintf('%d.%d.%d%s', $major, $minor, $patch, $dev) if (length($major) && length($minor) && length($patch));
return $hash;
}
## parse the changelog from the <content> tags of the feed <entry> section
sub feed_get_changelog_by_version {
my $feed = shift;
my $version = shift;
return {'version' => $version->{'string'}, 'changes' => $feed->{'entry'}->{$version->{'url'}}->{content} };
}
sub meta_add_changelog {
}
sub rpm_changes_add_changelog {
}
sub debian_add_changelog {
}
1;