Commit 27aaf6ef authored by Tom Lane's avatar Tom Lane

Minor improvements for reformat_dat_file.pl.

Use Getopt::Long in preference to hand-rolled option parsing code.

Also, remove "-I .../backend/catalog" switch from the Makefile
invocations.  That's been unnecessary for some time, and leaving it
there gives the false impression it's needed in manual invocations.

John Naylor (extracted from a larger but more controversial patch)

Discussion: https://postgr.es/m/CACPNZCsHdcQN2jQ1=ptbi1Co2Nj3aHgRCUMk62=ThgWNabPY+Q@mail.gmail.com
parent e1e0e8d5
...@@ -13,19 +13,16 @@ subdir = src/include/catalog ...@@ -13,19 +13,16 @@ subdir = src/include/catalog
top_builddir = ../../.. top_builddir = ../../..
include $(top_builddir)/src/Makefile.global include $(top_builddir)/src/Makefile.global
# location of Catalog.pm
catalogdir = $(top_srcdir)/src/backend/catalog
# 'make reformat-dat-files' is a convenience target for rewriting the # 'make reformat-dat-files' is a convenience target for rewriting the
# catalog data files in our standard format. This includes collapsing # catalog data files in our standard format. This includes collapsing
# out any entries that are redundant with a BKI_DEFAULT annotation. # out any entries that are redundant with a BKI_DEFAULT annotation.
reformat-dat-files: reformat-dat-files:
$(PERL) -I $(catalogdir) $(srcdir)/reformat_dat_file.pl -o $(srcdir) $(srcdir)/pg_*.dat $(PERL) $(srcdir)/reformat_dat_file.pl --output $(srcdir) $(srcdir)/pg_*.dat
# 'make expand-dat-files' is a convenience target for expanding out all # 'make expand-dat-files' is a convenience target for expanding out all
# default values in the catalog data files. This should be run before # default values in the catalog data files. This should be run before
# altering or removing any BKI_DEFAULT annotation. # altering or removing any BKI_DEFAULT annotation.
expand-dat-files: expand-dat-files:
$(PERL) -I $(catalogdir) $(srcdir)/reformat_dat_file.pl -o $(srcdir) $(srcdir)/pg_*.dat --full-tuples $(PERL) $(srcdir)/reformat_dat_file.pl --output $(srcdir) $(srcdir)/pg_*.dat --full-tuples
.PHONY: reformat-dat-files expand-dat-files .PHONY: reformat-dat-files expand-dat-files
...@@ -20,9 +20,11 @@ ...@@ -20,9 +20,11 @@
use strict; use strict;
use warnings; use warnings;
use FindBin;
use Getopt::Long;
# If you copy this script to somewhere other than src/include/catalog, # If you copy this script to somewhere other than src/include/catalog,
# you'll need to modify this "use lib" or provide a suitable -I switch. # you'll need to modify this "use lib" or provide a suitable -I switch.
use FindBin;
use lib "$FindBin::RealBin/../../backend/catalog/"; use lib "$FindBin::RealBin/../../backend/catalog/";
use Catalog; use Catalog;
...@@ -34,35 +36,16 @@ use Catalog; ...@@ -34,35 +36,16 @@ use Catalog;
my @METADATA = my @METADATA =
('oid', 'oid_symbol', 'array_type_oid', 'descr', 'autogenerated'); ('oid', 'oid_symbol', 'array_type_oid', 'descr', 'autogenerated');
my @input_files; # Process command line switches.
my $output_path = ''; my $output_path = '';
my $full_tuples = 0; my $full_tuples = 0;
# Process command line switches. GetOptions(
while (@ARGV) 'output=s' => \$output_path,
{ 'full-tuples' => \$full_tuples) || usage();
my $arg = shift @ARGV;
if ($arg !~ /^-/)
{
push @input_files, $arg;
}
elsif ($arg =~ /^-o/)
{
$output_path = length($arg) > 2 ? substr($arg, 2) : shift @ARGV;
}
elsif ($arg eq '--full-tuples')
{
$full_tuples = 1;
}
else
{
usage();
}
}
# Sanity check arguments. # Sanity check arguments.
die "No input files.\n" die "No input files.\n" unless @ARGV;
if !@input_files;
# Make sure output_path ends in a slash. # Make sure output_path ends in a slash.
if ($output_path ne '' && substr($output_path, -1) ne '/') if ($output_path ne '' && substr($output_path, -1) ne '/')
...@@ -76,7 +59,7 @@ if ($output_path ne '' && substr($output_path, -1) ne '/') ...@@ -76,7 +59,7 @@ if ($output_path ne '' && substr($output_path, -1) ne '/')
my %catalogs; my %catalogs;
my %catalog_data; my %catalog_data;
my @catnames; my @catnames;
foreach my $datfile (@input_files) foreach my $datfile (@ARGV)
{ {
$datfile =~ /(.+)\.dat$/ $datfile =~ /(.+)\.dat$/
or die "Input files need to be data (.dat) files.\n"; or die "Input files need to be data (.dat) files.\n";
...@@ -130,7 +113,7 @@ foreach my $catname (@catnames) ...@@ -130,7 +113,7 @@ foreach my $catname (@catnames)
if !(grep { $_ eq $attname } @METADATA); if !(grep { $_ eq $attname } @METADATA);
} }
# Overwrite .dat files in place, since they are under version control. # Write output files to specified directory.
my $datfile = "$output_path$catname.dat"; my $datfile = "$output_path$catname.dat";
open my $dat, '>', $datfile open my $dat, '>', $datfile
or die "can't open $datfile: $!"; or die "can't open $datfile: $!";
...@@ -318,10 +301,12 @@ sub usage ...@@ -318,10 +301,12 @@ sub usage
Usage: reformat_dat_file.pl [options] datafile... Usage: reformat_dat_file.pl [options] datafile...
Options: Options:
-o PATH write output files to PATH instead of current directory --output PATH output directory (default '.')
--full-tuples write out full tuples, including default values --full-tuples write out full tuples, including default values
Expects a list of .dat files as arguments. Non-option arguments are the names of input .dat files.
Updated files are written to the output directory,
possibly overwriting the input files.
EOM EOM
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment