| 1 |
ninoborges |
8 |
## Commence_ToolBar_Fix.py
|
| 2 |
|
|
## This program will correct the problem that happens to a Commence user's tool bar once RM in installed.
|
| 3 |
|
|
## Since this will be used to install databases other than just the Labor Database, it will require that
|
| 4 |
|
|
## you pass to it the name of the BD you are upgrading
|
| 5 |
|
|
## EBorges
|
| 6 |
|
|
## 03.26.03
|
| 7 |
|
|
|
| 8 |
|
|
import os,string,time,sys
|
| 9 |
|
|
from _winreg import *
|
| 10 |
|
|
|
| 11 |
|
|
## Function: addToLogFile
|
| 12 |
|
|
## Purpose: Writes the string passed in to the designated log file
|
| 13 |
|
|
## Arguments: fileName - name of the log file
|
| 14 |
|
|
## logEntry - string to add to end of log file
|
| 15 |
|
|
## userSpecific - set to 1 if log file stored in the user's netchk dir, 0 if stored globally
|
| 16 |
|
|
## echo - set to 1 to also echo the logEntry string to stdout
|
| 17 |
|
|
def addToLogFile(fileName, logEntry, userSpecific=0, echo=0 ):
|
| 18 |
|
|
# 1. Check if log file to be global or local to user
|
| 19 |
|
|
if userSpecific == 1:
|
| 20 |
|
|
# Since user specific, pull user's log directory from the registry
|
| 21 |
|
|
# ...
|
| 22 |
|
|
hkey = OpenKey( HKEY_CURRENT_USER, 'Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders' )
|
| 23 |
|
|
AppData = QueryValueEx( hkey, 'Local AppData' )
|
| 24 |
|
|
logDir = AppData[0] + '\\McGuireWoods LLP\\NetChk'
|
| 25 |
|
|
else:
|
| 26 |
|
|
# Since not user specific, use the global log directory
|
| 27 |
|
|
WinDir = os.getenv('SystemRoot')
|
| 28 |
|
|
WinDrive = string.split( WinDir, ':' )
|
| 29 |
|
|
logDir = WinDrive[0] + ':\\NetChk'
|
| 30 |
|
|
logFileName = logDir + '\\' + fileName
|
| 31 |
|
|
try:
|
| 32 |
|
|
logFile = open( logFileName, 'a' )
|
| 33 |
|
|
except IOError:
|
| 34 |
|
|
os.makedirs(logDir)
|
| 35 |
|
|
logFile = open( logFileName, 'a' )
|
| 36 |
|
|
logFile.write( time.strftime('%d/%m/%Y\t%H:%M:%S -\t',time.localtime()) + logEntry + '\n' )
|
| 37 |
|
|
if echo == 1: print logEntry
|
| 38 |
|
|
logFile.close()
|
| 39 |
|
|
|
| 40 |
|
|
## EBorges
|
| 41 |
|
|
## Function: changeWinRegValue
|
| 42 |
|
|
## Purpose: This function will change 1 registry value in place based on a search and replace string.
|
| 43 |
|
|
## Arguments: rootHandle - one of the HKEY* constants.
|
| 44 |
|
|
## regKey - the key with the value you wish to replace
|
| 45 |
|
|
## valueName - the value name whose information you wish to change
|
| 46 |
|
|
def changeWinRegValue(rootHandle,regKey,valueName,value):
|
| 47 |
|
|
iKeyObj = OpenKey(rootHandle, regKey)
|
| 48 |
|
|
result = QueryValueEx(iKeyObj,valueName)
|
| 49 |
|
|
iKeyObj.Close()
|
| 50 |
|
|
value = int(value)
|
| 51 |
|
|
#value = result[0]
|
| 52 |
|
|
type = result[1]
|
| 53 |
|
|
#value = value.replace(stringFind,stringReplace)
|
| 54 |
|
|
oKeyObj = OpenKey(rootHandle, regKey,0,KEY_WRITE)
|
| 55 |
|
|
SetValueEx(oKeyObj,valueName,'',type,value)
|
| 56 |
|
|
oKeyObj.Close()
|
| 57 |
|
|
|
| 58 |
|
|
## Function: deleteRegKeyWithKeys
|
| 59 |
|
|
## Purpose: This function will allow the deletion of registry keys that have subkeys
|
| 60 |
|
|
## by deleting the subkeys of a registry key.
|
| 61 |
|
|
## This is currently a no-no from within Python, with good reason. So
|
| 62 |
|
|
## use with EXTREAM CAUTION!!
|
| 63 |
|
|
def deleteRegKeyWithKeys(handle,regKey):
|
| 64 |
|
|
# Enumerate the keys
|
| 65 |
|
|
subKeys = []
|
| 66 |
|
|
rKeyObj = OpenKey(handle,regKey)
|
| 67 |
|
|
numbSubKeys = QueryInfoKey(rKeyObj)[0]
|
| 68 |
|
|
for i in range (0,numbSubKeys):
|
| 69 |
|
|
subKeys.append(EnumKey(rKeyObj,i))
|
| 70 |
|
|
rKeyObj.Close()
|
| 71 |
|
|
# Delete the key and subkeys
|
| 72 |
|
|
wKeyObj = OpenKey(handle,regKey,0,KEY_WRITE)
|
| 73 |
|
|
for x in subKeys:
|
| 74 |
|
|
DeleteKey(wKeyObj,x)
|
| 75 |
|
|
wKeyObj.Close()
|
| 76 |
|
|
|
| 77 |
|
|
if __name__ == '__main__':
|
| 78 |
|
|
logFile = 'Commence_RM_Upgrade_py.log'
|
| 79 |
|
|
addToLogFile( logFile, 'Starting the Commence ToolBar Fix.')
|
| 80 |
|
|
try:
|
| 81 |
|
|
DataBase = sys.argv[1]
|
| 82 |
|
|
except:
|
| 83 |
|
|
DataBase = ""
|
| 84 |
|
|
if DataBase:
|
| 85 |
|
|
addToLogFile( logFile, 'Searching for the %s Database in the registry...' % DataBase)
|
| 86 |
|
|
DBKeyObj = OpenKey(HKEY_CURRENT_USER, r'Software\Commence\Databases\6.0')
|
| 87 |
|
|
numbOfKeys = QueryInfoKey(DBKeyObj)[0]
|
| 88 |
|
|
DBList=[]
|
| 89 |
|
|
for i in range (numbOfKeys):
|
| 90 |
|
|
key = EnumKey(DBKeyObj,i)
|
| 91 |
|
|
DBList.append(key)
|
| 92 |
|
|
DBKeyObj.Close()
|
| 93 |
|
|
KeyNumber = ""
|
| 94 |
|
|
for x in DBList:
|
| 95 |
|
|
tempKeyObj = OpenKey(HKEY_CURRENT_USER, 'Software\\Commence\\Databases\\6.0\\%s'%x)
|
| 96 |
|
|
databaseName = QueryValueEx(tempKeyObj,'Name')[0]
|
| 97 |
|
|
if databaseName == 'Labor Database':
|
| 98 |
|
|
KeyNumber = x
|
| 99 |
|
|
addToLogFile( logFile, 'Found the %s database in the registry. Will now repair the toolbar...'% DataBase)
|
| 100 |
|
|
addToLogFile( logFile, 'MW Database = %s'%x)
|
| 101 |
|
|
break
|
| 102 |
|
|
tempKeyObj.Close()
|
| 103 |
|
|
if KeyNumber:
|
| 104 |
|
|
foundDBKeyObj = OpenKey(HKEY_CURRENT_USER, 'Software\\Commence\\Commence\\6.1\\windows',0,KEY_WRITE)
|
| 105 |
|
|
deleteRegKeyWithKeys(HKEY_CURRENT_USER, 'Software\\Commence\\Commence\\6.1\\windows\\%s'% KeyNumber)
|
| 106 |
|
|
DeleteKey(foundDBKeyObj,KeyNumber)
|
| 107 |
|
|
addToLogFile( logFile, '%s Database toolbar has been reset.'% DataBase)
|
| 108 |
|
|
addToLogFile( logFile, 'Setting %s as the default database...'% DataBase)
|
| 109 |
|
|
changeWinRegValue( HKEY_CURRENT_USER, 'Software\\Commence\\Databases\\6.0', 'Active', KeyNumber)
|
| 110 |
|
|
addToLogFile( logFile, 'The default database has been set. Returning to installer script.')
|
| 111 |
|
|
foundDBKeyObj.Close()
|
| 112 |
|
|
else:
|
| 113 |
|
|
addToLogFile( logFile, 'ERROR: I was not able to find the %s database in the registry. Exiting...'% DataBase)
|
| 114 |
|
|
else:
|
| 115 |
|
|
addToLogFile( logFile, 'ERROR: No database was passed to me from the calling program. Exiting without changing the Registry')
|