-
Notifications
You must be signed in to change notification settings - Fork 345
/
DirectoryManagement.py
87 lines (77 loc) · 2.86 KB
/
DirectoryManagement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
# -*- coding: utf-8 -*-
from OracleDatabase import OracleDatabase
import logging
class DirectoryManagement(OracleDatabase):
'''
Allow to manage directories
'''
def __init__(self,args):
'''
Constructor
'''
logging.debug("DirectoryManagement object created")
OracleDatabase.__init__(self,args)
self.PREFIX = "ODATPREFIX"
#self.__dropAllOldDirectories__()
self.__setDirectoryName__()
def __setDirectoryName__(self):
'''
Generate a new directory name
'''
logging.debug('Generate a new directory name localy')
self.directoryName = self.PREFIX + self.__generateRandomString__(nb=20).upper()
def __createOrRemplaceDirectory__(self,path):
'''
Create a directory and grant READ,WRITE to PUBLIC
Return True if no error, otherwise return exception
'''
logging.debug('Create or remplace the {0} directory to {1}'.format(self.directoryName, path))
CREATE_REQUEST = "CREATE OR REPLACE DIRECTORY {0} AS '{1}'".format(self.directoryName, path)
GRANT_REQUEST = "GRANT READ,WRITE ON DIRECTORY {0} TO PUBLIC".format(self.directoryName)
response = self.__execPLSQL__(CREATE_REQUEST)
if isinstance(response,Exception):
logging.info("Impossible to create the directory: {0}".format(str(response).replace('\n',' ')))
return response
response = self.__execPLSQL__(GRANT_REQUEST)
if isinstance(response,Exception):
logging.info("Impossible to grant privileges on the directory: {0}".format(str(response).replace('\n',' ')))
return response
return True
def __dropThisDirectory__(self, nameOfTheDirectory):
'''
Drop the directoryName directory
Return True if no error, otherwise return exception
'''
logging.debug('Drop the {0} directory'.format(nameOfTheDirectory))
DROP_REQUEST = "DROP DIRECTORY {0}".format(nameOfTheDirectory)
response = self.__execPLSQL__(DROP_REQUEST)
if isinstance(response,Exception):
logging.info("Impossible to drop the directory: {0}".format(str(response).replace('\n',' ')))
return response
return True
def __dropDirectory__(self):
'''
Drop the directoryName directory
Return True if no error, otherwise return exception
'''
return self.__dropThisDirectory__(self.directoryName)
def __dropAllOldDirectories__(self):
'''
Drop all directories created
Return False if error
Otherwise return True
'''
logging.debug('Drop all directories created')
SELECT_REQ = "SELECT directory_name FROM all_directories WHERE directory_name LIKE '{0}%'".format(self.PREFIX)
response = self. __execThisQuery__(query=SELECT_REQ,ld=['directory_name'])
if isinstance(response,Exception) :
logging.info('Error with the SQL request {0}: {1}'.format(SELECT_REQ,str(response)))
return ErrorSQLRequest(response)
if response == [] :
logging.debug("No directory to delete")
return True
else :
for aDir in response :
self.__dropThisDirectory__(aDir['directory_name'])
return True