source: main/waeup.kofa/branches/uli-stud-utils-cleanup/tools/reload_fpm.py @ 12677

Last change on this file since 12677 was 11618, checked in by Henrik Bettermann, 11 years ago

Add tool script to copy fpm files created with enroll-script.py to their right place in datacenter.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1##
2## This script is called without parameters.
3##
4## It moves fingerprint minutiae 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_fpm"
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'
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 final_subfolder(stud_id):
58    # compute new folder name from stud_id
59    num = int(stud_id[1:])
60    num = num / 1000
61    return '%05d' % num
62
63def copy_file(file_src, file_dst):
64    print "COPY FILE: %s -> %s" % (file_src, file_dst)
65    shutil.copyfile(file_src, file_dst)
66    set_perms(file_dst)
67    return
68
69def create_path(path):
70    # create path with subdirs, if it does not exist (completely)
71    if os.path.exists(path):
72        return
73    parent = os.path.dirname(path)
74    if not os.path.exists(parent):
75        # create parent first
76        create_path(parent)
77    print "CREATE PATH ", path
78    os.mkdir(path)
79    set_perms(path)
80    return
81
82if not os.path.exists(SRC_DIR):
83    sys.exit("No source folder %s. Skipping." % SRC_DIR)
84
85if len(os.listdir(SRC_DIR)) == 0:
86    sys.exit("Empty source folder for %s. Skipping." % SRC_DIR)
87
88for stud_id in os.listdir(SRC_DIR):
89    subfolder_name = final_subfolder(stud_id)
90    dst_folder = os.path.join(DST_DIR, subfolder_name, stud_id)
91    if not os.path.exists(dst_folder):
92        #print "Destination folder does not exist: %s. Skipping" % (dst_folder)
93        #continue
94        create_path(dst_folder)
95
96    src_subfolder = os.path.join(SRC_DIR, stud_id)
97    for fingernumber in os.listdir(src_subfolder):
98        src_file = os.path.join(SRC_DIR, src_subfolder, fingernumber)
99        dst_file = os.path.join(DST_DIR, subfolder_name, stud_id,
100            'finger%s_%s.fpm' % (fingernumber, stud_id))
101        if os.path.exists(dst_file):
102            print "Destination file exists: %s. Skipping" % (dst_file)
103            continue
104        copy_file(src_file, dst_file)
Note: See TracBrowser for help on using the repository browser.