def cmd_log(*args, **kwargs): import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pootle.settings") from datetime import datetime from django.conf import settings from pootle_app.project_tree import ensure_target_dir_exists _log = settings.LOGGING filename = _log.get("handlers", {}).get("log_action", {}).get("filename") if not filename: return ensure_target_dir_exists(filename) logfile = open(filename, "a") datefmt = _log.get("formatters", {}).get("action", {}).get("datefmt", "%Y-%m-%dT%H:%M:%S") message = "%(user)s\t%(action)s\t%(cmd)s" % { 'user': '******', 'action': CMD_EXECUTED, 'cmd': " ".join(args) } d = { 'message': message, 'datefmt': datetime.now().strftime(datefmt) } logfile.write("[%(datefmt)s]\t%(message)s\n" % d) logfile.close()
def export_as_xliff(request, store): """Export given file to xliff for offline translation.""" path = store.real_path if not path: # bug 2106 project = request.translation_project.project if project.get_treestyle() == "gnu": path = "/".join(store.pootle_path.split(os.path.sep)[2:]) else: parts = store.pootle_path.split(os.path.sep)[1:] path = "%s/%s/%s" % (parts[1], parts[0], "/".join(parts[2:])) path, ext = os.path.splitext(path) export_path = "/".join(['POOTLE_EXPORT', path + os.path.extsep + 'xlf']) abs_export_path = absolute_real_path(export_path) key = iri_to_uri("%s:export_as_xliff" % store.pootle_path) last_export = cache.get(key) if (not (last_export and last_export == store.get_mtime() and os.path.isfile(abs_export_path))): from pootle_app.project_tree import ensure_target_dir_exists from translate.storage.poxliff import PoXliffFile from pootle_misc import ptempfile as tempfile import shutil ensure_target_dir_exists(abs_export_path) outputstore = store.convert(PoXliffFile) outputstore.switchfile(store.name, createifmissing=True) fd, tempstore = tempfile.mkstemp(prefix=store.name, suffix='.xlf') os.close(fd) outputstore.savefile(tempstore) shutil.move(tempstore, abs_export_path) cache.set(key, store.get_mtime(), settings.OBJECT_CACHE_TIMEOUT) return redirect('/export/' + export_path)
def export_zip(request, translation_project, file_path): translation_project.sync() pootle_path = translation_project.pootle_path + (file_path or '') archivename = '%s-%s' % (translation_project.project.code, translation_project.language.code) if file_path.endswith('/'): file_path = file_path[:-1] if file_path: archivename += '-' + file_path.replace('/', '-') archivename += '.zip' export_path = os.path.join('POOTLE_EXPORT', translation_project.real_path, archivename) abs_export_path = absolute_real_path(export_path) key = iri_to_uri("%s:export_zip" % pootle_path) last_export = cache.get(key) if (not (last_export and last_export == translation_project.get_mtime() and os.path.isfile(abs_export_path))): ensure_target_dir_exists(abs_export_path) stores = Store.objects.filter(pootle_path__startswith=pootle_path) \ .exclude(file='') translation_project.get_archive(stores, abs_export_path) cache.set(key, translation_project.get_mtime(), settings.OBJECT_CACHE_TIMEOUT) return redirect('/export/' + export_path)
def export_as_type(request, store, filetype): """Export given file to xliff for offline translation.""" from pootle_store.filetypes import factory_classes, is_monolingual klass = factory_classes.get(filetype, None) if (not klass or is_monolingual(klass) or store.pootle_path.endswith(filetype)): raise ValueError path, ext = os.path.splitext(store.real_path) export_path = os.path.join('POOTLE_EXPORT', path + os.path.extsep + filetype) abs_export_path = absolute_real_path(export_path) key = iri_to_uri("%s:export_as_%s" % (store.pootle_path, filetype)) last_export = cache.get(key) if (not (last_export and last_export == store.get_mtime() and os.path.isfile(abs_export_path))): from pootle_app.project_tree import ensure_target_dir_exists from pootle_misc import ptempfile as tempfile import shutil ensure_target_dir_exists(abs_export_path) outputstore = store.convert(klass) fd, tempstore = tempfile.mkstemp(prefix=store.name, suffix=os.path.extsep + filetype) os.close(fd) outputstore.savefile(tempstore) shutil.move(tempstore, abs_export_path) cache.set(key, store.get_mtime(), settings.OBJECT_CACHE_TIMEOUT) return redirect('/export/' + export_path)
def export_as_type(request, pootle_path, filetype): """export given file to xliff for offline translation""" if pootle_path[0] != '/': pootle_path = '/' + pootle_path store = get_object_or_404(Store, pootle_path=pootle_path) klass = factory_classes.get(filetype, None) if not klass or is_monolingual(klass) or pootle_path.endswith(filetype): raise ValueError path, ext = os.path.splitext(store.real_path) export_path = os.path.join('POOTLE_EXPORT', path + os.path.extsep + filetype) abs_export_path = absolute_real_path(export_path) key = iri_to_uri("%s:export_as_%s" % (pootle_path, filetype)) last_export = cache.get(key) if not (last_export and last_export == store.get_mtime() and os.path.isfile(abs_export_path)): ensure_target_dir_exists(abs_export_path) outputstore = store.convert(klass) fd, tempstore = tempfile.mkstemp(prefix=store.name, suffix=os.path.extsep + filetype) os.close(fd) outputstore.savefile(tempstore) shutil.move(tempstore, abs_export_path) cache.set(key, store.get_mtime(), settings.OBJECT_CACHE_TIMEOUT) return redirect('/export/' + export_path)
def export_zip(request, translation_project, file_path): if not check_permission("archive", request): raise PermissionDenied(_('You do not have the right to create ZIP archives.')) translation_project.sync() pootle_path = translation_project.pootle_path + (file_path or '') archivename = '%s-%s' % (translation_project.project.code, translation_project.language.code) if file_path.endswith('/'): file_path = file_path[:-1] if file_path: archivename += '-' + file_path.replace('/', '-') archivename += '.zip' export_path = os.path.join('POOTLE_EXPORT', translation_project.real_path, archivename) abs_export_path = absolute_real_path(export_path) key = "%s:export_zip" % pootle_path last_export = cache.get(key) if not (last_export and last_export == translation_project.get_mtime() and os.path.isfile(abs_export_path)): ensure_target_dir_exists(abs_export_path) stores = Store.objects.filter(pootle_path__startswith=pootle_path).exclude(file='') translation_project.get_archive(stores, abs_export_path) cache.set(key, translation_project.get_mtime(), settings.OBJECT_CACHE_TIMEOUT) return redirect('/export/' + export_path)
def export_as_type(request, pootle_path, filetype): """export given file to xliff for offline translation""" if pootle_path[0] != '/': pootle_path = '/' + pootle_path store = get_object_or_404(Store, pootle_path=pootle_path) klass = factory_classes.get(filetype, None) if not klass or is_monolingual(klass) or \ pootle_path.endswith(filetype): raise ValueError path, ext = os.path.splitext(store.real_path) export_path = os.path.join( 'POOTLE_EXPORT', path + os.path.extsep + filetype) abs_export_path = absolute_real_path(export_path) key = iri_to_uri("%s:export_as_%s" % (pootle_path, filetype)) last_export = cache.get(key) if not (last_export and last_export == store.get_mtime() and os.path.isfile(abs_export_path)): ensure_target_dir_exists(abs_export_path) outputstore = store.convert(klass) file_desc, tempstore = tempfile.mkstemp( prefix=store.name, suffix=os.path.extsep + filetype) os.close(file_desc) outputstore.savefile(tempstore) shutil.move(tempstore, abs_export_path) cache.set(key, store.get_mtime(), settings.OBJECT_CACHE_TIMEOUT) return redirect('/export/' + export_path)
def set_download_file(self, path_obj, filepath): """Set file for download """ filename = relative_real_path(filepath) export_path = os.path.join('POOTLE_EXPORT', filename) abs_export_path = absolute_real_path(export_path) try: ensure_target_dir_exists(abs_export_path) shutil.copyfile(filepath, abs_export_path) except (IOError, OSError, shutil.Error), e: msg = (_("Failed to copy download file to export directory %s") % abs_export_path) logger.exception('%s', msg) return ''.join([msg, ": ", str(e)])
def set_download_file(self, path_obj, filepath): """Set file for download """ filename = relative_real_path(filepath) export_path = os.path.join('POOTLE_EXPORT', filename) abs_export_path = absolute_real_path(export_path) try: ensure_target_dir_exists(abs_export_path) shutil.copyfile(filepath, abs_export_path) except (IOError, OSError, shutil.Error) as e: msg = (_("Failed to copy download file to export directory %s") % abs_export_path) logger.exception('%s', msg) return ''.join([msg, ": ", str(e)]) cache.set(self._cache_key(path_obj), path_obj.get_mtime(), settings.OBJECT_CACHE_TIMEOUT) self._dl_path[path_obj.pootle_path] = export_path return ''
def export_zip(request, translation_project, file_path): from django.core.cache import cache from django.utils.encoding import iri_to_uri from django.utils.timezone import utc translation_project.sync() pootle_path = translation_project.pootle_path + (file_path or '') archivename = '%s-%s' % (translation_project.project.code, translation_project.language.code) if file_path.endswith('/'): file_path = file_path[:-1] if file_path: archivename += '-' + file_path.replace('/', '-') archivename += '.zip' export_path = os.path.join('POOTLE_EXPORT', translation_project.real_path, archivename) abs_export_path = absolute_real_path(export_path) key = iri_to_uri("%s:export_zip" % pootle_path) last_export = cache.get(key) tp_time = translation_project.get_mtime().replace(tzinfo=utc) up_to_date = False if last_export: # Make both datetimes tz-aware to avoid a crash here last_export = last_export.replace(tzinfo=utc) up_to_date = last_export == tp_time if not (up_to_date and os.path.isfile(abs_export_path)): ensure_target_dir_exists(abs_export_path) stores = Store.objects.filter(pootle_path__startswith=pootle_path) \ .exclude(file='') translation_project.get_archive(stores, abs_export_path) cache.set(key, tp_time, settings.OBJECT_CACHE_TIMEOUT) return redirect('/export/' + export_path)
def export_as_xliff(request, pootle_path): """export given file to xliff for offline translation""" if pootle_path[0] != '/': pootle_path = '/' + pootle_path store = get_object_or_404(Store, pootle_path=pootle_path) path, ext = os.path.splitext(store.real_path) export_path = os.path.join('POOTLE_EXPORT', path + os.path.extsep + 'xlf') abs_export_path = absolute_real_path(export_path) key = "%s:export_as_xliff" last_export = cache.get(key) if not (last_export and last_export == store.get_mtime() and os.path.isfile(abs_export_path)): ensure_target_dir_exists(abs_export_path) outputstore = store.convert(PoXliffFile) outputstore.switchfile(store.name, createifmissing=True) fd, tempstore = tempfile.mkstemp(prefix=store.name, suffix='.xlf') os.close(fd) outputstore.savefile(tempstore) shutil.move(tempstore, abs_export_path) cache.set(key, store.get_mtime(), settings.OBJECT_CACHE_TIMEOUT) return redirect('/export/' + export_path)
def export_as_xliff(request, store): """export given file to xliff for offline translation""" path, ext = os.path.splitext(store.real_path) export_path = os.path.join('POOTLE_EXPORT', path + os.path.extsep + 'xlf') abs_export_path = absolute_real_path(export_path) key = iri_to_uri("%s:export_as_xliff" % store.pootle_path) last_export = cache.get(key) if not (last_export and last_export == store.get_mtime() and os.path.isfile(abs_export_path)): from pootle_app.project_tree import ensure_target_dir_exists from translate.storage.poxliff import PoXliffFile import tempfile import shutil ensure_target_dir_exists(abs_export_path) outputstore = store.convert(PoXliffFile) outputstore.switchfile(store.name, createifmissing=True) fd, tempstore = tempfile.mkstemp(prefix=store.name, suffix='.xlf') os.close(fd) outputstore.savefile(tempstore) shutil.move(tempstore, abs_export_path) cache.set(key, store.get_mtime(), settings.OBJECT_CACHE_TIMEOUT) return redirect('/export/' + export_path)
def export_as_xliff(request, pootle_path): """export given file to xliff for offline translation""" if pootle_path[0] != '/': pootle_path = '/' + pootle_path store = get_object_or_404(Store, pootle_path=pootle_path) path, ext = os.path.splitext(store.real_path) export_path = os.path.join('POOTLE_EXPORT', path + os.path.extsep + 'xlf') abs_export_path = absolute_real_path(export_path) key = iri_to_uri("%s:export_as_xliff" % pootle_path) last_export = cache.get(key) if not (last_export and last_export == store.get_mtime() and os.path.isfile(abs_export_path)): ensure_target_dir_exists(abs_export_path) outputstore = store.convert(PoXliffFile) outputstore.switchfile(store.name, createifmissing=True) fd, tempstore = tempfile.mkstemp(prefix=store.name, suffix='.xlf') os.close(fd) outputstore.savefile(tempstore) shutil.move(tempstore, abs_export_path) cache.set(key, store.get_mtime(), settings.OBJECT_CACHE_TIMEOUT) return redirect('/export/' + export_path)