2 # archive_analysis archives a set of analysis files
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
6 # Copyright 2013 by Don Armstrong <don@donarmstrong.com>.
17 archive_analysis - archives a set of analysis files
21 archive_analysis [options]
24 --debug, -d debugging level (Default 0)
25 --help, -h display this help
26 --man, -m display manual
34 Debug verbosity. (Default 0)
38 Display brief usage information.
59 my %options = (debug => 0,
65 'archive_dir|archive-dir=s@',
66 'git_annex|git-annex!',
67 'debug|d+','help|h|?','man|m');
69 pod2usage() if $options{help};
70 pod2usage({verbose=>2}) if $options{man};
72 $DEBUG = $options{debug};
74 # maybe eventually use Config::IniFiles or similar
76 if (not defined $options{archive_dir}) {
77 push @USAGE_ERRORS, "You must pass an --archive-dir";
81 push @USAGE_ERRORS,"You must give files to archive";
84 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
87 for my $file (@ARGV) {
89 fail("file $file does not exist or is not readable.\n");
91 # figure out which VCS is in use
92 my $vcs = determine_vcs($file);
93 if (not defined $vcs) {
94 fail("Unable to determine which VCS this is");
96 my $revision = find_vcs_revision($file,$vcs);
97 if (not defined $revision) {
98 fail("Unable to determine which revision this is");
100 my $new_file = $file;
101 $new_file =~ s/(\.[^\.\/]{1,5}|)$/_$revision$1/;
102 my ($v,$d,$nf) = File::Spec->splitpath($new_file);
103 for my $dir (make_list($options{archive_dir})) {
104 my $loc = File::Spec->join($dir,$nf);
107 fail("Unable to copy file $file to $loc: $!\n");
108 if ($options{git_annex}) {
117 my $abs_path = File::Spec->rel2abs($file);
118 my ($v,$d,$nf) = File::Spec->splitpath($abs_path);
119 my $old_dir = getcwd();
121 system('git','annex','add',$nf);
128 my $abs_path = File::Spec->rel2abs($file);
129 my @dirs = File::Spec->splitdir($abs_path);
130 for my $i (reverse 0..$#dirs) {
131 my $dir = File::Spec->catdir(@dirs[0..$i]);
132 for my $vcs (qw(git svn bzr)) {
133 if ( -e File::Spec->catdir($dir,'.'.$vcs)) {
141 sub find_vcs_revision{
142 my ($file,$vcs) = @_;
143 if (not defined $vcs) {
144 $vcs = determine_vcs($file);
148 my $old_dir = getcwd();
149 my $abs_path = File::Spec->rel2abs($file);
150 my ($v,$d,$nf) = File::Spec->splitpath($abs_path);
152 my $branch = qx(git name-rev --name-only HEAD);
154 my $rev = qx(git rev-parse --short HEAD);
157 return $branch.'@'.$rev;
159 fail("vcs $vcs not currently supported");
171 return map {(ref($_) eq 'ARRAY')?@{$_}:$_} @_;