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.
58 my %options = (debug => 0,
64 'archive_dir|archive-dir=s@',
65 'git_annex|git-annex!',
66 'debug|d+','help|h|?','man|m');
68 pod2usage() if $options{help};
69 pod2usage({verbose=>2}) if $options{man};
71 $DEBUG = $options{debug};
73 # maybe eventually use Config::IniFiles or similar
75 if (not defined $options{archive_dir}) {
76 push @USAGE_ERRORS, "You must pass an --archive-dir";
80 push @USAGE_ERRORS,"You must give files to archive";
83 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
86 for my $file (@ARGV) {
88 fail("file $file does not exist or is not readable.\n");
90 # figure out which VCS is in use
91 my $vcs = determine_vcs($file);
92 if (not defined $vcs) {
93 fail("Unable to determine which VCS this is");
95 my $revision = find_vcs_revision($vcs,$file);
96 if (not defined $revision) {
97 fail("Unable to determine which revision this is");
100 $new_file =~ s{(\.[^\./]{,5}|)$}{_$revision$1};
101 my ($v,$d,$nf) = File::Spec->splitfile($new_file);
102 for my $dir (make_list($options{archive_dir})) {
103 my $loc = File::Spec->join($dir,$nf);
105 fail("Unable to copy file $file to $loc: $!\n");
112 my $abs_path = File::Spec->rel2abs($file);
113 my @dirs = File::Spec->splitdir($abs_path);
114 for my $i ($#dirs..0) {
115 my $dir = File::Spec->catdir(@dirs[$i..0]);
116 for my $vcs (qw(git svn bzr)) {
117 if ( -e File::Spec->catdir($dir,'.'.$vcs)) {
125 sub find_vcs_revision{
126 my ($vcs,$file) = @_;
129 my $old_dir = getcwd();
130 my $abs_path = File::Spec->rel2abs($file);
131 my ($v,$d,$nf) = File::Spec->splitfile($abs_path);
133 my $branch = qx(git name-rev --name-only HEAD);
134 my $rev = qx(git rev-parse --short HEAD);
135 return $branch.'@'.$rev;
137 fail("vcs $vcs not currently supported");