| 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 logging |
|---|
| 17 | import datetime |
|---|
| 18 | from django.core.management import setup_environ |
|---|
| 19 | from django.core.files.base import ContentFile |
|---|
| 20 | |
|---|
| 21 | class Logger: |
|---|
| 22 | |
|---|
| 23 | def __init__(self, file): |
|---|
| 24 | self.logger = logging.getLogger('myapp') |
|---|
| 25 | self.hdlr = logging.FileHandler(file) |
|---|
| 26 | self.formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') |
|---|
| 27 | self.hdlr.setFormatter(self.formatter) |
|---|
| 28 | self.logger.addHandler(self.hdlr) |
|---|
| 29 | self.logger.setLevel(logging.INFO) |
|---|
| 30 | |
|---|
| 31 | def write_info(self, prefix, message): |
|---|
| 32 | self.logger.info(' ' + prefix + ' : ' + message.decode('utf8')) |
|---|
| 33 | |
|---|
| 34 | def write_error(self, prefix, message): |
|---|
| 35 | self.logger.error(prefix + ' : ' + message.decode('utf8')) |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | class TelemetaWavImport: |
|---|
| 39 | |
|---|
| 40 | def __init__(self, source_dir, log_file, pattern): |
|---|
| 41 | self.logger = Logger(log_file) |
|---|
| 42 | self.source_dir = source_dir |
|---|
| 43 | self.collections = os.listdir(self.source_dir) |
|---|
| 44 | self.pattern = pattern |
|---|
| 45 | |
|---|
| 46 | def wav_import(self): |
|---|
| 47 | from telemeta.models import MediaItem |
|---|
| 48 | for collection in self.collections: |
|---|
| 49 | collection_dir = self.source_dir + os.sep + collection |
|---|
| 50 | if not '/.' in collection_dir and self.pattern in collection_dir: |
|---|
| 51 | self.collection_name = collection.split(os.sep)[-1] |
|---|
| 52 | msg = '************************ ' + collection + ' ******************************' |
|---|
| 53 | self.logger.write_info(collection, msg[:70]) |
|---|
| 54 | |
|---|
| 55 | collection_files = os.listdir(collection_dir) |
|---|
| 56 | for filename in collection_files: |
|---|
| 57 | wav_file = self.source_dir + os.sep + collection + os.sep + filename |
|---|
| 58 | code = filename.split('.')[0] |
|---|
| 59 | print code |
|---|
| 60 | items = MediaItem.objects.filter(code=code) |
|---|
| 61 | if len(items) != 0: |
|---|
| 62 | item = items[0] |
|---|
| 63 | print item.code + ' : id = ' + str(item.id) + " : title = " + item.title |
|---|
| 64 | if os.path.exists(wav_file): |
|---|
| 65 | if not item.file : |
|---|
| 66 | f = open(wav_file, 'r') |
|---|
| 67 | file_content = ContentFile(f.read()) |
|---|
| 68 | item.file.save(filename, file_content) |
|---|
| 69 | f.close() |
|---|
| 70 | item.code = new_ref |
|---|
| 71 | item.save() |
|---|
| 72 | else: |
|---|
| 73 | msg = code + ' : fichier ' + wav_file + ' déjà ajouté !' |
|---|
| 74 | print msg |
|---|
| 75 | self.logger.write_error(collection, msg) |
|---|
| 76 | else: |
|---|
| 77 | msg = code + ' : fichier audio ' + wav_file + ' inexistant !' |
|---|
| 78 | print msg |
|---|
| 79 | self.logger.write_error(collection, msg) |
|---|
| 80 | else: |
|---|
| 81 | msg = code + ' : item inexistant dans la base de données !' |
|---|
| 82 | print msg |
|---|
| 83 | self.logger.write_error(collection, msg) |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | def print_usage(tool_name): |
|---|
| 87 | print "Usage: "+tool_name+" <project_dir> <source_dir> <pattern> <log_file>" |
|---|
| 88 | print " project_dir: the directory of the Django project which hosts Telemeta" |
|---|
| 89 | print " source_dir: the directory containing the wav files to include" |
|---|
| 90 | print " pattern: a pattern to match the collection names" |
|---|
| 91 | print " log_file: a log file to write logs" |
|---|
| 92 | |
|---|
| 93 | def run(): |
|---|
| 94 | if len(sys.argv) < 3: |
|---|
| 95 | print_usage(os.path.basename(sys.argv[0])) |
|---|
| 96 | sys.exit(1) |
|---|
| 97 | else: |
|---|
| 98 | project_dir = sys.argv[-4] |
|---|
| 99 | source_dir = sys.argv[-3] |
|---|
| 100 | pattern = sys.argv[-2] |
|---|
| 101 | log_file = sys.argv[-1] |
|---|
| 102 | sys.path.append(project_dir) |
|---|
| 103 | import settings |
|---|
| 104 | setup_environ(settings) |
|---|
| 105 | t = TelemetaWavImport(source_dir, log_file, pattern) |
|---|
| 106 | t.wav_import() |
|---|
| 107 | |
|---|
| 108 | if __name__ == '__main__': |
|---|
| 109 | run() |
|---|