From 7a19e5c36db73a17e7bb59acc67d149c9c8f582b Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=98yvind=20A=2E=20Holm?= Date: Sun, 22 Feb 2009 06:37:10 +0100 Subject: [PATCH] =?utf8?q?Lager=20gpst-file=20som=20skal=20lage=20TAB-str?= =?utf8?q?=C3=B8mmer=20av=20fildatoer=20som=20skal=20importeres=20inn=20i?= =?utf8?q?=20Postgres.=20Skal=20blant=20annet=20brukes=20til=20.mp4-filene?= =?utf8?q?=20fra=20Nokiaen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * /trunk/src/gpstools/gpst-file Kopi av gpst-pic, bruker den som utgangspunkt. * /trunk/gpst-file → src/gpstools/gpst-file Ny symlink. * /trunk/src/gpstools/tests/gpst-file/Makefile * /trunk/src/gpstools/tests/gpst-file/gpst-file.t Nytt, laget med std. * /trunk/src/gpstools/tests/gpst-file/ * /trunk/src/gpstools/tests/gpst-file/log/ Nye kataloger. * /trunk/src/gpstools/tests/gpst-file/log/tests.log * /trunk/src/gpstools/tests/gpst-file/log/todo-tests.log Nytt etter make. * /trunk/src/gpstools/tests/Makefile (all, clean): La inn katalogen for gpst-file. d35c8c16-00a2-11de-bdde-000475e441b9 git-svn-id: svn+ssh://svnhost/utils@3002 ddca7c6a-6bd8-0310-abd8-a0f6a7c394c1 --- gpst-file | 400 +++++++++++++++++++++++++++++++++++++ tests/Makefile | 2 + tests/gpst-file/Makefile | 12 ++ tests/gpst-file/gpst-file.t | 342 +++++++++++++++++++++++++++++++ tests/gpst-file/log/tests.log | 13 ++ tests/gpst-file/log/todo-tests.log | 4 + 6 files changed, 773 insertions(+) create mode 100755 gpst-file create mode 100644 tests/gpst-file/Makefile create mode 100755 tests/gpst-file/gpst-file.t create mode 100644 tests/gpst-file/log/tests.log create mode 100644 tests/gpst-file/log/todo-tests.log diff --git a/gpst-file b/gpst-file new file mode 100755 index 0000000..dae6542 --- /dev/null +++ b/gpst-file @@ -0,0 +1,400 @@ +#!/usr/bin/perl -w + +#======================================================================= +# $Id$ +# File ID: 86736de4-00a1-11de-acc1-000475e441b9 +# Extract EXIF data from pictures for use with COPY in Postgres +# +# Character set: UTF-8 +# ©opyleft 2008– Øyvind A. Holm +# License: GNU General Public License version 2 or later, see end of +# file for legal stuff. +#======================================================================= + +BEGIN { + our @version_array; +} + +use strict; +use Getopt::Long; + +BEGIN { + push(@INC, "$ENV{'HOME'}/bin/src/gpstools"); + our @version_array; +} + +use GPST; +use GPSTxml; + +$| = 1; + +our $Debug = 0; +our $NA = '\N'; +our %Std = ( + + 'output-format' => 'pgtab', + 'timezone' => '', + +); +our %Opt = ( + + 'author' => '', + 'debug' => 0, + 'description' => '', + 'help' => 0, + 'output-format' => $Std{'output-format'}, + 'strip-whitespace' => 0, + 'timezone' => $Std{'timezone'}, + 'verbose' => 0, + 'version' => 0, + +); + +our $progname = $0; +$progname =~ s/^.*\/(.*?)$/$1/; + +my $rcs_id = '$Id$'; +my $id_date = $rcs_id; +$id_date =~ s/^.*?\d+ (\d\d\d\d-.*?\d\d:\d\d:\d\d\S+).*/$1/; + +push(@main::version_array, $rcs_id); + +Getopt::Long::Configure("bundling"); +GetOptions( + + "author|a=s" => \$Opt{'author'}, + "debug" => \$Opt{'debug'}, + "description|d=s" => \$Opt{'description'}, + "help|h" => \$Opt{'help'}, + "output-format|o=s" => \$Opt{'output-format'}, + "strip-whitespace|w" => \$Opt{'strip-whitespace'}, + "timezone|T=s" => \$Opt{'timezone'}, + "verbose|v+" => \$Opt{'verbose'}, + "version" => \$Opt{'version'}, + +) || die("$progname: Option error. Use -h for help.\n"); + +$Opt{'debug'} && ($Debug = 1); +$Opt{'help'} && usage(0); +if ($Opt{'version'}) { + print_version(); + exit(0); +} + +$GPST::Spc = $Opt{'strip-whitespace'} ? "" : " "; +my $Spc = $GPST::Spc; # FIXME +my $tz_str = ""; +if (length($Opt{'timezone'})) { + if ($Opt{'timezone'} =~ /^[\+\-][0-2][0-9]{3}$/) { + $tz_str = $Opt{'timezone'}; + } elsif ($Opt{'timezone'} =~ /^z$/i) { + $tz_str = $Opt{'timezone'}; + } elsif ($Opt{'timezone'} =~ /^[a-z]+$/i) { + $tz_str = " $Opt{'timezone'}"; + } else { + die("$progname: $Opt{'timezone'}: Invalid time zone\n"); + } +} + +if ($Opt{'output-format'} eq "xml") { + print("\n\n"); +} + +if ($#ARGV < 0) { + while (<>) { + chomp(); + print_entry($_); + } +} else { + for my $fname (@ARGV) { + print_entry($fname); + } +} + +if ($Opt{'output-format'} eq "xml") { + print("\n"); +} + +sub print_entry { + # {{{ + my $filename = shift; + my $Retval = 0; + my ($date, $coor) = + ( '', ''); + my @Dates = (); + local *PicFP; + D("filename = '$filename'"); + if (open(PicFP, "exifprobe -L \"$filename\" |")) { # FIXME: Quick & Dirty™ + while () { + if (/DateTime/) { + s/^.*'(.*?)'.*$/$1/; + chomp($date = $_); + $date =~ s/^(\d\d\d\d)(.)(\d\d)(.)(\d\d)(.)(\d\d:\d\d:\d\d)(.*)/$1-$3-${5}T$7$8/; + D("date = '$date'"); + push(@Dates, $date); + } + } + close(PicFP); + @Dates = reverse sort @Dates; + $date = $Dates[0]; + defined($date) || ($date = ''); + D("final date = '$date'"); + if ($date =~ /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d$/) { + $filename =~ s/^.*\/(.*?)$/$1/; + my $Output = ""; + if ($Opt{'output-format'} eq "xml") { + if (length("$filename$date")) { + $Output = join("", + "$Spc$Spc\n", + length($filename) + ? sprintf("$Spc$Spc$Spc$Spc%s\n", + txt_to_xml($filename)) + : "", + length($date) + ? sprintf("$Spc$Spc$Spc$Spc%s\n", + txt_to_xml($date)) + : "", + "$Spc$Spc\n", + ); + } + } elsif ($Opt{'output-format'} eq "pgtab") { + # Version information {{{ + # Without version field (same as version 1): + # date \t + # "(lat,lon)"-coordinates \t + # description \t + # filename \t + # author \t + # Version 1: + # "1" \t + # date \t + # "(lat,lon)"-coordinates \t + # description \t + # filename \t + # author \n + # }}} + $Output = pgtab_entry( + 1, # Version number + $date, + $coor, + $Opt{'description'}, + $filename, + $Opt{'author'} + ); + } else { + die("$progname: $Opt{'output-format'}: Unknown output format\n"); + } + print($Output); + $Opt{'verbose'} && print(STDERR $Output); + } else { + if (length($date)) { + warn("$filename: $date: Invalid date format"); + $Retval = 2; + } + } + } else { + warn("$filename: Cannot open exifprobe(1) pipe: $!"); + $Retval = 1; + } + return($Retval); + # }}} +} # print_entry() + +sub pgtab_entry { + # {{{ + my ($Version, $Date, $Coor, $Descr, $Filename, $Author) = @_; + defined($Date) || ($Date = $NA); + defined($Coor) || ($Coor = $NA); + defined($Descr) || ($Descr = $NA); + defined($Filename) || ($Filename = $NA); + defined($Author) || ($Author = $NA); + my $Retval = ""; + if ($Version == 1) { + $Retval = join("\t", + 1, # Version number + postgresql_copy_safe($Date) . $tz_str, + length($Coor) + ? postgresql_copy_safe($Coor) + : $NA, + length($Opt{'description'}) + ? postgresql_copy_safe($Opt{'description'}) + : $NA, + length($Filename) + ? postgresql_copy_safe($Filename) + : $NA, + length($Opt{'author'}) + ? postgresql_copy_safe($Opt{'author'}) + : $NA + ) . "\n"; + } + return($Retval); + # }}} +} # pgtab_entry() + +sub print_version { + # Print program version {{{ + for (@main::version_array) { + print("$_\n"); + } + # }}} +} # print_version() + +sub usage { + # Send the help message to stdout {{{ + my $Retval = shift; + + if ($Opt{'verbose'}) { + print("\n"); + print_version(); + } + print(<= $verbose_level) { + print(STDERR "$progname: $Txt\n"); + } + # }}} +} # msg() + +sub D { + # Print a debugging message {{{ + $Debug || return; + my @call_info = caller; + chomp(my $Txt = shift); + my $File = $call_info[1]; + $File =~ s#\\#/#g; + $File =~ s#^.*/(.*?)$#$1#; + print(STDERR "$File:$call_info[2] $$ $Txt\n"); + return(""); + # }}} +} # D() + +__END__ + +# Plain Old Documentation (POD) {{{ + +=pod + +=head1 NAME + + + +=head1 REVISION + +$Id$ + +=head1 SYNOPSIS + + [options] [file [files [...]]] + +=head1 DESCRIPTION + + + +=head1 OPTIONS + +=over 4 + +=item B<-h>, B<--help> + +Print a brief help summary. + +=item B<-v>, B<--verbose> + +Increase level of verbosity. Can be repeated. + +=item B<--version> + +Print version information. + +=item B<--debug> + +Print debugging messages. + +=back + +=head1 BUGS + + + +=head1 AUTHOR + +Made by Øyvind A. Holm Ssunny@sunbase.orgE>. + +=head1 COPYRIGHT + +Copyleft © Øyvind A. Holm Esunny@sunbase.orgE +This is free software; see the file F for legalese stuff. + +=head1 LICENCE + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2 of the License, or (at your +option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +=head1 SEE ALSO + +=cut + +# }}} + +# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w : +# End of file $Id$ diff --git a/tests/Makefile b/tests/Makefile index 0563ed3..e48866b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -6,6 +6,7 @@ all: ./run-tests.pl 2>&1 | remove_perltestnumbers >log/tests.log ./run-tests.pl --todo 2>&1 | remove_perltestnumbers >log/todo-tests.log + cd gpst-file && make cd gpst-pic && make cd addpoints && make @@ -13,4 +14,5 @@ clean: rm -vf gpst-stderr.tmp svn revert log/tests.log log/todo-tests.log cd addpoints && make clean + cd gpst-file && make clean cd gpst-pic && make clean diff --git a/tests/gpst-file/Makefile b/tests/gpst-file/Makefile new file mode 100644 index 0000000..809208e --- /dev/null +++ b/tests/gpst-file/Makefile @@ -0,0 +1,12 @@ +#!/usr/bin/make + +# $Id$ +# File ID: ceae878a-009f-11de-a87e-000475e441b9 + +all: + ./gpst-file.t 2>&1 | remove_perltestnumbers >log/tests.log + ./gpst-file.t --todo 2>&1 | remove_perltestnumbers >log/todo-tests.log + +clean: + rm -vf STDprognameDTS-stderr.tmp + svn revert log/tests.log log/todo-tests.log diff --git a/tests/gpst-file/gpst-file.t b/tests/gpst-file/gpst-file.t new file mode 100755 index 0000000..3b66073 --- /dev/null +++ b/tests/gpst-file/gpst-file.t @@ -0,0 +1,342 @@ +#!/usr/bin/perl -w + +#======================================================================= +# $Id$ +# File ID: bbcbed10-009f-11de-824e-000475e441b9 +# Test suite for gpst-file(1). +# +# Character set: UTF-8 +# ©opyleft 2009– Øyvind A. Holm +# License: GNU General Public License version 2 or later, see end of +# file for legal stuff. +#======================================================================= + +BEGIN { + # push(@INC, "$ENV{'HOME'}/bin/STDlibdirDTS"); + our @version_array; + use Test::More qw{no_plan}; + # use_ok() goes here +} + +use strict; +use Getopt::Long; + +$| = 1; + +our $Debug = 0; +our $CMD = "../../gpst-file"; + +our %Opt = ( + + 'all' => 0, + 'debug' => 0, + 'help' => 0, + 'todo' => 0, + 'verbose' => 0, + 'version' => 0, + +); + +our $progname = $0; +$progname =~ s/^.*\/(.*?)$/$1/; + +my $rcs_id = '$Id$'; +my $id_date = $rcs_id; +$id_date =~ s/^.*?\d+ (\d\d\d\d-.*?\d\d:\d\d:\d\d\S+).*/$1/; + +push(@main::version_array, $rcs_id); + +my @cmdline_array = @ARGV; + +Getopt::Long::Configure("bundling"); +GetOptions( + + "all|a" => \$Opt{'all'}, + "debug" => \$Opt{'debug'}, + "help|h" => \$Opt{'help'}, + "todo|t" => \$Opt{'todo'}, + "verbose|v+" => \$Opt{'verbose'}, + "version" => \$Opt{'version'}, + +) || die("$progname: Option error. Use -h for help.\n"); + +$Opt{'debug'} && ($Debug = 1); +$Opt{'help'} && usage(0); +if ($Opt{'version'}) { + print_version(); + exit(0); +} + +diag(sprintf("========== Executing \"%s%s%s\" ==========", + $progname, + scalar(@cmdline_array) ? " " : "", + join(" ", @cmdline_array))); + +if ($Opt{'todo'} && !$Opt{'all'}) { + goto todo_section; +} + +=pod + +testcmd("$CMD command", # {{{ + <); + close(FP); + return($Txt); + } else { + return undef; + } + # }}} +} + +sub print_version { + # Print program version {{{ + for (@main::version_array) { + print("$_\n"); + } + # }}} +} # print_version() + +sub usage { + # Send the help message to stdout {{{ + my $Retval = shift; + + if ($Opt{'verbose'}) { + print("\n"); + print_version(); + } + print(<= $verbose_level) { + print(STDERR "$progname: $Txt\n"); + } + # }}} +} # msg() + +__END__ + +# Plain Old Documentation (POD) {{{ + +=pod + +=head1 NAME + +run-tests.pl + +=head1 REVISION + +$Id$ + +=head1 SYNOPSIS + +./gpst-file.t [options] [file [files [...]]] + +=head1 DESCRIPTION + +Contains tests for the gpst-file(1) program. + +=head1 OPTIONS + +=over 4 + +=item B<-a>, B<--all> + +Run all tests, also TODOs. + +=item B<-h>, B<--help> + +Print a brief help summary. + +=item B<-t>, B<--todo> + +Run only the TODO tests. + +=item B<-v>, B<--verbose> + +Increase level of verbosity. Can be repeated. + +=item B<--version> + +Print version information. + +=item B<--debug> + +Print debugging messages. + +=back + +=head1 AUTHOR + +Made by Øyvind A. Holm Ssunny@sunbase.orgE>. + +=head1 COPYRIGHT + +Copyleft © Øyvind A. Holm Esunny@sunbase.orgE +This is free software; see the file F for legalese stuff. + +=head1 LICENCE + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2 of the License, or (at your +option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +=head1 SEE ALSO + +=cut + +# }}} + +# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w : +# End of file $Id$ diff --git a/tests/gpst-file/log/tests.log b/tests/gpst-file/log/tests.log new file mode 100644 index 0000000..d83da06 --- /dev/null +++ b/tests/gpst-file/log/tests.log @@ -0,0 +1,13 @@ +# ========== Executing "gpst-file.t" ========== +# Testing -h (--help) option... +ok - "../../gpst-file -h" - Option -h prints help screen +ok - "../../gpst-file -h" - Option -h prints help screen (stderr) +ok - "../../gpst-file -h" - No Id with only -h +# Testing -v (--verbose) option... +ok - "../../gpst-file -hv" - Option --version with -h returns Id string and help screen +ok - "../../gpst-file -hv" - Option --version with -h returns Id string and help screen (stderr) +# Testing --version option... +ok - "../../gpst-file --version" - Option --version returns Id string +ok - "../../gpst-file --version" - Option --version returns Id string (stderr) +# Testing finished. +1..7 diff --git a/tests/gpst-file/log/todo-tests.log b/tests/gpst-file/log/todo-tests.log new file mode 100644 index 0000000..3b70deb --- /dev/null +++ b/tests/gpst-file/log/todo-tests.log @@ -0,0 +1,4 @@ +# ========== Executing "gpst-file.t --todo" ========== +# Running TODO tests... +# Testing finished. +# No tests run! -- 2.11.4.GIT