author terom
Fri, 21 Dec 2007 21:19:01 +0000
changeset 15 0fe851a29bc6
permissions -rw-r--r--, meant to be a collection of utility operations, currently lets you move images and their html/thumb/preview files at the same time
#!/usr/bin/env python2.4
# DeGAL - A pretty simple web image gallery
# Copyright (C) 2007 Tero Marttila
# 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
# 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) :
  "mkdir %s" % 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))
      "%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 :
    else :
    if not args :
        parser.error("Must supply a command. See ` help` for a list of commands")
    command = args.pop(0).lower()
    if command not in COMMANDS :
        parser.error("Unknown command '%s'. Try ` help`" % command)
    func = COMMANDS[command]
    func(options, args)