Last active
May 26, 2016 19:57
-
-
Save lutter/a6bc56fdb5509a3a311158631924a5f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/ruby | |
# To use this, untar the forge backup tarball from | |
# http://forge-dl-backup.s3-website-us-west-2.amazonaws.com/ somewhere and | |
# then run this script, passing the name of the directory where you | |
# unpacked the tarball. | |
# | |
# The script will print the names of the latest version of each tarball; | |
# you can feed that into something like 'xargs -iF tar xf DIR/F' | |
TOP_DIR=File::expand_path(File::dirname($0) + "/..") | |
# Turn a version string like "1.2.3-4" into an array of integers [1,2,3,4] | |
def to_array(version) | |
version.split(/[.-]/).map do |x| | |
if x =~ /\A.*([0-9]+).*\Z/ | |
$1.to_i | |
else | |
0 | |
end | |
end | |
end | |
def vercmp(a, b) | |
# Poor man's version comparison | |
to_array(a) <=> to_array(b) | |
end | |
if ARGV.size != 1 | |
puts "Usage: latest DIR" | |
puts "For each package in DIR, list the latest tarball" | |
exit 1 | |
end | |
# This horror is used to split the name of a tarball into a name and a | |
# version string. It ignores an initial prefix of numbers followed by an | |
# underscore. Version strings can be any of the following: | |
# 1.2.3 | |
# 1.2.3-4 | |
# 1.2.3-rc3 | |
# 1.2.3-pre5 | |
# 1.2.3-SNAPSHOT | |
# There's still a few tarballs that this doesn't catch, but it seems good enough | |
TAR_RX = /\A(?:[0-9]*_)?(.*?)-([0-9.-]+((rc|pre)[0-9]+|SNAPSHOT|beta)?).tar.gz\Z/ | |
pkgs = {} | |
orig = {} | |
Dir.foreach(ARGV[0]) do |e| | |
if e =~ TAR_RX | |
pkgs[$1] ||= [] | |
pkgs[$1] << $2 | |
orig[$1] ||= {} | |
orig[$1][$2] = e | |
end | |
end | |
pkgs.keys.each do |name| | |
v = pkgs[name].sort { |a,b| vercmp(a,b) }.last | |
puts orig[name][v] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment