473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

getopt or optparse options/arguments wrapping?

I wonder is there any way to make the wrapper program can wrap options
&& arguments for the the subprocess/command the wrapper will
execute? by getopt or optparse module?

This is something like the shell script like this:
optwrap=""
while [ $# -gt 0 ]; do
case $1 in
-a) do-something; shift
;;
-b) do-something; shift
;;
*) optwrap="$optwrap $1 $2"
# collect the option and argument
shift
;;
esac
shift
done

$command $optwrap
# execute the command with $optwrap as subprocess

I want that all the options and arguments which are feed to this
scripts command line will be used by executing the sub command.

I need this because I now have finished a fs_backup script written in
python, it will execute tar && find to complete full and
differentiating/incremental backup for the file system level files,
and their restoring. This script will do different things by the
command line arguments, and execute find/tar in different ways, but
for being more flexible, I want some options of tar/find can also been
specified from the command line directly, and the script just transmit
those to tar/find simply.

Is there anyone can give me some advices?

Thank you.

Mar 16 '07 #1
3 1930
Rocky Zhou wrote:
I wonder is there any way to make the wrapper program can wrap options
&& arguments for the the subprocess/command the wrapper will
execute? by getopt or optparse module?
[snip]
I need this because I now have finished a fs_backup script written in
python, it will execute tar && find to complete full and
differentiating/incremental backup for the file system level files,
and their restoring. This script will do different things by the
command line arguments, and execute find/tar in different ways, but
for being more flexible, I want some options of tar/find can also been
specified from the command line directly, and the script just transmit
those to tar/find simply.
I'm not clear on exactly what it is you want. Are you trying to do
something like::

fs_backup --foo --bar x y z --tar-foo --tar-bar tx ty tz

where ``--foo --bar x y z`` gets handled by your command and ``--tar-foo
--tar-bar tx ty tz`` gets passed on through to the tar command?

