def export(self, local_path, path = "/", revision = 0, retry = 1): """ export a directory from the subversion server that is spesified by path parameter @param local_path: local path on local machine where the directory will be checked out. @param path: path of directory on subversion servern that will be checked out. @param replace: if true replaces the existing local working diretory. @param revision: revision number to checkout, 0 to checkout the last revision. @param retry: number of retries before fail. @raise CheckOutError in case of failure """ if not Validator.validate_non_empty_string(local_path): raise InvalidParameterError("local_path", "Must be non-empty string") if not Validator.validate_string(path): raise InvalidParameterError("path", "Must be a string value") if not Validator.validate_integer(revision): raise InvalidParameterError("revision", "Must be an integer value") if not Validator.validate_integer(retry): raise InvalidParameterError("retry", "Must be an integer value") export_path = os.path.join(self.repository_path, path.lstrip("/")) if os.path.exists(local_path): raise FileExistsError, "the path where you check out is already exists" while True: try: if 0 == revision: return self.__client.export( export_path, local_path, True, ) else: return self.__client.export( export_path, local_path, True, pysvn.Revision(pysvn.opt_revision_kind.number, revision) ) except Exception, e: retry -= 1 if 0 == retry: raise CheckOutError( 'Failed to export "%s" to "%s"."' % (export_path, local_path) ) else: shutil.rmtree(local_path, True)
def get_last_modified_time(self, path, retry = 1): ''' get the last modification time of file using the FTP command MDTM @param path: path of the file to get the last modification time @param retry: number of the retry on the function failure @rtype: datetime object @type last_modified_time: last modified time of the specified path @raise InvalidParameterError: If the required argument(s) are not specified. @raise GetLastModificationTimeError: If any failure occurred while performing the command @raise FileNotExistError: if the file is not found ''' if not Validator.validate_non_empty_string(path): raise InvalidParameterError("path", "path can not be None or empty string") if not Validator.validate_integer(retry): raise InvalidParameterError("retry", "retry must be integer") while retry > 0: retry = retry - 1 try: path = self.append_to_root(path) __time = self.__get_last_modified_time(path) return datetime.datetime(int(__time[0]), int(__time[1]), int(__time[2]), int(__time[3]), int(__time[4]), int(__time[5])) except Exception, e: message = str(e) if message.find('No such file') > 0: raise FileNotExistsError, path
def __init__(self, platform, host, port = 22, username = "", password = "", certificate_file = "", passphrase = "", root = ""): """ Constructor for the class. @param id: The identifier of this server, must be a non-empty string. @param host: The host of the Rsync server. @param port: The port of the server. @param username: Username. @param password: Password. @param certificate_file: Path to a local certificate file. @param passphrase: The passphrase encrypting the certificate file. """ if not Validator.validate_non_empty_string(host): raise InvalidParameterError('host', 'host can not be None or empty string') if not Validator.validate_integer(port): raise InvalidParameterError('port', 'port must be integer') if not Validator.validate_non_empty_string(certificate_file) and ( not Validator.validate_non_empty_string(username) or not Validator.validate_non_empty_string(password)): raise InvalidParameterError('username', 'A username and password must be specified if not using certificate login') self.__certificate_file = certificate_file self.__passphrase = passphrase super(SshClient, self). __init__(platform, host, username, password, port, root) self.connect()
def chown(self, path, owner, group, excludes = None, contents_only = False, recursive = False, retry = 1): ''' changing the ownership of file @param path: file path @param owner: the target owner id of the file @param group: the target group id of the file @param excludes: list of the excluded file from the operation @param contents_only: if True the contents of the file only will be changed not the file itself @param recursive: if True the operation will be done recursively @param retry: the number of retries on function failure @raise InvalidParameterError: If the required argument(s) are not specified. @raise FileNotExistError: if the path is not exist @raise FtpChownError: If the ownership can not be changed ''' if not Validator.validate_non_empty_string(path): raise InvalidParameterError("path", "path can not be None or empty string") if not Validator.validate_integer(owner): raise InvalidParameterError("owner", "owner can not be None or empty string") if not Validator.validate_integer(group): raise InvalidParameterError("group", "group can not be None or empty string") try: path = self.append_to_root(path) if not self.is_exists(path): raise FileNotExistsError, path excluded = False path_type = self.get_type(path) if excludes is not None: if excludes.match(path, self.get_platform().basename(path), path_type): excluded = True if not contents_only and not excluded: self.__chown(path, owner, group, retry) if recursive and path_type is FileObject.DIRECTORY: children = self.list(path, excludes, True, retry) self.__chown_list(child.get_path(), owner, group, retry) except Exception, e: raise FileChownError(path, str(e))
def set_default_timeout(cls, timeout): ''' sets default timeout for all requests ''' if not Validator.validate_integer(timeout): raise InvalidParameterError("timeout", "Must be an integer value") socket.setdefaulttimeout(timeout)
def get_file(self, local_path, file_path, replace = False, retry = 1): """ get a file spesified by file path from the server @param local_path: local path on local machine where the directory will be checked out @param file_path: path of file on subversion that will be got @param replace: if true replaces the existing local working diretory @param retry: number of retries before fail @raise GetFileError on failure """ if not Validator.validate_non_empty_string(local_path): raise InvalidParameterError("local_path", "Must be non-empty string") if not Validator.validate_non_empty_string(file_path): raise InvalidParameterError("file_path", "Must be non-empty string") if not Validator.validate_boolean(replace): raise InvalidParameterError("replace", "Must be a boolean value") if not Validator.validate_integer(retry): raise InvalidParameterError("retry", "Must be an integer value") local_path = self.local_client.get_platform().trim_path(local_path) file_path = self.local_client.get_platform().trim_path(file_path) if os.path.exists(local_path): if os.path.isdir(local_path): base_name = self.local_client.get_platform().basename(file_path) local_path += self.local_client.get_platform().get_separator() + base_name while retry: retry -= 1 try: file_str = self.__client.cat(self.repository_path + file_path) except: if retry == 0: raise GetFileError("repository path: ") try: if os.path.exists(local_path): if os.path.isfile(local_path): if replace: self.local_client.delete(local_path, None, False, True, retry) else: raise FileExistsError(local_path + " already exists") local_file = open(local_path, 'w') local_file.write(file_str) local_file.close() except Exception, e: if retry == 0: raise FileWriteError () else: break
def rename(self, source, destination, retry = 1): """ rename a file or directory @param source: the source path to be renamed @param destination: the new name of file or dorectory @param retry: number of retries on failure @rtype: bool @return: True on success @raise InvalidParameterError: if parameters are not valid @raise FileNotExistsError: if source file or directory is not exist or if create_dirs equal False and destination directory is not exist @raise FileExistsError: if the destination exists @raise PermissionDeniedError: if the operation is not permitted @raise FileRenameError: if error occurred during moving """ if not Validator.validate_non_empty_string(source): raise InvalidParameterError("source", "source can not be None or empty string") if not Validator.validate_non_empty_string(destination): raise InvalidParameterError("destination", "destination can not be None or empty string") if not Validator.validate_integer(retry): raise InvalidParameterError("retry", "retry must be integer") #add root if the paths is relative source = self.append_to_root(source) destination = self.append_to_root(destination) if self.is_exists(destination): raise FileExistsError, destination while retry > 0: retry = retry - 1 try: self.__rename(source, destination) return except Exception, e: message = str(e) if message.find("No such file") > 0: raise FileNotExistsError(source) elif message.find("Permission denied") > 0: raise PermissionDeniedError else: if retry == 0: raise FileRenameError(source + " " + str(e))
def __init__( self, platform, path, file_type, permissions, modifiation_date, creation_date="", access_date="", user="", group="", ): if Platform != platform.__class__: raise InvalidParameterError("platform", "Must be an instance of Platform.") elif "" == path: raise InvalidParameterError("path", "Must be a non-empty string.") elif not Validator.validate_integer(file_type): raise InvalidParameterError("type", "Must be FileObject.FILE, Fie.DIRECTORY or FileObject.LINK.") elif "" == modifiation_date: raise InvalidParameterError("modifiation_date", "Must be non empty string.") elif Platform.PLATFORM_POSIX == platform.get_platform(): if "" == user: raise InvalidParameterError("user", "Must be non empty.") elif "" == group: raise InvalidParameterError("group", "Must be non empty.") self.__platform = platform self.__path = path self.__type = file_type self.__permissions = None self.__modification_date = None self.__creation_date = None self.__access_date = None self.set_permissions(permissions) self.set_modification_date(modifiation_date) if "" != creation_date: self.set_creation_date(creation_date) if "" != access_date: self.set_access_date(access_date) self.__user = user self.__group = group self.__childs = []
def __init__(self, host, port, username = "", password = ""): """ """ if not Validator.validate_non_empty_string(host): raise InvalidParameterError("host", "Must be non-empty String") if not Validator.validate_integer(port): raise InvalidParameterError("port", "Must be an integer value") if not Validator.validate_string(username): raise InvalidParameterError("username", "Must be a string value") if not Validator.validate_string(password): raise InvalidParameterError("password", "Must be a string value") super(SqlClient, self).__init__() self.__host = host self.__port = port self.__username = username self.__password = password self.__platform = Platform.get_current()
def list(self, path, excludes = None, recursive = False, retry = 1): ''' list directory contents @param path: directory to be listed @param excludes: excluded files @param recursive: boolean for list the directory recursively or not @param retry: count of function retry @return: list of current working directory files @raise InvalidParameterError: If the required argument(s) are not specified. @raise ListDirectoryError: If the specified directory can not be listed @raise FileNotExistsError: if the path not exist ''' if not Validator.validate_non_empty_string(path): raise InvalidParameterError("path", "path can not be None or empty string") if not Validator.validate_boolean(recursive): raise InavlidParameterError("recursive", "recursive must be boolean") if not Validator.validate_integer(retry): raise InavlidParameterError("retry", "retry must be integer") try: if path == '.': path = self.get_cwd() else: path = self.append_to_root(path) if not self.is_exists(path): raise FileNotExistsError(path) return self.__list(path, excludes, recursive, retry) except Exception, e: raise #ListDirectoryError('Can not list directory ' + path + " " + str(e))
def get_file_list(self, path, recursive = False, retry = 1): """ list file on subversion server given a spesified path raise ListFileError on failure """ if not Validator.validate_string(path): raise InvalidParameterError("local_path", "Must be a string value") if not Validator.validate_boolean(recursive): raise InvalidParameterError("recursive", "Must be a boolean value") if not Validator.validate_integer(retry): raise InvalidParameterError("retry", "Must be an integer value") path = self.local_client.get_platform().trim_path(path) while retry: retry -= 1 try: return self.__client.ls(self.repository_path + path, recurse = recursive) except Exception, e: if retry == 0: raise ListFileError("Repository path: " + self.repository_path + path, str(e)) else: break
def convert_permissions(cls, permissions, platform, convert_type=1000): """ """ if not Validator.validate_non_empty_string(permissions): raise InvalidParameterError("permissions", "Must be non-empty string") if not Validator.validate_integer(convert_type): raise InvalidParameterError("convert_type", "Must be an integer value") if Platform.PLATFORM_POSIX == platform.get_platform(): if True == Validator.validate_non_empty_string(permissions) and re.match( "([-r][-w][-xsStT]){3,3}", permissions ): result = 0 if permissions[0] != "-": result |= FileObject.S_IRUSR if permissions[1] != "-": result |= FileObject.S_IWUSR if permissions[2] != "-": result |= FileObject.S_IXUSR if permissions[3] != "-": result |= FileObject.S_IRGRP if permissions[4] != "-": result |= FileObject.S_IWGRP if permissions[5] != "-": result |= FileObject.S_IXGRP if permissions[6] != "-": result |= FileObject.S_IROTH if permissions[7] != "-": result |= FileObject.S_IWOTH if permissions[8] != "-": result |= FileObject.S_IXOTH elif True == Validator.validate_non_empty_string(permissions) and re.match("(0)?[0-7]{3,3}", permissions): if len(permissions) == 3: permissions = "0" + permissions result = octstr_to_int(permissions) else: raise PermissionsInvalidError() elif Platform.PLATFORM_WINDOWS == platform.get_platform(): if permissions.__class__ == str and re.match("[-r][-w]", permissions): result = 0 if permissions[0] != "-": result |= FileObject.S_IREAD if permissions[1] != "-": result |= FileObject.S_IWRITE elif permissions.__class__ == str and re.match("(0)?[0-7]{3,3}", permissions): if len(permissions) == 3: permissions = "0" + permissions result = octstr_to_int(permissions) else: raise PermissionsInvalidError() else: raise PlatformNotSupportedError() if convert_type != FileObject.PERMISSIONS_AS_INT: return result else: return int(oct(result))