]> git.donarmstrong.com Git - bin.git/blob - archive_analysis
add reset usb bus command
[bin.git] / archive_analysis
1 #!/usr/bin/perl
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
5 # more information.
6 # Copyright 2013 by Don Armstrong <don@donarmstrong.com>.
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 archive_analysis - archives a set of analysis files
18
19 =head1 SYNOPSIS
20
21 archive_analysis [options]
22
23  Options:
24   --debug, -d debugging level (Default 0)
25   --help, -h display this help
26   --man, -m display manual
27
28 =head1 OPTIONS
29
30 =over
31
32 =item B<--debug, -d>
33
34 Debug verbosity. (Default 0)
35
36 =item B<--help, -h>
37
38 Display brief usage information.
39
40 =item B<--man, -m>
41
42 Display this manual.
43
44 =back
45
46 =head1 EXAMPLES
47
48 archive_analysis
49
50 =cut
51
52
53 use File::Spec;
54 use File::Copy;
55 use Cwd;
56
57 use vars qw($DEBUG);
58
59 my %options = (debug           => 0,
60                help            => 0,
61                man             => 0,
62                );
63
64 GetOptions(\%options,
65            'archive_dir|archive-dir=s@',
66            'git_annex|git-annex!',
67            'debug|d+','help|h|?','man|m');
68
69 pod2usage() if $options{help};
70 pod2usage({verbose=>2}) if $options{man};
71
72 $DEBUG = $options{debug};
73
74 # maybe eventually use Config::IniFiles or similar
75 my @USAGE_ERRORS;
76 if (not defined $options{archive_dir}) {
77     push @USAGE_ERRORS, "You must pass an --archive-dir";
78 }
79
80 if (not @ARGV) {
81      push @USAGE_ERRORS,"You must give files to archive";
82 }
83
84 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
85
86
87 for my $file (@ARGV) {
88     if (not -r $file) {
89         fail("file $file does not exist or is not readable.\n");
90     }
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");
95     }
96     my $revision = find_vcs_revision($file,$vcs);
97     if (not defined $revision) {
98         fail("Unable to determine which revision this is");
99     }
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);
105         next if -e $loc;
106         copy($file,$loc) or
107             fail("Unable to copy file $file to $loc: $!\n");
108         if ($options{git_annex}) {
109             git_annex($loc);
110         }
111     }
112 }
113
114 sub git_annex{
115     my ($file) = @_;
116
117     my $abs_path = File::Spec->rel2abs($file);
118     my ($v,$d,$nf) = File::Spec->splitpath($abs_path);
119     my $old_dir = getcwd();
120     chdir($d);
121     system('git','annex','add',$nf);
122     chdir($old_dir);
123 }
124
125 sub determine_vcs {
126     my ($file) = @_;
127
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)) {
134                 return $vcs;
135             }
136         }
137     }
138     return undef;
139 }
140
141 sub find_vcs_revision{
142     my ($file,$vcs) = @_;
143     if (not defined $vcs) {
144         $vcs = determine_vcs($file);
145     }
146
147     if ($vcs eq 'git') {
148         my $old_dir = getcwd();
149         my $abs_path = File::Spec->rel2abs($file);
150         my ($v,$d,$nf) = File::Spec->splitpath($abs_path);
151         chdir($d);
152         my $branch = qx(git name-rev --name-only HEAD);
153         chomp $branch;
154         my $rev = qx(git rev-parse --short HEAD);
155         chomp $rev;
156         chdir($old_dir);
157         return $branch.'@'.$rev;
158     } else {
159         fail("vcs $vcs not currently supported");
160     }
161 }
162
163
164
165 sub fail {
166     print STDERR @_;
167     exit 1;
168 }
169
170 sub make_list {
171      return map {(ref($_) eq 'ARRAY')?@{$_}:$_} @_;
172 }
173
174
175 __END__