terom@15: #!/usr/bin/env python2.4 terom@15: # terom@15: # DeGAL - A pretty simple web image gallery terom@15: # Copyright (C) 2007 Tero Marttila terom@15: # http://marttila.de/~terom/degal/ terom@15: # terom@15: # This program is free software; you can redistribute it and/or modify terom@15: # it under the terms of the GNU General Public License as published by terom@15: # the Free Software Foundation; either version 2 of the License, or terom@15: # (at your option) any later version. terom@15: # terom@15: # This program is distributed in the hope that it will be useful, terom@15: # but WITHOUT ANY WARRANTY; without even the implied warranty of terom@15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terom@15: # GNU General Public License for more details. terom@15: # terom@15: # You should have received a copy of the GNU General Public License terom@15: # along with this program; if not, write to the terom@15: # Free Software Foundation, Inc., terom@15: # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. terom@15: # terom@15: terom@15: from lib import settings terom@15: from lib.log import misc as log terom@15: from lib.utils import path_join terom@15: import os, os.path, logging terom@15: terom@15: def move (options, args) : terom@15: if len(args) < 2 : terom@15: raise Exception("Must give one or more source files, and a destination dir") terom@15: terom@15: dest = args.pop(-1) terom@15: srcs = args terom@15: terom@15: if not os.path.isdir(dest) : terom@15: raise Exception("Given destination '%s' is not a directory" % dest) terom@15: terom@15: for subdir in (settings.THUMB_DIR, settings.PREVIEW_DIR) : terom@15: path = os.path.join(dest, subdir) terom@15: terom@15: if not os.path.exists(path) : terom@15: log.info("mkdir %s" % path) terom@15: os.mkdir(path) terom@15: terom@15: for src in srcs : terom@15: if not os.path.isfile(src) : terom@15: raise Exception("Given source file '%s' is not a valid file" % src) terom@15: terom@15: for (pre, post) in ( terom@15: (None, None), terom@15: (settings.THUMB_DIR, None), terom@15: (settings.PREVIEW_DIR, None), terom@15: (None, '.html'), terom@15: ) : terom@15: dir, fname = os.path.split(src) terom@15: terom@15: if post : terom@15: fname += post terom@15: terom@15: src_path = path_join(dir, pre, fname) terom@15: dst_path = path_join(dest, pre, fname) terom@15: terom@15: if os.path.isfile(src_path) : terom@15: if not options.overwite and os.path.exists(dst_path) : terom@15: log.warning("%s exists; skipping %s" % (dst_path, src_path)) terom@15: log.info("%s -> %s" % (src_path, dst_path)) terom@15: os.rename(src_path, dst_path) terom@15: terom@15: def help (options, args) : terom@15: print "Available commands:" terom@15: terom@15: for name, func in COMMANDS.iteritems() : terom@15: print "\t%s" % name terom@15: terom@15: COMMANDS = dict( terom@15: move = move, terom@15: mv = move, terom@15: help = help, terom@15: ) terom@15: terom@15: if __name__ == '__main__' : terom@15: from optparse import OptionParser terom@15: terom@15: parser = OptionParser(usage="usage: %prog [options] [args ...]", version=settings.VERSION) terom@15: terom@15: parser.add_option("-q", "--quiet", dest="verbose", default=True) terom@15: parser.add_option("-i", "--careful", dest="overwrite", help="Do not overwrite files", default=True) terom@15: terom@15: options, args = parser.parse_args() terom@15: terom@15: if options.verbose : terom@15: log.setLevel(logging.INFO) terom@15: else : terom@15: log.setLevel(logging.ERROR) terom@15: terom@15: if not args : terom@15: parser.error("Must supply a command. See `detool.py help` for a list of commands") terom@15: terom@15: command = args.pop(0).lower() terom@15: terom@15: if command not in COMMANDS : terom@15: parser.error("Unknown command '%s'. Try `detool.py help`" % command) terom@15: terom@15: func = COMMANDS[command] terom@15: terom@15: func(options, args)