convert diskUsage to WebGUI::Command

This commit is contained in:
Graham Knop 2011-07-05 09:21:13 -05:00
parent 1f201260ba
commit 3929d16adb

126
sbin/diskUsage.pl → lib/WebGUI/Command/diskUsage.pm Executable file → Normal file
View file

@ -1,4 +1,4 @@
#!/usr/bin/env perl package WebGUI::Command::diskUsage;
#------------------------------------------------------------------- #-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation. # WebGUI is Copyright 2001-2009 Plain Black Corporation.
@ -10,58 +10,41 @@
# http://www.plainblack.com info@plainblack.com # http://www.plainblack.com info@plainblack.com
#------------------------------------------------------------------- #-------------------------------------------------------------------
use WebGUI::Command -command;
use strict; use strict;
use Getopt::Long; use warnings;
use Pod::Usage;
use WebGUI::Paths -inc;
use WebGUI::Session;
use WebGUI::Asset;
my $configFile; sub opt_spec {
my $quiet; return (
my $assetId; [ 'configFile=s', 'The WebGUI config file to use. This parameter is required.'],
my $assetUrl; [ 'assetId=s', 'AssetId to start with (optional) uses default page if not specified.' ],
my $summarize = 0; [ 'assetUrl=s', 'AssetUrl to start with (optional) uses default page if not specified'],
my $blockSize = 1; [ 'quiet', 'No output except for numeric file size (default unit is bytes, will use blockSize if specified)'],
my $recurse = 1; [ 'summary!', 'Displays total space used for asset and descendants (unless recurse flag is set to false in which case only the asset specified will be used)'],
my $help; [ 'blockSize=i', 'Change units in which space used is specified, defaults to bytes.'],
[ 'recurse!', 'Flag indicating whether the disk space usage should consider asset and all descendants (default) or just the asset specified.'],
$| = 1; # No buffering );
GetOptions(
'configFile=s'=>\$configFile, # WebGUI Config file
'assetId=s' =>\$assetId, # AssetId to start with (optional) uses default page if not specified.
'assetUrl=s' =>\$assetUrl, # AssetUrl to start with (optional) uses default page if not specified
'quiet' =>\$quiet, # No output except for numeric file size (default unit is bytes, will use blockSize if specified)
'summary!' =>\$summarize, # Displays total space used for asset and descendants (unless recurse flag is set to false in which case only the asset specified will be used)
'blockSize=i' =>\$blockSize, # Change units in which space used is specified, defaults to bytes.
'recurse!' =>\$recurse, # Flag indicating whether the disk space usage should consider asset and all descendants (default) or just the asset specified.
'help!' =>\$help,
);
pod2usage( verbose => 2 ) if $help;
pod2usage() unless $configFile;
my $session = start();
du();
finish($session);
#-------------------------------------------------
sub start {
my $session = WebGUI::Session->open($configFile);
$session->user({userId=>3});
return $session;
} }
#------------------------------------------------- sub validate_args {
sub finish { my ($self, $opt, $args) = @_;
my $session = shift; if (! $opt->{configfile}) {
$session->var->end(); $self->usage_error('You must specify the --configFile option.');
$session->close(); }
} }
#------------------------------------------------------- sub run {
sub du { my ($self, $opt, $args) = @_;
my ($configFile, $assetId, $assetUrl, $quiet, $summarize, $blockSize, $recurse) =
@{$opt}{qw(configfile assetid asseturl quiet summary blocksize recurse)};
$summarize //= 0;
$blockSize //= 1;
$recurse //= 1;
my $session = WebGUI::Session->open($configFile);
$session->user({userId=>3});
my $asset; my $asset;
my $totalSize; # disk space used my $totalSize; # disk space used
@ -120,27 +103,28 @@ sub du {
else { # return script friendly output of the size only. else { # return script friendly output of the size only.
print $totalSize; print $totalSize;
} }
$session->var->end;
$session->close;
} }
1;
__END__ __END__
=head1 NAME =head1 NAME
diskUsage - Display amount of disk space used by a WebGUI asset WebGUI::Command::diskUsage - Display amount of disk space used by a WebGUI asset and its desecendants.
an its desecendants.
=head1 SYNOPSIS =head1 SYNOPSIS
webgui.pl diskusage --configFile config.conf
diskUsage --configFile config.conf [--assetId id]
[--assetId id] [--assetUrl url]
[--assetUrl url] [--blockSize bytes]
[--blockSize bytes] [--norecurse]
[--norecurse] [--quiet]
[--quiet] [--summary]
[--summary]
diskUsage --help
=head1 DESCRIPTION =head1 DESCRIPTION
@ -150,50 +134,46 @@ utility.
=over =over
=item B<--configFile config.conf> =item C<--configFile config.conf>
The WebGUI config file to use. Only the file name needs to be specified, The WebGUI config file to use. Only the file name needs to be specified,
since it will be looked up inside WebGUI's configuration directory. since it will be looked up inside WebGUI's configuration directory.
This parameter is required. This parameter is required.
=item B<--assetId id> =item C<--assetId id>
Calculate disk usage starting from WebGUI's Asset identified by B<id>. Calculate disk usage starting from WebGUI's Asset identified by B<id>.
If this parameter is not supplied, calculations will start from If this parameter is not supplied, calculations will start from
WebGUI's default page as defined in the Site settings. WebGUI's default page as defined in the Site settings.
=item B<--assetUrl url> =item C<--assetUrl url>
Calculate disk usage starting from the particular URL given by B<url>, Calculate disk usage starting from the particular URL given by B<url>,
which must be relative to the server (e.g. B</home> instead of which must be relative to the server (e.g. C</home> instead of
B<http://your.server/home>). If this parameter is not supplied, calculations B<http://your.server/home>). If this parameter is not supplied, calculations
will start from WebGUI's default page as defined in the Site settings. will start from WebGUI's default page as defined in the Site settings.
=item B<--blockSize bytes> =item C<--blockSize bytes>
Use B<bytes> as scaling factor to change the units in which disk space Use C<bytes> as scaling factor to change the units in which disk space
will be reported. If this parameter is not supplied, it defaults to B<1>, will be reported. If this parameter is not supplied, it defaults to B<1>,
hence the results will be expressed in bytes. If you want to have kb, hence the results will be expressed in bytes. If you want to have kb,
use B<--blockSize 1024>. use C<--blockSize 1024>.
=item B<--norecurse> =item C<--norecurse>
Prevent recursive calculation of disk space. This effectively computes Prevent recursive calculation of disk space. This effectively computes
the used disk space for the starting Asset only, without including the used disk space for the starting Asset only, without including
its descendants. its descendants.
=item B<--quiet> =item C<--quiet>
Just display the total amount of disk space as a raw value. Just display the total amount of disk space as a raw value.
=item B<--summary> =item C<--summary>
Just display the total amount of disk space in a human readable format. Just display the total amount of disk space in a human readable format.
=item B<--help>
Shows this documentation, then exits.
=back =back
=head1 AUTHOR =head1 AUTHOR