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

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

Create folders if they do not exist. This is dangerous and should only be applied to old student ids.

  • Property svn:keywords set to Id
File size: 3.5 KB
RevLine 
[8924]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)
[9018]20#SRC_DIR = "/kofa/uniben/var/datacenter/media/students/reload"
[10288]21SRC_DIR = "/kofa/uniben_passport_reload"
[8924]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
[9018]30OWNER = 'kofa'
31GROUP = 'kofa'
[8924]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):
[10288]72    # compute new folder name from stud_id (old stud_ids or new stud_ids)
73    try:
74        num = int(stud_id[1:])
75    except ValueError:
76        # new old stud_ids
77        num = int(stud_id[2:])
[8924]78    num = num / 10000 * 10
79    return '%05d' % num
80
81def copy_file(file_src, file_dst):
82    print "COPY FILE: %s -> %s" % (file_src, file_dst)
83    shutil.copyfile(file_src, file_dst)
84    set_perms(file_dst)
85    return
86
[10288]87def create_path(path):
88    # create path with subdirs, if it does not exist (completely)
89    if os.path.exists(path):
90        return
91    parent = os.path.dirname(path)
92    if not os.path.exists(parent):
93        # create parent first
94        create_path(parent)
95    print "CREATE PATH ", path
96    os.mkdir(path)
97    set_perms(path)
98    return
99
[8924]100if not os.path.exists(SRC_DIR):
101    sys.exit("No source folder %s. Skipping." % SRC_DIR)
102
103if len(os.listdir(SRC_DIR)) == 0:
104    sys.exit("Empty source folder for %s. Skipping." % SRC_DIR)
105
106for filename in os.listdir(SRC_DIR):
107    new_stud_id, stud_id = new_and_old_id(filename)
108    if not new_stud_id:
109        print "Wrong filename: %s" % filename
110        continue
111    subfolder = final_subfolder(stud_id)
112    dst_folder = os.path.join(DST_DIR, subfolder, new_stud_id)
113    if not os.path.exists(dst_folder):
[10288]114        #print "Destination folder does not exist: %s. Skipping" % (dst_folder)
115        #continue
116        create_path(dst_folder)
[8924]117    dst_file = os.path.join(DST_DIR, subfolder, new_stud_id,
[9020]118        'passport_%s.jpg' % new_stud_id)
[8924]119    if os.path.exists(dst_file):
120        print "Destination file exists: %s. Skipping" % (dst_file)
121        continue
122    file_src = os.path.join(SRC_DIR, filename)
123    file_dst = os.path.join(dst_file)
124    copy_file(file_src, file_dst)
Note: See TracBrowser for help on using the repository browser.