Add support for running SQL scripts in lampconfigure.

parent e298fa41
...@@ -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')
...@@ -91,6 +97,9 @@ def setup(arguments): ...@@ -91,6 +97,9 @@ def setup(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,18 +128,18 @@ def checkAction(arguments): ...@@ -119,18 +128,18 @@ 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: else:
return os.path.exists(os.path.join(arguments['target_directory'], arguments['file_token'])) return False
else:
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'])
...@@ -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
...@@ -168,6 +178,28 @@ def run_script(arguments): ...@@ -168,6 +178,28 @@ def run_script(arguments):
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