/pkgcl/pkgcl
Ruby | 162 lines | 135 code | 17 blank | 10 comment | 19 complexity | 48024a33b1453d20f5c8e8f969eb5655 MD5 | raw file
Possible License(s): LGPL-2.0, Unlicense, AGPL-1.0, BitTorrent-1.0, EPL-1.0, GPL-3.0, BSD-3-Clause, GPL-2.0, MIT, CC-BY-SA-3.0, BSD-2-Clause, MPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, JSON, AGPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0, LGPL-2.1, ISC, CC-BY-3.0, WTFPL, 0BSD, CC0-1.0, LGPL-3.0, Cube, Apache-2.0
- #!/usr/bin/env ruby
- # Author: Alex Brown
- # email: code@alexpbrown.me
- # license: WTFPL
- require 'nokogiri'
- require 'open-uri'
- require 'optparse'
- VERSION = "0.3.1"
- @options = {}
- @options[:commits] = 1
- @options[:show_git_svn] = false
- @options[:testing_repo] = false
- opts = OptionParser.new do |opts|
- opts.banner = "Usage: pkgcl [options] <packages>"
- opts.separator ""
- opts.on("-c", "--commits=NUM", Integer, "Show NUM commit messages") do |n|
- @options[:commits] = n
- end
- opts.on("--show-git-svn-id", "Show git-svn-id messages") do
- @options[:show_git_svn] = true
- end
- opts.on("--enable-testing", "Show commits relevant to packages in [testing]") do
- @options[:testing_repo] = true
- end
- opts.on("-v", "--version", "Show version information") do
- puts VERSION
- exit
- end
- opts.on_tail("-h", "--help", "Show this message") do
- puts opts
- exit
- end
- end
- opts.parse!(ARGV)
- # check for stdin and get package list accordingly
- # I even decided to be nice and parse both 'pacman -Qu' and 'pacman -Qqu' on stdin
- packages = []
- if STDIN.tty? == false
- ARGF.lines do |line|
- packages << {:name => line.strip!.split(" ")[0]}
- end
- else
- ARGV.each {|a| packages << {:name => a} }
- end
- puts opts if packages.empty?
- def colorize(text, color_code)
- "\e[#{color_code}m#{text}\e[0m"
- end
- def get_pkg(pkg)
- repo, arch = "", ""
- IO.popen([{"LC_ALL"=>"C"}, "pacman", "-Si", pkg[:name], :err => :close]) do |io|
- io.lines do |line|
- case line
- when /^Repository(\s?)*: (.+)$/
- pkg[:repo] = $2
- when /^Architecture(\s?)*: (.+)$/
- pkg[:arch] = $2
- when /^Build Date(\s?)*: (.+)$/
- build_date = Time.parse($2)
- if build_date.strftime("%F") == Time.now.strftime("%F")
- build_date = Time.now
- end
- pkg[:build_date] = build_date
- when /^$/
- break
- end
- end
- end
- end
- def get_pkg_info(pkg)
- # I need to get the correct git url from here, since some packages use a
- # different tracking repo (e.g. linux-headers => linux)
- # I'm also getting the last updated date from this url
- uri = "http://www.archlinux.org/packages/#{pkg[:repo]}/#{pkg[:arch]}/#{pkg[:name]}"
- doc = Nokogiri::HTML(open(uri))
- pkg[:git_url] = doc.css("div#actionlist ul li a")[1]['href']
- end
- def age_test(commit_age, pkg)
- case commit_age
- when /days/
- git_time = Time.now - commit_age.to_i*60*60*24
- when /hours/
- git_time = Time.now - commit_age.to_i*60*60
- when /min/
- git_time = Time.now - commit_age.to_i*60
- else
- git_time = Time.parse(commit_age)
- end
- if git_time > pkg[:build_date] then
- return false
- else
- return true
- end
- end
- def get_commits(pkg)
- pkg[:commits] = []
- doc = Nokogiri::HTML(open("#{pkg[:git_url]}&showmsg=1"))
- table = doc.css("table.list.nowrap")
- table.css("td.logsubject").each_with_index do |n,i|
- age = n.parent.children[0].content
- if @options[:testing_repo] == false then
- next if age_test(age, pkg) == false
- end
- commit = {}
- age+=" ago" if /(days|hours|min)/ === age
- commit[:age] = age
- commit[:subject] = n.content
- content = table.css("td.logmsg")[i].content
- content.gsub!(/^git-svn-id.*$/, "") unless @options[:show_git_svn] == true
- content.strip!
- commit[:msg] = content.empty? ? "None" : content
- pkg[:commits] << commit
- end
- end
- def show_output(pkg)
- # Format and print the result
- print "["+colorize(pkg[:name], 31)+"]"
- if pkg[:commits].count < @options[:commits]
- print colorize(" (Only #{pkg[:commits].count} commits)\n", 33)
- else
- print "\n"
- end
- output = 0
- pkg[:commits].each do |p|
- break if output == @options[:commits]
- puts colorize(p[:subject], 4)
- print colorize("Age: ", 37)
- print colorize(p[:age], 36)+" - "
- puts colorize(p[:msg], 32)
- puts
- output +=1
- end
- end
- packages.each do |p|
- get_pkg(p)
- if p.has_key?(:arch) == false && p.has_key?(:repo) == false
- puts "["+colorize(p[:name], 31)+"]"
- puts "Not Found"
- puts
- next
- end
- get_pkg_info(p)
- get_commits(p)
- show_output(p)
- end