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 = "/ikoba/uniben/var/datacenter/media/students/reload" |
---|
21 | SRC_DIR = "/ikoba/uniben_fpm" |
---|
22 | |
---|
23 | ## The students folder in Ikoba where files should go to |
---|
24 | DST_DIR = "/ikoba/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. |
---|
29 | UMASK = 0664 |
---|
30 | OWNER = 'ikoba' |
---|
31 | GROUP = 'ikoba' |
---|
32 | |
---|
33 | ## |
---|
34 | ## CONFIGURATION END |
---|
35 | ## ###################################################################### |
---|
36 | |
---|
37 | import csv |
---|
38 | import os |
---|
39 | import shutil |
---|
40 | import sys |
---|
41 | from grp import getgrnam |
---|
42 | from pwd import getpwnam |
---|
43 | |
---|
44 | OWNER_ID = OWNER and getpwnam(OWNER)[2] or -1 |
---|
45 | GRP_ID = GROUP and getgrnam(GROUP)[2] or -1 |
---|
46 | DIR_UMASK = UMASK | 0111 # directories need extra x-permission |
---|
47 | |
---|
48 | def 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 | |
---|
57 | def 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 | |
---|
63 | def 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 | |
---|
69 | def 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 | |
---|
82 | if not os.path.exists(SRC_DIR): |
---|
83 | sys.exit("No source folder %s. Skipping." % SRC_DIR) |
---|
84 | |
---|
85 | if len(os.listdir(SRC_DIR)) == 0: |
---|
86 | sys.exit("Empty source folder for %s. Skipping." % SRC_DIR) |
---|
87 | |
---|
88 | for 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) |
---|