def run(self, source, destination, _from, to, excludes = None, chmod = None, delete = False, retry = 1, test_mode = False): """ run synchronization process @param source: source server @param destination: destination server @param _from: path on source server @param to: path on destination server @param exclude: instance of FileSet @param chmod: value for destination files to be chmoded to @param delete: if true any files in destination that is non-exist in source will be deleted @param test_mode: if true run in test mode @rtype: bool @return: True on success @raise InvalidParameterError: If one of the arguments is invalid. @raise Other: from localclient methods """ #validating that source or destination are servers and at least one of them is local server if source.__class__ != LocalServer: raise InvalidParameterError('source', 'Must be instance of LocalServer class') if destination.__class__ != FtpServer: raise InvalidParameterError('destination', 'Must be instance of FtpSerevr class') if not Validator.validate_non_empty_string(_from): raise InvalidParameterError('_from', 'Must be non-empty string') if not Validator.validate_non_empty_string(to): raise InvalidParameterError('to', 'Must be non-empty string') if excludes.__class__ != FileSet and excludes != None: raise InvalidParameterError('excludes', 'should be instance of FileSet') if not Validator.validate_non_empty_string(chmod) and chmod != None: raise InvalidParameterError('chmod', 'should be a string value in form like "0777"') if not Validator.validate_boolean(delete): raise InvalidParameterError('delete', 'should be boolean value') if not Validator.validate_boolean(test_mode): raise InvalidParameterError('test_mode', 'should be boolean value') local_platform = source.get_platform() ftp_platform = destination.get_platform() if not destination.is_exists(to): try: destination.mkdir(to) except Exception, e: raise
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 create_db(self, name, character_set = "", collate = "", if_not_exists = True, retry = 1): """ creates new database using data base name, character_set, and collate @param name: name of database to be created @param character_set: character set of the database @param collate: the collation of the database @param if_not_exist: only creates the database if it does not exist @param retry: The number of times to retry execution on error @rtype: bool @return: True on success @raise InvalidParameterError: If method parameters are invalid in type. @raise DbCreateError: if failed to create the database. """ if False == Validator.validate_non_empty_string(name): raise InvalidParameterError('name', 'name should be a non-empty string') if False == Validator.validate_string(character_set): raise InvalidParameterError('character_set', 'character_set should be a string') if False == Validator.validate_string(collate): raise InvalidParameterError('collate', 'collate should be a string') if False == Validator.validate_boolean(if_not_exists): raise InvalidParameterError('if_not_exist', 'should be a boolean') while True: try: statement = "CREATE DATABASE %s %s" conn = MySQLdb.connect(host = self.__host, user = self.__username, passwd = self.__password, port = self.__port) transaction = conn.cursor() if True == if_not_exists: statement = statement % ("IF NOT EXISTS", name) else: statement = statement % ("", name) if 0 != len(character_set): statement = statement + " CHARACTER SET = %s " % (character_set) if 0 != len(collate): statement = statement + " COLLATE = %s " % (collate) AppLogger.debug('Executing SQL command "%s"' % (statement)) transaction.execute(statement) return True except Exception, e: retry = retry - 1 if 0 == retry: raise DbCreateError, str(e)
def execute_sql(self, statements, database, transaction = False): """ Executes a sequence of SQL statements. @param statements: list of statements to be executed. @param database: the name of the database to run the statements. @param transaction: if it is True, statements will be executed as a single transaction. @rtype: bool @return: True on success @raise InvalidParameterError: If method parameters are invalid in type. @raise DbStatementError: if method fails in executing statements or connecting to database. """ if statements.__class__ != list: raise InvalidParameterError('statements', 'statements should be a list of statements') if False == Validator.validate_non_empty_string(database): raise InvalidParameterError('database', 'must be a non empty string') if False == Validator.validate_boolean(transaction): raise InvalidParameterError('transaction', 'must be a boolean') while True: try: cursor = None conn = MySQLdb.connect(host = self.__host, user = self.__username, passwd = self.__password, port = self.__port, db = database) cursor = conn.cursor() for statement in statements: cursor.execute(statement) if not transaction: conn.commit() cursor.close() conn.commit() conn.close() return True except Exception, e: if cursor: cursor.close() conn.rollback() conn.close() raise DbStatementError, str(e)
def drop_db(self, name, if_exists = True, retry = 1): """ drops a database. @param name: name of database to be dropped @param if_exists: only drop the database if it did not exist @param retry: The number of times to retry execution on error @rtype: bool @return: True on success @raise InvalidParameterError: If method parameters are invalid in type. @raise DbDropError: if method fails in creating database or connecting to database """ if False == Validator.validate_non_empty_string(name): raise InvalidParameterError('name', 'name should be a non-empty string') if False == Validator.validate_boolean(if_exists): raise InvalidParameterError('if_exists', 'should be a boolean') while True: try: statement = "DROP DATABASE %s %s" conn = MySQLdb.connect(host = self.__host, user = self.__username, passwd = self.__password, port = self.__port) transaction = conn.cursor() if True == if_exists: statement = statement % ("IF EXISTS", name) else: statement = statement % ("", name) AppLogger.debug('Executing SQL command "%s"' % (statement)) transaction.execute(statement) return True except Exception, e: retry = retry - 1 if 0 == retry: raise DbDropError, str(e)
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