예제 #1
0
파일: ftpbase.py 프로젝트: vimov/Deployer
	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
예제 #2
0
파일: server.py 프로젝트: vimov/Deployer
	def __init__(self, id, address, port, document_root, language):
		"""
		Constructor for the class.

		@param id: The identifier of this server, must be a non-empty string.
		@param address: The web address (excluding the protocol, for the web server.
		@param port: The port of the server.
		@param document_root: The document root of the server.
		@param language: A language this server supports, like PHP5.
		"""

		if not Validator.validate(address, 'domain'):
			raise InvalidParameterError('address', 'Must be a valid domain name, excluding the protocol or path.')
		if not Validator.validate(port, 'port'):
			raise InvalidParameterError('port', 'Must be an integer value between 0 and 65535')
		if not Validator.validate(document_root, 'non_empty_string'):
			raise InvalidParameterError('document_root', 'Must be a non-empty string')
		if not Validator.validate(language, 'non_empty_string'):
			raise InvalidParameterError('language', 'Must be a non-empty string')

		self.__address = address
		self.__port = port
		self.__document_root = document_root
		self.__language = language

		Server.__init__(self, id, Server.REMOTE)
		WebClient.__init__(self)
예제 #3
0
파일: ftpbase.py 프로젝트: vimov/Deployer
	def put(self, source, destination, excludes = None, contents_only = False, recursive = False, create_dirs = False, replace = False, filter_chain = None, retry = 1):
		'''
		upload file or folder from the source into the destination
		
		@param source: local path
		@param destination: destination path
		@param excludes: list of excluded files or folders from the uploading operation 
		@param recusive: boolean for upload the folder with its subfolders or not
		@param contents_only: boolean for uploading the contents of the dir directly to the server without creating it on the server
								or create it first and upload the data inside it  
		@param create_dirs: boolean for creating the destination if not exist or not
		@param replace: replace the existing files or not
		@param filter_chain: filter for the uploaded files
		@param retry: if file uploading failed try again with number of retry 
		
		@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 destination already exists and and replace is false
		@raise FilePutError: if error occurred during copying
		'''
		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")

		#if the client is None
		if self.__ftp_client is None:			
			self.connect()
		
		source = source.rstrip(' ')
		source = source.rstrip(os.sep)

		if destination == '.':
			destination = self.get_cwd()
		else:
			destination = self.append_to_root(destination)

		if not os.path.exists(source):
			raise FileNotExistsError, source

		if not self.is_exists(destination):
			if create_dirs:
				self.mkdir(destination, recursive = True)
			else:
				raise FileNotExistsError, destination
		else:
			if self.get_type(destination) == FileObject.FILE:
				raise FileExistsError, destination

		newdestination = self.get_platform().join(destination, os.path.basename(source))
		if self.is_exists(newdestination):
			 if not replace:
					raise FileExistsError, newdestination
		
		if os.path.isfile(source):
			try:
				self.__put(source, destination, excludes, replace, recursive, filter_chain, retry)
			except Exception, e:
				raise FilePutError('can not upload ' + source, str(e))
예제 #4
0
파일: sshclient.py 프로젝트: vimov/Deployer
	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()
예제 #5
0
파일: ftpbase.py 프로젝트: vimov/Deployer
	def mkdir(self, path, mode = "755", recursive = False, retry = 1):
		'''
		make a directory on the remote server on the current directory, if the parameter is path 
		it must be found except the basename 
		Example:
		dir = a/b/c
		a/b must be found and c will be created
		 if dir = a then a will be created 
		
		@param path: directory path
		@param mode: mode of the directory
		@param recurisve : boolean for creating the full path or the last level only
		@param retry: count of the function retry 

		@raise InvalidParameterError: If the required argument(s) are not specified.
		  		
		@raise MakeDirError: If the function failed to make the directory
		'''

		if not Validator.validate_non_empty_string(path):
			raise InvalidParameterError("path", "path can not be None or empty string")
		
		if not Validator.validate_non_empty_string(mode):
			raise InvalidParameterError("mode", "mode can not be None or empty string")
		
		try:
			mode = self.__get_permissions(mode)
			path = self.append_to_root(path)
			if not recursive:
					if self.is_exists(path):
						if  self.get_type(path) == FileObject.FILE:
							raise FileExistsError("can not replace file with directory " + path)
					else:
						self.__mkdir(path, mode, retry)
			else:
				base =  self.get_root()
				tmp = path[len(base):] 
				list = tmp.split(self.get_platform().get_separator())
				for dir in list:
					if len(dir) > 0:
						base = base + dir
						if self.is_exists(base):
							if  self.get_type(base) == FileObject.FILE:
								raise FileExistsError("can not replace file with directory " + base)
						else:
							self.__mkdir(base, mode, retry)
						base = base + self.get_platform().get_separator()
		except Exception, e:
			message = str(e)
			if message.find("Permission denied") > 0:
				raise PermissionDeniedError, path
			elif message.find("File exists") > 0:
				return 
			else:
				raise MakeDirError('can not make dir ' + path, message)
