[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) |
---|
[11949] | 20 | #SRC_DIR = "/ikoba/uniben/var/datacenter/media/students/reload" |
---|
| 21 | SRC_DIR = "/ikoba/uniben_passport_reload" |
---|
[8924] | 22 | |
---|
[11949] | 23 | ## The students folder in Ikoba where files should go to |
---|
| 24 | DST_DIR = "/ikoba/uniben/var/datacenter/media/students" |
---|
[8924] | 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 |
---|
[11949] | 30 | OWNER = 'ikoba' |
---|
| 31 | GROUP = 'ikoba' |
---|
[8924] | 32 | LETTER = 'B' |
---|
| 33 | |
---|
| 34 | ## |
---|
| 35 | ## CONFIGURATION END |
---|
| 36 | ## ###################################################################### |
---|
| 37 | |
---|
| 38 | import csv |
---|
| 39 | import os |
---|
| 40 | import shutil |
---|
| 41 | import sys |
---|
| 42 | from grp import getgrnam |
---|
| 43 | from pwd import getpwnam |
---|
| 44 | |
---|
| 45 | OWNER_ID = OWNER and getpwnam(OWNER)[2] or -1 |
---|
| 46 | GRP_ID = GROUP and getgrnam(GROUP)[2] or -1 |
---|
| 47 | DIR_UMASK = UMASK | 0111 # directories need extra x-permission |
---|
| 48 | |
---|
| 49 | def 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 | |
---|
| 58 | def 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 | |
---|
| 71 | def final_subfolder(stud_id): |
---|
[10289] | 72 | # compute new folder name from stud_id |
---|
[10288] | 73 | try: |
---|
| 74 | num = int(stud_id[1:]) |
---|
[10289] | 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 |
---|
[10288] | 81 | except ValueError: |
---|
| 82 | # new old stud_ids |
---|
| 83 | num = int(stud_id[2:]) |
---|
[10289] | 84 | num = num / 10000 * 10 |
---|
[8924] | 85 | return '%05d' % num |
---|
| 86 | |
---|
| 87 | def 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 | |
---|
[10288] | 93 | def 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 | |
---|
[8924] | 106 | if not os.path.exists(SRC_DIR): |
---|
| 107 | sys.exit("No source folder %s. Skipping." % SRC_DIR) |
---|
| 108 | |
---|
| 109 | if len(os.listdir(SRC_DIR)) == 0: |
---|
| 110 | sys.exit("Empty source folder for %s. Skipping." % SRC_DIR) |
---|
| 111 | |
---|
| 112 | for 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): |
---|
[10288] | 120 | #print "Destination folder does not exist: %s. Skipping" % (dst_folder) |
---|
| 121 | #continue |
---|
| 122 | create_path(dst_folder) |
---|
[8924] | 123 | dst_file = os.path.join(DST_DIR, subfolder, new_stud_id, |
---|
[9020] | 124 | 'passport_%s.jpg' % new_stud_id) |
---|
[8924] | 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) |
---|