scripts/detool
changeset 139 d3167c40e7b9
parent 138 130fb8a8dbb9
child 140 7ea9766e33ed
equal deleted inserted replaced
138:130fb8a8dbb9 139:d3167c40e7b9
     1 #!/usr/bin/env python2.4
       
     2 #
       
     3 # DeGAL - A pretty simple web image gallery
       
     4 # Copyright (C) 2007 Tero Marttila
       
     5 # http://marttila.de/~terom/degal/
       
     6 #
       
     7 # This program is free software; you can redistribute it and/or modify
       
     8 # it under the terms of the GNU General Public License as published by
       
     9 # the Free Software Foundation; either version 2 of the License, or
       
    10 # (at your option) any later version.
       
    11 #
       
    12 # This program is distributed in the hope that it will be useful,
       
    13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 # GNU General Public License for more details.
       
    16 #
       
    17 # You should have received a copy of the GNU General Public License
       
    18 # along with this program; if not, write to the
       
    19 # Free Software Foundation, Inc.,
       
    20 # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
       
    21 #
       
    22 
       
    23 from lib import settings
       
    24 from lib.log import misc as log
       
    25 from lib.utils import path_join
       
    26 import os, os.path, logging
       
    27 
       
    28 def move (options, args) :
       
    29     if len(args) < 2 :
       
    30         raise Exception("Must give one or more source files, and a destination dir")
       
    31     
       
    32     dest = args.pop(-1)
       
    33     srcs = args
       
    34     
       
    35     if not os.path.isdir(dest) :
       
    36         raise Exception("Given destination '%s' is not a directory" % dest)
       
    37     
       
    38     for subdir in (settings.THUMB_DIR, settings.PREVIEW_DIR) :
       
    39         path = os.path.join(dest, subdir)
       
    40         
       
    41         if not os.path.exists(path) :
       
    42             log.info("mkdir %s" % path)
       
    43             os.mkdir(path)
       
    44     
       
    45     for src in srcs :
       
    46         if not os.path.isfile(src) :
       
    47             raise Exception("Given source file '%s' is not a valid file" % src)
       
    48             
       
    49         for (pre, post) in (
       
    50             (None, None),
       
    51             (settings.THUMB_DIR, None),
       
    52             (settings.PREVIEW_DIR, None),
       
    53             (None, '.html'),
       
    54         ) :
       
    55             dir, fname = os.path.split(src)
       
    56             
       
    57             if post :
       
    58                 fname += post
       
    59             
       
    60             src_path = path_join(dir, pre, fname)
       
    61             dst_path = path_join(dest, pre, fname)
       
    62             
       
    63             if os.path.isfile(src_path) :
       
    64                 if not options.overwite and os.path.exists(dst_path) :
       
    65                     log.warning("%s exists; skipping %s" % (dst_path, src_path))
       
    66                 log.info("%s -> %s" % (src_path, dst_path))
       
    67                 os.rename(src_path, dst_path)
       
    68     
       
    69 def help (options, args) :
       
    70     print "Available commands:"
       
    71     
       
    72     for name, func in COMMANDS.iteritems() :
       
    73         print "\t%s" % name
       
    74     
       
    75 COMMANDS = dict(
       
    76     move    = move,
       
    77     mv      = move,
       
    78     help    = help,
       
    79 )
       
    80     
       
    81 if __name__ == '__main__' :
       
    82     from optparse import OptionParser
       
    83     
       
    84     parser = OptionParser(usage="usage: %prog <command> [options] [args ...]", version=settings.VERSION)
       
    85     
       
    86     parser.add_option("-q", "--quiet", dest="verbose", default=True)
       
    87     parser.add_option("-i", "--careful", dest="overwrite", help="Do not overwrite files", default=True)
       
    88     
       
    89     options, args = parser.parse_args()
       
    90     
       
    91     if options.verbose :
       
    92         log.setLevel(logging.INFO)
       
    93     else :
       
    94         log.setLevel(logging.ERROR)
       
    95     
       
    96     if not args :
       
    97         parser.error("Must supply a command. See `detool.py help` for a list of commands")
       
    98     
       
    99     command = args.pop(0).lower()
       
   100     
       
   101     if command not in COMMANDS :
       
   102         parser.error("Unknown command '%s'. Try `detool.py help`" % command)
       
   103     
       
   104     func = COMMANDS[command]
       
   105     
       
   106     func(options, args)