예제 #6
0
	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
예제 #7
0
	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)
예제 #8
0
	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)
예제 #9
0
파일: ftpbase.py 프로젝트: vimov/Deployer
	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)) 
예제 #10
0
	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)
예제 #11
0
파일: server.py 프로젝트: vimov/Deployer
	def __init__(self, id, medium):
		"""
		Constructor for the class.

		@param id: The identifier of this server, must be a non-empty string.
		@param medium: The medium of the server, can be either LOCAL or REMOTE.

		@raise ServerIdentifierExistsError: If the identifier has already been used by another Server.
		@raise InvalidParameterError: If the value of medium is invalid.
		@raise DepFileParsingError: This class cannot be directly instantiated.
		"""

		if Server == self.__class__:
			raise DepFileParsingError()

		if False == Validator.validate_variable_name(id):
			raise InvalidParameterError("id", 'The server ID "%s" is invalid.' % (id))
		else:
			Server.__add_server_id(id)
			self.id = id

		if Server.LOCAL != medium and Server.REMOTE != medium:
			raise InvalidParameterError("medium", "Medium can be either LOCAL or REMOTE")
		else:
			self.medium = medium
예제 #12
0
	def get_last_modified_time(self, path):
		"""
		gets the last modification time of a file or directory
		
		@param path: path of directory to
		 
		@rtype: tuple
		@return: last modification time
		
		@raise InvalidParameterError: if parameters are not valid
		@raise FileNotExistsError: if path directory is already exist
		"""	
			
		if False == Validator.validate_non_empty_string(path):
			raise InvalidParameterError('path', 'should be a not empty string')
		
		if not (re.match(r'/', path) or re.match(r'[a-zA-Z]:\\', path)):
			path = path.rstrip(self.get_platform().get_separator())
		if not os.path.exists(path):
			raise FileNotExistsError, 'file or directory not exists'
		
		#test if path is relative
		if self.get_platform().is_relative(path) :
			path = self.__root + path
			
		tmp_time = time.localtime(os.path.getmtime(path))
		
		return datetime(tmp_time[0], tmp_time[1], tmp_time[2], tmp_time[3], tmp_time[4], tmp_time[5],)
예제 #13
0
파일: browser.py 프로젝트: vimov/Deployer
	def __init__(self, url, proxy = None, auth = None, post_encoding = HTTP_METHOD_POST_XWWW_DATA):
		'''
		@param url: the url of the request
		@param proxy: dictionary with two entries have keys ('type' and 'host')
		@param auth: dictionary with four entries have keys ('realm', 'uri', 'user', 'passwd')
		@param ost_encoding : one of types 'Request.HTTP_METHOD_POST_XWWW_DATA' or 'Request.HTTP_METHOD_POST_FORM_DATA'
		'''
		
		if not Validator.validate_non_empty_string(url):
			raise InvalidParameterError("url", "Must be non-empty string")
		if proxy.__class__ != dict and proxy != None:
			raise InvalidParameterError("proxy", "Must be a dictionary")
		if auth.__class__ != dict and auth != None:
			raise InvalidParameterError("auth", "Must be a dictionary")
		
		
		self.method = Request.HTTP_METHOD_GET
		self.url = url
		self.referrer = url
		self.proxy = proxy
		self.auth = auth
		self.post_encoding = post_encoding
		self.data = {}
		self.params = {}
		self.headers = {}
		self.response_validator = []
		self.requested_data = []
		self.cookies = cookielib.CookieJar()
예제 #14
0
파일: _platform.py 프로젝트: vimov/Deployer
	def basename(self, path):
		"""
		get the base name of path
		i.e. basename of /a/b/c is c
		i.e. basename of /a/b/c/ is ""
		
		@param paht: path that you want to get the base parent of.
		
		@rtype: string
		@return: the directory parent of the path.
		
		@raise InvalidParameterError: If the path is empty.
		"""
		
		if not Validator.validate_non_empty_string(path):
			raise InvalidParameterError("path", "Cannot be an empty string")
		
		path = self.trim_path(path)
		
		if path[len(path)-1] == self.get_separator():
			return ""
		
		index = path.rfind(self.get_separator())
		name = path[index+1:]
		
		return name
