source: main/waeup.kofa/trunk/tools/reload_portraits.py @ 9009

Last change on this file since 9009 was 8924, checked in by Henrik Bettermann, 12 years ago

Add Python script for batch reloading passport images.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1##
2## This script is called without parameters.
3##
4## It moves passport images from a reload folder to their right place in
5## the media folder.
6##
7## Configuration is done below.
8##
9##
10## Please note that for changing groups of files/directories, you
11## normally have to be root. This script is therefore normally run with
12## sudo.
13##
14
15## ######################################################################
16## CONFIGURATION
17##
18
19## The folder where all source docs can be found (can be any folder)
20SRC_DIR = "/kofa/uniben/var/datacenter/media/students/reload"
21
22## The students folder in Kofa where files should go to
23DST_DIR = "/kofa/uniben/var/datacenter/media/students"
24
25
26## Permissions to be set on new files/dirs. Set OWNER and/or GROUP to
27## None to leave them unchanged after creation.
28UMASK = 0664
29OWNER = 'henrik'
30GROUP = 'henrik'
31LETTER = 'B'
32
33##
34## CONFIGURATION END
35## ######################################################################
36
37import csv
38import os
39import shutil
40import sys
41from grp import getgrnam
42from pwd import getpwnam
43
44OWNER_ID = OWNER and getpwnam(OWNER)[2] or -1
45GRP_ID = GROUP and getgrnam(GROUP)[2] or -1
46DIR_UMASK = UMASK | 0111  # directories need extra x-permission
47
48def set_perms(path):
49    # set permissions and ownership for path
50    if os.path.isdir(path):
51        os.chmod(path, DIR_UMASK)
52    else:
53        os.chmod(path, UMASK)
54    os.chown(path, OWNER_ID, GRP_ID)
55    return
56
57def new_and_old_id(filename):
58    try:
59        stud_id, ext = filename.split('.')
60    except:
61        return None, None
62    if ext != 'jpg':
63        return None, None
64    if len(stud_id) == 7:
65        return LETTER + stud_id, stud_id
66    if len(stud_id) == 8:
67        return stud_id, stud_id
68    return None, None
69
70def final_subfolder(stud_id):
71    # compute new folder name from stud_id (old stud_ids or new stud_ids
72    # but not new old stud_ids)
73    num = int(stud_id[1:])
74    num = num / 10000 * 10
75    return '%05d' % num
76
77def copy_file(file_src, file_dst):
78    print "COPY FILE: %s -> %s" % (file_src, file_dst)
79    shutil.copyfile(file_src, file_dst)
80    set_perms(file_dst)
81    return
82
83if not os.path.exists(SRC_DIR):
84    sys.exit("No source folder %s. Skipping." % SRC_DIR)
85
86if len(os.listdir(SRC_DIR)) == 0:
87    sys.exit("Empty source folder for %s. Skipping." % SRC_DIR)
88
89for filename in os.listdir(SRC_DIR):
90    new_stud_id, stud_id = new_and_old_id(filename)
91    if not new_stud_id:
92        print "Wrong filename: %s" % filename
93        continue
94    subfolder = final_subfolder(stud_id)
95    dst_folder = os.path.join(DST_DIR, subfolder, new_stud_id)
96    if not os.path.exists(dst_folder):
97        print "Destination folder does not exist: %s. Skipping" % (dst_folder)
98        continue
99    dst_file = os.path.join(DST_DIR, subfolder, new_stud_id,
100        '%s_passport.jpg' % new_stud_id)
101    if os.path.exists(dst_file):
102        print "Destination file exists: %s. Skipping" % (dst_file)
103        continue
104    file_src = os.path.join(SRC_DIR, filename)
105    file_dst = os.path.join(dst_file)
106    copy_file(file_src, file_dst)
Note: See TracBrowser for help on using the repository browser.