Created
May 16, 2014 18:10
-
-
Save rmacqueen/c679aac8025838f7b9d7 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
### Helpers ### | |
# Returns true if file exists AND file | |
# is directory | |
def directory_exists?(directory) | |
File.directory?(directory) | |
end | |
# Returns true if file exists | |
def file_exists?(filename) | |
File.file?(filename) | |
end | |
# Makes a directory, and catches error | |
# if operation failed. Useful in cases where directory | |
# already exists when we try to create it | |
def safe_mkdir(dirname) | |
begin | |
Dir.mkdir(dirname) | |
rescue SystemCallError | |
$stderr.print "Making directory failed! " + $!.to_s | |
return false | |
end | |
end | |
def zip_directory(dirname, zipname) | |
command = ['zip', '-q', '-r', zipname, dirname] | |
system *command | |
end | |
# Unzips directory to specified location. | |
# If no location is specified, will default to current directory | |
def unzip_directory(zipname, location='.') | |
command = ['unzip', '-q', zipname, '-d', location] | |
system *command | |
end | |
def dir_delta_zipped(old_dir_zip, new_dir_zip, patch_name) | |
puts "Generating delta patch..." | |
command = ['xdelta3', '-S djw', '-e', '-9', '-q'] | |
command += ['-s', old_dir_zip, new_dir_zip, patch_name] | |
puts command.join(' ') | |
system *command | |
end | |
def dir_patch_zipped(patch_name, old_dir_zip, patched_dir_zip) | |
puts "Applying patch..." | |
command = ['xdelta3', '-d', '-q'] | |
command += ['-s', old_dir_zip, patch_name, patched_dir_zip] | |
puts command.join(' ') | |
system *command | |
end | |
# Computes recursive patch for two directories | |
# | |
# Parameters: | |
# => old_dir: name of old version of directory | |
# => new_dir: name of new version of directory | |
# => patch_dir: name of directory representing the patch from old to new. | |
# | |
# The patch_dir will be populated with .xdelta files representing diffs | |
# from old_dir to new_dir | |
# | |
# Returns: True on success, False on failure | |
def dir_delta(old_dir, new_dir, patch_dir) | |
puts "Generating delta patch..." | |
if not safe_mkdir(patch_dir) | |
return false | |
end | |
# If we need to create a directory for old, and that creation fails | |
# return an error | |
if not directory_exists?(old_dir) and not safe_mkdir(old_dir) | |
return false | |
end | |
# Get list of all files except '.' and '..' | |
old_files = Dir.entries(old_dir) - ['.', '..'] | |
new_files = Dir.entries(new_dir) - ['.', '..'] | |
# Get list of files which have been deleted in the new directory | |
deleted_files = old_files - new_files | |
for filename in new_files | |
old_file = File.join(old_dir, filename) | |
new_file = File.join(new_dir, filename) | |
patch_file = File.join(patch_dir, filename) | |
# If it is a directory, recurse the delta function on it | |
if directory_exists?(new_file) | |
dir_delta(old_file, new_file, patch_file) | |
next | |
elsif old_files.include? filename | |
puts "Modified file: " + new_file | |
else | |
puts "New file: " + new_file | |
end | |
patch_file += ".xdelta" | |
# Set up xdelta3 command to execute | |
command = ['xdelta3', '-S djw', '-e', '-9', '-q'] | |
if old_files.include? filename | |
command << '-s' | |
command << old_file | |
end | |
command << new_file | |
command << patch_file | |
# Execute command | |
system *command | |
end | |
end | |
# Applies recursive patch to directory | |
# | |
# Parameters: | |
# => patch_dir: name of directory representing the patch from old to new. | |
# => old_dir: name of old version of directory | |
# => new_dir: name of new directory to be created by applying patch_dir to old_dir | |
# | |
# The patch_dir will be applied to old_dir, and the result will be placed in | |
# new_dir | |
# | |
# Returns: True on success, False on failure | |
def dir_patch(patch_dir, old_dir, new_dir) | |
puts "Patching directory..." | |
if not safe_mkdir(new_dir) | |
return false | |
end | |
# Get list of patch files (excluding '.' and '..') | |
patch_files = Dir.entries(patch_dir) - ['.', '..'] | |
for filename in patch_files | |
old_file = File.join(old_dir, filename) | |
new_file = File.join(new_dir, filename) | |
patch_file = File.join(patch_dir, filename) | |
# If patch file is a directory, recurse on it | |
if directory_exists?(patch_file) | |
dir_patch(patch_file, old_file, new_file) | |
next | |
elsif filename.end_with? '.xdelta' | |
# Remove '.xdelta' from the file name of old_file | |
# and new_file | |
old_file = old_file.gsub(/.xdelta$/, '') | |
new_file = new_file.gsub(/.xdelta$/, '') | |
# Setup xdelta3 command | |
command = ['xdelta3', '-d', '-q'] | |
if file_exists?(old_file) | |
command << '-s' | |
command << old_file | |
end | |
command << patch_file | |
command << new_file | |
# Execute command | |
system *command | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment