def get_archive(self, stores, path=None): """returns an archive of the given filenames""" tempzipfile = None from pootle_misc import ptempfile as tempfile import shutil try: # using zip command line is fast # The temporary file below is opened and immediately closed for # security reasons fd, tempzipfile = tempfile.mkstemp(prefix='pootle', suffix='.zip') os.close(fd) result = os.system("cd %s ; zip -r - %s > %s" % (self.abs_real_path, " ".join(store.abs_real_path[len(self.abs_real_path)+1:] for \ store in stores.iterator()), tempzipfile)) if result == 0: if path is not None: shutil.move(tempzipfile, path) return else: filedata = open(tempzipfile, "r").read() if filedata: return filedata finally: if tempzipfile is not None and os.path.exists(tempzipfile): os.remove(tempzipfile) # but if it doesn't work, we can do it from python archivecontents = None try: if path is not None: fd, tempzipfile = tempfile.mkstemp(prefix='pootle', suffix='.zip') os.close(fd) archivecontents = open(tempzipfile, "wb") else: import cStringIO archivecontents = cStringIO.StringIO() import zipfile archive = zipfile.ZipFile(archivecontents, 'w', zipfile.ZIP_DEFLATED) for store in stores.iterator(): archive.write(store.abs_real_path.encode('utf-8'), store.abs_real_path[len(self.abs_real_path)+1:].encode(\ 'utf-8')) archive.close() if path is not None: shutil.move(tempzipfile, path) else: return archivecontents.getvalue() finally: if tempzipfile is not None and os.path.exists(tempzipfile): os.remove(tempzipfile) try: archivecontents.close() except: pass
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_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 unzip_external(request, directory, django_file, overwrite): # Make a temporary directory to hold a zip file and its unzipped contents. from pootle_misc import ptempfile as tempfile tempdir = tempfile.mkdtemp(prefix="pootle") # Make a temporary file to hold the zip file. tempzipfd, tempzipname = tempfile.mkstemp(prefix="pootle", suffix=".zip") try: # Dump the uploaded file to the temporary file. try: os.write(tempzipfd, django_file.read()) finally: os.close(tempzipfd) # Unzip the temporary zip file. import subprocess if subprocess.call(["unzip", tempzipname, "-d", tempdir]): import zipfile raise zipfile.BadZipfile(_("Error while extracting archive")) # Enumerate the temporary directory. maybe_skip = True prefix = tempdir for basedir, dirs, files in os.walk(tempdir): if maybe_skip and not files and len(dirs) == 1: try: directory.child_dirs.get(name=dirs[0]) maybe_skip = False except Directory.DoesNotExist: prefix = os.path.join(basedir, dirs[0]) continue else: maybe_skip = False for fname in files: # Read the contents of a file. fcontents = open(os.path.join(basedir, fname), "rb").read() newfile = StringIO.StringIO(fcontents) newfile.name = os.path.basename(fname) # Get the filesystem path relative to the temporary directory. subdir = host_to_unix_path(basedir[len(prefix) + len(os.sep) :]) if subdir: target_dir = directory.get_or_make_subdir(subdir) else: target_dir = directory # Construct a full UNIX path relative to the current # translation project URL by attaching a UNIXified # 'relative_host_dir' to the root relative path, i.e. the path # from which the user is uploading the ZIP file. try: upload_file(request, target_dir, newfile, overwrite) except ValueError: logging.exception(u"Error adding file %s", fname) finally: # Clean up temporary file and directory used in try-block. import shutil os.unlink(tempzipname) shutil.rmtree(tempdir)
def unzip_external(request, directory, django_file, overwrite): # Make a temporary directory to hold a zip file and its unzipped contents from pootle_misc import ptempfile as tempfile tempdir = tempfile.mkdtemp(prefix='pootle') # Make a temporary file to hold the zip file tempzipfd, tempzipname = tempfile.mkstemp(prefix='pootle', suffix='.zip') try: # Dump the uploaded file to the temporary file try: os.write(tempzipfd, django_file.read()) finally: os.close(tempzipfd) # Unzip the temporary zip file import subprocess if subprocess.call(["unzip", tempzipname, "-d", tempdir]): import zipfile raise zipfile.BadZipfile(_("Error while extracting archive")) # Enumerate the temporary directory... maybe_skip = True prefix = tempdir for basedir, dirs, files in os.walk(tempdir): if maybe_skip and not files and len(dirs) == 1: try: directory.child_dirs.get(name=dirs[0]) maybe_skip = False except Directory.DoesNotExist: prefix = os.path.join(basedir, dirs[0]) continue else: maybe_skip = False for fname in files: # Read the contents of a file... fcontents = open(os.path.join(basedir, fname), 'rb').read() newfile = StringIO.StringIO(fcontents) newfile.name = os.path.basename(fname) # Get the filesystem path relative to the temporary directory subdir = host_to_unix_path(basedir[len(prefix) + len(os.sep):]) if subdir: target_dir = directory.get_or_make_subdir(subdir) else: target_dir = directory # Construct a full UNIX path relative to the current # translation project URL by attaching a UNIXified # 'relative_host_dir' to the root relative path # (i.e. the path from which the user is uploading the # ZIP file. try: upload_file(request, target_dir, newfile, overwrite) except ValueError, e: logging.error(u"Error adding %s\t%s", fname, e) finally: # Clean up temporary file and directory used in try-block import shutil os.unlink(tempzipname) shutil.rmtree(tempdir)
def savestore(self): """Saves to temporary file then moves over original file. This way we avoid the need for locking.""" import shutil from pootle_misc import ptempfile as tempfile tmpfile, tmpfilename = tempfile.mkstemp(suffix=self.filename) os.close(tmpfile) self.store.savefile(tmpfilename) shutil.move(tmpfilename, self.realpath) self._touch_store_cache()
def savestore(self): """Saves to temporary file then moves over original file. This way we avoid the need for locking.""" import shutil from pootle_misc import ptempfile as tempfile tmpfile, tmpfilename = tempfile.mkstemp(suffix=self.filename) os.close(tmpfile) self.store.savefile(tmpfilename) #### HACK WHICH GLOBS MSGSTR ONTO SINGLE LINE with open(tmpfilename, 'r') as f: text = f.read() new_text = re.sub(r'msgid ".+"\nmsgstr ".*"\n(?:".*"\n)*', lambda m: m.group(0).replace('"\n"', ''), text) if new_text != text: with open(tmpfilename, 'w') as f: f.write(new_text) #### shutil.move(tmpfilename, self.realpath) self._touch_store_cache()
def get_archive(self, stores, path=None): """Returns an archive of the given files.""" import shutil import subprocess from pootle_misc import ptempfile as tempfile tempzipfile = None archivecontents = None try: # Using zip command line is fast # The temporary file below is opened and immediately closed for # security reasons fd, tempzipfile = tempfile.mkstemp(prefix='pootle', suffix='.zip') os.close(fd) archivecontents = open(tempzipfile, "wb") file_list = u" ".join( store.abs_real_path[len(self.abs_real_path)+1:] \ for store in stores.iterator() ) process = subprocess.Popen(['zip', '-r', '-', file_list], cwd=self.abs_real_path, stdout=archivecontents) result = process.wait() if result == 0: if path is not None: shutil.move(tempzipfile, path) return else: filedata = open(tempzipfile, "r").read() if filedata: return filedata else: raise Exception("failed to read temporary zip file") else: raise Exception("zip command returned error code: %d" % result) except Exception as e: # But if it doesn't work, we can do it from Python. logging.debug(e) logging.debug("falling back to zipfile module") if path is not None: if tempzipfile is None: fd, tempzipfile = tempfile.mkstemp(prefix='pootle', suffix='.zip') os.close(fd) archivecontents = open(tempzipfile, "wb") else: import cStringIO archivecontents = cStringIO.StringIO() import zipfile archive = zipfile.ZipFile(archivecontents, 'w', zipfile.ZIP_DEFLATED) for store in stores.iterator(): archive.write(store.abs_real_path.encode('utf-8'), store.abs_real_path[len(self.abs_real_path)+1:] .encode('utf-8')) archive.close() if path is not None: shutil.move(tempzipfile, path) else: return archivecontents.getvalue() finally: if tempzipfile is not None and os.path.exists(tempzipfile): os.remove(tempzipfile) try: archivecontents.close() except: pass
def get_archive(self, stores, path=None): """Returns an archive of the given files.""" import shutil from pootle_misc import ptempfile as tempfile tempzipfile = None try: # Using zip command line is fast # The temporary file below is opened and immediately closed for # security reasons fd, tempzipfile = tempfile.mkstemp(prefix='pootle', suffix='.zip') os.close(fd) file_list = u" ".join( store.abs_real_path[len(self.abs_real_path)+1:] \ for store in stores.iterator() ) cmd = u"cd %(path)s ; zip -r - %(file_list)s > %(tmpfile)s" % { 'path': self.abs_real_path, 'file_list': file_list, 'tmpfile': tempzipfile, } result = os.system(cmd.encode('utf-8')) if result == 0: if path is not None: shutil.move(tempzipfile, path) return else: filedata = open(tempzipfile, "r").read() if filedata: return filedata finally: if tempzipfile is not None and os.path.exists(tempzipfile): os.remove(tempzipfile) # But if it doesn't work, we can do it from python archivecontents = None try: if path is not None: fd, tempzipfile = tempfile.mkstemp(prefix='pootle', suffix='.zip') os.close(fd) archivecontents = open(tempzipfile, "wb") else: import cStringIO archivecontents = cStringIO.StringIO() import zipfile archive = zipfile.ZipFile(archivecontents, 'w', zipfile.ZIP_DEFLATED) for store in stores.iterator(): archive.write( store.abs_real_path.encode('utf-8'), store.abs_real_path[len(self.abs_real_path) + 1:].encode('utf-8')) archive.close() if path is not None: shutil.move(tempzipfile, path) else: return archivecontents.getvalue() finally: if tempzipfile is not None and os.path.exists(tempzipfile): os.remove(tempzipfile) try: archivecontents.close() except: pass
def get_archive(self, stores, path=None): """Returns an archive of the given files.""" import shutil from pootle_misc import ptempfile as tempfile tempzipfile = None try: # Using zip command line is fast # The temporary file below is opened and immediately closed for # security reasons fd, tempzipfile = tempfile.mkstemp(prefix="pootle", suffix=".zip") os.close(fd) file_list = u" ".join(store.abs_real_path[len(self.abs_real_path) + 1 :] for store in stores.iterator()) cmd = u"cd %(path)s ; zip -r - %(file_list)s > %(tmpfile)s" % { "path": self.abs_real_path, "file_list": file_list, "tmpfile": tempzipfile, } result = os.system(cmd.encode("utf-8")) if result == 0: if path is not None: shutil.move(tempzipfile, path) return else: filedata = open(tempzipfile, "r").read() if filedata: return filedata finally: if tempzipfile is not None and os.path.exists(tempzipfile): os.remove(tempzipfile) # But if it doesn't work, we can do it from python archivecontents = None try: if path is not None: fd, tempzipfile = tempfile.mkstemp(prefix="pootle", suffix=".zip") os.close(fd) archivecontents = open(tempzipfile, "wb") else: import cStringIO archivecontents = cStringIO.StringIO() import zipfile archive = zipfile.ZipFile(archivecontents, "w", zipfile.ZIP_DEFLATED) for store in stores.iterator(): archive.write( store.abs_real_path.encode("utf-8"), store.abs_real_path[len(self.abs_real_path) + 1 :].encode("utf-8"), ) archive.close() if path is not None: shutil.move(tempzipfile, path) else: return archivecontents.getvalue() finally: if tempzipfile is not None and os.path.exists(tempzipfile): os.remove(tempzipfile) try: archivecontents.close() except: pass