From 38488004c207508834543e02e991e6129669bc8c Mon Sep 17 00:00:00 2001 From: Jacob Pipkin Date: Thu, 30 Aug 2012 07:20:32 -0500 Subject: changes for REQUIRES in SBos for 14, and many cleanups, fixes, enhancements --- sboconfig | 63 +++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 32 deletions(-) (limited to 'sboconfig') diff --git a/sboconfig b/sboconfig index 829d7a3..b5c1d6d 100755 --- a/sboconfig +++ b/sboconfig @@ -9,6 +9,7 @@ # date: Pungenday, the 40th day of Discord in the YOLD 3178 # license: WTFPL +use 5.16.0; use strict; use warnings FATAL => 'all'; use SBO::Lib; @@ -21,7 +22,7 @@ use File::Temp qw(tempfile);; my %config = %SBO::Lib::config; my $self = basename ($0); -sub show_usage { +sub show_usage () { print < 'NOCLEAN', @@ -66,7 +59,17 @@ my %valid_confs = ( p => 'PKG_DIR', s => 'SBO_HOME', ); - + +my %params = reverse %valid_confs; + +if (exists $options{l}) { + my @keys = sort {$a cmp $b} keys %config; + say "sboconfig -$params{$_}:\n $_=$config{$_}" for @keys; + exit 0; +} + +show_usage and exit 0 unless %options; + # setup what's being changed. my %changes; while (my ($key, $value) = each %valid_confs) { @@ -77,52 +80,48 @@ if (exists $changes{JOBS}) { ($changes{JOBS} =~ /^\d+$/ || $changes{JOBS} eq 'FALSE'); } -my $conf_dir = $SBO::Lib::conf_dir;; +my $conf_dir = $SBO::Lib::conf_dir; my $conf_file = $SBO::Lib::conf_file; # safely modify our conf file; copy to a temp location, edit the temp file, # move the edited file into place -sub config_write { - exists $_[1] or script_error ('config_write requires two arguments.'); +sub config_write ($$) { + exists $_[1] or script_error 'config_write requires two arguments.'; my ($key, $val) = @_; if (! -d $conf_dir) { - mkdir ($conf_dir) or die "Unable to create $conf_dir. Exiting.\n"; + mkdir $conf_dir or die "Unable to create $conf_dir. Exiting.\n"; } if (-f $conf_file) { - my ($fh, $filename) = tempfile (DIR => $SBO::Lib::tempdir); - close $fh; - copy ($conf_file, $filename); + my $tempfh = tempfile (DIR => $SBO::Lib::tempdir); + my $tempfn = get_tmp_perlfn $tempfh; + copy ($conf_file, $tempfn); # tie the file so that if $key is already there, we just change that # line and untie it - tie my @temp, 'Tie::File', $filename; - my $has = 'FALSE'; + tie my @temp, 'Tie::File', $tempfn; + my $has = 0; my $regex = qr/\A\Q$key\E=/; FIRST: for my $tmpline (@temp) { - if ($tmpline =~ $regex) { - $has = 'TRUE'; - $tmpline = "$key=$val"; - last FIRST; - } + $has++, $tmpline = "$key=$val", last FIRST if $tmpline =~ $regex;; } untie @temp; # otherwise, append our new $key=$value pair - if ($has eq 'FALSE') { - my $fh = open_fh ($filename, '>>'); + unless ($has) { + my $fh = open_fh ($tempfn, '>>'); print {$fh} "$key=$val\n"; close $fh; } - move ($filename, $conf_file); + move ($tempfn, $conf_file); } else { # no config file, easiest case of all. - my $fh = open_fh ($conf_file, '>'); - print {$fh} "$key=$val\n"; + my $fh = open_fh $conf_file, '>'; + say {$fh} "$key=$val"; close $fh; } } while (my ($key, $value) = each %changes) { print "Setting $key to $value...\n"; - config_write ($key, $value); + config_write $key, $value; } exit 0; -- cgit v1.2.3 From 5ca399e0e9ab12063f15c4ab14443ba762877634 Mon Sep 17 00:00:00 2001 From: Jacob Pipkin Date: Thu, 30 Aug 2012 14:03:49 -0500 Subject: bugfixes++ --- sboconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sboconfig') diff --git a/sboconfig b/sboconfig index b5c1d6d..9c2682f 100755 --- a/sboconfig +++ b/sboconfig @@ -101,7 +101,7 @@ sub config_write ($$) { my $has = 0; my $regex = qr/\A\Q$key\E=/; FIRST: for my $tmpline (@temp) { - $has++, $tmpline = "$key=$val", last FIRST if $tmpline =~ $regex;; + $has++, $tmpline = "$key=$val", last FIRST if $tmpline =~ $regex; } untie @temp; # otherwise, append our new $key=$value pair -- cgit v1.2.3 From d55dbdf17977ed9b1dfd91c98a4a569960b851cd Mon Sep 17 00:00:00 2001 From: Jacob Pipkin Date: Fri, 31 Aug 2012 15:53:21 -0500 Subject: epic changes and fixes and much further testing --- sboconfig | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'sboconfig') diff --git a/sboconfig b/sboconfig index 9c2682f..2065a27 100755 --- a/sboconfig +++ b/sboconfig @@ -98,7 +98,7 @@ sub config_write ($$) { # tie the file so that if $key is already there, we just change that # line and untie it tie my @temp, 'Tie::File', $tempfn; - my $has = 0; + my $has; my $regex = qr/\A\Q$key\E=/; FIRST: for my $tmpline (@temp) { $has++, $tmpline = "$key=$val", last FIRST if $tmpline =~ $regex; @@ -110,18 +110,19 @@ sub config_write ($$) { print {$fh} "$key=$val\n"; close $fh; } - move ($tempfn, $conf_file); + move ($tempfn, $conf_file) || return; } else { # no config file, easiest case of all. - my $fh = open_fh $conf_file, '>'; + my $fh = open_fh $conf_file, '>' || return; say {$fh} "$key=$val"; close $fh; } + return 1; } while (my ($key, $value) = each %changes) { print "Setting $key to $value...\n"; - config_write $key, $value; + config_write $key, $value or warn "Unable to write to $conf_file\n"; } exit 0; -- cgit v1.2.3 From 6be3ef603fefc5d04b506b17cbad140790698042 Mon Sep 17 00:00:00 2001 From: Jacob Pipkin Date: Sat, 1 Sep 2012 02:14:32 -0500 Subject: more cleanups and fixes and such --- sboconfig | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'sboconfig') diff --git a/sboconfig b/sboconfig index 2065a27..aca2ad1 100755 --- a/sboconfig +++ b/sboconfig @@ -19,7 +19,7 @@ use File::Copy; use File::Path qw(make_path); use File::Temp qw(tempfile);; -my %config = %SBO::Lib::config; +#my %config = %SBO::Lib::config; my $self = basename ($0); sub show_usage () { @@ -80,11 +80,11 @@ if (exists $changes{JOBS}) { ($changes{JOBS} =~ /^\d+$/ || $changes{JOBS} eq 'FALSE'); } -my $conf_dir = $SBO::Lib::conf_dir; -my $conf_file = $SBO::Lib::conf_file; +#my $conf_dir = $SBO::Lib::conf_dir; +#my $conf_file = $SBO::Lib::conf_file; -# safely modify our conf file; copy to a temp location, edit the temp file, -# move the edited file into place +# safely modify our conf file; write its contents to a temp file, modify the +# temp file, write the contents of the temp file back to the conf file sub config_write ($$) { exists $_[1] or script_error 'config_write requires two arguments.'; my ($key, $val) = @_; @@ -92,12 +92,14 @@ sub config_write ($$) { mkdir $conf_dir or die "Unable to create $conf_dir. Exiting.\n"; } if (-f $conf_file) { - my $tempfh = tempfile (DIR => $SBO::Lib::tempdir); - my $tempfn = get_tmp_perlfn $tempfh; - copy ($conf_file, $tempfn); +# my $tempfh = tempfile (DIR => $SBO::Lib::tempdir); + my $tempfh = tempfile (DIR => $tempdir); + my $conffh = open_read $conf_file; + my $conftents = do {local $/; <$conffh>}; + print {$tempfh} $conftents; # tie the file so that if $key is already there, we just change that # line and untie it - tie my @temp, 'Tie::File', $tempfn; + tie my @temp, 'Tie::File', $tempfh; my $has; my $regex = qr/\A\Q$key\E=/; FIRST: for my $tmpline (@temp) { @@ -105,16 +107,18 @@ sub config_write ($$) { } untie @temp; # otherwise, append our new $key=$value pair - unless ($has) { - my $fh = open_fh ($tempfn, '>>'); - print {$fh} "$key=$val\n"; - close $fh; - } - move ($tempfn, $conf_file) || return; + print {$tempfh} "$key=$val\n" unless $has; + seek $tempfh, 0, 0; + my $contents = do {local $/; <$tempfh>}; + close $conffh; + eval { $conffh = open_fh $conf_file, '>>' }; + warn "Cannot write configuration: $@\n", return if $@; + print {$conffh} $contents or return; + close $conffh, close $tempfh; } else { # no config file, easiest case of all. - my $fh = open_fh $conf_file, '>' || return; - say {$fh} "$key=$val"; + my $fh = open_fh $conf_file, '>' or return; + print {$fh} "$key=$val\n"; close $fh; } return 1; -- cgit v1.2.3 From 90cd9a74b8b562bebd4a2eaeb01ccdb3d2a75c42 Mon Sep 17 00:00:00 2001 From: Jacob Pipkin Date: Sat, 1 Sep 2012 02:22:57 -0500 Subject: fix >> to > --- sboconfig | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'sboconfig') diff --git a/sboconfig b/sboconfig index aca2ad1..e993430 100755 --- a/sboconfig +++ b/sboconfig @@ -97,8 +97,8 @@ sub config_write ($$) { my $conffh = open_read $conf_file; my $conftents = do {local $/; <$conffh>}; print {$tempfh} $conftents; - # tie the file so that if $key is already there, we just change that - # line and untie it + # tie the temp file so that if $key is already there, we just change + # that line and untie it tie my @temp, 'Tie::File', $tempfh; my $has; my $regex = qr/\A\Q$key\E=/; @@ -108,11 +108,12 @@ sub config_write ($$) { untie @temp; # otherwise, append our new $key=$value pair print {$tempfh} "$key=$val\n" unless $has; + # then over the conf file with the contents of the temp file seek $tempfh, 0, 0; my $contents = do {local $/; <$tempfh>}; close $conffh; - eval { $conffh = open_fh $conf_file, '>>' }; - warn "Cannot write configuration: $@\n", return if $@; + eval { $conffh = open_fh $conf_file, '>' }; + warn "Cannot write configuration: $@\n" and return if $@; print {$conffh} $contents or return; close $conffh, close $tempfh; } else { -- cgit v1.2.3 From 22e595ded4d1bc5bf6b8987ffcdd0f522d1a1bd0 Mon Sep 17 00:00:00 2001 From: Jacob Pipkin Date: Sat, 1 Sep 2012 03:29:58 -0500 Subject: more cleanups --- sboconfig | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'sboconfig') diff --git a/sboconfig b/sboconfig index e993430..c181b29 100755 --- a/sboconfig +++ b/sboconfig @@ -19,7 +19,6 @@ use File::Copy; use File::Path qw(make_path); use File::Temp qw(tempfile);; -#my %config = %SBO::Lib::config; my $self = basename ($0); sub show_usage () { @@ -80,9 +79,6 @@ if (exists $changes{JOBS}) { ($changes{JOBS} =~ /^\d+$/ || $changes{JOBS} eq 'FALSE'); } -#my $conf_dir = $SBO::Lib::conf_dir; -#my $conf_file = $SBO::Lib::conf_file; - # safely modify our conf file; write its contents to a temp file, modify the # temp file, write the contents of the temp file back to the conf file sub config_write ($$) { @@ -92,7 +88,6 @@ sub config_write ($$) { mkdir $conf_dir or die "Unable to create $conf_dir. Exiting.\n"; } if (-f $conf_file) { -# my $tempfh = tempfile (DIR => $SBO::Lib::tempdir); my $tempfh = tempfile (DIR => $tempdir); my $conffh = open_read $conf_file; my $conftents = do {local $/; <$conffh>}; @@ -108,7 +103,7 @@ sub config_write ($$) { untie @temp; # otherwise, append our new $key=$value pair print {$tempfh} "$key=$val\n" unless $has; - # then over the conf file with the contents of the temp file + # then over write the conf file with the contents of the temp file seek $tempfh, 0, 0; my $contents = do {local $/; <$tempfh>}; close $conffh; -- cgit v1.2.3 From 8b08c603ae79c145bc3b344f6dca4f0a95ed6201 Mon Sep 17 00:00:00 2001 From: Jacob Pipkin Date: Sat, 1 Sep 2012 04:52:02 -0500 Subject: more and more cleanups and fixes --- sboconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sboconfig') diff --git a/sboconfig b/sboconfig index c181b29..27d3353 100755 --- a/sboconfig +++ b/sboconfig @@ -121,7 +121,7 @@ sub config_write ($$) { } while (my ($key, $value) = each %changes) { - print "Setting $key to $value...\n"; + say "Setting $key to $value..."; config_write $key, $value or warn "Unable to write to $conf_file\n"; } -- cgit v1.2.3 From ae4a6e80488279460453a387d28fc08219a0341a Mon Sep 17 00:00:00 2001 From: Jacob Pipkin Date: Sun, 2 Sep 2012 00:05:39 -0500 Subject: sanity checks for sboconfig --- sboconfig | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'sboconfig') diff --git a/sboconfig b/sboconfig index 27d3353..85d4886 100755 --- a/sboconfig +++ b/sboconfig @@ -74,9 +74,23 @@ my %changes; while (my ($key, $value) = each %valid_confs) { $changes{$value} = $options{$key} if exists $options{$key}; } + +my $die = "You have provided an invalid parameter for"; + +if (exists $changes{NOCLEAN}) { + die "$die -c\n" unless $changes{NOCLEAN} =~ /^(TRUE|FALSE)$/; +} +if (exists $changes{DISTCLEAN}) { + die "$die -d\n" unless $changes{DISTCLEAN} =~ /^(TRUE|FALSE)$/; +} if (exists $changes{JOBS}) { - die "You have provided an invalid parameter for -j\n" unless - ($changes{JOBS} =~ /^\d+$/ || $changes{JOBS} eq 'FALSE'); + die "$die -j\n" unless $changes{JOBS} =~ /^(\d+|FALSE)$/; +} +if (exists $changes{PKG_DIR}) { + die "$die -p\n" unless $changes{PKG_DIR} =~ qr#^(/|FALSE$)#; +} +if (exists $changes{SBO_HOME}) { + die "$die -s\n" unless $changes{SBO_HOME} =~ qr#^/#; } # safely modify our conf file; write its contents to a temp file, modify the -- cgit v1.2.3 From 027d01c433b17b1914ba58fae90d6b422d4a45e7 Mon Sep 17 00:00:00 2001 From: Jacob Pipkin Date: Fri, 21 Sep 2012 07:00:56 -0500 Subject: sboconfig converted to long options and fixes added from 0.8 --- sboconfig | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'sboconfig') diff --git a/sboconfig b/sboconfig index 85d4886..f07b589 100755 --- a/sboconfig +++ b/sboconfig @@ -14,7 +14,7 @@ use strict; use warnings FATAL => 'all'; use SBO::Lib; use File::Basename; -use Getopt::Std; +use Getopt::Long; use File::Copy; use File::Path qw(make_path); use File::Temp qw(tempfile);; @@ -31,45 +31,53 @@ Options: -l: show current options. Config options (defaults shown): - -c FALSE: + -c|--clean FALSE: NOCLEAN: if TRUE, do NOT clean up after building by default. - -d FALSE: + -d|--distclean FALSE: DISTCLEAN: if TRUE, DO clean distfiles by default after building. - -j FALSE: + -j|--jobs FALSE: JOBS: numeric -j setting to feed to make for multicore systems. - -p FALSE: + -p|--pkg-dir FALSE: PKG_DIR: set a directory to store packages in. - -s /usr/sbo: + -s|--sbo-home /usr/sbo: SBO_HOME: set the SBo directory. EOF } my %options; -getopts ('hvlc:d:p:s:j:', \%options); -show_usage and exit 0 if exists $options{h}; -show_version and exit 0 if exists $options{v}; +GetOptions (\%options, 'help|h', 'version|v', 'list|l', 'noclean|c=s', + 'distclean|d=s', 'jobs|j=s', 'pkg-dir|p=s', 'sbo-home|s=s'); + +show_usage and exit 0 if exists $options{help}; +show_version and exit 0 if exists $options{version}; my %valid_confs = ( - c => 'NOCLEAN', - d => 'DISTCLEAN', - j => 'JOBS', - p => 'PKG_DIR', - s => 'SBO_HOME', + noclean => 'NOCLEAN', + distclean => 'DISTCLEAN', + jobs => 'JOBS', + 'pkg-dir' => 'PKG_DIR', + 'sbo-home' => 'SBO_HOME', ); -my %params = reverse %valid_confs; +my %params = ( + NOCLEAN => 'c|--noclean', + DISTCLEAN => 'd|--distclean', + JOBS => 'j|--jobs', + PKG_DIR => 'p|--pkg-dir', + SBO_HOME => 's|--sbo-home', +); -if (exists $options{l}) { +if (exists $options{list}) { my @keys = sort {$a cmp $b} keys %config; say "sboconfig -$params{$_}:\n $_=$config{$_}" for @keys; exit 0; } -show_usage and exit 0 unless %options; +show_usage and exit 0 unless keys %options > 0; -# setup what's being changed. +# setup what's being changed, sanity check. my %changes; while (my ($key, $value) = each %valid_confs) { $changes{$value} = $options{$key} if exists $options{$key}; -- cgit v1.2.3 From 62405748ab1470aac11811167c001ce7cbeb040a Mon Sep 17 00:00:00 2001 From: Jacob Pipkin Date: Thu, 4 Oct 2012 05:05:52 -0500 Subject: removed 'date:' line from comments, cleanups for quote consistency, etc --- sboconfig | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'sboconfig') diff --git a/sboconfig b/sboconfig index f07b589..892279c 100755 --- a/sboconfig +++ b/sboconfig @@ -6,7 +6,6 @@ # script to handle sbotools configuration # # author: Jacob Pipkin -# date: Pungenday, the 40th day of Discord in the YOLD 3178 # license: WTFPL use 5.16.0; @@ -83,7 +82,7 @@ while (my ($key, $value) = each %valid_confs) { $changes{$value} = $options{$key} if exists $options{$key}; } -my $die = "You have provided an invalid parameter for"; +my $die = 'You have provided an invalid parameter for'; if (exists $changes{NOCLEAN}) { die "$die -c\n" unless $changes{NOCLEAN} =~ /^(TRUE|FALSE)$/; @@ -103,6 +102,9 @@ if (exists $changes{SBO_HOME}) { # safely modify our conf file; write its contents to a temp file, modify the # temp file, write the contents of the temp file back to the conf file +# TODO: if multiple options are provided to this script, this sub should write +# them all at once, instead of only a single one and having to call it once for +# each option specified to the script. sub config_write ($$) { exists $_[1] or script_error 'config_write requires two arguments.'; my ($key, $val) = @_; @@ -125,7 +127,7 @@ sub config_write ($$) { untie @temp; # otherwise, append our new $key=$value pair print {$tempfh} "$key=$val\n" unless $has; - # then over write the conf file with the contents of the temp file + # then overwrite the conf file with the contents of the temp file seek $tempfh, 0, 0; my $contents = do {local $/; <$tempfh>}; close $conffh; -- cgit v1.2.3