1 | from django.core.servers.basehttp import app_media_dirs
|
---|
2 | from os import listdir, sep, mkdir
|
---|
3 | from os.path import abspath, basename, isdir
|
---|
4 | from shutil import copy
|
---|
5 |
|
---|
6 | def list_media_dirs():
|
---|
7 | for media_dir in app_media_dirs:
|
---|
8 | print media_dir
|
---|
9 |
|
---|
10 | def sync_media_root():
|
---|
11 | from django.conf import settings
|
---|
12 | media_file_list = []
|
---|
13 | media_dir_list = []
|
---|
14 | for media_dir in app_media_dirs:
|
---|
15 | files, dirs = tree(media_dir)
|
---|
16 | media_file_list = media_file_list + files
|
---|
17 | media_dir_list = media_dir_list + dirs
|
---|
18 | for dir in media_dir_list:
|
---|
19 | idx = dir.index('media')
|
---|
20 | rel_media_path = dir[idx+6:]
|
---|
21 | new_media_path = settings.MEDIA_ROOT + rel_media_path
|
---|
22 | if not isdir(new_media_path):
|
---|
23 | print "Creating %s in MEDIA_ROOT" % rel_media_path
|
---|
24 | mkdir(new_media_path)
|
---|
25 | for file in media_file_list:
|
---|
26 | idx = file.index('media')
|
---|
27 | rel_media_path = file[idx+6:]
|
---|
28 | new_media_path = settings.MEDIA_ROOT + rel_media_path
|
---|
29 | print "Syncing %s" % rel_media_path
|
---|
30 | copy(file, new_media_path)
|
---|
31 |
|
---|
32 |
|
---|
33 | def tree(dir):
|
---|
34 | files_list = []
|
---|
35 | dirs_list = []
|
---|
36 | files = []
|
---|
37 | files = listdir(dir)
|
---|
38 | count = 0
|
---|
39 | for file in files:
|
---|
40 | if file == '.svn':
|
---|
41 | continue
|
---|
42 | path = dir + sep + file
|
---|
43 | if isdir(path):
|
---|
44 | dirs_list.append(abspath(path))
|
---|
45 | files, dirs = tree(path)
|
---|
46 | files_list = files_list + files
|
---|
47 | dirs_list = dirs_list + dirs
|
---|
48 | else:
|
---|
49 | files_list.append(abspath(path))
|
---|
50 | return files_list, dirs_list
|
---|