detool.py
author terom
Thu, 31 Jan 2008 19:19:05 +0000
changeset 30 b1d5c32ab771
parent 15 0fe851a29bc6
permissions -rw-r--r--
moar tweaks/small bugfixes
#!/usr/bin/env python2.4
#
# DeGAL - A pretty simple web image gallery
# Copyright (C) 2007 Tero Marttila
# http://marttila.de/~terom/degal/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

from lib import settings
from lib.log import misc as log
from lib.utils import path_join
import os, os.path, logging

def move (options, args) :
    if len(args) < 2 :
        raise Exception("Must give one or more source files, and a destination dir")
    
    dest = args.pop(-1)
    srcs = args
    
    if not os.path.isdir(dest) :
        raise Exception("Given destination '%s' is not a directory" % dest)
    
    for subdir in (settings.THUMB_DIR, settings.PREVIEW_DIR) :
        path = os.path.join(dest, subdir)
        
        if not os.path.exists(path) :
            log.info("mkdir %s" % path)
            os.mkdir(path)
    
    for src in srcs :
        if not os.path.isfile(src) :
            raise Exception("Given source file '%s' is not a valid file" % src)
            
        for (pre, post) in (
            (None, None),
            (settings.THUMB_DIR, None),
            (settings.PREVIEW_DIR, None),
            (None, '.html'),
        ) :
            dir, fname = os.path.split(src)
            
            if post :
                fname += post
            
            src_path = path_join(dir, pre, fname)
            dst_path = path_join(dest, pre, fname)
            
            if os.path.isfile(src_path) :
                if not options.overwite and os.path.exists(dst_path) :
                    log.warning("%s exists; skipping %s" % (dst_path, src_path))
                log.info("%s -> %s" % (src_path, dst_path))
                os.rename(src_path, dst_path)
    
def help (options, args) :
    print "Available commands:"
    
    for name, func in COMMANDS.iteritems() :
        print "\t%s" % name
    
COMMANDS = dict(
    move    = move,
    mv      = move,
    help    = help,
)
    
if __name__ == '__main__' :
    from optparse import OptionParser
    
    parser = OptionParser(usage="usage: %prog <command> [options] [args ...]", version=settings.VERSION)
    
    parser.add_option("-q", "--quiet", dest="verbose", default=True)
    parser.add_option("-i", "--careful", dest="overwrite", help="Do not overwrite files", default=True)
    
    options, args = parser.parse_args()
    
    if options.verbose :
        log.setLevel(logging.INFO)
    else :
        log.setLevel(logging.ERROR)
    
    if not args :
        parser.error("Must supply a command. See `detool.py help` for a list of commands")
    
    command = args.pop(0).lower()
    
    if command not in COMMANDS :
        parser.error("Unknown command '%s'. Try `detool.py help`" % command)
    
    func = COMMANDS[command]
    
    func(options, args)