import os import shutil from elodie import constants def _decode(string, encoding='utf8'): """Return a utf8 encoded unicode string. Python2 and Python3 differ in how they handle strings. So we do a few checks to see if the string is ascii or unicode. Then we decode it if needed. """ if hasattr(string, 'decode'): # If the string is already unicode we return it. try: if isinstance(string, unicode): return string except NameError: pass return string.decode(encoding) return string def _copyfile(src, dst): # Python 3 hangs using open/write method if (constants.python_version == 3): shutil.copy(src, dst) return try: O_BINARY = os.O_BINARY except: O_BINARY = 0 READ_FLAGS = os.O_RDONLY | O_BINARY WRITE_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | O_BINARY TEN_MEGABYTES = 10485760 BUFFER_SIZE = min(TEN_MEGABYTES, os.path.getsize(src)) try: fin = os.open(src, READ_FLAGS) stat = os.fstat(fin) fout = os.open(dst, WRITE_FLAGS, stat.st_mode) for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""): os.write(fout, x) finally: try: os.close(fin) except: pass try: os.close(fout) except: pass