STeVe
Mar 16 '07 #2
Well, I think I must have more explanation on my script. The script's
usage is like this:
~# fs_backup
Lack of backup identity name
program usage: fs_backup [OPTIONS] $identity
OPTIONS:
-a|--append [t/x[L]:$path, append 1 dir or file to $identity
t/x: include/exclude list, as -T/-X of 'tar'
L:file, append a batch of dirs/files to $identity by LIST
-d|--delete $path, delete 1 dir or file from $identity
-t|--type $type, specify the backup type: full/diff/incr
-D|--delete-outdated [f/d], delete outdated files/empty-dirs when
restoring.
-o 'option=value'
-w|--wrap 'wrap of tar options'
-h|--help, print this help message
For example, I want to backup /var/www/html, with excluding /var/www/
html/syssite/home/cache, and name this backup as pages, I will do
this:
sh# fs_backup -a t:/var/www/html pages
sh# fs_backup -a x:/var/www/html/syssite/home/cache pages
# These t/x have the same meaning of tar's '-T/-X'
sh# fs_backup -t full pages
sh# fs_backup -t incr pages

These will update /var/fs_backup/.table, (/var/fs_backup is 'datadir',
you can configure it to anywhere you like by editing the configuration
file, /etc/new/fs_backup.config):
pages, full, 20070204-231749, pages/pages.full.20070204-231749
pages, incr, 20070205-101011, pages/pages.incr.20070205-101011
.......

May be I want add something to 'pages':
sh# fs_backup -a t:/usr/local/apache2/htdocs pages
sh# fs_backup -t full pages

Once I want recovery from the backup, I will do this:
sh# fs_rstore pages
This fs_rstore is just a symlink to fs_backup. All the files from the
last full backup will be restored.

The actually thing the script done is like this:
2007-03-18 20:32:20 fs_backup DEBUG find /mnt/file/data/ -cnewer /var/
fs_backup/work/.ts ! -type d -print >>/tmp/fs_backup.work.incr.
1174221140.0.list
2007-03-18 20:32:20 fs_backup DEBUG find /mnt/file/work/ -cnewer /var/
fs_backup/work/.ts ! -type d -print >>/tmp/fs_backup.work.incr.
1174221140.0.list
2007-03-18 20:32:20 fs_backup DEBUG tar -czp -P -f /var/fs_backup/work/
work.incr.20070318-203220.tgz -T /tmp/fs_backup.work.incr.
1174221140.0.list .identity .dirs .files

..dirs .files is just a snapshot of the current directories, which can
be used to delete-outdated files when restoring. Here I used absolute
path by using tar's -P parameter. When fs_rstore, it will do this:
command = "tar -xpz -P -f %s.tgz -T %s" % (archive, self.t_files)

maybe I want add some parameters of tar, for example, --strip-path=X,
so I hope the fs_rstore can do this:
sh# fs_rstore pages --strip-path=X
rather than:
sh# fs_rstore pages -w '--strip-path=X'

What should I do?

Thank you.

Mar 18 '07 #3
Rocky Zhou wrote:
.dirs .files is just a snapshot of the current directories, which can
be used to delete-outdated files when restoring. Here I used absolute
path by using tar's -P parameter. When fs_rstore, it will do this:
command = "tar -xpz -P -f %s.tgz -T %s" % (archive, self.t_files)

maybe I want add some parameters of tar, for example, --strip-path=X,
so I hope the fs_rstore can do this:
sh# fs_rstore pages --strip-path=X
rather than:
sh# fs_rstore pages -w '--strip-path=X'

What should I do?
One possibility using argparse (http://argparse.python-hosting.com/)
would be to require the '--' pseudo-argument before all tar options.
Then you could do something like::

fs_rstore pages -- --strip-path=X --same-owner

That '--' pseudo-argument makes everything else after it into a
positional argument (even if they start with '-' or '--'), so you can
then just collect those positional arguments and add them to your
"command". Here's what that code might look like::
>>parser = argparse.ArgumentParser(prog='fs_backup')
parser.add_argument('-a', '--append', metavar='[t/x[L]:]PATH')
parser.add_argument('-d', '--delete', metavar='PATH')
parser.add_argument('-t', '--type',
... choices=['full', 'diff', 'incr'])
>>parser.add_argument('identity')
parser.add_argument('tar_options', nargs='*')
parser.parse_args('-a foo pages -- --strip-path=X')
args = parser.parse_args(
... '-a foo pages -- --strip-path=X --same-owner'.split())
>>args.append
'foo'
>>args.identity
'pages'
>>args.tar_options
['--strip-path=X', '--same-owner']
>>"tar %s -f %s.tgz" % (' '.join(args.tar_options), args.identity)
'tar --strip-path=X --same-owner -f pages.tgz'

Note that all the tar options got collected in 'args.tar_options'.

You could get rid of the need for '--' by adding each tar options to
your parser with an add_argument() call, but I suspect that's too
tedious. I've added a feature request to argparse to make this kind of
thing easier::
http://argparse.python-hosting.com/ticket/28
Not sure when I'll have a chance to look into this though.

STeVe

P.S. You should be able to get similar behavior out of optparse, though
you'll have to parse the 'args' list yourself.
Mar 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Bear | last post by:
I'm stumped. Trying to follow the docs and .. failure. here's the args >>> args '-Middwb@mainex1.asu.edu -AKHAAM@prlinux+898 -CA --D2003-08-20-09:28:13.417 -Ff -Hprlinux...
3
by: Dominik Kaspar | last post by:
I tried to use getopt and copied the example from: http://www.python.org/doc/current/lib/module-getopt.html but nothing is working... getopt.GetoptError doesn't seem to exist and when i run the...
3
by: Don Low | last post by:
Hi, I'm going over a script that demonstrates the getopt function. I include the script here: #! /usr/bin/python import sys, getopt, string
8
by: Hans-Joachim Widmaier | last post by:
I was really pleased when the Optik module found its way into the standard Python battery compartment, as it matched all my option parsing requirements. But, as always, there's really nothing that...
6
by: Frans Englich | last post by:
Hello, In my use of getopt.getopt, I would like to make a certain parameter mandatory. I know how to specify such that a parameter must have a value if it's specified, but I also want to make...
4
by: pinkfloydhomer | last post by:
I want to be able to do something like: myscript.py * -o outputfile and then have the shell expand the * as usual, perhaps to hundreds of filenames. But as far as I can see, getopt can only...
0
by: Steven Bethard | last post by:
I feel like I must be reinventing the wheel here, so I figured I'd post to see what other people have been doing for this. In general, I love the optparse interface, but it doesn't do any checks...
4
by: rick | last post by:
Consider the following piece of code: parser = optparse.OptionParser(usage="usage: %prog <input filename> <output filename", add_help_option=False) parser.add_option("-d", type="string",...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.