| 1 | #!/usr/bin/env perl | ||||||||||
| 2 | # vim:ts=8 sw=4 sts=4 ai | ||||||||||
| 3 | 1 | require v5.6.1; | |||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | hash2fv - convert a hash db file into Tie::FieldVals data. | ||||||||||
| 10 | |||||||||||
| 11 | =head1 VERSION | ||||||||||
| 12 | |||||||||||
| 13 | This describes version B<0.6203> of hash2fv. | ||||||||||
| 14 | |||||||||||
| 15 | =cut | ||||||||||
| 16 | |||||||||||
| 17 | 1 | our $VERSION = '0.6203'; | |||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | hash2fv --help | --manpage | --version | ||||||||||
| 22 | |||||||||||
| 23 | hash2fv {--fields I<fieldname> } I<hashdbfile> [ I<outfile> ] | ||||||||||
| 24 | |||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||
| 26 | |||||||||||
| 27 | This script converts a hash db file (of the type used by | ||||||||||
| 28 | AutomatedArchive 3.x) into Tie::FieldVals data. | ||||||||||
| 29 | |||||||||||
| 30 | This requires the file name of the hash db file, not just the location | ||||||||||
| 31 | of the AutomatedArchive directory, so as to make this more flexible in | ||||||||||
| 32 | the files it is able to read. While the AutomatedArchive file is usually | ||||||||||
| 33 | "I<archive_dir>/cgi-bin/files/ARCHIVE_DB.pl", someone converting it | ||||||||||
| 34 | may wish to move it to a different location, or download it from | ||||||||||
| 35 | their website, or may wish to use this script for converting files | ||||||||||
| 36 | of the same format which aren't actually AutomatedArchive files. | ||||||||||
| 37 | |||||||||||
| 38 | =head1 OPTIONS | ||||||||||
| 39 | |||||||||||
| 40 | =over | ||||||||||
| 41 | |||||||||||
| 42 | =item --fields I<fieldname> | ||||||||||
| 43 | |||||||||||
| 44 | If you want to define the order of the Fields in the output file, | ||||||||||
| 45 | or simply define the legal fields you are interested in, then | ||||||||||
| 46 | use this option to override the default of getting the Field names | ||||||||||
| 47 | from the first record in the the Hash DB file. | ||||||||||
| 48 | |||||||||||
| 49 | Repeat the --fields option for each field name. Be sure to give | ||||||||||
| 50 | every field you want, because this I<replaces> the field definitions | ||||||||||
| 51 | from the Hash DB file. | ||||||||||
| 52 | |||||||||||
| 53 | --fields Author --fields Category --fields Title --fields Location ... | ||||||||||
| 54 | |||||||||||
| 55 | =item --help | ||||||||||
| 56 | |||||||||||
| 57 | Print help message and exit. | ||||||||||
| 58 | |||||||||||
| 59 | =item --manpage | ||||||||||
| 60 | |||||||||||
| 61 | Print the full help documentation (manual page) and exit. | ||||||||||
| 62 | |||||||||||
| 63 | =item --verbose | ||||||||||
| 64 | |||||||||||
| 65 | Print informational messages. | ||||||||||
| 66 | |||||||||||
| 67 | =item --version | ||||||||||
| 68 | |||||||||||
| 69 | Print version information and exit. | ||||||||||
| 70 | |||||||||||
| 71 | =back | ||||||||||
| 72 | |||||||||||
| 73 | =head1 FILE FORMATS | ||||||||||
| 74 | |||||||||||
| 75 | =head2 Hash DB File Format | ||||||||||
| 76 | |||||||||||
| 77 | The format of the input hash DB file is as follows: | ||||||||||
| 78 | |||||||||||
| 79 | %FILES = ( | ||||||||||
| 80 | 1 => { | ||||||||||
| 81 | Field => 'Value', | ||||||||||
| 82 | AnotherField => 'AnotherValue', | ||||||||||
| 83 | ... | ||||||||||
| 84 | }, | ||||||||||
| 85 | 2 => { | ||||||||||
| 86 | ... | ||||||||||
| 87 | }, | ||||||||||
| 88 | ... | ||||||||||
| 89 | ); | ||||||||||
| 90 | 1; | ||||||||||
| 91 | |||||||||||
| 92 | This is the format used by the AutomatedArchive suite version 3.x. | ||||||||||
| 93 | This may be the format used by version 2.x also. | ||||||||||
| 94 | |||||||||||
| 95 | This converter ignores the values of the '1', '2' (and so on) keys, | ||||||||||
| 96 | just converting the Fields and Values into records. | ||||||||||
| 97 | |||||||||||
| 98 | =head2 FieldVals Format | ||||||||||
| 99 | |||||||||||
| 100 | The output data file is in the form of Field:Value pairs, with each | ||||||||||
| 101 | record separated by a line with '=' on it. | ||||||||||
| 102 | |||||||||||
| 103 | See L<Tie::FieldVals/FILE FORMAT> for more information. | ||||||||||
| 104 | |||||||||||
| 105 | =head1 REQUIRES | ||||||||||
| 106 | |||||||||||
| 107 | Getopt::Long | ||||||||||
| 108 | Pod::Usage | ||||||||||
| 109 | Getopt::ArgvFile | ||||||||||
| 110 | Data::Dumper | ||||||||||
| 111 | File::Basename | ||||||||||
| 112 | Tie::FieldVals::Row | ||||||||||
| 113 | |||||||||||
| 114 | =head1 SEE ALSO | ||||||||||
| 115 | |||||||||||
| 116 | perl(1) | ||||||||||
| 117 | Getopt::Long | ||||||||||
| 118 | Getopt::ArgvFile | ||||||||||
| 119 | Pod::Usage | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | 1 | ▶ | use Getopt::Long 2.34; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 124 | 1 | ▶ | use Getopt::ArgvFile qw(argvFile); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 125 | 1 | ▶ | use Pod::Usage; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 126 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 127 | 1 | ▶ | use File::Basename; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 128 | 1 | ▶ | use Tie::FieldVals::Row; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 129 | |||||||||||
| 130 | #======================================================== | ||||||||||
| 131 | # Subroutines | ||||||||||
| 132 | 1 | ▶ | sub init_data ($) { | ||||||||
|
Subroutine
init_data: called CC=1 SCAR=0.0
| |||||||||||
| 133 | 1 | my $data_ref = shift; | |||||||||
| 134 | |||||||||||
| 135 | # options | ||||||||||
| 136 | 1 | my %default_conf = (); | |||||||||
| 137 | 1 | $default_conf{debug} = 0; | |||||||||
| 138 | 1 | $default_conf{manpage} = 0; | |||||||||
| 139 | 1 | $default_conf{version} = 0; | |||||||||
| 140 | 1 | $default_conf{verbose} = 0; | |||||||||
| 141 | 1 | $default_conf{outfile} = '-'; | |||||||||
| 142 | 1 | $default_conf{hashdbfile} = ''; | |||||||||
| 143 | 1 | $default_conf{fields} = []; | |||||||||
| 144 | 1 | $data_ref->{options} = \%default_conf; | |||||||||
| 145 | } # init_data | ||||||||||
| 146 | |||||||||||
| 147 | 1 | ▶ | sub process_args ($) { | ||||||||
|
Subroutine
process_args: called CC=4 SCAR=14.6
| |||||||||||
| 148 | 1 | my $data_ref = shift; | |||||||||
| 149 | |||||||||||
| 150 | 1 | my $ok = 1; | |||||||||
| 151 | |||||||||||
| 152 | 1 | argvFile(home=>1,current=>1,startupFilename=>'.hash2fvrc'); | |||||||||
| 153 | |||||||||||
| 154 | 1 | ▶ | pod2usage(2) unless @ARGV; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | |||||||||||
| 156 | 1 | my $op = new Getopt::Long::Parser; | |||||||||
| 157 | 1 | $op->configure(qw(auto_version auto_help)); | |||||||||
| 158 | $op->getoptions($data_ref->{options}, | ||||||||||
| 159 | 1 | ▶ | 'verbose!', | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 'manpage', | ||||||||||
| 161 | 'debug!', | ||||||||||
| 162 | 'fields=s@', | ||||||||||
| 163 | 'hashdbfile=s', | ||||||||||
| 164 | 'outfile=s', | ||||||||||
| 165 | ) or pod2usage(2); | ||||||||||
| 166 | |||||||||||
| 167 | 1 | ▶ | if ($data_ref->{options}->{'manpage'}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | { | ||||||||||
| 169 | 0 | pod2usage({ -message => "$0 version $VERSION", | |||||||||
| 170 | -exitval => 0, | ||||||||||
| 171 | -verbose => 2, | ||||||||||
| 172 | }); | ||||||||||
| 173 | } | ||||||||||
| 174 | |||||||||||
| 175 | } # process_args | ||||||||||
| 176 | |||||||||||
| 177 | 1 | ▶ | sub convert_file ($) { | ||||||||
|
Subroutine
convert_file: called CC=15 SCAR=28.3
| |||||||||||
| 178 | 1 | my $data_ref = shift; | |||||||||
| 179 | |||||||||||
| 180 | 1 | my $outfile = $data_ref->{options}->{outfile}; | |||||||||
| 181 | |||||||||||
| 182 | print STDERR "hashdbfile: ", $data_ref->{options}->{hashdbfile}, | ||||||||||
| 183 | " outfile: ", $outfile, | ||||||||||
| 184 | 1 | ▶ | "\n" if ($data_ref->{options}->{verbose}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 185 | 1 | ▶ | if ($data_ref->{options}->{debug}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | { | ||||||||||
| 187 | 0 | print STDERR Data::Dumper->Dump([$data_ref], [qw(hash2fv)]); | |||||||||
| 188 | } | ||||||||||
| 189 | |||||||||||
| 190 | 1 | my $outhandle = \*STDOUT; | |||||||||
| 191 | 1 | ▶ | if ($outfile ne '-') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 192 | { | ||||||||||
| 193 | 1 | ▶ | open(OUTFILE, ">$outfile") || die "Can't open '$outfile' for writing: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | 1 | $outhandle = \*OUTFILE; | |||||||||
| 195 | } | ||||||||||
| 196 | # open the Hash DB file | ||||||||||
| 197 | # this will give a %FILES hash | ||||||||||
| 198 | 1 | our %FILES = (); | |||||||||
| 199 | 1 | my $hashdb = $data_ref->{options}->{hashdbfile}; | |||||||||
| 200 | 1 | my ($dbfile,$dir,$suffix) = fileparse($hashdb); | |||||||||
| 201 | 1 | unshift(@INC, $dir); | |||||||||
| 202 | 1 | ▶ | require $dbfile or die "Cannot open hash DB $hashdb: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | |||||||||||
| 204 | 1 | ▶ | if ($data_ref->{options}->{debug}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 205 | { | ||||||||||
| 206 | 0 | print STDERR "hashdb=$hashdb, dir=$dir, dbfile=$dbfile\n"; | |||||||||
| 207 | 0 | print STDERR Data::Dumper->Dump([\%FILES], [qw(FILES)]); | |||||||||
| 208 | } | ||||||||||
| 209 | # go through the $FILES hash | ||||||||||
| 210 | 1 | my $count = 0; | |||||||||
| 211 | 1 | my @field_names = @{$data_ref->{options}->{fields}}; | |||||||||
| 212 | 1 | ▶ | while (my ($key, $rec_ref) = each %FILES) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 213 | { | ||||||||||
| 214 | 2 | ▶ | warn "$key\n" if $data_ref->{options}->{debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 215 | 2 | ▶ | if (defined $rec_ref) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 216 | { | ||||||||||
| 217 | 2 | my %row = (); | |||||||||
| 218 | 2 | my $row_obj; | |||||||||
| 219 | 2 | ▶ | if (@field_names) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 220 | { | ||||||||||
| 221 | 1 | $row_obj = tie %row, 'Tie::FieldVals::Row', | |||||||||
| 222 | fields=>\@field_names; | ||||||||||
| 223 | 1 | $row_obj->set_from_hash($rec_ref); | |||||||||
| 224 | } | ||||||||||
| 225 | else # get the field names from the first hash | ||||||||||
| 226 | { | ||||||||||
| 227 | 1 | $row_obj = tie %row, 'Tie::FieldVals::Row', | |||||||||
| 228 | fields=>[qw(dummy)]; | ||||||||||
| 229 | 1 | $row_obj->set_from_hash($rec_ref, override_keys=>1); | |||||||||
| 230 | 1 | @field_names = @{$row_obj->field_names()}; | |||||||||
| 231 | # print the initial empty record | ||||||||||
| 232 | 1 | foreach my $fn (@field_names) | |||||||||
| 233 | { | ||||||||||
| 234 | 10 | print $outhandle "$fn:\n"; | |||||||||
| 235 | } | ||||||||||
| 236 | 1 | print $outhandle "=\n"; | |||||||||
| 237 | } | ||||||||||
| 238 | # print the current record | ||||||||||
| 239 | 2 | print $outhandle $row_obj->get_as_string(); | |||||||||
| 240 | 2 | print $outhandle "\n=\n"; | |||||||||
| 241 | 2 | $count++; | |||||||||
| 242 | } | ||||||||||
| 243 | } | ||||||||||
| 244 | |||||||||||
| 245 | 1 | ▶ | print STDERR "$count records processed\n" if ($data_ref->{options}->{verbose}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 246 | |||||||||||
| 247 | 1 | ▶ | if ($outfile ne '-') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 248 | { | ||||||||||
| 249 | 1 | close(OUTFILE); | |||||||||
| 250 | } | ||||||||||
| 251 | 1 | ▶ | print STDERR "done!\n" if ($data_ref->{options}->{verbose}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 252 | } # convert_file | ||||||||||
| 253 | |||||||||||
| 254 | #======================================================== | ||||||||||
| 255 | # Main | ||||||||||
| 256 | |||||||||||
| 257 | MAIN: { | ||||||||||
| 258 | 1 | my %data = (); | |||||||||
| 259 | |||||||||||
| 260 | 1 | init_data(\%data); | |||||||||
| 261 | 1 | process_args(\%data); | |||||||||
| 262 | |||||||||||
| 263 | # first argument is the hashdbfile | ||||||||||
| 264 | 1 | ▶ | if (@ARGV) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 265 | { | ||||||||||
| 266 | 1 | $data{options}->{hashdbfile} = shift @ARGV; | |||||||||
| 267 | } | ||||||||||
| 268 | 1 | ▶ | if (!$data{options}->{hashdbfile}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 269 | { | ||||||||||
| 270 | 0 | print STDERR "$0 no hashdbfile\n"; | |||||||||
| 271 | 0 | return 1; | |||||||||
| 272 | } | ||||||||||
| 273 | 1 | ▶ | if (!-e $data{options}->{hashdbfile}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 274 | { | ||||||||||
| 275 | 0 | print STDERR "$0 hashdbfile not found\n"; | |||||||||
| 276 | 0 | return 1; | |||||||||
| 277 | } | ||||||||||
| 278 | |||||||||||
| 279 | # remaining argument is the output file | ||||||||||
| 280 | 1 | ▶ | if (@ARGV) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 281 | { | ||||||||||
| 282 | 1 | $data{options}->{outfile} = shift @ARGV; | |||||||||
| 283 | } | ||||||||||
| 284 | 1 | convert_file(\%data); | |||||||||
| 285 | } | ||||||||||
| 286 | |||||||||||
| 287 | =head1 BUGS | ||||||||||
| 288 | |||||||||||
| 289 | Please report any bugs or feature requests to the author. | ||||||||||
| 290 | |||||||||||
| 291 | =head1 AUTHOR | ||||||||||
| 292 | |||||||||||
| 293 | Kathryn Andersen (RUBYKAT) | ||||||||||
| 294 | perlkat AT katspace dot com | ||||||||||
| 295 | http://www.katspace.com | ||||||||||
| 296 | |||||||||||
| 297 | =head1 COPYRIGHT AND LICENCE | ||||||||||
| 298 | |||||||||||
| 299 | Copyright (c) 2004 by Kathryn Andersen | ||||||||||
| 300 | |||||||||||
| 301 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 302 | under the same terms as Perl itself. | ||||||||||
| 303 | |||||||||||
| 304 | |||||||||||
| 305 | =cut | ||||||||||
| 306 | |||||||||||
| 307 | __END__ | ||||||||||