Add --one-entry option to specify default handling for one-entry archives. trunk

Thu, 24 Jul 2008 22:15:19 -0400

author
Brett Smith <brettcsmith@brettcsmith.org>
date
Thu, 24 Jul 2008 22:15:19 -0400
branch
trunk
changeset 84
d78d63cb4c4e
parent 83
cb56c72f3d42
child 85
ad73f75c9046

Add --one-entry option to specify default handling for one-entry archives.

As part of this I ended up cleaning up the option-generating code a little
bit, mostly to be more careful about the ordering of options in --help
output.

README file | annotate | diff | comparison | revisions
scripts/dtrx file | annotate | diff | comparison | revisions
tests/tests.yml file | annotate | diff | comparison | revisions
--- a/README	Wed Jul 23 12:15:45 2008 -0400
+++ b/README	Thu Jul 24 22:15:19 2008 -0400
@@ -29,16 +29,34 @@
    if any of the contents are themselves archives, and extract those as
    well.
 
+--one, --one-entry
+   Normally, if an archive only contains one file or directory with a name
+   that doesn't match the archive's, dtrx will ask you how to handle it.
+   With this option, you can specify ahead of time what should happen.
+   Possible values are:
+
+   inside
+      Extract the file/directory inside another directory named after the
+      archive.  This is the default.
+
+   rename
+      Extract the file/directory in the current directory, and then rename
+      it to match the name of the archive.
+
+   here
+      Extract the file/directory in the current directory.
+
 -o, --overwrite
    Normally, dtrx will avoid extracting into a directory that already exists,
    and instead try to find an alternative name to use.  If this option is
    listed, dtrx will use the default directory name no matter what.
 
 -f, --flat
-   Extract archive contents into the current directory, instead of their
-   own dedicated directory.  This is handy if you have multiple archive
-   files which all need to be extracted into the same directory structure.
-   Note that existing files may be overwritten with this option.
+   Extract all archive contents into the current directory, instead of
+   their own dedicated directory.  This is handy if you have multiple
+   archive files which all need to be extracted into the same directory
+   structure.  Note that existing files may be overwritten with this
+   option.
 
 -n, --noninteractive
    dtrx will normally ask the user how to handle certain corner cases, such
--- a/scripts/dtrx	Wed Jul 23 12:15:45 2008 -0400
+++ b/scripts/dtrx	Thu Jul 24 22:15:19 2008 -0400
@@ -670,7 +670,19 @@
     def __init__(self, options):
         BasePolicy.__init__(self, options)
         if options.flat:
+            default = 'h'
+        elif options.one_entry_default is not None:
+            default = options.one_entry_default.lower()
+        else:
+            return
+        if 'here'.startswith(default):
             self.permanent_policy = EXTRACT_HERE
+        elif 'rename'.startswith(default):
+            self.permanent_policy = EXTRACT_RENAME
+        elif 'inside'.startswith(default):
+            self.permanent_policy = EXTRACT_WRAP
+        elif default is not None:
+            raise ValueError("bad value %s for default policy" % (default,))
 
     def prep(self, archive_filename, extractor):
         question = ["%s contains one %s, but it has a weird name." %
@@ -1012,37 +1024,44 @@
             description="Intelligent archive extractor",
             version=VERSION_BANNER
             )
-        parser.add_option('-r', '--recursive', dest='recursive',
-                          action='store_true', default=False,
-                          help='extract archives contained in the ones listed')
-        parser.add_option('-q', '--quiet', dest='quiet',
-                          action='count', default=3,
-                          help='suppress warning/error messages')
-        parser.add_option('-v', '--verbose', dest='verbose',
-                          action='count', default=0,
-                          help='be verbose/print debugging information')
-        parser.add_option('-o', '--overwrite', dest='overwrite',
-                          action='store_true', default=False,
-                          help='overwrite any existing target directory')
-        parser.add_option('-f', '--flat', '--no-directory', dest='flat',
-                          action='store_true', default=False,
-                          help="don't put contents in their own directory")
         parser.add_option('-l', '-t', '--list', '--table', dest='show_list',
                           action='store_true', default=False,
                           help="list contents of archives on standard output")
