예제 #1
0
	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
예제 #2
0
	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
예제 #3
0
	def _FtpBase__get_permissions(self, permissions):
		return FileObject.convert_permissions(permissions, self.get_platform(), convert_type = FileObject.PERMISSIONS_AS_OCT)