2011-08-25 17:50:03 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
2011-08-26 18:02:01 +00:00
|
|
|
require 'fileutils'
|
2011-08-25 17:50:03 +00:00
|
|
|
require 'rubygems'
|
|
|
|
require 'find'
|
|
|
|
require 'json'
|
|
|
|
require 'rexml/document'
|
|
|
|
require 'optparse'
|
|
|
|
|
|
|
|
# syntax:
|
|
|
|
#
|
2011-08-27 15:09:11 +00:00
|
|
|
# create-packages.rb <input directory> <config file> <output directory>
|
2011-08-25 17:50:03 +00:00
|
|
|
#
|
|
|
|
# Takes the set of files that make up a release and splits them up into
|
2011-08-27 15:09:11 +00:00
|
|
|
# a set of .zip packages along with a file_list.xml file listing all
|
|
|
|
# the files in the release and mapping them to their respective
|
|
|
|
# packages.
|
2011-08-25 17:50:03 +00:00
|
|
|
#
|
|
|
|
# Outputs:
|
|
|
|
#
|
|
|
|
# <output-dir>/$PACKAGE_NAME.zip
|
|
|
|
# These are packages containing the files in this version of the software.
|
|
|
|
#
|
|
|
|
# <output-dir>/file_list.xml
|
|
|
|
# This file lists all the files contained in this version of the software and
|
|
|
|
# the packages which they are contained in.
|
|
|
|
#
|
2011-09-01 11:47:10 +00:00
|
|
|
# <output-dir>/$UPDATER_BINARY
|
|
|
|
# The standalone auto-update installer binary. This is not compressed so that
|
|
|
|
# it can be downloaded and executed directly.
|
|
|
|
#
|
2011-08-25 17:50:03 +00:00
|
|
|
|
|
|
|
# Represents a group of updates in a release
|
|
|
|
class UpdateScriptPackage
|
|
|
|
# name - The name of the package (without any extension)
|
|
|
|
# hash - The SHA-1 hash of the package
|
|
|
|
# size - The size of the package in bytes
|
|
|
|
attr_reader :name,:hash,:size
|
|
|
|
attr_writer :name,:hash,:size
|
|
|
|
end
|
|
|
|
|
|
|
|
# Represents a single file in a release
|
|
|
|
class UpdateScriptFile
|
|
|
|
# path - The path of the file relative to the installation directory
|
|
|
|
# hash - The SHA-1 hash of the file
|
2011-09-04 13:53:51 +00:00
|
|
|
# permissions - The permissions of the file (See File::stat)
|
2011-08-25 17:50:03 +00:00
|
|
|
# size - The size of the file in bytes
|
|
|
|
# package - The name of the package containing this file
|
2011-08-27 15:09:11 +00:00
|
|
|
attr_reader :path,:hash,:permissions,:size,:package,:target,:is_main_binary
|
|
|
|
attr_writer :path,:hash,:permissions,:size,:package,:target,:is_main_binary
|
2011-08-25 17:50:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Utility method - convert a hash map to an REXML element
|
|
|
|
#
|
|
|
|
# Hash keys are converted to elements and hash values either
|
|
|
|
# to text contents or child elements (if the value is a Hash)
|
|
|
|
#
|
|
|
|
# 'root' - the root REXML::Element
|
|
|
|
# 'map' - a hash mapping element names to text contents or
|
|
|
|
# hash maps
|
|
|
|
#
|
|
|
|
def hash_to_xml(root,map)
|
|
|
|
map.each do |key,value|
|
|
|
|
element = REXML::Element.new(key)
|
|
|
|
if value.instance_of?(String)
|
|
|
|
element.text = value
|
|
|
|
elsif value.instance_of?(Hash)
|
|
|
|
hash_to_xml(element,value)
|
|
|
|
elsif !value.nil?
|
|
|
|
raise "Unsupported value type #{value.class}"
|
|
|
|
end
|
|
|
|
root.add_element element
|
|
|
|
end
|
|
|
|
return root
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip_prefix(string,prefix)
|
|
|
|
if (!string.start_with?(prefix))
|
|
|
|
raise "String does not start with prefix"
|
|
|
|
end
|
|
|
|
return string.sub(prefix,"")
|
|
|
|
end
|
|
|
|
|
2011-09-01 14:56:26 +00:00
|
|
|
def file_sha1(path)
|
|
|
|
return `sha1sum "#{path}"`.split(' ')[0]
|
|
|
|
end
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
class UpdateScriptGenerator
|
2011-08-27 15:09:11 +00:00
|
|
|
|
2011-09-01 11:47:10 +00:00
|
|
|
# target_version - The version string for the build in this update
|
|
|
|
# platform - The platform which this build is for
|
2011-08-27 15:09:11 +00:00
|
|
|
# input_dir - The directory containing files that make up the install
|
|
|
|
# output_dir - The directory containing the generated packages
|
|
|
|
# package_config - The PackageConfig specifying the file -> package map
|
|
|
|
# for the application and other config options
|
|
|
|
# file_list - A list of all files in 'input_dir' which make up the install
|
|
|
|
# package_file_map - A map of (package name -> [paths of files in this package])
|
|
|
|
|
2011-09-01 11:47:10 +00:00
|
|
|
def initialize(target_version,
|
|
|
|
platform,
|
|
|
|
input_dir,
|
|
|
|
output_dir,
|
|
|
|
package_config,
|
|
|
|
file_list,
|
|
|
|
package_file_map)
|
|
|
|
|
|
|
|
@target_version = target_version
|
|
|
|
@platform = platform
|
2011-08-27 15:09:11 +00:00
|
|
|
@config = package_config
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
# List of files to install in this version
|
|
|
|
@files_to_install = []
|
|
|
|
file_list.each do |path|
|
|
|
|
file = UpdateScriptFile.new
|
|
|
|
file.path = strip_prefix(path,input_dir)
|
2011-09-01 16:37:50 +00:00
|
|
|
|
|
|
|
if (File.basename(path) == @config.updater_binary)
|
|
|
|
# for the updater binary, use the possibly substituted
|
|
|
|
# version in the output directory
|
|
|
|
path = "#{output_dir}/#{@config.updater_binary}"
|
|
|
|
end
|
|
|
|
|
2011-08-27 15:09:11 +00:00
|
|
|
file.is_main_binary = (file.path == package_config.main_binary)
|
2011-08-25 17:50:03 +00:00
|
|
|
|
|
|
|
if (File.symlink?(path))
|
|
|
|
file.target = File.readlink(path)
|
|
|
|
else
|
|
|
|
file.hash = file_sha1(path)
|
2011-09-04 13:53:51 +00:00
|
|
|
file.permissions = File.stat(path).mode
|
2011-08-25 17:50:03 +00:00
|
|
|
file.size = File.size(path)
|
2011-09-01 16:56:36 +00:00
|
|
|
file.package = @config.package_for_file(file.path)
|
|
|
|
|
|
|
|
if (!file.package)
|
|
|
|
raise "Could not find package for file #{file.path}"
|
2011-08-25 17:50:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@files_to_install << file
|
|
|
|
end
|
|
|
|
|
|
|
|
# List of packages containing files for this version
|
|
|
|
@packages = []
|
|
|
|
package_file_map.each do |package_name,files|
|
2011-09-01 16:37:50 +00:00
|
|
|
if (package_name == @config.updater_binary)
|
|
|
|
path = "#{output_dir}/#{package_name}"
|
|
|
|
else
|
|
|
|
path = "#{output_dir}/#{package_name}.zip"
|
|
|
|
end
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
package = UpdateScriptPackage.new
|
|
|
|
package.name = package_name
|
|
|
|
package.size = File.size(path)
|
|
|
|
package.hash = file_sha1(path)
|
|
|
|
@packages << package
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-27 15:09:11 +00:00
|
|
|
def to_xml()
|
2011-08-25 17:50:03 +00:00
|
|
|
doc = REXML::Document.new
|
|
|
|
update_elem = REXML::Element.new("update")
|
|
|
|
doc.add_element update_elem
|
|
|
|
|
2011-09-01 11:47:10 +00:00
|
|
|
version_elem = REXML::Element.new("targetVersion")
|
|
|
|
version_elem.text = @target_version
|
|
|
|
|
|
|
|
platform_elem = REXML::Element.new("platform")
|
|
|
|
platform_elem.text = @platform
|
|
|
|
|
2011-09-01 14:56:26 +00:00
|
|
|
update_elem.add_attribute("version","3")
|
2011-09-01 11:47:10 +00:00
|
|
|
update_elem.add_element version_elem
|
|
|
|
update_elem.add_element platform_elem
|
2011-08-25 17:50:03 +00:00
|
|
|
update_elem.add_element deps_to_xml()
|
|
|
|
update_elem.add_element packages_to_xml()
|
|
|
|
update_elem.add_element install_to_xml()
|
|
|
|
|
|
|
|
output = ""
|
|
|
|
doc.write output
|
|
|
|
return output
|
|
|
|
end
|
|
|
|
|
|
|
|
def deps_to_xml()
|
|
|
|
deps_elem = REXML::Element.new("dependencies")
|
2011-08-27 15:09:11 +00:00
|
|
|
deps = @config.updater_binary
|
2011-08-25 17:50:03 +00:00
|
|
|
deps.each do |dependency|
|
|
|
|
dep_elem = REXML::Element.new("file")
|
|
|
|
dep_elem.text = dependency
|
|
|
|
deps_elem.add_element dep_elem
|
|
|
|
end
|
|
|
|
return deps_elem
|
|
|
|
end
|
|
|
|
|
|
|
|
def packages_to_xml()
|
|
|
|
packages_elem = REXML::Element.new("packages")
|
|
|
|
@packages.each do |package|
|
|
|
|
package_elem = REXML::Element.new("package")
|
|
|
|
packages_elem.add_element package_elem
|
|
|
|
hash_to_xml(package_elem,{
|
|
|
|
"name" => package.name,
|
|
|
|
"size" => package.size.to_s,
|
|
|
|
"hash" => package.hash
|
|
|
|
})
|
|
|
|
end
|
|
|
|
return packages_elem
|
|
|
|
end
|
|
|
|
|
|
|
|
def install_to_xml()
|
|
|
|
install_elem = REXML::Element.new("install")
|
|
|
|
@files_to_install.each do |file|
|
|
|
|
file_elem = REXML::Element.new("file")
|
|
|
|
install_elem.add_element(file_elem)
|
|
|
|
|
|
|
|
attributes = {"name" => file.path}
|
|
|
|
if (file.target)
|
|
|
|
attributes["target"] = file.target
|
|
|
|
else
|
|
|
|
attributes["size"] = file.size.to_s
|
2011-09-04 13:53:51 +00:00
|
|
|
attributes["permissions"] = file_mode_string(file.permissions)
|
2011-08-25 17:50:03 +00:00
|
|
|
attributes["hash"] = file.hash
|
|
|
|
attributes["package"] = file.package
|
|
|
|
end
|
2011-08-27 15:09:11 +00:00
|
|
|
|
|
|
|
if (file.is_main_binary)
|
|
|
|
attributes["is-main-binary"] = "true"
|
|
|
|
end
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
hash_to_xml(file_elem,attributes)
|
|
|
|
end
|
|
|
|
return install_elem
|
|
|
|
end
|
|
|
|
|
2011-09-04 13:53:51 +00:00
|
|
|
def file_mode_string(mode)
|
|
|
|
mode_octal = (mode & 0777).to_s(8)
|
|
|
|
return "0#{mode_octal}"
|
2011-08-25 17:50:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-27 15:09:11 +00:00
|
|
|
class PackageConfig
|
|
|
|
attr_reader :main_binary, :updater_binary
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
def initialize(map_file)
|
|
|
|
@rule_map = {}
|
2011-08-27 15:09:11 +00:00
|
|
|
config_json = JSON.parse(File.read(map_file))
|
|
|
|
config_json["packages"].each do |package,rules|
|
2011-08-25 17:50:03 +00:00
|
|
|
rules.each do |rule|
|
2011-09-01 16:37:50 +00:00
|
|
|
rule_regex = Regexp.new("^#{rule}")
|
2011-08-25 17:50:03 +00:00
|
|
|
@rule_map[rule_regex] = package
|
|
|
|
end
|
|
|
|
end
|
2011-08-27 15:09:11 +00:00
|
|
|
|
|
|
|
@main_binary = config_json["main-binary"]
|
|
|
|
@updater_binary = config_json["updater-binary"]
|
2011-08-25 17:50:03 +00:00
|
|
|
end
|
|
|
|
|
2011-09-01 11:47:10 +00:00
|
|
|
def is_updater(file)
|
2011-09-01 16:37:50 +00:00
|
|
|
return File.basename(file) == @updater_binary
|
2011-09-01 11:47:10 +00:00
|
|
|
end
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
def package_for_file(file)
|
2011-09-01 14:56:26 +00:00
|
|
|
match = nil
|
2011-08-25 17:50:03 +00:00
|
|
|
@rule_map.each do |rule,package|
|
|
|
|
if (file =~ rule)
|
2011-09-01 14:56:26 +00:00
|
|
|
if match && match != package
|
|
|
|
raise "Multiple packages match file #{file} - found '#{match}' and '#{package}'"
|
|
|
|
end
|
|
|
|
match = package
|
2011-08-25 17:50:03 +00:00
|
|
|
end
|
|
|
|
end
|
2011-09-01 14:56:26 +00:00
|
|
|
return match
|
2011-08-25 17:50:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-01 11:47:10 +00:00
|
|
|
updater_binary_input_path = nil
|
|
|
|
target_version = nil
|
|
|
|
target_platform = nil
|
2011-09-05 15:33:28 +00:00
|
|
|
zip_flags = nil
|
2011-09-01 11:47:10 +00:00
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
OptionParser.new do |parser|
|
2011-09-04 11:27:47 +00:00
|
|
|
parser.banner = "#{$0} [options] <input dir> <config file> <output dir>"
|
2011-09-01 11:47:10 +00:00
|
|
|
parser.on("-u","--updater [updater binary]","Specifies the updater binary to use") do |updater|
|
|
|
|
updater_binary_input_path = updater
|
|
|
|
end
|
|
|
|
parser.on("-v","--version [version]","Specifies the target version string for this update") do |version|
|
|
|
|
target_version = version
|
|
|
|
end
|
|
|
|
parser.on("-p","--platform [platform]","Specifies the target platform for this update") do |platform|
|
|
|
|
target_platform = platform
|
|
|
|
end
|
2011-09-05 15:33:28 +00:00
|
|
|
parser.on(nil,"--bzip2","Use bzip2 compression (requires that 'zip' supports the -Z bz2 argument)") do
|
|
|
|
zip_flags = "-Z bzip2"
|
|
|
|
end
|
2011-08-25 17:50:03 +00:00
|
|
|
end.parse!
|
|
|
|
|
2011-09-01 11:47:10 +00:00
|
|
|
raise "Platform not specified (use -p option)" if !target_platform
|
|
|
|
raise "Target version not specified (use -v option)" if !target_version
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
if ARGV.length < 3
|
|
|
|
raise "Missing arguments"
|
|
|
|
end
|
|
|
|
|
|
|
|
input_dir = ARGV[0]
|
|
|
|
package_map_file = ARGV[1]
|
|
|
|
output_dir = ARGV[2]
|
|
|
|
|
2011-08-26 18:02:01 +00:00
|
|
|
FileUtils.mkpath(output_dir)
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
# get the details of each input file
|
|
|
|
input_file_list = []
|
|
|
|
Find.find(input_dir) do |path|
|
2011-09-01 16:42:46 +00:00
|
|
|
next if (File.directory?(path) && !File.symlink?(path))
|
2011-08-25 17:50:03 +00:00
|
|
|
input_file_list << path
|
|
|
|
end
|
|
|
|
|
|
|
|
# map each input file to a corresponding package
|
|
|
|
|
|
|
|
# read the package map
|
2011-08-27 15:09:11 +00:00
|
|
|
package_config = PackageConfig.new(package_map_file)
|
2011-08-25 17:50:03 +00:00
|
|
|
|
|
|
|
# map of package name -> array of files
|
|
|
|
package_file_map = {}
|
2011-09-01 11:47:10 +00:00
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
input_file_list.each do |file|
|
|
|
|
next if File.symlink?(file)
|
|
|
|
|
2011-09-01 16:37:50 +00:00
|
|
|
relative_file = strip_prefix(file,input_dir)
|
|
|
|
package = package_config.package_for_file(relative_file)
|
2011-08-25 17:50:03 +00:00
|
|
|
if (!package)
|
|
|
|
raise "Unable to find package for file #{file}"
|
|
|
|
end
|
|
|
|
package_file_map[package] = [] if !package_file_map[package]
|
|
|
|
package_file_map[package] << file
|
|
|
|
end
|
|
|
|
|
|
|
|
# generate each package
|
|
|
|
package_file_map.each do |package,files|
|
|
|
|
puts "Generating package #{package}"
|
|
|
|
|
|
|
|
quoted_files = []
|
|
|
|
files.each do |file|
|
2011-09-01 16:37:50 +00:00
|
|
|
# do not package the updater binary into a zip file -
|
|
|
|
# it must be downloaded uncompressed
|
|
|
|
if package_config.is_updater(file)
|
|
|
|
next
|
|
|
|
end
|
2011-08-25 17:50:03 +00:00
|
|
|
quoted_files << "\"#{strip_prefix(file,input_dir)}\""
|
|
|
|
end
|
2011-09-01 14:56:26 +00:00
|
|
|
|
|
|
|
if (quoted_files.empty?)
|
|
|
|
puts "Skipping generation of empty package #{package}"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
# sort the files in alphabetical order to ensure
|
|
|
|
# that if the input files for a package have the same
|
|
|
|
# name and content, the resulting package will have the
|
|
|
|
# same SHA-1
|
|
|
|
#
|
|
|
|
# This means that repeated runs of the package creation tool
|
|
|
|
# on the same set of files should generated packages with the
|
|
|
|
# same SHA-1
|
|
|
|
quoted_files.sort! do |a,b|
|
|
|
|
a <=> b
|
|
|
|
end
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
quoted_file_list = quoted_files.join(" ")
|
|
|
|
|
|
|
|
output_path = File.expand_path(output_dir)
|
2011-08-27 15:09:11 +00:00
|
|
|
output_file = "#{output_path}/#{package}.zip"
|
|
|
|
|
|
|
|
File.unlink(output_file) if File.exist?(output_file)
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
Dir.chdir(input_dir) do
|
2011-09-05 15:33:28 +00:00
|
|
|
if (!system("zip #{zip_flags} #{output_file} #{quoted_file_list}"))
|
2011-08-26 17:55:06 +00:00
|
|
|
raise "Failed to generate package #{package}"
|
2011-09-01 14:56:26 +00:00
|
|
|
else
|
|
|
|
puts "Generated package #{package} : #{file_sha1(output_file)}"
|
2011-08-26 17:55:06 +00:00
|
|
|
end
|
2011-08-25 17:50:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-01 11:47:10 +00:00
|
|
|
# copy the updater to the output directory
|
|
|
|
if !updater_binary_input_path
|
|
|
|
puts "Updater binary not found in input directory: #{input_dir}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.cp(updater_binary_input_path,"#{output_dir}/#{File.basename(updater_binary_input_path)}")
|
|
|
|
|
2011-08-25 17:50:03 +00:00
|
|
|
# output the file_list.xml file
|
2011-09-01 11:47:10 +00:00
|
|
|
update_script = UpdateScriptGenerator.new(target_version,target_platform,input_dir,
|
|
|
|
output_dir,package_config,input_file_list,package_file_map)
|
2011-08-25 17:50:03 +00:00
|
|
|
output_xml_file = "#{output_dir}/file_list.unformatted.xml"
|
|
|
|
File.open(output_xml_file,'w') do |file|
|
2011-08-27 15:09:11 +00:00
|
|
|
file.write update_script.to_xml()
|
2011-08-25 17:50:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# xmllint generates more readable formatted XML than REXML, so write unformatted
|
|
|
|
# XML first and then format it with xmllint.
|
|
|
|
system("xmllint --format #{output_xml_file} > #{output_dir}/file_list.xml")
|
|
|
|
File.delete(output_xml_file)
|
|
|
|
|
|
|
|
|