def update_package(self, package): db_package, created = Package.objects.get(name=package) languages = Language.objects.all() for language in languages: try: url = "%s.%s.po" % (db_package.src_url, language.short_name) remote_file = urlopen(url) (fd, filename) = tempfile.mkstemp(package) f = os.fdopen(fd, "w") for line in remote_file.readlines(): f.write(line) f.close() try: po = pofile(filename, autodetect_encoding=True, encoding='utf-8') except Exception, e: logging.error("Failed to open po file %s for %s" % (package, language)) logging.error("Error: %s" % str(e)) return populate_db(po, db_package, db_language) except Exception, e: logging.error("Failed to download the file located on %s" % url) logging.error("Error: %s" % str(e)) return
def handle(self, *args, **options): # Check if exists any Job and create them if necessary for language in Language.objects.all(): for package in Package.objects.all(): try: Job.objects.get(language=language, package=package) except Job.DoesNotExist: Job(language=language, package=package).save() active_job = Job.objects.filter(active=True) if active_job: logging.info( "There's a job being run right now for %s - %s later" % (active_job[0].package.name, active_job[0].language.short_name)) logging.info("Will try again later!") return job = Job.objects.order_by('update_on')[0] logging.info("Running task for %s - %s" % (job.package.name, job.language.short_name)) logging.info("Setting job to 'active'.") job.active = True job.save() try: url = "%s.%s.po" % (job.package.src_url, job.language.short_name) logging.info("Fetching file from %s" % url) remote_file = urlopen(url) (fd, filename) = tempfile.mkstemp(job.package.name) f = os.fdopen(fd, "w") for line in remote_file.readlines(): f.write(line) f.close() logging.info("File has been downloaded.") try: po = pofile(filename, autodetect_encoding=True, encoding='utf-8') populate_db(po, job.language, job.package) except Exception, e: logging.error("Failed to open po file %s for %s" % (job.package.name, job.language.short_name)) logging.error("Error: %s" % str(e)) except Exception, e: logging.error("Failed to download the file located on %s" % url) logging.error("Error: %s" % str(e))
def handle(self, *args, **options): # Check if exists any Job and create them if necessary for language in Language.objects.all(): for package in Package.objects.all(): try: Job.objects.get(language=language, package=package) except Job.DoesNotExist: Job(language=language, package=package).save() active_job = Job.objects.filter(active = True) if active_job: logging.info("There's a job being run right now for %s - %s later" % (active_job[0].package.name, active_job[0].language.short_name)) logging.info("Will try again later!") return job = Job.objects.order_by('update_on')[0] logging.info("Running task for %s - %s" % (job.package.name, job.language.short_name)) logging.info("Setting job to 'active'.") job.active = True job.save() try: url = "%s.%s.po" % (job.package.src_url, job.language.short_name) logging.info("Fetching file from %s" % url) remote_file = urlopen(url) (fd, filename) = tempfile.mkstemp(job.package.name) f = os.fdopen(fd, "w") for line in remote_file.readlines(): f.write(line) f.close() logging.info("File has been downloaded.") try: po = pofile(filename, autodetect_encoding=True, encoding='utf-8') populate_db(po, job.language, job.package) except Exception, e: logging.error("Failed to open po file %s for %s" % (job.package.name, job.language.short_name)) logging.error("Error: %s" % str(e)) except Exception, e: logging.error("Failed to download the file located on %s" % url) logging.error("Error: %s" % str(e))