Advertisement
Guest User

oracle clean

a guest
Sep 29th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.52 KB | None | 0 0
  1. from winreg import *
  2. import re
  3. import os
  4. import shutil
  5.  
  6. local_machine_registry = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
  7. def find_subkeys(key_name, search_regex):
  8.     subkeys_found=[]
  9.     try:
  10.         key = OpenKey(local_machine_registry, key_name, 0, KEY_WOW64_64KEY | KEY_ALL_ACCESS)
  11.         try:
  12.             i = 0
  13.             while True:
  14.                 subkey = EnumKey(key, i)
  15.                 if re.search(search_regex, subkey, re.I) is not None:
  16.                     if re.search("microsoft", subkey, re.I) is None:
  17.                         subkeys_found.append(subkey)
  18.                 i += 1
  19.         except WindowsError:
  20.             pass
  21.         CloseKey(key)
  22.     except WindowsError:
  23.         pass
  24.     return subkeys_found
  25.  
  26. def find_values(key_name, search_regex):
  27.     found_values=[]
  28.     try:
  29.         key = OpenKey(local_machine_registry, key_name, 0, KEY_WOW64_64KEY | KEY_ALL_ACCESS)
  30.  
  31.         try:
  32.             i = 0
  33.             while True:
  34.                 name, value, value_type = EnumValue(key, i)
  35.                 if re.search(search_regex, name, re.I) is not None:
  36.                     found_values.append([name, value, type])
  37.                 if re.search(search_regex, value, re.I) is not None:
  38.                     found_values.append([name, value, type])
  39.                 i += 1
  40.         except WindowsError:
  41.             pass
  42.         CloseKey(key)
  43.     except WindowsError:
  44.         pass
  45.     return found_values
  46.  
  47. def delete_subkeys(key_name, subkey_names):
  48.     try:
  49.         key = OpenKey(local_machine_registry, key_name, 0, KEY_WOW64_64KEY | KEY_ALL_ACCESS)
  50.         if subkey_names is not None:
  51.             for subkey_name in subkey_names:
  52.                 print("Deleting subkey in " + key_name + ": " + subkey_name)
  53.                 DeleteKey(key, subkey_name)
  54.     except WindowsError:
  55.         pass
  56.    
  57. def delete_values(key_name, values):
  58.     try:
  59.         key = OpenKey(local_machine_registry, key_name, 0, KEY_WOW64_64KEY | KEY_ALL_ACCESS)
  60.         if values is not None:
  61.             for value in values:
  62.                 print("Deleting value in " + key_name + ": " + value[0])
  63.                 DeleteKey(key, value[0])
  64.     except WindowsError:
  65.         pass
  66.    
  67. def get_value_names(values):
  68.     names=[]
  69.     for value in values:
  70.         names.append(value[0])
  71.     return names
  72.  
  73. def get_oracle_homes(subkey_name, search_regex):
  74.     oracle_homes=[]
  75.     oracle_installs=find_subkeys(subkey_name, search_regex)
  76.     if oracle_installs is not None:
  77.         for oracle_install in oracle_installs:
  78.             oracle_key=subkey_name + "\\" + oracle_install
  79.             found_homes = find_values(oracle_key, "^oracle_home$")
  80.             if found_homes is not None:
  81.                 for found_home in found_homes:
  82.                     oracle_homes.append(found_home[1])
  83.     return oracle_homes
  84.  
  85. def rm_oracle_homes(oracle_homes):
  86.     if oracle_homes is not None:
  87.         for oracle_home in oracle_homes:
  88.             # check if folder exists
  89.             if os.path.exists(oracle_home):
  90.                  # remove if exists
  91.                  shutil.rmtree(oracle_home)
  92.                  print("Removing dir: " + oracle_home)
  93.    
  94. def clean_oracle_installs(key_prefix, bit_size):
  95.     print("Cleaning " + bit_size + " bit registry...")
  96.     oracle_odbc_insts = find_subkeys(key_prefix + "\\ODBC\\ODBCINST.INI", "oracle")
  97.     delete_subkeys(key_prefix + "\\ODBC\\ODBCINST.INI", oracle_odbc_insts)
  98.     oracle_odbc_drivers = find_values(key_prefix + "ODBC\\ODBCINST.INI\\ODBC Drivers", "oracle")
  99.     delete_values(key_prefix + "\\ODBC\\ODBCINST.INI\\ODBC Drivers", oracle_odbc_drivers)
  100.     oracle_datasources = find_values(key_prefix + "\\ODBC\\ODBC.INI\\ODBC Data Sources", "oracle")
  101.     delete_values(key_prefix + "\\ODBC\\ODBC.INI\\ODBC Data Sources", oracle_datasources)
  102.     delete_subkeys(key_prefix + "\\ODBC\\ODBC.INI", get_value_names(oracle_datasources))
  103.  
  104.     # get all oracle homes
  105.     oracle_homes = get_oracle_homes(key_prefix + "\\Oracle", "KEY_")
  106.     rm_oracle_homes(oracle_homes)
  107.  
  108.     # remove all info about oracle
  109.     delete_subkeys(key_prefix, ["Oracle"])
  110.    
  111.  
  112. clean_oracle_installs(r"SOFTWARE", "64")
  113. clean_oracle_installs(r"SOFTWARE\Wow6432Node", "32")
  114.  
  115. # remove oracle directory completely
  116. if os.path.exists(r"C:\oracle"):
  117.      # remove if exists
  118.      shutil.rmtree(r"C:\oracle")
  119.      print(r"Removing dir: C:\oracle")
  120.  
  121. install_directory = os.getcwd()
  122. # install odac 64
  123. print("Installing 64 bit ODAC...")
  124. os.chdir(install_directory + "\\ODAC112040Xcopy_64bit\\")
  125. os.system(install_directory + "\\ODAC112040Xcopy_64bit\\install.bat all C:\\Oracle\\ora64 ora64")
  126. # register
  127. os.chdir(r"C:\Oracle\ora64")
  128. os.system(r"C:\Oracle\ora64\odbc_install.exe")
  129.  
  130. # install odac 32
  131. print("Installing 32 bit ODAC...")
  132. os.chdir(install_directory + "\\ODAC112040Xcopy_32bit\\")
  133. os.system(install_directory + "\\ODAC112040Xcopy_32bit\\install.bat all C:\\Oracle\\ora32 ora32")
  134. # register
  135. os.chdir(r"C:\Oracle\ora32")
  136. os.system(r"C:\Oracle\ora32\odbc_install.exe")
  137.  
  138. # add tns_admin directory
  139. try:
  140.     shutil.copytree(install_directory + "\\network" , "C:\\Oracle\\network")
  141. except OSError as exc: # python >2.5
  142.     if exc.errno == errno.ENOTDIR:
  143.         shutil.copy(install_directory + "\\network" , "C:\\Oracle\\network")
  144.     else: raise
  145.  
  146. # add tns_names to registry
  147. os.chdir(install_directory)
  148. for root, dirs, files in os.walk(install_directory):
  149.     for file in files:
  150.         if file.endswith('.reg'):
  151.             os.system("regedit /s " + install_directory + "\\" + file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement