Add support for running SQL scripts in lampconfigure.

parent e298fa41
...@@ -22,7 +22,7 @@ import sys ...@@ -22,7 +22,7 @@ import sys
import argparse import argparse
def run(): def run():
"""Start the instance runner. this may run a python script, move or/and rename """Start the instance runner. this may run a python script, move or/and rename
file or directory when dondition is filled. the condition may be when file exist or when an entry file or directory when dondition is filled. the condition may be when file exist or when an entry
exist into database. exist into database.
""" """
...@@ -50,7 +50,7 @@ def run(): ...@@ -50,7 +50,7 @@ def run():
subparsers = parser.add_subparsers(help='commands') subparsers = parser.add_subparsers(help='commands')
# A delete command # A delete command
delete_parser = subparsers.add_parser('delete', help='Remove file or directory') delete_parser = subparsers.add_parser('delete', help='Remove file or directory')
delete_parser.add_argument('delete_target', action='store', help='The list of file or directory to remove', delete_parser.add_argument('delete_target', action='store', help='The list of file or directory to remove',
nargs='+') nargs='+')
# A rename command # A rename command
rename_parser = subparsers.add_parser('rename', help='Rename SOURCE to DESTINATION') rename_parser = subparsers.add_parser('rename', help='Rename SOURCE to DESTINATION')
...@@ -62,6 +62,12 @@ def run(): ...@@ -62,6 +62,12 @@ def run():
script_parser = subparsers.add_parser('run', help='Run python script') script_parser = subparsers.add_parser('run', help='Run python script')
script_parser.add_argument('script', action='store', help='The path of python script to execute') script_parser.add_argument('script', action='store', help='The path of python script to execute')
script_parser.add_argument('-v', action='store', help='Other argument to pass to the script', nargs='+', dest='args') script_parser.add_argument('-v', action='store', help='Other argument to pass to the script', nargs='+', dest='args')
# A SQL script command
sql_parser = subparsers.add_parser('sql', help='Run SQL script')
sql_parser.add_argument('sql_script', action='store', help='The path of python script to execute')
sql_parser.add_argument('-v', action='store', help='Other argument to pass to the script', nargs='+', dest='args')
#A chmod command #A chmod command
chmod_parser = subparsers.add_parser('chmod', help='Set the mode of file or directory') chmod_parser = subparsers.add_parser('chmod', help='Set the mode of file or directory')
chmod_parser.add_argument('mode', action='store', help='the mode to apply to TARGET') chmod_parser.add_argument('mode', action='store', help='the mode to apply to TARGET')
...@@ -73,7 +79,7 @@ def run(): ...@@ -73,7 +79,7 @@ def run():
print "lampconfigure: Error: Please specify where condition will be taken, use -d or -f option" print "lampconfigure: Error: Please specify where condition will be taken, use -d or -f option"
return return
setup(arguments) setup(arguments)
def setup(arguments): def setup(arguments):
timeout = 5; timeout = 5;
while True: while True:
...@@ -84,13 +90,16 @@ def setup(arguments): ...@@ -84,13 +90,16 @@ def setup(arguments):
time.sleep(timeout) time.sleep(timeout)
if arguments.has_key('delete_target'): if arguments.has_key('delete_target'):
delete(arguments) delete(arguments)
if arguments.has_key('source'): if arguments.has_key('source'):
rename(arguments) rename(arguments)
if arguments.has_key('script'): if arguments.has_key('script'):
run_script(arguments) run_script(arguments)
if arguments.has_key('sql_script'):
run_sql_script(arguments)
if arguments.has_key('chmod_target'): if arguments.has_key('chmod_target'):
chmod(arguments) chmod(arguments)
return return
...@@ -119,19 +128,19 @@ def checkAction(arguments): ...@@ -119,19 +128,19 @@ def checkAction(arguments):
row = cursor.fetchone () row = cursor.fetchone ()
if row == None: if row == None:
conn.close() conn.close()
return False return True
else: else:
arguments['table'] = row[0] arguments['table'] = row[0]
cursor.execute ("SELECT * FROM " + arguments['table'] + " WHERE " + arguments['condition']) cursor.execute ("SELECT * FROM " + arguments['table'] + " WHERE " + arguments['condition'])
row = cursor.fetchone () row = cursor.fetchone ()
conn.close() conn.close()
if row == None: if row == None:
return False
else:
return True return True
else:
return False
else: else:
return os.path.exists(os.path.join(arguments['target_directory'], arguments['file_token'])) return not os.path.exists(os.path.join(arguments['target_directory'], arguments['file_token']))
def rename(arguments): def rename(arguments):
source = os.path.join(arguments['target_directory'], arguments['source']) source = os.path.join(arguments['target_directory'], arguments['source'])
destination = os.path.join(arguments['target_directory'], arguments['destination']) destination = os.path.join(arguments['target_directory'], arguments['destination'])
...@@ -141,7 +150,7 @@ def rename(arguments): ...@@ -141,7 +150,7 @@ def rename(arguments):
os.rename(source, destination) os.rename(source, destination)
if arguments['mode'] != None: if arguments['mode'] != None:
os.chmod(destination, int(arguments['mode'], 8)) os.chmod(destination, int(arguments['mode'], 8))
def delete(arguments): def delete(arguments):
for path in arguments['delete_target']: for path in arguments['delete_target']:
path = os.path.join(arguments['target_directory'], path) path = os.path.join(arguments['target_directory'], path)
...@@ -155,6 +164,7 @@ def delete(arguments): ...@@ -155,6 +164,7 @@ def delete(arguments):
def run_script(arguments): def run_script(arguments):
script = os.path.join(arguments['target_directory'], arguments['script']) script = os.path.join(arguments['target_directory'], arguments['script'])
print 'Running script: %s' % script
if os.path.exists(script): if os.path.exists(script):
import subprocess import subprocess
#run python script with predefined data #run python script with predefined data
...@@ -167,7 +177,29 @@ def run_script(arguments): ...@@ -167,7 +177,29 @@ def run_script(arguments):
result.wait() result.wait()
else: else:
print "Error: can not read file '%s'" % script print "Error: can not read file '%s'" % script
def run_sql_script(arguments):
script = os.path.join(arguments['target_directory'], arguments['sql_script'])
print 'Running SQL script: %s' % script
if os.path.exists(script):
conn = MySQLdb.connect(host=arguments['mysql_host'],
port=int(arguments['mysql_port']),
user=arguments['mysql_user'],
passwd=arguments['mysql_password'],
# XXX ugly, to simplify
db=arguments['args'][0])
cursor = conn.cursor ()
with open(script, 'r') as f:
sql_script = f.read()
cursor.execute(sql_script)
conn.close()
else:
print "Error: can not read file '%s'" % script
def chmod(arguments): def chmod(arguments):
for path in arguments['chmod_target']: for path in arguments['chmod_target']:
path = os.path.join(arguments['target_directory'], path) path = os.path.join(arguments['target_directory'], path)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment