check_upstream.rb 4.5 KB
Newer Older
S
Shinwell Hu 已提交
1 2 3 4 5
#!/usr/bin/ruby

require 'yaml'
require 'json'
require 'date'
6
require 'optparse'
S
Shinwell Hu 已提交
7 8 9 10 11 12 13 14

require './check_upstream/github'
require './check_upstream/git'
require './check_upstream/hg'
require './check_upstream/svn'
require './check_upstream/metacpan'
require './check_upstream/gnome'
require './check_upstream/pypi'
15 16
require './helper/download_spec'
require './helper/rpmparser'
17
require './gitee/advisor'
S
Shinwell Hu 已提交
18

19
options = {}
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
OptionParser.new do |opts|
	opts.banner = "Usage: ./check_upstream.rb [options]"
	opts.on("-p", "--push", "Push the advise to gitee.com/src-openeuler") do |v|
		options[:push] = v
	end
	opts.on("-r", "--repo REPO_NAME", "Repo to check upstream info") do |n|
		puts "Checking #{n}"
		options[:repo] = n
	end
	opts.on("-h", "--help", "Prints this help") do
		puts opts
		exit
	end
end.parse!

if not options[:repo] then
	puts "Missing repo name\n"
	exit 1
end 

Prj_name = options[:repo]
42 43 44 45 46 47 48
specfile=download_spec(Prj_name)
if specfile == "" then
	puts "no specfile found for project\n"
	exit 1
end
spec_struct = Specfile.new(specfile)
Cur_ver = spec_struct.get_version
S
Shinwell Hu 已提交
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

Prj_info = YAML.load(File.read "upstream-info/"+Prj_name+".yaml")

def compare_tags (a, b)
	arr_a = a.split(".")
	arr_b = b.split(".")
	len = [arr_a.length, arr_b.length].min
	idx = 0
	while idx < len do
		res1 = arr_a[idx].to_i <=> arr_b[idx].to_i
		return res1 if res1 != 0
		res2 = arr_a[idx].length <=> arr_b[idx].length
		return -res2 if res2 != 0
		res3 = arr_a[idx][-1].to_i <=> arr_b[idx][-1].to_i
		return res3 if res3 != 0
		idx = idx + 1
	end
	return arr_a.length <=> arr_b.length
end

def sort_tags (tags)
	tags.sort! { |a, b|
		compare_tags(a,b)
	}
	return tags
end

def clean_tags(tags)
	new_tags = []
	tags.each{|line|
		new_tags = new_tags.append clean_tag(line, Prj_info)
	}
	return new_tags
end

def upgrade_recommend(tags_param, cur_tag, policy)
	tags = tags_param.reverse
	tag1 = cur_tag
	tag2 = cur_tag
	if policy == "latest" then
		return tags[0]
	elsif policy == "latest-stable" then
		tags.each { |tag|
			if tag.split(".").count {|f| f.to_i != 0 } >= 3 then
				tag1 = tag
				break
			end
		}
		tags.each { |tag|
			if tag.split(".").count {|f| f.to_i != 0} >= 2 then
				tag2 = tag
				break
			end
		}
		if tag2[0].to_i > tag1[0].to_i then
			return tag2
		else
			return tag1
		end
	elsif policy == "perfer-stable" then
		tags.each { |tag|
			if tag.start_with?(cur_tag) then
				return tag
			end
		}
		if cur_tag.split(".").length >= 3 then
			search_tag = cur_tag.split(".")[0..1].join(".")
			tags.each { |tag|
				if tag.start_with?(search_tag) then
					return tag
				end
			}
		end
		return cur_tag
	else
		return cur_tag
	end

end

print Prj_name, ":\n"

if Prj_info["version_control"] == "svn" then
	tags = check_upstream_svn(Prj_info)
elsif Prj_info["version_control"] == "github" then
	tags = check_upstream_github_by_api(Prj_info)
	if tags == nil or tags == "" then
		tags = check_upstream_github_by_git(Prj_info)
	end
	tags = clean_tags(tags.lines)
elsif Prj_info["version_control"] == "git" then
	tags = check_upstream_git(Prj_info)
	tags = clean_tags(tags.lines)
elsif Prj_info["version_control"] == "hg" then
	tags = check_upstream_hg(Prj_info)
	tags = clean_tags(tags.lines)
elsif Prj_info["version_control"] == "metacpan" then
	tags = check_upstream_metacpan(Prj_info)
	tags = clean_tags(tags.lines)
elsif Prj_info["version_control"] == "gitlab.gnome" then
	tags = check_upstream_gnome(Prj_info)
	tags = clean_tags(tags.lines)
elsif Prj_info["version_control"] == "pypi" then
	tags = check_upstream_pypi(Prj_info)
	tags = clean_tags(tags.lines)
end

tags = sort_tags(tags)
print "Latest upstream is ", tags[-1], "\n"
158
#print "Recommended is     ", upgrade_recommend(tags, Cur_ver, "latest-stable"), "\n"
S
Shinwell Hu 已提交
159 160 161 162 163 164 165 166 167
print "Current version is ", Cur_ver, "\n"

if tags.length == 0 or compare_tags(tags[-1], Cur_ver) < 0 then
	STDERR.puts "DEBUG #{Prj_name} > tags are #{tags}"
	File.delete("upstream-info/"+Prj_name+".yaml") if File.exist?("upstream-info/"+Prj_name+".yaml")
	File.open("known-issues/"+Prj_name+".yaml", "w") { |file| file.write(Prj_info.to_yaml) }
else
	File.open("upstream-info/"+Prj_name+".yaml", "w") { |file| file.write(Prj_info.to_yaml) }
end
168
File.delete(specfile) if specfile != ""
169 170 171 172 173 174 175 176

if options[:push] then
	puts "Push to gitee\n"
	ad = Advisor.new
	ad.new_issue("src-openeuler", Prj_name, "Upgrade to Latest Release", "Deer #{Prj_name} maintainer:\n\n  We found the latst version of #{Prj_name} is #{tags[-1]}, while the current version in openEuler is #{Cur_ver}.\n\n  Please consider upgrading.\n\n\nYours openEuler Advisor.")
else
	puts "keep it to us\n"
end