| 1 | #!/usr/bin/python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Copyright (C) 2010 Guillaume Pellerin |
|---|
| 5 | # All rights reserved. |
|---|
| 6 | # |
|---|
| 7 | # This software is licensed as described in the file COPYING, which |
|---|
| 8 | # you should have received as part of this distribution. The terms |
|---|
| 9 | # are also available at http://svn.parisson.org/telemeta/TelemetaLicense. |
|---|
| 10 | # |
|---|
| 11 | # Author: Guillaume Pellerin <yomguy@parisson.com> |
|---|
| 12 | # |
|---|
| 13 | |
|---|
| 14 | import os |
|---|
| 15 | import sys |
|---|
| 16 | import csv |
|---|
| 17 | import logging |
|---|
| 18 | import datetime |
|---|
| 19 | from django.core.management import setup_environ |
|---|
| 20 | from django.core.files.base import ContentFile |
|---|
| 21 | from django.contrib.auth.models import User |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | class Logger: |
|---|
| 25 | |
|---|
| 26 | def __init__(self, file): |
|---|
| 27 | self.logger = logging.getLogger('myapp') |
|---|
| 28 | self.hdlr = logging.FileHandler(file) |
|---|
| 29 | self.formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') |
|---|
| 30 | self.hdlr.setFormatter(self.formatter) |
|---|
| 31 | self.logger.addHandler(self.hdlr) |
|---|
| 32 | self.logger.setLevel(logging.INFO) |
|---|
| 33 | |
|---|
| 34 | def info(self, prefix, message): |
|---|
| 35 | self.logger.info(' ' + prefix + ' : ' + message.decode('utf8')) |
|---|
| 36 | |
|---|
| 37 | def error(self, prefix, message): |
|---|
| 38 | self.logger.error(prefix + ' : ' + message.decode('utf8')) |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | class TelemetaWavImport: |
|---|
| 42 | |
|---|
| 43 | def __init__(self, source_dir, log_file, pattern): |
|---|
| 44 | self.logger = Logger(log_file) |
|---|
| 45 | self.source_dir = source_dir |
|---|
| 46 | self.collections = os.listdir(self.source_dir) |
|---|
| 47 | self.pattern = pattern |
|---|
| 48 | self.user = User.objects.filter(username='admin') |
|---|
| 49 | |
|---|
| 50 | def write_file(self, item, wav_file, overwrite=False): |
|---|
| 51 | if os.path.exists(wav_file): |
|---|
| 52 | if not item.file or overwrite: |
|---|
| 53 | f = open(wav_file, 'r') |
|---|
| 54 | file_content = ContentFile(f.read()) |
|---|
| 55 | item.file.save(filename, file_content) |
|---|
| 56 | f.close() |
|---|
| 57 | item.code = new_ref |
|---|
| 58 | item.save() |
|---|
| 59 | item.set_revision(self.user) |
|---|
| 60 | else: |
|---|
| 61 | msg = code + ' : fichier ' + wav_file + ' déjà existant !' |
|---|
| 62 | self.logger.error(collection, msg) |
|---|
| 63 | else: |
|---|
| 64 | msg = code + ' : fichier audio ' + wav_file + ' inexistant !' |
|---|
| 65 | self.logger.error(collection, msg) |
|---|
| 66 | |
|---|
| 67 | def wav_import(self): |
|---|
| 68 | from telemeta.models import MediaItem, MediaCollection |
|---|
| 69 | |
|---|
| 70 | for collection in self.collections: |
|---|
| 71 | collection_dir = self.source_dir + os.sep + collection |
|---|
| 72 | collection_files = os.listdir(collection_dir) |
|---|
| 73 | if not '/.' in collection_dir and self.pattern in collection_dir: |
|---|
| 74 | collection_name = collection.split(os.sep)[-1] |
|---|
| 75 | c = MediaCollection.objects.filter(code=collection_name) |
|---|
| 76 | if not c and collection + '.csv' in collection_files: |
|---|
| 77 | sys.exit(msg = collection + ' collection NON présente dans la base de données, SORTIE ') |
|---|
| 78 | elif not c: |
|---|
| 79 | msg = 'collection NON présente dans la base de données, SORTIE ' |
|---|
| 80 | self.logger.info(collection, msg) |
|---|
| 81 | c = MediaCollection(code=collection_name) |
|---|
| 82 | c.save() |
|---|
| 83 | c.set_revision(self.user) |
|---|
| 84 | else: |
|---|
| 85 | msg = 'collection présente dans la base de données, CONTINUE ' |
|---|
| 86 | self.logger.info(collection, msg) |
|---|
| 87 | |
|---|
| 88 | for collection in self.collections: |
|---|
| 89 | collection_dir = self.source_dir + os.sep + collection |
|---|
| 90 | if not '/.' in collection_dir and self.pattern in collection_dir: |
|---|
| 91 | collection_name = collection.split(os.sep)[-1] |
|---|
| 92 | msg = '************************ ' + collection + ' ******************************' |
|---|
| 93 | self.logger.info(collection, msg[:70]) |
|---|
| 94 | collection_files = os.listdir(collection_dir) |
|---|
| 95 | |
|---|
| 96 | if not collection + '.csv' in collection_files: |
|---|
| 97 | msg = collection + ' pas de fichier CSV dans la collection' |
|---|
| 98 | self.logger.info(collection, msg[:70]) |
|---|
| 99 | c = MediaCollection.objects.filter(code=collection_name) |
|---|
| 100 | if not c: |
|---|
| 101 | msg = collection + ' collection NON présente dans la BDD, CREATION ' |
|---|
| 102 | self.logger.info(collection, msg) |
|---|
| 103 | c = MediaCollection(code=collection_name) |
|---|
| 104 | c.save() |
|---|
| 105 | else: |
|---|
| 106 | c = c[0] |
|---|
| 107 | |
|---|
| 108 | for filename in collection_files: |
|---|
| 109 | wav_file = self.source_dir + os.sep + collection + os.sep + filename |
|---|
| 110 | code = filename.split('.')[0] |
|---|
| 111 | items = MediaItem.objects.filter(code=code) |
|---|
| 112 | if len(items) != 0: |
|---|
| 113 | msg = ' : id = '+str(item.id)+" : title = "+item.title+' SELECTION' |
|---|
| 114 | self.logger.info(item, msg) |
|---|
| 115 | self.logger.info(collection, msg) |
|---|
| 116 | item = items[0] |
|---|
| 117 | else: |
|---|
| 118 | msg = item.code + ' : item NON présent dans la base de données, CREATION' |
|---|
| 119 | self.logger.info(item, msg) |
|---|
| 120 | item = MediaItem(code=code, collection=c) |
|---|
| 121 | self.write_file(item, wav_file, True) |
|---|
| 122 | |
|---|
| 123 | else: |
|---|
| 124 | c = csv.reader(open(self.source_dir + os.sep + collection + os.sep + collection + '.csv'), delimiter=';') |
|---|
| 125 | for row in c: |
|---|
| 126 | old_ref = row[0] |
|---|
| 127 | new_ref = row[1] |
|---|
| 128 | filename = new_ref + '.wav' |
|---|
| 129 | wav_file = self.source_dir + os.sep + collection + os.sep + filename |
|---|
| 130 | items = MediaItem.objects.filter(old_code=old_ref) |
|---|
| 131 | if items: |
|---|
| 132 | item = items[0] |
|---|
| 133 | msg = item.old_code + ' : id = ' + str(item.id) + " : title = " + item.title |
|---|
| 134 | self.logger.info(item, msg) |
|---|
| 135 | self.write_file(item, wav_file, True) |
|---|
| 136 | else: |
|---|
| 137 | msg = old_ref + ' : item inexistant dans la base de données ! PASS' |
|---|
| 138 | self.logger.error(item, msg) |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | def print_usage(tool_name): |
|---|
| 142 | print "Usage: "+tool_name+" <project_dir> <source_dir> <pattern> <log_file>" |
|---|
| 143 | print " project_dir: the directory of the Django project which hosts Telemeta" |
|---|
| 144 | print " source_dir: the directory containing the wav files to include" |
|---|
| 145 | print " pattern: a pattern to match the collection names" |
|---|
| 146 | print " log_file: a log file to write logs" |
|---|
| 147 | |
|---|
| 148 | def run(): |
|---|
| 149 | if len(sys.argv) < 3: |
|---|
| 150 | print_usage(os.path.basename(sys.argv[0])) |
|---|
| 151 | sys.exit(1) |
|---|
| 152 | else: |
|---|
| 153 | project_dir = sys.argv[-4] |
|---|
| 154 | source_dir = sys.argv[-3] |
|---|
| 155 | pattern = sys.argv[-2] |
|---|
| 156 | log_file = sys.argv[-1] |
|---|
| 157 | sys.path.append(project_dir) |
|---|
| 158 | import settings |
|---|
| 159 | setup_environ(settings) |
|---|
| 160 | t = TelemetaWavImport(source_dir, log_file, pattern) |
|---|
| 161 | t.wav_import() |
|---|
| 162 | |
|---|
| 163 | if __name__ == '__main__': |
|---|
| 164 | run() |
|---|