/CCGOnline/DB/SQLServer/Python/ccg_sqlserver_utils.py

https://github.com/bretambrose/CCGOnlinePublic · Python · 190 lines · 121 code · 58 blank · 11 comment · 28 complexity · ef5102583e7e48103a3fdaf3c51d5530 MD5 · raw file

  1. import re
  2. import subprocess
  3. import os
  4. import random
  5. import time
  6. def SQLException( Exception ):
  7. def __init__( self, error_code, filename ):
  8. self.ErrorCode = error_code
  9. self.FileName = filename
  10. def LoadDBSettings():
  11. assign_re = re.compile( "(?P<var>\w+)=(?P<value>\S*)" )
  12. db_kv_pairs = {}
  13. try:
  14. db_settings_file = open( "../DBSettings.txt", 'r' )
  15. # read the file, pulling out key value pairs in the form of "var=value"
  16. line = db_settings_file.readline()
  17. while line != "":
  18. result = assign_re.search( line )
  19. if result != None:
  20. variable_name = result.group( "var" )
  21. value = result.group( "value" )
  22. db_kv_pairs[ variable_name ] = value
  23. line = db_settings_file.readline()
  24. db_settings_file.close()
  25. except IOError:
  26. print( "ERROR: Unable to find DBSettings.txt" )
  27. return db_kv_pairs
  28. def CleanTempFiles():
  29. # check for Temp directory existence, create if doesn't exist
  30. if os.path.exists( "Temp" ) == False:
  31. os.mkdir( "Temp" )
  32. return
  33. for file_name in os.listdir( "Temp" ):
  34. os.remove( "Temp/" + file_name )
  35. def BuildTempSQLFile( sql_script_file_path, log_file_name, procedure_user ):
  36. full_script_path = "SQL/" + sql_script_file_path
  37. # make sure sql script exists
  38. if os.path.exists( full_script_path ) == False:
  39. print( "File not found: " + full_script_path )
  40. return None
  41. filename_list = []
  42. if os.path.isdir( full_script_path ):
  43. for filename in os.listdir( full_script_path ):
  44. filename_list.append( full_script_path + "/" + filename )
  45. else:
  46. filename_list.append( full_script_path )
  47. random_number = random.randint( 0, 1000000 )
  48. temp_filename = "Temp/" + log_file_name + str( random_number ) + ".sql"
  49. with open( temp_filename, "w" ) as temp_file:
  50. for filename in filename_list:
  51. temp_file.write( ":r " + filename + "\n" )
  52. return temp_filename
  53. def InitLogFiles( log_file_name ):
  54. # check for Logs directory existence, create if doesn't exist
  55. if os.path.exists( "Logs" ) == False:
  56. os.mkdir( "Logs" )
  57. # Open log files.
  58. stdout_file_name = "Logs/" + log_file_name + "_output.txt"
  59. stderr_file_name = "Logs/" + log_file_name + "_error.txt"
  60. print( "Script output going to: ", stdout_file_name )
  61. print( "Script errors going to: ", stderr_file_name )
  62. return ( stdout_file_name, stderr_file_name )
  63. def CCGRunDBScriptsAux( user, sql_script_list, log_file_name, granted_procedure_user ):
  64. base_start_time = time.clock()
  65. # load our local DB settings
  66. kv_pairs = LoadDBSettings()
  67. if "DB" in kv_pairs == False:
  68. print( "No DB name specific in DBSettings.txt" )
  69. return
  70. db_name = kv_pairs[ "DB" ]
  71. # extract the correct db username
  72. username_key = user + "Account"
  73. if username_key in kv_pairs == False:
  74. print( "Unknown DB User: " + user )
  75. return
  76. username = kv_pairs[ username_key ]
  77. # extract the correct password
  78. username_password_key = user + "Password"
  79. if username_password_key in kv_pairs == False:
  80. print( "No password entry for DB user: " + user )
  81. return
  82. password = kv_pairs[ username_password_key ]
  83. print( "Login: " + username + "/" + password );
  84. procedure_user = ""
  85. if granted_procedure_user != "":
  86. procedure_user_key = granted_procedure_user + "Account"
  87. if procedure_user_key in kv_pairs == False:
  88. print( "Unknown Procedure Grantee: " + granted_procedure_user )
  89. return
  90. procedure_user = kv_pairs[ procedure_user_key ]
  91. # assumes being run from the DB/Python subdirectory
  92. old_directory = os.getcwd()
  93. try:
  94. os.chdir( ".." )
  95. CleanTempFiles()
  96. stdout_file_name, stderr_file_name = InitLogFiles( log_file_name )
  97. with open( stdout_file_name, "w" ) as test_log_file, open( stderr_file_name, "w" ) as test_error_file:
  98. for sql_script_file_path in sql_script_list:
  99. temp_filename = BuildTempSQLFile( sql_script_file_path, log_file_name, procedure_user )
  100. if temp_filename != None:
  101. print( "Processing " + sql_script_file_path + " using temp file: " + temp_filename )
  102. test_log_file.write( "\n***************************************************************\n" )
  103. test_log_file.write( "Processing file: " + sql_script_file_path + "\n\n" )
  104. test_log_file.flush()
  105. # build the argument list
  106. command_line_args = "-S " + db_name + " -U " + username + " -P " + password + " -i " + temp_filename
  107. print( "Command line args: " + command_line_args )
  108. # Execute the SQL script
  109. return_code = subprocess.call( "sqlcmd.exe " + command_line_args, stdout = test_log_file, stderr = test_error_file )
  110. if return_code != 0:
  111. test_log_file.write( "SQLServer Error: " + str( return_code ) + "\n" )
  112. test_log_file.write( "*****ABORTING script execution*****\n" )
  113. test_log_file.flush()
  114. raise SQLException( return_code, sql_script_file_path )
  115. except SQLException as sql_exception:
  116. print( "SQL Error ( " + str( sql_exception.ErrorCode ) + " ) while executing file: " + sql_exception.FileName )
  117. raise sql_exception
  118. finally:
  119. end_time = time.clock()
  120. print( "Total Time: " + str( end_time - base_start_time ) + " seconds" )
  121. os.chdir( old_directory )
  122. def CCGRunDBScript( user, sql_script_path, script_type, log_file_name, granted_procedure_user = "" ):
  123. CCGRunDBScripts( user, [ ( sql_script_path, script_type ) ], log_file_name, granted_procedure_user )
  124. def CCGRunDBScripts( user, sql_script_path_list, log_file_name, granted_procedure_user = "", exit_immediately = False ):
  125. try:
  126. CCGRunDBScriptsAux( user, sql_script_path_list, log_file_name, granted_procedure_user )
  127. print( "Success!" )
  128. except:
  129. print( "There were errors =(" )
  130. finally:
  131. if exit_immediately == False:
  132. input( "Press Enter to exit" )