home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2570 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  15.4 KB  |  449 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4.  
  5. class error(Exception):
  6.     pass
  7.  
  8. import sys
  9.  
  10. def FileExists(fname):
  11.     import os
  12.     
  13.     try:
  14.         os.stat(fname)
  15.         return 1
  16.     except os.error:
  17.         details = None
  18.         return 0
  19.  
  20.  
  21.  
  22. def IsPackageDir(path, packageName, knownFileName):
  23.     import os
  24.     if knownFileName is None:
  25.         knownFileName = '.'
  26.     
  27.     return FileExists(os.path.join(os.path.join(path, packageName), knownFileName))
  28.  
  29.  
  30. def IsDebug():
  31.     import imp
  32.     for suffix_item in imp.get_suffixes():
  33.         if suffix_item[0] == '_d.pyd':
  34.             return '_d'
  35.     
  36.     return ''
  37.  
  38.  
  39. def FindPackagePath(packageName, knownFileName, searchPaths):
  40.     import regutil
  41.     import os
  42.     pathLook = regutil.GetRegisteredNamedPath(packageName)
  43.     if pathLook and IsPackageDir(pathLook, packageName, knownFileName):
  44.         return (pathLook, None)
  45.     for pathLook in searchPaths:
  46.         if IsPackageDir(pathLook, packageName, knownFileName):
  47.             ret = os.path.abspath(pathLook)
  48.             return (ret, ret)
  49.     
  50.     raise error, 'The package %s can not be located' % packageName
  51.  
  52.  
  53. def FindHelpPath(helpFile, helpDesc, searchPaths):
  54.     import os
  55.     import win32api
  56.     import win32con
  57.     
  58.     try:
  59.         key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'Software\\Microsoft\\Windows\\Help', 0, win32con.KEY_ALL_ACCESS)
  60.         
  61.         try:
  62.             path = win32api.RegQueryValueEx(key, helpDesc)[0]
  63.             if FileExists(os.path.join(path, helpFile)):
  64.                 return os.path.abspath(path)
  65.         except win32api.error:
  66.             pass
  67.         finally:
  68.             key.Close()
  69.  
  70.     except win32api.error:
  71.         pass
  72.  
  73.     for pathLook in searchPaths:
  74.         if FileExists(os.path.join(pathLook, helpFile)):
  75.             return os.path.abspath(pathLook)
  76.         pathLook = os.path.join(pathLook, 'Help')
  77.         if FileExists(os.path.join(pathLook, helpFile)):
  78.             return os.path.abspath(pathLook)
  79.     
  80.     raise error, 'The help file %s can not be located' % helpFile
  81.  
  82.  
  83. def FindAppPath(appName, knownFileName, searchPaths):
  84.     import regutil
  85.     import string
  86.     import os
  87.     regPath = regutil.GetRegisteredNamedPath(appName)
  88.     if regPath:
  89.         pathLook = string.split(regPath, ';')[0]
  90.     
  91.     if regPath and FileExists(os.path.join(pathLook, knownFileName)):
  92.         return None
  93.     for pathLook in searchPaths:
  94.         if FileExists(os.path.join(pathLook, knownFileName)):
  95.             return os.path.abspath(pathLook)
  96.     
  97.     raise error, 'The file %s can not be located for application %s' % (knownFileName, appName)
  98.  
  99.  
  100. def FindPythonExe(exeAlias, possibleRealNames, searchPaths):
  101.     import win32api
  102.     import regutil
  103.     import string
  104.     import os
  105.     import sys
  106.     if possibleRealNames is None:
  107.         possibleRealNames = exeAlias
  108.     
  109.     found = os.path.join(sys.prefix, possibleRealNames)
  110.     if not FileExists(found):
  111.         found = os.path.join(sys.prefix, 'PCBuild', possibleRealNames)
  112.     
  113.     if not FileExists(found):
  114.         found = LocateFileName(possibleRealNames, searchPaths)
  115.     
  116.     registered_ok = 0
  117.     
  118.     try:
  119.         registered = win32api.RegQueryValue(regutil.GetRootKey(), regutil.GetAppPathsKey() + '\\' + exeAlias)
  120.         registered_ok = found == registered
  121.     except win32api.error:
  122.         pass
  123.  
  124.     return (found, registered_ok)
  125.  
  126.  
  127. def QuotedFileName(fname):
  128.     import regutil
  129.     import string
  130.     
  131.     try:
  132.         string.index(fname, ' ')
  133.         return '"%s"' % fname
  134.     except ValueError:
  135.         return fname
  136.  
  137.  
  138.  
  139. def LocateFileName(fileNamesString, searchPaths):
  140.     import regutil
  141.     import string
  142.     import os
  143.     fileNames = string.split(fileNamesString, ';')
  144.     for path in searchPaths:
  145.         for fileName in fileNames:
  146.             
  147.             try:
  148.                 retPath = os.path.join(path, fileName)
  149.                 os.stat(retPath)
  150.             continue
  151.             except os.error:
  152.                 retPath = None
  153.                 continue
  154.             
  155.  
  156.         
  157.         if retPath:
  158.             break
  159.             continue
  160.         None<EXCEPTION MATCH>os.error
  161.     else:
  162.         fileName = fileNames[0]
  163.         
  164.         try:
  165.             import win32ui
  166.             import win32con
  167.         except ImportError:
  168.             raise error, 'Need to locate the file %s, but the win32ui module is not available\nPlease run the program again, passing as a parameter the path to this file.' % fileName
  169.  
  170.         flags = win32con.OFN_FILEMUSTEXIST
  171.         ext = os.path.splitext(fileName)[1]
  172.         filter = 'Files of requested type (*%s)|*%s||' % (ext, ext)
  173.         dlg = win32ui.CreateFileDialog(1, None, fileName, flags, filter, None)
  174.         if dlg.DoModal() != win32con.IDOK:
  175.             raise KeyboardInterrupt, 'User cancelled the process'
  176.         dlg.DoModal() != win32con.IDOK
  177.         retPath = dlg.GetPathName()
  178.     return os.path.abspath(retPath)
  179.  
  180.  
  181. def LocatePath(fileName, searchPaths):
  182.     import os
  183.     return os.path.abspath(os.path.split(LocateFileName(fileName, searchPaths))[0])
  184.  
  185.  
  186. def LocateOptionalPath(fileName, searchPaths):
  187.     
  188.     try:
  189.         return LocatePath(fileName, searchPaths)
  190.     except KeyboardInterrupt:
  191.         return None
  192.  
  193.  
  194.  
  195. def LocateOptionalFileName(fileName, searchPaths = None):
  196.     
  197.     try:
  198.         return LocateFileName(fileName, searchPaths)
  199.     except KeyboardInterrupt:
  200.         return None
  201.  
  202.  
  203.  
  204. def LocatePythonCore(searchPaths):
  205.     import string
  206.     import os
  207.     import regutil
  208.     currentPath = regutil.GetRegisteredNamedPath(None)
  209.     if currentPath:
  210.         presearchPaths = string.split(currentPath, ';')
  211.     else:
  212.         presearchPaths = [
  213.             os.path.abspath('.')]
  214.     libPath = None
  215.     for path in presearchPaths:
  216.         if FileExists(os.path.join(path, 'os.py')):
  217.             libPath = path
  218.             break
  219.             continue
  220.     
  221.     if libPath is None and searchPaths is not None:
  222.         libPath = LocatePath('os.py', searchPaths)
  223.     
  224.     if libPath is None:
  225.         raise error, 'The core Python library could not be located.'
  226.     libPath is None
  227.     corePath = None
  228.     suffix = IsDebug()
  229.     for path in presearchPaths:
  230.         if FileExists(os.path.join(path, 'unicodedata%s.pyd' % suffix)):
  231.             corePath = path
  232.             break
  233.             continue
  234.     
  235.     if corePath is None and searchPaths is not None:
  236.         corePath = LocatePath('unicodedata%s.pyd' % suffix, searchPaths)
  237.     
  238.     if corePath is None:
  239.         raise error, 'The core Python path could not be located.'
  240.     corePath is None
  241.     installPath = os.path.abspath(os.path.join(libPath, '..'))
  242.     return (installPath, [
  243.         libPath,
  244.         corePath])
  245.  
  246.  
  247. def FindRegisterPackage(packageName, knownFile, searchPaths, registryAppName = None):
  248.     import regutil
  249.     import string
  250.     if not packageName:
  251.         raise error, 'A package name must be supplied'
  252.     packageName
  253.     corePaths = string.split(regutil.GetRegisteredNamedPath(None), ';')
  254.     if not searchPaths:
  255.         searchPaths = corePaths
  256.     
  257.     if not registryAppName:
  258.         pass
  259.     registryAppName = packageName
  260.     
  261.     try:
  262.         (pathLook, pathAdd) = FindPackagePath(packageName, knownFile, searchPaths)
  263.         if pathAdd is not None:
  264.             if pathAdd in corePaths:
  265.                 pathAdd = ''
  266.             
  267.             regutil.RegisterNamedPath(registryAppName, pathAdd)
  268.         
  269.         return pathLook
  270.     except error:
  271.         details = None
  272.         print '*** The %s package could not be registered - %s' % (packageName, details)
  273.         print '*** Please ensure you have passed the correct paths on the command line.'
  274.         print '*** - For packages, you should pass a path to the packages parent directory,'
  275.         print '*** - and not the package directory itself...'
  276.  
  277.  
  278.  
  279. def FindRegisterApp(appName, knownFiles, searchPaths):
  280.     import regutil
  281.     import string
  282.     if type(knownFiles) == type(''):
  283.         knownFiles = [
  284.             knownFiles]
  285.     
  286.     paths = []
  287.     
  288.     try:
  289.         for knownFile in knownFiles:
  290.             pathLook = FindAppPath(appName, knownFile, searchPaths)
  291.             if pathLook:
  292.                 paths.append(pathLook)
  293.                 continue
  294.     except error:
  295.         details = None
  296.         print '*** ', details
  297.         return None
  298.  
  299.     regutil.RegisterNamedPath(appName, string.join(paths, ';'))
  300.  
  301.  
  302. def FindRegisterPythonExe(exeAlias, searchPaths, actualFileNames = None):
  303.     import regutil
  304.     import string
  305.     (fname, ok) = FindPythonExe(exeAlias, actualFileNames, searchPaths)
  306.     if not ok:
  307.         regutil.RegisterPythonExe(fname, exeAlias)
  308.     
  309.     return fname
  310.  
  311.  
  312. def FindRegisterHelpFile(helpFile, searchPaths, helpDesc = None):
  313.     import regutil
  314.     
  315.     try:
  316.         pathLook = FindHelpPath(helpFile, helpDesc, searchPaths)
  317.     except error:
  318.         details = None
  319.         print '*** ', details
  320.         return None
  321.  
  322.     regutil.RegisterHelpFile(helpFile, pathLook, helpDesc)
  323.  
  324.  
  325. def SetupCore(searchPaths):
  326.     import sys
  327.     for path in searchPaths:
  328.         sys.path.append(path)
  329.     
  330.     import string
  331.     import os
  332.     import regutil
  333.     import win32api
  334.     import win32con
  335.     (installPath, corePaths) = LocatePythonCore(searchPaths)
  336.     print corePaths
  337.     regutil.RegisterNamedPath(None, string.join(corePaths, ';'))
  338.     hKey = win32api.RegCreateKey(regutil.GetRootKey(), regutil.BuildDefaultPythonKey())
  339.     
  340.     try:
  341.         win32api.RegSetValue(hKey, 'InstallPath', win32con.REG_SZ, installPath)
  342.     finally:
  343.         win32api.RegCloseKey(hKey)
  344.  
  345.     win32paths = os.path.abspath(os.path.split(win32api.__file__)[0]) + ';' + os.path.abspath(os.path.split(LocateFileName('win32con.py;win32con.pyc', sys.path))[0])
  346.     check = os.path.join(sys.prefix, 'PCBuild')
  347.     if os.path.isdir(check):
  348.         regutil.RegisterNamedPath('PCBuild', check)
  349.     
  350.  
  351.  
  352. def RegisterShellInfo(searchPaths):
  353.     import regutil
  354.     import win32con
  355.     suffix = IsDebug()
  356.     exePath = FindRegisterPythonExe('Python%s.exe' % suffix, searchPaths)
  357.     regutil.SetRegistryDefaultValue('.py', 'Python.File', win32con.HKEY_CLASSES_ROOT)
  358.     regutil.RegisterShellCommand('Open', QuotedFileName(exePath) + ' "%1" %*', '&Run')
  359.     regutil.SetRegistryDefaultValue('Python.File\\DefaultIcon', '%s,0' % exePath, win32con.HKEY_CLASSES_ROOT)
  360.     FindRegisterHelpFile('Python.hlp', searchPaths, 'Main Python Documentation')
  361.     FindRegisterHelpFile('ActivePython.chm', searchPaths, 'Main Python Documentation')
  362.  
  363. usage = 'regsetup.py - Setup/maintain the registry for Python apps.\n\nRun without options, (but possibly search paths) to repair a totally broken\npython registry setup.  This should allow other options to work.\n\nUsage:   %s [options ...] paths ...\n-p packageName  -- Find and register a package.  Looks in the paths for\n                   a sub-directory with the name of the package, and\n                   adds a path entry for the package.\n-a appName      -- Unconditionally add an application name to the path.\n                   A new path entry is create with the app name, and the\n                   paths specified are added to the registry.\n-c              -- Add the specified paths to the core Pythonpath.\n                   If a path appears on the core path, and a package also\n                   needs that same path, the package will not bother\n                   registering it.  Therefore, By adding paths to the\n                   core path, you can avoid packages re-registering the same path.\n-m filename     -- Find and register the specific file name as a module.\n                   Do not include a path on the filename!\n--shell         -- Register everything with the Win95/NT shell.\n--upackage name -- Unregister the package\n--uapp name     -- Unregister the app (identical to --upackage)\n--umodule name  -- Unregister the module\n\n--description   -- Print a description of the usage.\n--examples      -- Print examples of usage.\n' % sys.argv[0]
  364. description = 'If no options are processed, the program attempts to validate and set\nthe standard Python path to the point where the standard library is\navailable.  This can be handy if you move Python to a new drive/sub-directory,\nin which case most of the options would fail (as they need at least string.py,\nos.py etc to function.)\nRunning without options should repair Python well enough to run with\nthe other options.\n\npaths are search paths that the program will use to seek out a file.\nFor example, when registering the core Python, you may wish to\nprovide paths to non-standard places to look for the Python help files,\nlibrary files, etc.\n\nSee also the "regcheck.py" utility which will check and dump the contents\nof the registry.\n'
  365. examples = 'Examples:\n"regsetup c:\\wierd\\spot\\1 c:\\wierd\\spot\\2"\nAttempts to setup the core Python.  Looks in some standard places,\nas well as the 2 wierd spots to locate the core Python files (eg, Python.exe,\npython14.dll, the standard library and Win32 Extensions.\n\n"regsetup -a myappname . .\\subdir"\nRegisters a new Pythonpath entry named myappname, with "C:\\I\\AM\\HERE" and\n"C:\\I\\AM\\HERE\\subdir" added to the path (ie, all args are converted to\nabsolute paths)\n\n"regsetup -c c:\\my\\python\\files"\nUnconditionally add "c:\\my\\python\\files" to the \'core\' Python path.\n\n"regsetup -m some.pyd \\windows\\system"\nRegister the module some.pyd in \\windows\\system as a registered\nmodule.  This will allow some.pyd to be imported, even though the\nwindows system directory is not (usually!) on the Python Path.\n\n"regsetup --umodule some"\nUnregister the module "some".  This means normal import rules then apply\nfor that module.\n'
  366. if __name__ == '__main__':
  367.     if len(sys.argv) > 1 and sys.argv[1] in ('/?', '-?', '-help', '-h'):
  368.         print usage
  369.     elif len(sys.argv) == 1 or sys.argv[1][0] not in ('/', '-'):
  370.         searchPath = sys.path[:]
  371.         for arg in sys.argv[1:]:
  372.             searchPath.append(arg)
  373.         
  374.         searchPath.append('..\\Build')
  375.         searchPath.append('..\\Lib')
  376.         searchPath.append('..')
  377.         searchPath.append('..\\..')
  378.         searchPath.append('..\\..\\lib')
  379.         searchPath.append('..\\build')
  380.         searchPath.append('..\\..\\pcbuild')
  381.         print 'Attempting to setup/repair the Python core'
  382.         SetupCore(searchPath)
  383.         RegisterShellInfo(searchPath)
  384.         FindRegisterHelpFile('PyWin32.chm', searchPath, 'Pythonwin Reference')
  385.         print 'Registration complete - checking the registry...'
  386.         import regcheck
  387.         regcheck.CheckRegistry()
  388.     else:
  389.         searchPaths = []
  390.         import getopt
  391.         import string
  392.         (opts, args) = getopt.getopt(sys.argv[1:], 'p:a:m:c', [
  393.             'shell',
  394.             'upackage=',
  395.             'uapp=',
  396.             'umodule=',
  397.             'description',
  398.             'examples'])
  399.         for arg in args:
  400.             searchPaths.append(arg)
  401.         
  402.         for o, a in opts:
  403.             if o == '--description':
  404.                 print description
  405.             
  406.             if o == '--examples':
  407.                 print examples
  408.             
  409.             if o == '--shell':
  410.                 print 'Registering the Python core.'
  411.                 RegisterShellInfo(searchPaths)
  412.             
  413.             if o == '-p':
  414.                 print 'Registering package', a
  415.                 FindRegisterPackage(a, None, searchPaths)
  416.             
  417.             if o in ('--upackage', '--uapp'):
  418.                 import regutil
  419.                 print 'Unregistering application/package', a
  420.                 regutil.UnregisterNamedPath(a)
  421.             
  422.             if o == '-a':
  423.                 import regutil
  424.                 path = string.join(searchPaths, ';')
  425.                 print 'Registering application', a, 'to path', path
  426.                 regutil.RegisterNamedPath(a, path)
  427.             
  428.             if o == '-c':
  429.                 if not len(searchPaths):
  430.                     raise error, '-c option must provide at least one additional path'
  431.                 len(searchPaths)
  432.                 import win32api
  433.                 import regutil
  434.                 currentPaths = string.split(regutil.GetRegisteredNamedPath(None), ';')
  435.                 oldLen = len(currentPaths)
  436.                 for newPath in searchPaths:
  437.                     if newPath not in currentPaths:
  438.                         currentPaths.append(newPath)
  439.                         continue
  440.                 
  441.                 if len(currentPaths) != oldLen:
  442.                     print 'Registering %d new core paths' % (len(currentPaths) - oldLen)
  443.                     regutil.RegisterNamedPath(None, string.join(currentPaths, ';'))
  444.                 else:
  445.                     print 'All specified paths are already registered.'
  446.             len(currentPaths) != oldLen
  447.         
  448.  
  449.