/
choose.py
executable file
·47 lines (37 loc) · 1.16 KB
/
choose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
"""Part of my photo revising suite of tools, including choose.sh and rename.sh.
Handles POSTs from the local HTML page generated by rename.sh for choosing which
pictures I want to keep.
There are two query parameters. dir is the fully qualified directory that
contains the pictures. keep is one or more picture filenames. This script makes
a no/ subdirectory and moves all the files there except the 'keep' ones.
"""
__author__ = ['Ryan Barrett <public@ryanb.org>']
import cgi
import cgitb
import errno
import glob
import os
import shutil
import stat
cgitb.enable()
form = cgi.FieldStorage()
dir = form.getfirst('dir')
no_subdir = os.path.join(dir, 'no')
try:
os.mkdir(no_subdir)
except OSError, e:
if e.errno != errno.EEXIST:
raise
os.chmod(no_subdir, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
to_keep = form.getlist('keep')
all_files = glob.glob(os.path.join(dir, '*.*'))
for f in all_files:
if os.path.isfile(f):
shutil.move(f, no_subdir)
print 'Content-Type: text/html'
print
print 'Keeping %d out of %d files in %s.' % (len(to_keep), len(all_files), dir)
for f in to_keep:
print '<br />' + f
shutil.move(os.path.join(no_subdir, f), dir)