+        parser.add_option('-m', '--metadata', dest='metadata',
+                          action='store_true', default=False,
+                          help="extract metadata from a .deb/.gem")
+        parser.add_option('-r', '--recursive', dest='recursive',
+                          action='store_true', default=False,
+                          help="extract archives contained in the ones listed")
+        parser.add_option('--one', '--one-entry', dest='one_entry_default',
+                          default=None,
+                          help=("specify extraction policy for one-entry " +
+                                "archives: inside/rename/here"))
         parser.add_option('-n', '--noninteractive', dest='batch',
                           action='store_true', default=False,
                           help="don't ask how to handle special cases")
-        parser.add_option('-m', '--metadata', dest='metadata',
+        parser.add_option('-o', '--overwrite', dest='overwrite',
+                          action='store_true', default=False,
+                          help="overwrite any existing target output")
+        parser.add_option('-f', '--flat', '--no-directory', dest='flat',
                           action='store_true', default=False,
-                          help="extract metadata from a .deb/.gem")
+                          help="extract everything to the current directory")
+        parser.add_option('-v', '--verbose', dest='verbose',
+                          action='count', default=0,
+                          help="be verbose/print debugging information")
+        parser.add_option('-q', '--quiet', dest='quiet',
+                          action='count', default=3,
+                          help="suppress warning/error messages")
         self.options, filenames = parser.parse_args(arguments)
         if not filenames:
             parser.error("you did not list any archives")
         # This makes WARNING is the default.
         self.options.log_level = (10 * (self.options.quiet -
                                         self.options.verbose))
-        self.options.one_entry_policy = OneEntryPolicy(self.options)
+        try:
+            self.options.one_entry_policy = OneEntryPolicy(self.options)
+        except ValueError:
+            parser.error("invalid value for --one-entry option")
         self.options.recursion_policy = RecursionPolicy(self.options)
         self.archives = {os.path.realpath(os.curdir): filenames}
 
--- a/tests/tests.yml	Wed Jul 23 12:15:45 2008 -0400
+++ b/tests/tests.yml	Thu Jul 24 22:15:19 2008 -0400
@@ -243,7 +243,7 @@
     cd test-onedir
     tar -zxf ../$1
 
-- name: one directory extracted inside another (gz)
+- name: one directory extracted inside another interactively (gz)
   options: ""
   filenames: test-onedir.tar.gz
   grep: one directory
@@ -253,7 +253,7 @@
     cd test-onedir
     tar -zxf ../$1
 
-- name: one directory extracted with rename (gz)
+- name: one directory extracted with rename interactively (gz)
   options: ""
   filenames: test-onedir.tar.gz
   input: r
@@ -261,13 +261,34 @@
     tar -zxf $1
     mv test test-onedir
 
-- name: one directory extracted here (gz)
+- name: one directory extracted here interactively (gz)
   options: ""
   filenames: test-onedir.tar.gz
   input: h
   baseline: |
     tar -zxf $1
 
+- name: --one=inside
+  options: "--one=inside -n"
+  filenames: test-onedir.tar.gz
+  baseline: |
+    mkdir test-onedir
+    cd test-onedir
+    tar -zxf ../$1
+
+- name: --one=rename
+  options: "--one-entry=rename -n"
+  filenames: test-onedir.tar.gz
+  baseline: |
+    tar -zxf $1
+    mv test test-onedir
+
+- name: --one=here
+  options: "--one=here -n"
+  filenames: test-onedir.tar.gz
+  baseline: |
+    tar -zxf $1
+
 - name: default behavior with one directory (bz2)
   options: -n
   filenames: test-onedir.tar.gz

mercurial