예제 #15
0
파일: _platform.py 프로젝트: vimov/Deployer
	def dirname(self, path):
		'''
		get the directrory name of path
		i.e. dirname of /a/b/c is /a/b
		
		@param paht: path that you want to get dirname of it
		@type path: String
		
		@rtype: String
		@return: the dirname of the path
		
		@raise InvalidParameterError: If the required argument(s) are not specified.
		'''

		if not Validator.validate_non_empty_string(path):
			raise InvalidParameterError("path", "Cannot be an empty string")
		
		path = self.trim_path(path)
		
		if path[len(path)-1] == self.get_separator():
			return path
		
		index = path.rfind(self.get_separator())
		name = path[0:index]
		
		return name
예제 #16
0
	def to_absolute(self, paths):
		"""
		Converts one or more paths to absolute, if they were relative.

		@param paths: Can be a list, or a string.

		@return: A list, or a string, based on the type of the argument.
		"""

		if paths.__class__ == list:

			for i in range(0, len(paths)):
				if self.get_platform().is_relative(paths[i]):
					paths[i] = self.get_platform().join(self.get_root(), paths[i])

			return paths

		elif True == Validator.validate_non_empty_string(paths):

			if self.get_platform().is_relative(paths):
				return self.get_platform().join(self.get_root(), paths)
			else:
				return paths

		else:
			raise InvalidParameterError("paths", "Must be a string or a list.")
예제 #17
0
파일: ftpsync.py 프로젝트: vimov/Deployer
	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
예제 #18
0
파일: ftpbase.py 프로젝트: vimov/Deployer
	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))
예제 #19
0
파일: browser.py 프로젝트: vimov/Deployer
	def __init__(self, match_pattern):
		'''
		@param match_pattern: regular expretion string 
		'''
		
		if not Validator.validate_string(match_pattern):
			raise InvalidParameterError("match_pattern", "Must be a string value")
		
		self.match_pattern = match_pattern
예제 #20
0
파일: browser.py 프로젝트: vimov/Deployer
	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)
예제 #21
0
파일: ftpbase.py 프로젝트: vimov/Deployer
	def chmod(self, path, permissions, excludes = None, contents_only = False, recursive = False, retry = 1):
		'''
		Change the permissions of file or directory on ftp server.
		
		@param path: path of the remote file
		@param permissions: target permissions
		@param excludes: the excluded files from the operation
		@param contents_only: boolean for changing the contents of the directory only or also the directory itself
		@param recursive: boolean for chmoding the directory recursively or not
		@param retry: the retry count on failure 
		
		@raise InvalidParameterError: If the required argument(s) are not specified. 
		@raise FileChmodError: If the function fails to chmod the file
		'''

		if not Validator.validate_non_empty_string(path):
			raise InvalidParameterError("path", "path can not be None or empty string")

		if not Validator.validate_non_empty_string(permissions):
			raise InvalidParameterError("permissions", "permissions can not be None or empty string")
		
		path = self.append_to_root(path)
		
		if not self.is_exists(path):
			raise FileNotExistsError, path
		try:
			permissions = self.__get_permissions(permissions)
			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.__chmod(path, permissions, retry)
				
			if (contents_only or recursive) and path_type is FileObject.DIRECTORY:
				list = self.list(path, excludes,  recursive, retry)
				self.__chmod_list(list, permissions, retry)
		except Exception, e:
			message = str(e)
			if message.find("Operation not permitted") > 0:
				raise PermissionDeniedError
			else:
				raise FileChmodError(path, message)
예제 #22
0
	def rename(self, source, destination, retry = 1):
		"""
		rename a file or dirctory
		
		@param source: the source path to be renamed
		@param destination: the new name of file or dorectory
		@param retry: number of retries befor fail
		
		@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 FileRenameError: if error occurred during moving
		"""
		
		if False == Validator.validate_non_empty_string(source):
			raise InvalidParameterError('source', 'should be a not empty string')
		if False == Validator.validate_non_empty_string(destination):
			raise InvalidParameterError('destination', 'should be a not empty string')
		if retry.__class__ != int:
			raise InvalidParameterError('retry', 'should be an integer value')
		
		#trim the '/' or '\' from the end of path
		source = self.get_platform().trim_path(source)
		
		#test if source is relative
		if self.get_platform().is_relative(source) :
			source = self.__root + source
		
		if not os.path.exists(source):
			raise FileNotExistError, 'the file or directory to be moved is not exist'
		
		while retry:
			retry -= 1
			try:
				parent = self.get_platform().dirname(source)
				destination = parent + self.get_platform().get_separator() + destination
				os.rename(source, destination)
			except Exception, e:
				if retry == 0:
					raise FileRenameError, str(e)
			else:
				break
