source: main/waeup.kofa/backup-trunk/tools/reload_portraits.py @ 10371

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

Use right filename.

  • Property svn:keywords set to Id
File size: 3.1 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 = "/zope/instances/unibenpg-import/pictures_admitted_latest"
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 (old stud_ids or new stud_ids
73    # but not new old stud_ids)
74    num = int(stud_id[1:])
75    num = num / 10000 * 10
76    return '%05d' % num
77
78def copy_file(file_src, file_dst):
79    print "COPY FILE: %s -> %s" % (file_src, file_dst)
80    shutil.copyfile(file_src, file_dst)
81    set_perms(file_dst)
82    return
83
84if not os.path.exists(SRC_DIR):
85    sys.exit("No source folder %s. Skipping." % SRC_DIR)
86
87if len(os.listdir(SRC_DIR)) == 0:
88    sys.exit("Empty source folder for %s. Skipping." % SRC_DIR)
89
90for filename in os.listdir(SRC_DIR):
91    new_stud_id, stud_id = new_and_old_id(filename)
92    if not new_stud_id:
93        print "Wrong filename: %s" % filename
94        continue
95    subfolder = final_subfolder(stud_id)
96    dst_folder = os.path.join(DST_DIR, subfolder, new_stud_id)
97    if not os.path.exists(dst_folder):
98        print "Destination folder does not exist: %s. Skipping" % (dst_folder)
99        continue
100    dst_file = os.path.join(DST_DIR, subfolder, new_stud_id,
101        'passport_%s.jpg' % new_stud_id)
102    if os.path.exists(dst_file):
103        print "Destination file exists: %s. Skipping" % (dst_file)
104        continue
105    file_src = os.path.join(SRC_DIR, filename)
106    file_dst = os.path.join(dst_file)
107    copy_file(file_src, file_dst)
Note: See TracBrowser for help on using the repository browser.