[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" |
---|
| 21 | SRC_DIR = "/zope/instances/unibenpg-import/pictures_admitted_latest" |
---|
[8924] | 22 | |
---|
| 23 | ## The students folder in Kofa where files should go to |
---|
| 24 | DST_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. |
---|
| 29 | UMASK = 0664 |
---|
[9018] | 30 | OWNER = 'kofa' |
---|
| 31 | GROUP = 'kofa' |
---|
[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): |
---|
| 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 | |
---|
| 78 | def 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 | |
---|
| 84 | if not os.path.exists(SRC_DIR): |
---|
| 85 | sys.exit("No source folder %s. Skipping." % SRC_DIR) |
---|
| 86 | |
---|
| 87 | if len(os.listdir(SRC_DIR)) == 0: |
---|
| 88 | sys.exit("Empty source folder for %s. Skipping." % SRC_DIR) |
---|
| 89 | |
---|
| 90 | for 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, |
---|
[9020] | 101 | 'passport_%s.jpg' % new_stud_id) |
---|
[8924] | 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) |
---|