예제 #23
0
	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()
예제 #24
0
파일: browser.py 프로젝트: vimov/Deployer
	def set_default_user_agent(cls, user_agent):
		'''
		sets default User Agent for all requests
		
		@param user_agent:
		'''
		
		if not Validator.validate_non_empty_string(user_agent):
			raise InvalidParameterError("user_agent", "Must be non-empty string")
		
		Request.USER_AGENT = user_agent
예제 #25
0
파일: browser.py 프로젝트: vimov/Deployer
	def set_referrer(self, referrer):
		'''
		sets the request referrer
		
		@param referrer: 
		'''
		
		if not Validator.validate_non_empty_string(referrer):
			raise InvalidParameterError("referrer", "Must be non-empty String")
		
		self.referrer = referrer
예제 #26
0
	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)
예제 #27
0
	def __init__(self, repository_path, username = '', password = ''):
		"""
		@param repository_path: path of the root of the repository
		@param username: user name to access the subversion (if it was authenticated)
		@param password: password to access the subversion (if it was authenticated)
		"""

		if not Validator.validate_non_empty_string(repository_path):
			raise InvalidParameterError("repository_path", "Must be a non-empty string")
		if not Validator.validate_string(username):
			raise InvalidParameterError("username", "Must be a string")
		if not Validator.validate_string(password):
			raise InvalidParameterError("password", "Must be a string")

		VersionControlSystemClient.__init__(self, repository_path, username, password)
		self.__client = pysvn.Client()

		if username and password:
			self.set_user(username, password)

		self.local_client = LocalClient()
예제 #28
0
	def __init__(self, name):
		"""
		Constructor for the class.

		@param name: Identifier name for this FilterChain.
		"""

		if not Validator.validate_non_empty_string(name):
			raise InvalidParameterError("name", "Must be a non-empty string")

		self.__name = name
		self.__filters = []
예제 #29
0
	def delete(self, path, excludes = None, contents_only = False, recursive = False, retry = 1):
		"""
		deletes file or directory recursively or non  recursively
		
		@param path: path of the file or directory to be deleted
		@param excludes: An instance of FileSet that describes the exclusion patterns.
		@param contents_only: if True, contents of directory only will be chowned
		@param recursive: if it is True the contents of non empty directory will be removed recursively else only file or
		empty directories will be deleted
		@param retry: number of retries 
		
		@rtype: bool
		@return: True on success
		
		@raise InvalidParameterError: if parameters are not valid
		@raise FileRemoveError: if error occurred during removing
		"""
		
		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 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')
		
		#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
						
			while retry:
				retry -= 1
				try:
					os.remove(path)
				except Exception, e:
					if retry == 0:
						raise FileRemoveError, str(e)
				else:
					break
예제 #30
0
파일: ftpbase.py 프로젝트: vimov/Deployer
	def delete(self, path, excludes = None, contents_only = False, recursive = False, retry = 1):
		'''
		delete file or directory from the server
		
		@param path: path to be deleted
		@param excludes: list of the excluded file from the operation
		@param contents_only: boolean for deleting the contents of the file and only or the file its self also
		@param recursive: if the path is directory delete it recusively or not
		@param retry: retry count if the operation is failed 

		@raise InvalidParameterError: If the required argument(s) are not specified.
		'''
		if not Validator.validate_non_empty_string(path):
			raise InvalidParameterError("path", "path can not be None or empty string")

		if self.__ftp_client is None:			
			self.connect()

		if path == '.':
			path = self.get_cwd()
		else:
			path = self.append_to_root(path)
		
		try:
			if self.is_exists(path):
				path_type = self.get_type(path)
				file_name = self.get_platform().basename(path)
				
				if not (excludes is not None and  excludes.match(path, file_name, path_type)):
					if path_type is FileObject.DIRECTORY:
						"""
						delete directory 
						"""
						files = self.list(path , excludes, recursive)
						if not recursive and not contents_only and len(files) == 0:
							self.__delete_empty_dir(path, retry)
						elif not recursive and not contents_only and len(files) > 0:
							raise DirectoryNotEmptyError, path
						elif not recursive and contents_only:
							return 
						else:												
							self.__delete(files, excludes, recursive, retry)
							if not contents_only:
								self.__delete_empty_dir(path, retry)
					else:
						self.__delete_file(path, retry)

		except Exception, e:
			message = 'can not delete the file ' + path
			raise FileDeleteError(message, str(e))