source: main/waeup.kofa/branches/uli-zc-async/tools/copy_media_files.py @ 10009

Last change on this file since 10009 was 9211, checked in by uli, 12 years ago

Rollback r9209. Looks like multiple merges from trunk confuse svn when merging back into trunk.

File size: 3.3 KB
RevLine 
[8523]1##
2## This script is called without parameters
3##
4## It copies media files from an old SRP folder over to a Kofa
5## instance media folder. It does not remove copied folders.
6##
7## Configuration is done below.
8##
9## At end a list of dirs that were copied or are empty is returned.
10##
11## Please note that for changing groups of files/directories, you
12## normally have to be root. This script is therefore normally run with
13## sudo.
14##
15
16## ######################################################################
17## CONFIGURATION
18##
19
20## The folder where all source docs can be found
21#SRC_DIR = "/zope/instances/aaue-images"
22SRC_DIR = "/home/uli/tmp/migration/src"
23
24## The students folder in Kofa where files should go to
25#DST_DIR = "/kofa/aaue/var/datacenter/media/students"
26DST_DIR = "/home/uli/tmp/migration/dst/media/students"
27
28## The Ids of new students to search for in old portal.
29NEW_IDS_CSV = "StudentIds.csv"
30
31## Permissions to be set on new files/dirs. Set OWNER and/or GROUP to
32## None to leave them unchanged after creation.
33UMASK = 0664
34OWNER = 'uli'
35GROUP = 'nogroup'
36
37##
38## CONFIGURATION END
39## ######################################################################
40
41import csv
42import os
43import shutil
44import sys
45from grp import getgrnam
46from pwd import getpwnam
47
48OWNER_ID = OWNER and getpwnam(OWNER)[2] or -1
49GRP_ID = GROUP and getgrnam(GROUP)[2] or -1
50DIR_UMASK = UMASK | 0111  # directories need extra x-permission
51
52def set_perms(path):
53    # set permissions and ownership for path
54    if os.path.isdir(path):
55        os.chmod(path, DIR_UMASK)
56    else:
57        os.chmod(path, UMASK)
58    os.chown(path, OWNER_ID, GRP_ID)
59    return
60
61def new_folder_name(stud_id):
62    # compute new folder name from stud_id (old stud_ids only)
63    num = int(stud_id[1:])
64    num = num / 10000 * 10
65    return '%05d' % num
66
67def create_path(path):
68    # create path with subdirs, if it does not exist (completely)
69    if os.path.exists(path):
70        return
71    parent = os.path.dirname(path)
72    if not os.path.exists(parent):
73        # create parent first
74        create_path(parent)
75    print "CREATE PATH ", path
76    os.mkdir(path)
77    set_perms(path)
78    return
79
[9211]80def copy_file(src, dst):
[8523]81    create_path(os.path.dirname(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
87removable_dirs = []
88reader = csv.DictReader(open(NEW_IDS_CSV, 'rb'))
89for row in reader:
90    new_stud_id = row['student_id']
91    stud_id = new_stud_id[1:]
92    src_folder = os.path.join(SRC_DIR, stud_id[0], stud_id)
93    dst_folder = os.path.join(DST_DIR, new_folder_name(stud_id), new_stud_id)
94    if not os.path.exists(src_folder):
95        print "No old data for %s. Skipping." % stud_id
96        continue
97    if os.path.exists(dst_folder):
98        print "Destination folder exists already: %s. Skipping" % (dst_folder)
99        continue
100    removable_dirs.append(src_folder)
101    src_files = os.listdir(src_folder)
102    if len(src_files) == 0:
103        print "Empty source folder for %s. Skipping." % stud_id
104        continue
105    for name in src_files:
106        file_src = os.path.join(src_folder, name)
107        file_dst = os.path.join(dst_folder, name.replace(stud_id, new_stud_id))
108        copy_file(file_src, file_dst)
109
110print "DIRS TO REMOVE: "
111for name in removable_dirs:
112    print name
Note: See TracBrowser for help on using the repository browser.