def __list(self, path, excludes, recursive, retry): files_list = self.__get_dir_list(path, retry) files = [] for file_list in files_list: ftp_file = FtpListParser(self.get_platform(), file_list) file_path = self.get_platform().join(path, ftp_file.get_name()) file_type = '' if ftp_file.get_type() == 'd': file_type = FileObject.DIRECTORY elif ftp_file.get_type() == 'l': file_type = FileObject.LINK elif ftp_file.get_type() == 's': file_type = FileObject.SOCKET elif ftp_file.get_type() == 'c': file_type = FileObject.CHARCTER_DEVICE elif ftp_file.get_type() == 'p': file_type = FileObject.PIPE elif ftp_file.get_type() == 'b': file_type = FileObject.BLOCK_DEVICE elif ftp_file.get_type() == 'D': file_type = FileObject.DOOR else: file_type = FileObject.FILE if not (excludes is not None and excludes.match(path, ftp_file.get_name(), file_type)): resfile = FileObject(self.get_platform(), file_path, file_type, ftp_file.get_mode(), ftp_file.get_date(), '', '', ftp_file.get_owner(), ftp_file.get_group()) children = [] if ftp_file.get_type() == 'd' and recursive: children = self.__list(file_path, excludes, True, retry) for child in children: resfile.add_child(child) files.append(resfile) return files
def mkdir(self, path, mode = "0777", recursive = False, retry = 1): """ Create a directory named path @param path: path of directory to be created @param mode: the of the created file @param recursive: if true the method could create path tree @param retry: number of retries @rtype: bool @return: True on success @raise InvalidParameterError: if parameters are not valid @raise FileExistsError: if path directory is already exist @raise MakeDirError: if error occurred while making a directory """ if False == Validator.validate_non_empty_string(path): raise InvalidParameterError('path', 'should be a not empty string') if mode.__class__ != str: raise InvalidParameterError('mode', 'should be a not empty string') if recursive.__class__ != bool: raise InvalidParameterError('recursive', 'should be boolean value') if retry.__class__ != int: raise InvalidParameterError('retry', 'should be a not empty string') mode = FileObject.convert_permissions(mode, self.get_platform()) #trim the '/' or '\' from the end of path path = self.get_platform().trim_path(path) if os.path.exists(path): raise FileExistsError, 'file of directory exists' #test if path is relative if self.get_platform().is_relative(path) : path = self.__root + path while retry: retry -= 1 try: if recursive: os.makedirs(path, mode) else: os.mkdir(path, mode) except Exception, e: if retry == 0: raise MakeDirError, str(e) else: break
def __list(self, path, excludes = None, recursive = False, retry = 1): if False == Validator.validate_non_empty_string(path): raise InvalidParameterError('path', 'should be a not empty string') if excludes.__class__ != FileSet and excludes != None: raise InvalidParameterError('excludes', 'should be instance of FileSet') if recursive.__class__ != bool: raise InvalidParameterError('recursive', 'should be a bool') if retry.__class__ != int: raise InvalidParameterError('retry', 'should be a bool') #trim the '/' or '\' from the end of path path = self.get_platform().trim_path(path) if not os.path.exists(path): raise FileNotExistsError, 'file or directory does not exist' #test if path is relative if self.get_platform().is_relative(path) : path = self.__root + path if not os.path.isdir(path): recursive = False if os.path.isfile(path): file_type = FileObject.FILE else: file_type = FileObject.LINK if Platform.PLATFORM_WINDOWS == self.get_platform().get_platform(): user_id = "" group_id = "" else: user_id = os.stat(path).st_uid group_id = os.stat(path).st_gid return FileObject(self.get_platform(), path, file_type, os.stat(path).st_mode & 0777 , time.localtime(os.path.getmtime(path)), time.localtime(os.path.getctime(path)), time.localtime(os.path.getatime(path)), user_id, group_id) else: if Platform.PLATFORM_WINDOWS == self.get_platform().get_platform(): user_id = "" group_id = "" else: user_id = os.stat(path).st_uid group_id = os.stat(path).st_gid file = FileObject(self.get_platform(), path, FileObject.DIRECTORY, os.stat(path).st_mode & 0777 , time.localtime(os.path.getmtime(path)), time.localtime(os.path.getctime(path)), time.localtime(os.path.getatime(path)), user_id, group_id) if recursive or inspect.stack()[1][3] == 'list': cont_list = os.listdir(path) for item in cont_list: new_path = path + self.get_platform().get_separator() + item base_name = self.get_platform().basename(new_path) parent_name = self.get_platform().dirname(new_path) if os.path.isfile(new_path): _type = FileObject.FILE else: _type = FileObject.DIRECTORY if not (excludes and excludes.match(parent_name, base_name, _type)): file.add_child(self.__list(new_path, excludes, recursive, retry)) return file
def chmod(self, path, permissions, excludes = None, contents_only = False, recursive = False, retry = 1): """ Change the mode of path to the numeric mode. mode may take one of the following values (as defined in the stat module) or bitwise or-ed combinations of them: * S_IREAD, S_IWRITE, S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH Windows supports chmod() but you can only set the file's read-only flag with it (via the S_IWRITE and S_IREAD constants) @param path: path of the directory or file to be chmoeded @param permissions: integer value of the permission @param excludes: An instance of FileSet that describes the exclusion patterns. @param contents_only: if True it will chmod the directory contents @param recursive: if True, contents of directory will be chmoded recursively @param retry: number of retries @rtype: bool @return: True on success @raise InvalidParameterError: if parameters are not valid @raise FileNotExistsError: if file or directory does not exist @raise FileChmodError: if error occurred during chmod """ if False == Validator.validate_non_empty_string(path): raise InvalidParameterError('path', 'should be a not empty string') if False == Validator.validate_non_empty_string(permissions): raise InvalidParameterError('permissions', 'should be string in form like "0777" , "777", "rwxr-xr-x"') if excludes.__class__ != FileSet and excludes != None: raise InvalidParameterError('excludes', 'should be instance of FileSet') if contents_only.__class__ != bool: raise InvalidParameterError('contents_only', 'should be a boolean value') if recursive.__class__ != bool: raise InvalidParameterError('recursive', 'should be a boolean value') if retry.__class__ != int: raise InvalidParameterError('retry', 'should be an integer value') int_permissions = FileObject.convert_permissions(permissions, self.get_platform()) #trim the '/' or '\' from the end of path path = self.get_platform().trim_path(path) if not os.path.exists(path): raise FileNotExistsError, 'file or directory does not exist' #test if path is relative if self.get_platform().is_relative(path) : path = self.__root + path if not os.path.isdir(path): recursive = False contents_only = False if recursive or contents_only: cont_list = os.listdir(path) for item in cont_list: new_path = path + self.get_platform().get_separator() + item base_name = self.get_platform().basename(new_path) parent_name = self.get_platform().dirname(new_path) if os.path.isfile(new_path): _type = FileObject.FILE else: _type = FileObject.DIRECTORY if not (excludes and excludes.match(parent_name, base_name, _type)): self.chmod(new_path, permissions, excludes, False, recursive, retry) if not contents_only: while retry: retry -= 1 try: os.chmod(path, int_permissions) except Exception, e: if retry == 0: raise FileChmodError, str(e) else: break
def _FtpBase__get_permissions(self, permissions): return FileObject.convert_permissions(permissions, self.get_platform(), convert_type = FileObject.PERMISSIONS_AS_OCT)