def write_and_read(self, encoding, content): with NamedTemporaryFile() as f: filename = f.name with io.open(filename, 'w+b') as fh: fh.write(content) fh.flush() with auto_open(filename, 'rt', encoding=encoding) as handle: result = handle.read() os.unlink(filename) return result
def write_and_read(self, encoding, content): f = NamedTemporaryFile(delete=False) filename = f.name f.close() with codecs.open(filename, 'w+b', encoding=encoding) as fh: fh.write(content) fh.flush() handle = auto_open(filename, 'rU') result = handle.read() os.unlink(filename) return result
def md5_hash_file(full_path_name): ''' return md5 hash of a file ''' try: with auto_open(full_path_name, mode='r') as source_file: if sys.version_info[0] == 3: code_md5 = hashlib.md5(source_file.read().encode('utf-8')) else: code_md5 = hashlib.md5(source_file.read()) return code_md5.hexdigest() except IOError: return None except UnicodeDecodeError: return None
def md5_hash_file(full_path_name): ''' return md5 hash of a file ''' try: with auto_open(full_path_name, mode='r') as source_file: if sys.version_info[0] == 3: code_md5 = hashlib.md5(source_file.read().encode('utf-8')) else: code_md5 = hashlib.md5(source_file.read()) return code_md5.hexdigest() except IOError: return None except UnicodeDecodeError: return None except UnicodeEncodeError: return None
def __call__(self, filename): try: return self.analyze_source_code( filename, auto_open(filename, 'rU').read()) except UnicodeDecodeError: sys.stderr.write("Error: doesn't support none utf encoding '%s'\n" % filename) except IOError: sys.stderr.write("Error: Fail to read source file '%s'\n" % filename) except IndexError: sys.stderr.write("Error: Fail to parse file '%s'\n" % filename) raise return FileInformation(filename, 0, [])