Revision e0fad0ea05aadfd05d68bee304b29be2c1130bee authored by apregier on 14 January 2013, 19:37:07 UTC, committed by apregier on 14 January 2013, 19:37:07 UTC
1 parent 27e1863
Raw File
hotfix
#!/usr/bin/env perl

require IO::File;
require Getopt::Long;

BEGIN {
    require Cwd;
	require File::Basename;
    my $lib_dir = Cwd::abs_path(File::Basename::dirname(__FILE__) . '/../lib/');
    unless (grep { $lib_dir eq Cwd::abs_path($_) } @INC) {
        push @INC, $lib_dir;
    }
}

require Defaults;
require Snapshot;

my $help = '';
unless(Getopt::Long::GetOptions('help' => \$help)) {
    exit;
}

if ( @ARGV < 3 || $help) {
	print "Usage: $0 pipeline ~/git/genome sdf123a 9843aac ~/git/ur 5s2gg2a\n";
	exit;
}

unless (getpwuid($<) eq 'apipe-tester') {
    print "ERROR: please create hotfix snapshots as apipe-tester.\n";
    exit;
}


my $version = shift @ARGV;
my $old_path;
if ( $version =~ /pipeline/ ) {
    $old_path = readlink(Defaults::CURRENT_PIPELINE());
} elsif ( $version =~ /user/ ) {
	$old_path = readlink(Defaults::CURRENT_USER());
} elsif ( $version =~ /web/ ) {
	$old_path = readlink(Defaults::CURRENT_WEB());
} else {
    $old_path = Snapshot::find_snapshot($version);
}
my $old_name = $old_path;
$old_name =~ s/\/+$//;
$old_name =~ s/.*\///;


my $git_repo = shift @ARGV;
unless (-d $git_repo) {
    print "ERROR: please specify a git repo as second argument. '$git_repo' is not a directory.\n";
    exit;
}
unless (@ARGV) {
    print "ERROR: please specify git hashes as third+ arguments\n";
    exit;
}


my $custom_path = Defaults::CUSTOM_PATH();
my $new_path = "$custom_path/$old_name";
if ( $old_name =~ /genome-\d+$/ ) {
    $new_path .= '-fix1';
}
elsif ($old_name =~ /genome-\d+-fix(\d+)/) {
    my $old_fix = $1;
    my $new_fix = $old_fix + 1;
    $new_path =~ s/\d+$/$new_fix/;
}

unless ( -d $old_path ) {
    print "ERROR: old_path does not exists: $old_path\n";
    exit;
}

if ( -d $new_path ) {
    print "ERROR: new_path already exists: $new_path\n";
    exit;
}

print "Old: $old_path\nNew: $new_path\n";

print "Copying $old_path to $new_path...\n";
unless(system("cp -a $old_path $new_path") == 0) {
    print "ERROR: Failed to 'cp -a $old_path $new_path'\n";
    print "You may want to remove $new_path.\n";
    exit;
}

print "\n\nWhat was the reason for this hotfix? (make this readable by public)\n";
my $hotfix_reason;
chomp($hotfix_reason = <STDIN>);
my $hotfixes_fh = IO::File->new(">>$new_path/hotfixes");
my $fix_version = $new_path; $fix_version =~ s/.*genome-\d+-fix//;
print $hotfixes_fh "Fix $fix_version: $hotfix_reason\n";

for my $argv (@ARGV) {
    my $git_hash;

    if ( -d $argv ) {
        $git_repo = $argv;
        next;
    }
    else {
        $git_hash = $argv;
    }

    my $git_repo_name;
    my $git_remote = `cd $git_repo && git remote -v | grep origin | head -n 1`;
    if ($git_remote) {
        chomp $git_remote;
        my @git_remote = split('/', $git_remote);
        $git_repo_name = $git_remote[-1];
        $git_repo_name =~ s/\ \(.*\)//;
        $git_repo_name =~ s/\.git//;
    }
    else {
        $git_repo_name = 'unknown';
    }

    my @git_data = `cd $git_repo && git show --oneline $git_hash`;
	my $hotfix_msg = `cd $git_repo && git show --oneline --name-only $git_hash`;
    my $git_msg = shift @git_data;
    print "Generating patch for $git_msg and applying to $new_path...\n";

    my @git_patch = grep { $_ !~ /^diff\ --git/ } @git_data;
    @git_patch = grep { $_ !~ /index\ [a-z0-9]+\.\.[a-z0-9]+/ } @git_patch;

	my $patch_file = "$new_path/$git_hash.patch";
    my $patch_fh = IO::File->new(">$patch_file");
    print $patch_fh join('', @git_patch);

    my $hotfixes_fh = IO::File->new(">>$new_path/hotfixes");
    my $fix_version = $new_path; $fix_version =~ s/.*genome-\d+-fix//;
    print $hotfixes_fh "fix$fix_version: $git_repo_name $hotfix_msg\n";

	# fix patch path since we move UR and Workflow's lib -> lib/perl
	if ( $git_repo_name !~ /genome/ ) {
        system("sed -r -e 's/(---|\\+\\+\\+)\\ (a|b)\\/lib\\//\\1\\ \\2\\/lib\\/perl\\//' -i $patch_file");
	}

    unless(system("patch -p 1 -d $new_path -i $patch_file") == 0) {
        print "ERROR: Failed to patch $new_path!\n";
        print "You may want to remove $new_path.\n";
        exit;
    }
}

print "Successfully created hotfix at $new_path\n";
back to top