pywin32_postinstall.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. # postinstall script for pywin32
  2. #
  3. # copies PyWinTypesxx.dll and PythonCOMxx.dll into the system directory,
  4. # and creates a pth file
  5. import os
  6. import sys
  7. import glob
  8. import shutil
  9. import time
  10. import distutils.sysconfig
  11. try:
  12. import winreg as winreg
  13. except:
  14. import winreg
  15. # Send output somewhere so it can be found if necessary...
  16. import tempfile
  17. tee_f = open(os.path.join(tempfile.gettempdir(), 'pywin32_postinstall.log'), "w")
  18. class Tee:
  19. def __init__(self, file):
  20. self.f = file
  21. def write(self, what):
  22. if self.f is not None:
  23. try:
  24. self.f.write(what.replace("\n", "\r\n"))
  25. except IOError:
  26. pass
  27. tee_f.write(what)
  28. def flush(self):
  29. if self.f is not None:
  30. try:
  31. self.f.flush()
  32. except IOError:
  33. pass
  34. tee_f.flush()
  35. # For some unknown reason, when running under bdist_wininst we will start up
  36. # with sys.stdout as None but stderr is hooked up. This work-around allows
  37. # bdist_wininst to see the output we write and display it at the end of
  38. # the install.
  39. if sys.stdout is None:
  40. sys.stdout = sys.stderr
  41. sys.stderr = Tee(sys.stderr)
  42. sys.stdout = Tee(sys.stdout)
  43. com_modules = [
  44. # module_name, class_names
  45. ("win32com.servers.interp", "Interpreter"),
  46. ("win32com.servers.dictionary", "DictionaryPolicy"),
  47. ("win32com.axscript.client.pyscript","PyScript"),
  48. ]
  49. # Is this a 'silent' install - ie, avoid all dialogs.
  50. # Different than 'verbose'
  51. silent = 0
  52. # Verbosity of output messages.
  53. verbose = 1
  54. ver_string = "%d.%d" % (sys.version_info[0], sys.version_info[1])
  55. root_key_name = "Software\\Python\\PythonCore\\" + ver_string
  56. try:
  57. # When this script is run from inside the bdist_wininst installer,
  58. # file_created() and directory_created() are additional builtin
  59. # functions which write lines to Python23\pywin32-install.log. This is
  60. # a list of actions for the uninstaller, the format is inspired by what
  61. # the Wise installer also creates.
  62. file_created
  63. is_bdist_wininst = True
  64. except NameError:
  65. is_bdist_wininst = False # we know what it is not - but not what it is :)
  66. def file_created(file):
  67. pass
  68. def directory_created(directory):
  69. pass
  70. def get_root_hkey():
  71. try:
  72. winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
  73. root_key_name, 0, winreg.KEY_CREATE_SUB_KEY)
  74. return winreg.HKEY_LOCAL_MACHINE
  75. except OSError as details:
  76. # Either not exist, or no permissions to create subkey means
  77. # must be HKCU
  78. return winreg.HKEY_CURRENT_USER
  79. try:
  80. create_shortcut
  81. except NameError:
  82. # Create a function with the same signature as create_shortcut provided
  83. # by bdist_wininst
  84. def create_shortcut(path, description, filename,
  85. arguments="", workdir="", iconpath="", iconindex=0):
  86. import pythoncom
  87. from win32com.shell import shell, shellcon
  88. ilink = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None,
  89. pythoncom.CLSCTX_INPROC_SERVER,
  90. shell.IID_IShellLink)
  91. ilink.SetPath(path)
  92. ilink.SetDescription(description)
  93. if arguments:
  94. ilink.SetArguments(arguments)
  95. if workdir:
  96. ilink.SetWorkingDirectory(workdir)
  97. if iconpath or iconindex:
  98. ilink.SetIconLocation(iconpath, iconindex)
  99. # now save it.
  100. ipf = ilink.QueryInterface(pythoncom.IID_IPersistFile)
  101. ipf.Save(filename, 0)
  102. # Support the same list of "path names" as bdist_wininst.
  103. def get_special_folder_path(path_name):
  104. import pythoncom
  105. from win32com.shell import shell, shellcon
  106. for maybe in """
  107. CSIDL_COMMON_STARTMENU CSIDL_STARTMENU CSIDL_COMMON_APPDATA
  108. CSIDL_LOCAL_APPDATA CSIDL_APPDATA CSIDL_COMMON_DESKTOPDIRECTORY
  109. CSIDL_DESKTOPDIRECTORY CSIDL_COMMON_STARTUP CSIDL_STARTUP
  110. CSIDL_COMMON_PROGRAMS CSIDL_PROGRAMS CSIDL_PROGRAM_FILES_COMMON
  111. CSIDL_PROGRAM_FILES CSIDL_FONTS""".split():
  112. if maybe == path_name:
  113. csidl = getattr(shellcon, maybe)
  114. return shell.SHGetSpecialFolderPath(0, csidl, False)
  115. raise ValueError("%s is an unknown path ID" % (path_name,))
  116. def CopyTo(desc, src, dest):
  117. import win32api, win32con
  118. while 1:
  119. try:
  120. win32api.CopyFile(src, dest, 0)
  121. return
  122. except win32api.error as details:
  123. if details.winerror==5: # access denied - user not admin.
  124. raise
  125. if silent:
  126. # Running silent mode - just re-raise the error.
  127. raise
  128. tb = None
  129. full_desc = "Error %s\n\n" \
  130. "If you have any Python applications running, " \
  131. "please close them now\nand select 'Retry'\n\n%s" \
  132. % (desc, details.strerror)
  133. rc = win32api.MessageBox(0,
  134. full_desc,
  135. "Installation Error",
  136. win32con.MB_ABORTRETRYIGNORE)
  137. if rc == win32con.IDABORT:
  138. raise
  139. elif rc == win32con.IDIGNORE:
  140. return
  141. # else retry - around we go again.
  142. # We need to import win32api to determine the Windows system directory,
  143. # so we can copy our system files there - but importing win32api will
  144. # load the pywintypes.dll already in the system directory preventing us
  145. # from updating them!
  146. # So, we pull the same trick pywintypes.py does, but it loads from
  147. # our pywintypes_system32 directory.
  148. def LoadSystemModule(lib_dir, modname):
  149. # See if this is a debug build.
  150. import imp
  151. for suffix_item in imp.get_suffixes():
  152. if suffix_item[0]=='_d.pyd':
  153. suffix = '_d'
  154. break
  155. else:
  156. suffix = ""
  157. filename = "%s%d%d%s.dll" % \
  158. (modname, sys.version_info[0], sys.version_info[1], suffix)
  159. filename = os.path.join(lib_dir, "pywin32_system32", filename)
  160. mod = imp.load_dynamic(modname, filename)
  161. def SetPyKeyVal(key_name, value_name, value):
  162. root_hkey = get_root_hkey()
  163. root_key = winreg.OpenKey(root_hkey, root_key_name)
  164. try:
  165. my_key = winreg.CreateKey(root_key, key_name)
  166. try:
  167. winreg.SetValueEx(my_key, value_name, 0, winreg.REG_SZ, value)
  168. finally:
  169. my_key.Close()
  170. finally:
  171. root_key.Close()
  172. if verbose:
  173. print(("-> %s\\%s[%s]=%r" % (root_key_name, key_name, value_name, value)))
  174. def RegisterCOMObjects(register = 1):
  175. import win32com.server.register
  176. if register:
  177. func = win32com.server.register.RegisterClasses
  178. else:
  179. func = win32com.server.register.UnregisterClasses
  180. flags = {}
  181. if not verbose:
  182. flags['quiet']=1
  183. for module, klass_name in com_modules:
  184. __import__(module)
  185. mod = sys.modules[module]
  186. flags["finalize_register"] = getattr(mod, "DllRegisterServer", None)
  187. flags["finalize_unregister"] = getattr(mod, "DllUnregisterServer", None)
  188. klass = getattr(mod, klass_name)
  189. func(klass, **flags)
  190. def RegisterPythonwin(register=True):
  191. """ Add (or remove) Pythonwin to context menu for python scripts.
  192. ??? Should probably also add Edit command for pys files also.
  193. Also need to remove these keys on uninstall, but there's no function
  194. like file_created to add registry entries to uninstall log ???
  195. """
  196. import os
  197. lib_dir = distutils.sysconfig.get_python_lib(plat_specific=1)
  198. classes_root=get_root_hkey()
  199. ## Installer executable doesn't seem to pass anything to postinstall script indicating if it's a debug build,
  200. pythonwin_exe = os.path.join(lib_dir, "Pythonwin", "Pythonwin.exe")
  201. pythonwin_edit_command=pythonwin_exe + ' /edit "%1"'
  202. keys_vals = [
  203. ('Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Pythonwin.exe', '', pythonwin_exe),
  204. ('Software\\Classes\\Python.File\\shell\\Edit with Pythonwin', 'command', pythonwin_edit_command),
  205. ('Software\\Classes\\Python.NoConFile\\shell\\Edit with Pythonwin', 'command', pythonwin_edit_command),
  206. ]
  207. try:
  208. if register:
  209. for key, sub_key, val in keys_vals:
  210. ## Since winreg only uses the character Api functions, this can fail if Python
  211. ## is installed to a path containing non-ascii characters
  212. hkey = winreg.CreateKey(classes_root, key)
  213. if sub_key:
  214. hkey = winreg.CreateKey(hkey, sub_key)
  215. winreg.SetValueEx(hkey, None, 0, winreg.REG_SZ, val)
  216. hkey.Close()
  217. else:
  218. for key, sub_key, val in keys_vals:
  219. try:
  220. winreg.DeleteKey(classes_root, key)
  221. except OSError as why:
  222. winerror = getattr(why, 'winerror', why.errno)
  223. if winerror != 2: # file not found
  224. raise
  225. finally:
  226. # tell windows about the change
  227. from win32com.shell import shell, shellcon
  228. shell.SHChangeNotify(shellcon.SHCNE_ASSOCCHANGED, shellcon.SHCNF_IDLIST, None, None)
  229. def get_shortcuts_folder():
  230. if get_root_hkey()==winreg.HKEY_LOCAL_MACHINE:
  231. try:
  232. fldr = get_special_folder_path("CSIDL_COMMON_PROGRAMS")
  233. except OSError:
  234. # No CSIDL_COMMON_PROGRAMS on this platform
  235. fldr = get_special_folder_path("CSIDL_PROGRAMS")
  236. else:
  237. # non-admin install - always goes in this user's start menu.
  238. fldr = get_special_folder_path("CSIDL_PROGRAMS")
  239. try:
  240. install_group = winreg.QueryValue(get_root_hkey(),
  241. root_key_name + "\\InstallPath\\InstallGroup")
  242. except OSError:
  243. vi = sys.version_info
  244. install_group = "Python %d.%d" % (vi[0], vi[1])
  245. return os.path.join(fldr, install_group)
  246. # Get the system directory, which may be the Wow64 directory if we are a 32bit
  247. # python on a 64bit OS.
  248. def get_system_dir():
  249. import win32api # we assume this exists.
  250. try:
  251. import pythoncom
  252. import win32process
  253. from win32com.shell import shell, shellcon
  254. try:
  255. if win32process.IsWow64Process():
  256. return shell.SHGetSpecialFolderPath(0,shellcon.CSIDL_SYSTEMX86)
  257. return shell.SHGetSpecialFolderPath(0,shellcon.CSIDL_SYSTEM)
  258. except (pythoncom.com_error, win32process.error):
  259. return win32api.GetSystemDirectory()
  260. except ImportError:
  261. return win32api.GetSystemDirectory()
  262. def fixup_dbi():
  263. # We used to have a dbi.pyd with our .pyd files, but now have a .py file.
  264. # If the user didn't uninstall, they will find the .pyd which will cause
  265. # problems - so handle that.
  266. import win32api, win32con
  267. pyd_name = os.path.join(os.path.dirname(win32api.__file__), "dbi.pyd")
  268. pyd_d_name = os.path.join(os.path.dirname(win32api.__file__), "dbi_d.pyd")
  269. py_name = os.path.join(os.path.dirname(win32con.__file__), "dbi.py")
  270. for this_pyd in (pyd_name, pyd_d_name):
  271. this_dest = this_pyd + ".old"
  272. if os.path.isfile(this_pyd) and os.path.isfile(py_name):
  273. try:
  274. if os.path.isfile(this_dest):
  275. print(("Old dbi '%s' already exists - deleting '%s'" % (this_dest, this_pyd)))
  276. os.remove(this_pyd)
  277. else:
  278. os.rename(this_pyd, this_dest)
  279. print(("renamed '%s'->'%s.old'" % (this_pyd, this_pyd)))
  280. file_created(this_pyd+".old")
  281. except os.error as exc:
  282. print(("FAILED to rename '%s': %s" % (this_pyd, exc)))
  283. def install(lib_dir):
  284. import traceback
  285. # The .pth file is now installed as a regular file.
  286. # Create the .pth file in the site-packages dir, and use only relative paths
  287. # We used to write a .pth directly to sys.prefix - clobber it.
  288. if os.path.isfile(os.path.join(sys.prefix, "pywin32.pth")):
  289. os.unlink(os.path.join(sys.prefix, "pywin32.pth"))
  290. # The .pth may be new and therefore not loaded in this session.
  291. # Setup the paths just in case.
  292. for name in "win32 win32\\lib Pythonwin".split():
  293. sys.path.append(os.path.join(lib_dir, name))
  294. # It is possible people with old versions installed with still have
  295. # pywintypes and pythoncom registered. We no longer need this, and stale
  296. # entries hurt us.
  297. for name in "pythoncom pywintypes".split():
  298. keyname = "Software\\Python\\PythonCore\\" + sys.winver + "\\Modules\\" + name
  299. for root in winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER:
  300. try:
  301. winreg.DeleteKey(root, keyname + "\\Debug")
  302. except WindowsError:
  303. pass
  304. try:
  305. winreg.DeleteKey(root, keyname)
  306. except WindowsError:
  307. pass
  308. LoadSystemModule(lib_dir, "pywintypes")
  309. LoadSystemModule(lib_dir, "pythoncom")
  310. import win32api
  311. # and now we can get the system directory:
  312. files = glob.glob(os.path.join(lib_dir, "pywin32_system32\\*.*"))
  313. if not files:
  314. raise RuntimeError("No system files to copy!!")
  315. # Try the system32 directory first - if that fails due to "access denied",
  316. # it implies a non-admin user, and we use sys.prefix
  317. for dest_dir in [get_system_dir(), sys.prefix]:
  318. # and copy some files over there
  319. worked = 0
  320. try:
  321. for fname in files:
  322. base = os.path.basename(fname)
  323. dst = os.path.join(dest_dir, base)
  324. CopyTo("installing %s" % base, fname, dst)
  325. if verbose:
  326. print(("Copied %s to %s" % (base, dst)))
  327. # Register the files with the uninstaller
  328. file_created(dst)
  329. worked = 1
  330. # If this isn't sys.prefix (ie, System32), then nuke
  331. # any versions that may exist in sys.prefix - having
  332. # duplicates causes major headaches.
  333. if dest_dir != sys.prefix:
  334. bad_fname = os.path.join(sys.prefix, base)
  335. if os.path.exists(bad_fname):
  336. # let exceptions go here - delete must succeed
  337. os.unlink(bad_fname)
  338. if worked:
  339. break
  340. except win32api.error as details:
  341. if details.winerror==5:
  342. # access denied - user not admin - try sys.prefix dir,
  343. # but first check that a version doesn't already exist
  344. # in that place - otherwise that one will still get used!
  345. if os.path.exists(dst):
  346. msg = "The file '%s' exists, but can not be replaced " \
  347. "due to insufficient permissions. You must " \
  348. "reinstall this software as an Administrator" \
  349. % dst
  350. print(msg)
  351. raise RuntimeError(msg)
  352. continue
  353. raise
  354. else:
  355. raise RuntimeError(
  356. "You don't have enough permissions to install the system files")
  357. # Pythonwin 'compiles' config files - record them for uninstall.
  358. pywin_dir = os.path.join(lib_dir, "Pythonwin", "pywin")
  359. for fname in glob.glob(os.path.join(pywin_dir, "*.cfg")):
  360. file_created(fname[:-1] + "c") # .cfg->.cfc
  361. # Register our demo COM objects.
  362. try:
  363. try:
  364. RegisterCOMObjects()
  365. except win32api.error as details:
  366. if details.winerror!=5: # ERROR_ACCESS_DENIED
  367. raise
  368. print("You do not have the permissions to install COM objects.")
  369. print("The sample COM objects were not registered.")
  370. except:
  371. print("FAILED to register the Python COM objects")
  372. traceback.print_exc()
  373. # There may be no main Python key in HKCU if, eg, an admin installed
  374. # python itself.
  375. winreg.CreateKey(get_root_hkey(), root_key_name)
  376. # Register the .chm help file.
  377. chm_file = os.path.join(lib_dir, "PyWin32.chm")
  378. if os.path.isfile(chm_file):
  379. # This isn't recursive, so if 'Help' doesn't exist, we croak
  380. SetPyKeyVal("Help", None, None)
  381. SetPyKeyVal("Help\\Pythonwin Reference", None, chm_file)
  382. else:
  383. print("NOTE: PyWin32.chm can not be located, so has not " \
  384. "been registered")
  385. # misc other fixups.
  386. fixup_dbi()
  387. # Register Pythonwin in context menu
  388. try:
  389. RegisterPythonwin()
  390. except:
  391. print('Failed to register pythonwin as editor')
  392. traceback.print_exc()
  393. else:
  394. if verbose:
  395. print('Pythonwin has been registered in context menu')
  396. # Create the win32com\gen_py directory.
  397. make_dir = os.path.join(lib_dir, "win32com", "gen_py")
  398. if not os.path.isdir(make_dir):
  399. if verbose:
  400. print(("Creating directory %s" % (make_dir,)))
  401. directory_created(make_dir)
  402. os.mkdir(make_dir)
  403. try:
  404. # create shortcuts
  405. # CSIDL_COMMON_PROGRAMS only available works on NT/2000/XP, and
  406. # will fail there if the user has no admin rights.
  407. fldr = get_shortcuts_folder()
  408. # If the group doesn't exist, then we don't make shortcuts - its
  409. # possible that this isn't a "normal" install.
  410. if os.path.isdir(fldr):
  411. dst = os.path.join(fldr, "PythonWin.lnk")
  412. create_shortcut(os.path.join(lib_dir, "Pythonwin\\Pythonwin.exe"),
  413. "The Pythonwin IDE", dst, "", sys.prefix)
  414. file_created(dst)
  415. if verbose:
  416. print("Shortcut for Pythonwin created")
  417. # And the docs.
  418. dst = os.path.join(fldr, "Python for Windows Documentation.lnk")
  419. doc = "Documentation for the PyWin32 extensions"
  420. create_shortcut(chm_file, doc, dst)
  421. file_created(dst)
  422. if verbose:
  423. print("Shortcut to documentation created")
  424. else:
  425. if verbose:
  426. print(("Can't install shortcuts - %r is not a folder" % (fldr,)))
  427. except Exception as details:
  428. print(details)
  429. # importing win32com.client ensures the gen_py dir created - not strictly
  430. # necessary to do now, but this makes the installation "complete"
  431. try:
  432. import win32com.client
  433. except ImportError:
  434. # Don't let this error sound fatal
  435. pass
  436. print("The pywin32 extensions were successfully installed.")
  437. def uninstall(lib_dir):
  438. # First ensure our system modules are loaded from pywin32_system, so
  439. # we can remove the ones we copied...
  440. LoadSystemModule(lib_dir, "pywintypes")
  441. LoadSystemModule(lib_dir, "pythoncom")
  442. try:
  443. RegisterCOMObjects(False)
  444. except Exception as why:
  445. print(("Failed to unregister COM objects: %s" % (why,)))
  446. try:
  447. RegisterPythonwin(False)
  448. except Exception as why:
  449. print(("Failed to unregister Pythonwin: %s" % (why,)))
  450. else:
  451. if verbose:
  452. print('Unregistered Pythonwin')
  453. try:
  454. # remove gen_py directory.
  455. gen_dir = os.path.join(lib_dir, "win32com", "gen_py")
  456. if os.path.isdir(gen_dir):
  457. shutil.rmtree(gen_dir)
  458. if verbose:
  459. print(("Removed directory %s" % (gen_dir,)))
  460. # Remove pythonwin compiled "config" files.
  461. pywin_dir = os.path.join(lib_dir, "Pythonwin", "pywin")
  462. for fname in glob.glob(os.path.join(pywin_dir, "*.cfc")):
  463. os.remove(fname)
  464. # The dbi.pyd.old files we may have created.
  465. try:
  466. os.remove(os.path.join(lib_dir, "win32", "dbi.pyd.old"))
  467. except os.error:
  468. pass
  469. try:
  470. os.remove(os.path.join(lib_dir, "win32", "dbi_d.pyd.old"))
  471. except os.error:
  472. pass
  473. except Exception as why:
  474. print(("Failed to remove misc files: %s" % (why,)))
  475. try:
  476. fldr = get_shortcuts_folder()
  477. for link in ("PythonWin.lnk", "Python for Windows Documentation.lnk"):
  478. fqlink = os.path.join(fldr, link)
  479. if os.path.isfile(fqlink):
  480. os.remove(fqlink)
  481. if verbose:
  482. print(("Removed %s" % (link,)))
  483. except Exception as why:
  484. print(("Failed to remove shortcuts: %s" % (why,)))
  485. # Now remove the system32 files.
  486. files = glob.glob(os.path.join(lib_dir, "pywin32_system32\\*.*"))
  487. # Try the system32 directory first - if that fails due to "access denied",
  488. # it implies a non-admin user, and we use sys.prefix
  489. try:
  490. for dest_dir in [get_system_dir(), sys.prefix]:
  491. # and copy some files over there
  492. worked = 0
  493. for fname in files:
  494. base = os.path.basename(fname)
  495. dst = os.path.join(dest_dir, base)
  496. if os.path.isfile(dst):
  497. try:
  498. os.remove(dst)
  499. worked = 1
  500. if verbose:
  501. print(("Removed file %s" % (dst)))
  502. except Exception:
  503. print(("FAILED to remove %s" % (dst,)))
  504. if worked:
  505. break
  506. except Exception as why:
  507. print(("FAILED to remove system files: %s" % (why,)))
  508. # NOTE: If this script is run from inside the bdist_wininst created
  509. # binary installer or uninstaller, the command line args are either
  510. # '-install' or '-remove'.
  511. # Important: From inside the binary installer this script MUST NOT
  512. # call sys.exit() or raise SystemExit, otherwise not only this script
  513. # but also the installer will terminate! (Is there a way to prevent
  514. # this from the bdist_wininst C code?)
  515. def verify_destination(location):
  516. if not os.path.isdir(location):
  517. raise argparse.ArgumentTypeError("Path \"{}\" does not exist!".format(location))
  518. return location
  519. if __name__ == '__main__':
  520. import argparse
  521. parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
  522. description="""A post-install script for the pywin32 extensions.
  523. * Typical usage:
  524. > python pywin32_postinstall.py -install
  525. If you installed pywin32 via a .exe installer, this should be run
  526. automatically after installation, but if it fails you can run it again.
  527. If you installed pywin32 via PIP, you almost certainly need to run this to
  528. setup the environment correctly.
  529. Execute with script with a '-install' parameter, to ensure the environment
  530. is setup correctly.
  531. """)
  532. parser.add_argument("-install",
  533. default=False,
  534. action='store_true',
  535. help="Configure the Python environment correctly for pywin32.")
  536. parser.add_argument("-remove",
  537. default=False,
  538. action='store_true',
  539. help="Try and remove everything that was installed or copied.")
  540. parser.add_argument("-wait",
  541. type=int,
  542. help="Wait for the specified process to terminate before starting.")
  543. parser.add_argument("-silent",
  544. default=False,
  545. action='store_true',
  546. help="Don't display the \"Abort/Retry/Ignore\" dialog for files in use.")
  547. parser.add_argument("-quiet",
  548. default=False,
  549. action='store_true',
  550. help="Don't display progress messages.")
  551. parser.add_argument("-destination",
  552. default=distutils.sysconfig.get_python_lib(plat_specific=1),
  553. type=verify_destination,
  554. help="Location of the PyWin32 installation")
  555. args = parser.parse_args()
  556. if not args.quiet:
  557. print(("Parsed arguments are: {}".format(args)))
  558. if not args.install ^ args.remove:
  559. parser.error("You need to either choose to -install or -remove!")
  560. if args.wait is not None:
  561. try:
  562. os.waitpid(args.wait, 0)
  563. except AttributeError:
  564. # Python 2.2 - no waitpid - just sleep.
  565. time.sleep(3)
  566. except os.error:
  567. # child already dead
  568. pass
  569. silent = args.silent
  570. verbose = not args.quiet
  571. if args.install:
  572. install(args.destination)
  573. if args.remove:
  574. if not is_bdist_wininst:
  575. uninstall(args.destination)