def test_check_file_symlink_broken(self): f = self.make_temp_file(content = 'kiwi') l = self.make_temp_file(content = 'link') file_symlink.symlink(f, l) file_util.remove(f) with self.assertRaises(IOError) as ctx: file_check.check_file(l)
def test_check_file_failure(self): d = self.make_temp_dir() with self.assertRaises(IOError) as ctx: file_check.check_file(d) s = self._make_temp_socket('kiwi.socket') with self.assertRaises(IOError) as ctx: file_check.check_file(s)
def extract(clazz, dmg, dst_dir): file_check.check_file(dmg) file_util.mkdir(dst_dir) mnt = clazz._mount_at_temp_dir(dmg) #clazz._fix_extracted_dir_permissions(mnt.mount_point) file_copy.copy_tree(mnt.mount_point, dst_dir) clazz._eject(mnt.mount_point)
def __new__(clazz, filename): filename = path.abspath(filename) check.check_string(filename) file_check.check_file(filename) content = file_util.read(filename, codec='utf-8') root_dir = path.normpath(path.join(path.dirname(filename), '..')) data = config_data.parse(content, filename=filename) return clazz.__bases__[0].__new__(clazz, root_dir, filename, data)
def build(self, script_filename, output_filename): file_check.check_file(script_filename) check.check_string(output_filename) script_filename_abs = path.abspath(script_filename) output_filename_abs = path.abspath(output_filename) result = pyinstaller_build.build(script_filename_abs) file_util.copy(result.output_exe, output_filename_abs) return 0
def ensure(self, versions, requirements, requirements_dev): check.check_string_seq(versions) requirements = file_check.check_file(requirements) requirements_dev = file_check.check_file(requirements_dev, allow_none=True) project = bes_project(options=self.options) project.ensure(versions, requirements, requirements_dev=requirements_dev) return 0
def reindent_file(clazz, filename, indent, backup): check.check_string(filename) check.check_int(indent) check.check_bool(backup) file_check.check_file(filename) clazz._log.log_method_d() backup_args = [] if backup else ['--nobackup'] args = ['--indent', str(indent)] + backup_args + [filename] reindent_main(args)
def remove_values(clazz, filename, func): filename = file_check.check_file(filename) check.check_callable(func) keys = [key for key in file_attributes.keys(filename) if func(key)] for key in keys: clazz.remove_value(filename, key)
def remove(clazz, filename, key): 'Remove the attirbute with key from filename.' filename = file_check.check_file(filename) key = clazz._check_key(key) clazz.check_file_is_writable(filename) xattr_exe.remove(filename, key)
def has_stream(clazz, filename, stream_name): 'Return True if filename has stream_name.' filename = file_check.check_file(filename) stream_name = clazz.check_stream_name(stream_name) ads_filename = clazz._make_ads_filename(filename, stream_name) return path.exists(ads_filename)
def has_key(clazz, filename, key): 'Return True if filename has an attributed with key.' filename = file_check.check_file(filename) key = clazz._check_key(key) clazz.check_file_is_readable(filename) return xattr_exe.has_key(filename, key)
def set_int(clazz, filename, key, value, encoding = 'utf-8'): 'Set the value of attribute with key to value for filename as string.' filename = file_check.check_file(filename) key = clazz._check_key(key) check.check_int(value) clazz.set_string(filename, key, str(value))
def detect_mime_type(clazz, filename): 'Detect the mime type for file.' filename = file_check.check_file(filename) import mimetypes mime_type, _ = mimetypes.guess_type(filename) return mime_type
def find_file_duplicates(clazz, filename, where, options=None): filename = file_check.check_file(filename) check.check_string_seq(where) check.check_file_duplicates_options(options, allow_none=True) options = options or file_duplicates_options() setup = clazz.setup(where, options=options) return clazz.find_file_duplicates_with_setup(filename, setup)
def media_type_matches(clazz, filename, media_types): filename = file_check.check_file(filename) check.check_string_seq(media_types) media_type = clazz.get_metadata(filename, 'media_type') if not media_type: return False return media_type in media_types
def get_string(clazz, filename, key, encoding = 'utf-8'): 'Return the attribute value with key for filename as string.' filename = file_check.check_file(filename) key = clazz._check_key(key) value = clazz.get_bytes(filename, key) if value == None: return None return value.decode(encoding)
def has_key(clazz, filename, key): 'Return True if filename has an attributed with key.' filename = file_check.check_file(filename) key = clazz._check_key(key) clazz.check_file_is_readable(filename) encoded_key = clazz._encode_key(key) return xattr.xattr(filename).has_key(encoded_key)
def remove(clazz, filename, key): 'Remove the attirbute with key from filename.' filename = file_check.check_file(filename) check.check_string(key) args = [ '-r', key, filename ] rv = clazz._call_linux_attr(args) linux_attr_command.check_result(rv, message = 'Failed to delete key "{}" for "{}"'.format(key, filename))
def get_bool(clazz, filename, key): 'Return the attribute value with key for filename as string.' filename = file_check.check_file(filename) key = clazz._check_key(key) value = clazz.get_string(filename, key) if value == None: return None return bool_util.parse_bool(value)
def set_bytes(clazz, filename, key, value): 'Set the value of attribute with key to value for filename.' filename = file_check.check_file(filename) key = clazz._check_key(key) check.check_bytes(value) clazz.check_file_is_writable(filename) clazz._log.log_method_d() xattr_exe.set_bytes(filename, key, value)
def get_date(clazz, filename, key): 'Return the attribute value with key for filename as string.' filename = file_check.check_file(filename) key = clazz._check_key(key) value = clazz.get_string(filename, key) if value == None: return None timestamp = float(value) return datetime.fromtimestamp(timestamp)
def read_values(clazz, filename, stream_name): 'Read the content of stream_name for filename as values.' filename = file_check.check_file(filename) stream_name = clazz.check_stream_name(stream_name) value = clazz.read_stream(filename, stream_name) if not value: return {} return pickle.loads(value)
def remove_stream(clazz, filename, stream_name): 'Remove stream_name from filename if it exists.' filename = file_check.check_file(filename) stream_name = clazz.check_stream_name(stream_name) if not clazz.has_stream(filename, stream_name): return ads_filename = clazz._make_ads_filename(filename, stream_name) file_util.remove(ads_filename)
def write_values(clazz, filename, stream_name, values): 'Write the content of stream_name for filename as values.' filename = file_check.check_file(filename) stream_name = clazz.check_stream_name(stream_name) check.check_dict(values) value = pickle.dumps(values) clazz.write_stream(filename, stream_name, value) assert value == clazz.read_stream(filename, stream_name)
def detect_mime_type(clazz, filename): 'Detect the mime type for file.' filename = file_check.check_file(filename) import puremagic rv = puremagic.magic_file(filename) if not rv: return None return clazz._find_mime_type(rv)
def detect_mime_type(clazz, filename): 'Detect the mime type for file.' filename = file_check.check_file(filename) import magic rv = magic.from_file(filename, mime = True) if not rv: return None return rv
def get_bytes(clazz, filename, key): 'Return the attribute value with key for filename.' filename = file_check.check_file(filename) key = clazz._check_key(key) clazz.check_file_is_readable(filename) if not xattr_exe.has_key(filename, key): return None return xattr_exe.get_bytes(filename, key)
def find_nodes(clazz, filename, node_type): check.check_refactor_ast_node_type(node_type) filename = file_check.check_file(filename) source_code = file_util.read(filename, codec='utf-8') tree = ast.parse(source_code) nodes = refactor_ast_util.find_nodes(tree, node_type) return nodes
def get_bytes(clazz, filename, key): 'Return the attribute value with key for filename.' filename = file_check.check_file(filename) key = clazz._check_key(key) clazz.check_file_is_writable(filename) encoded_key = clazz._encode_key(key) if not xattr.xattr(filename).has_key(encoded_key): return None return xattr.getxattr(filename, encoded_key)
def remove(clazz, filename, key): 'Remove the attirbute with key from filename.' filename = file_check.check_file(filename) key = clazz._check_key(key) clazz.check_file_is_writable(filename) clazz._log.log_method_d() encoded_key = clazz._encode_key(key) xattr.removexattr(filename, encoded_key)