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