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

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

This should be now a generic solution for all kinds of student ids.

  • Property svn:keywords set to Id
File size: 3.6 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)
20#SRC_DIR = "/kofa/uniben/var/datacenter/media/students/reload"
21SRC_DIR = "/kofa/uniben_passport_reload"
22
23## The students folder in Kofa where files should go to
24DST_DIR = "/kofa/uniben/var/datacenter/media/students"
25
26
27## Permissions to be set on new files/dirs. Set OWNER and/or GROUP to
28## None to leave them unchanged after creation.
29UMASK = 0664
30OWNER = 'kofa'
31GROUP = 'kofa'
32LETTER = 'B'
33
34##
35## CONFIGURATION END
36## ######################################################################
37
38import csv
39import os
40import shutil
41import sys
42from grp import getgrnam
43from pwd import getpwnam
44
45OWNER_ID = OWNER and getpwnam(OWNER)[2] or -1
46GRP_ID = GROUP and getgrnam(GROUP)[2] or -1
47DIR_UMASK = UMASK | 0111  # directories need extra x-permission
48
49def set_perms(path):
50    # set permissions and ownership for path
51    if os.path.isdir(path):
52        os.chmod(path, DIR_UMASK)
53    else:
54        os.chmod(path, UMASK)
55    os.chown(path, OWNER_ID, GRP_ID)
56    return
57
58def new_and_old_id(filename):
59    try:
60        stud_id, ext = filename.split('.')
61    except:
62        return None, None
63    if ext != 'jpg':
64        return None, None
65    if len(stud_id) == 7:
66        return LETTER + stud_id, stud_id
67    if len(stud_id) == 8:
68        return stud_id, stud_id
69    return None, None
70
71def final_subfolder(stud_id):
72    # compute new folder name from stud_id
73    try:
74        num = int(stud_id[1:])
75        if len(stud_id) == 7:
76            # old stud_id
77            num = num / 10000 * 10
78        if len(stud_id) == 8:
79            # new stud_id
80            num = num / 1000
81    except ValueError:
82        # new old stud_ids
83        num = int(stud_id[2:])
84        num = num / 10000 * 10
85    return '%05d' % num
86
87def copy_file(file_src, file_dst):
88    print "COPY FILE: %s -> %s" % (file_src, file_dst)
89    shutil.copyfile(file_src, file_dst)
90    set_perms(file_dst)
91    return
92
93def create_path(path):
94    # create path with subdirs, if it does not exist (completely)
95    if os.path.exists(path):
96        return
97    parent = os.path.dirname(path)
98    if not os.path.exists(parent):
99        # create parent first
100        create_path(parent)
101    print "CREATE PATH ", path
102    os.mkdir(path)
103    set_perms(path)
104    return
105
106if not os.path.exists(SRC_DIR):
107    sys.exit("No source folder %s. Skipping." % SRC_DIR)
108
109if len(os.listdir(SRC_DIR)) == 0:
110    sys.exit("Empty source folder for %s. Skipping." % SRC_DIR)
111
112for filename in os.listdir(SRC_DIR):
113    new_stud_id, stud_id = new_and_old_id(filename)
114    if not new_stud_id:
115        print "Wrong filename: %s" % filename
116        continue
117    subfolder = final_subfolder(stud_id)
118    dst_folder = os.path.join(DST_DIR, subfolder, new_stud_id)
119    if not os.path.exists(dst_folder):
120        #print "Destination folder does not exist: %s. Skipping" % (dst_folder)
121        #continue
122        create_path(dst_folder)
123    dst_file = os.path.join(DST_DIR, subfolder, new_stud_id,
124        'passport_%s.jpg' % new_stud_id)
125    if os.path.exists(dst_file):
126        print "Destination file exists: %s. Skipping" % (dst_file)
127        continue
128    file_src = os.path.join(SRC_DIR, filename)
129    file_dst = os.path.join(dst_file)
130    copy_file(file_src, file_dst)
Note: See TracBrowser for help on using the repository browser.