home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1957 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  10.2 KB  |  239 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __docformat__ = 'restructuredtext en'
  5. import sys
  6. sys.path.insert(0, '')
  7. from optparse import OptionParser
  8. import os
  9. import time
  10. import tempfile
  11. from twisted.application import internet, service
  12. from twisted.internet import reactor, error, defer
  13. from twisted.python import log
  14. from IPython.kernel.fcutil import Tub, UnauthenticatedTub, have_crypto
  15. from IPython.kernel.error import SecurityError
  16. from IPython.kernel import controllerservice
  17. from IPython.kernel.fcutil import check_furl_file_security
  18. from IPython.iplib import user_setup
  19. from IPython.genutils import get_ipython_dir, get_log_dir, get_security_dir
  20. if os.name == 'posix':
  21.     rc_suffix = ''
  22. else:
  23.     rc_suffix = '.ini'
  24. user_setup(get_ipython_dir(), rc_suffix, mode = 'install', interactive = False)
  25. get_log_dir()
  26. get_security_dir()
  27. from IPython.kernel.config import config_manager as kernel_config_manager
  28. from IPython.config.cutils import import_item
  29.  
  30. def get_temp_furlfile(filename):
  31.     return tempfile.mktemp(dir = os.path.dirname(filename), prefix = os.path.basename(filename))
  32.  
  33.  
  34. def make_tub(ip, port, secure, cert_file):
  35.     if secure:
  36.         if have_crypto:
  37.             tub = Tub(certFile = cert_file)
  38.         else:
  39.             raise SecurityError("\nOpenSSL/pyOpenSSL is not available, so we can't run in secure mode.\nTry running without security using 'ipcontroller -xy'.\n")
  40.     have_crypto
  41.     tub = UnauthenticatedTub()
  42.     if ip == '':
  43.         strport = 'tcp:%i' % port
  44.     else:
  45.         strport = 'tcp:%i:interface=%s' % (port, ip)
  46.     listener = tub.listenOn(strport)
  47.     return (tub, listener)
  48.  
  49.  
  50. def make_client_service(controller_service, config):
  51.     ip = config['controller']['client_tub']['ip']
  52.     port = config['controller']['client_tub'].as_int('port')
  53.     location = config['controller']['client_tub']['location']
  54.     secure = config['controller']['client_tub']['secure']
  55.     cert_file = config['controller']['client_tub']['cert_file']
  56.     (client_tub, client_listener) = make_tub(ip, port, secure, cert_file)
  57.     if ip == 'localhost' or ip == '127.0.0.1':
  58.         location = '127.0.0.1'
  59.     
  60.     if not secure:
  61.         log.msg('WARNING: you are running the controller with no client security')
  62.     
  63.     
  64.     def set_location_and_register():
  65.         
  66.         def register(empty, ref, furl_file):
  67.             temp_furl_file = get_temp_furlfile(furl_file)
  68.             client_tub.registerReference(ref, furlFile = temp_furl_file)
  69.             os.rename(temp_furl_file, furl_file)
  70.  
  71.         if location == '':
  72.             d = client_tub.setLocationAutomatically()
  73.         else:
  74.             d = defer.maybeDeferred(client_tub.setLocation, '%s:%i' % (location, client_listener.getPortnum()))
  75.         for ciname, ci in config['controller']['controller_interfaces'].iteritems():
  76.             log.msg('Adapting Controller to interface: %s' % ciname)
  77.             furl_file = ci['furl_file']
  78.             log.msg('Saving furl for interface [%s] to file: %s' % (ciname, furl_file))
  79.             check_furl_file_security(furl_file, secure)
  80.             adapted_controller = import_item(ci['controller_interface'])(controller_service)
  81.             d.addCallback(register, import_item(ci['fc_interface'])(adapted_controller), furl_file = ci['furl_file'])
  82.         
  83.  
  84.     reactor.callWhenRunning(set_location_and_register)
  85.     return client_tub
  86.  
  87.  
  88. def make_engine_service(controller_service, config):
  89.     ip = config['controller']['engine_tub']['ip']
  90.     port = config['controller']['engine_tub'].as_int('port')
  91.     location = config['controller']['engine_tub']['location']
  92.     secure = config['controller']['engine_tub']['secure']
  93.     cert_file = config['controller']['engine_tub']['cert_file']
  94.     (engine_tub, engine_listener) = make_tub(ip, port, secure, cert_file)
  95.     if ip == 'localhost' or ip == '127.0.0.1':
  96.         location = '127.0.0.1'
  97.     
  98.     if not secure:
  99.         log.msg('WARNING: you are running the controller with no engine security')
  100.     
  101.     
  102.     def set_location_and_register():
  103.         
  104.         def register(empty, ref, furl_file):
  105.             temp_furl_file = get_temp_furlfile(furl_file)
  106.             engine_tub.registerReference(ref, furlFile = temp_furl_file)
  107.             os.rename(temp_furl_file, furl_file)
  108.  
  109.         if location == '':
  110.             d = engine_tub.setLocationAutomatically()
  111.         else:
  112.             d = defer.maybeDeferred(engine_tub.setLocation, '%s:%i' % (location, engine_listener.getPortnum()))
  113.         furl_file = config['controller']['engine_furl_file']
  114.         engine_fc_interface = import_item(config['controller']['engine_fc_interface'])
  115.         log.msg('Saving furl for the engine to file: %s' % furl_file)
  116.         check_furl_file_security(furl_file, secure)
  117.         fc_controller = engine_fc_interface(controller_service)
  118.         d.addCallback(register, fc_controller, furl_file = furl_file)
  119.  
  120.     reactor.callWhenRunning(set_location_and_register)
  121.     return engine_tub
  122.  
  123.  
  124. def start_controller():
  125.     config = kernel_config_manager.get_config_obj()
  126.     logfile = config['controller']['logfile']
  127.     if logfile:
  128.         logfile = logfile + str(os.getpid()) + '.log'
  129.         
  130.         try:
  131.             openLogFile = open(logfile, 'w')
  132.         openLogFile = sys.stdout
  133.  
  134.     else:
  135.         openLogFile = sys.stdout
  136.     log.startLogging(openLogFile)
  137.     cis = config['controller']['import_statement']
  138.     if cis:
  139.         
  140.         try:
  141.             exec cis in globals(), locals()
  142.         log.msg('Error running import_statement: %s' % cis)
  143.  
  144.     
  145.     reuse = config['controller']['reuse_furls']
  146.     if not reuse:
  147.         paths = (config['controller']['engine_furl_file'], config['controller']['controller_interfaces']['task']['furl_file'], config['controller']['controller_interfaces']['multiengine']['furl_file'])
  148.         for p in paths:
  149.             if os.path.isfile(p):
  150.                 os.remove(p)
  151.                 continue
  152.         
  153.     
  154.     main_service = service.MultiService()
  155.     controller_service = controllerservice.ControllerService()
  156.     controller_service.setServiceParent(main_service)
  157.     client_service = make_client_service(controller_service, config)
  158.     client_service.setServiceParent(main_service)
  159.     engine_service = make_engine_service(controller_service, config)
  160.     engine_service.setServiceParent(main_service)
  161.     main_service.startService()
  162.     reactor.run()
  163.  
  164.  
  165. def init_config():
  166.     parser = OptionParser('ipcontroller [options]\n\nStart an IPython controller.\n\nUse the IPYTHONDIR environment variable to change your IPython directory \nfrom the default of .ipython or _ipython.  The log and security \nsubdirectories of your IPython directory will be used by this script \nfor log files and security files.')
  167.     parser.add_option('--client-ip', type = 'string', dest = 'client_ip', help = 'the IP address or hostname the controller will listen on for client connections')
  168.     parser.add_option('--client-port', type = 'int', dest = 'client_port', help = 'the port the controller will listen on for client connections')
  169.     parser.add_option('--client-location', type = 'string', dest = 'client_location', help = 'hostname or ip for clients to connect to')
  170.     parser.add_option('-x', action = 'store_false', dest = 'client_secure', help = 'turn off all client security')
  171.     parser.add_option('--client-cert-file', type = 'string', dest = 'client_cert_file', help = 'file to store the client SSL certificate')
  172.     parser.add_option('--task-furl-file', type = 'string', dest = 'task_furl_file', help = 'file to store the FURL for task clients to connect with')
  173.     parser.add_option('--multiengine-furl-file', type = 'string', dest = 'multiengine_furl_file', help = 'file to store the FURL for multiengine clients to connect with')
  174.     parser.add_option('--engine-ip', type = 'string', dest = 'engine_ip', help = 'the IP address or hostname the controller will listen on for engine connections')
  175.     parser.add_option('--engine-port', type = 'int', dest = 'engine_port', help = 'the port the controller will listen on for engine connections')
  176.     parser.add_option('--engine-location', type = 'string', dest = 'engine_location', help = 'hostname or ip for engines to connect to')
  177.     parser.add_option('-y', action = 'store_false', dest = 'engine_secure', help = 'turn off all engine security')
  178.     parser.add_option('--engine-cert-file', type = 'string', dest = 'engine_cert_file', help = 'file to store the engine SSL certificate')
  179.     parser.add_option('--engine-furl-file', type = 'string', dest = 'engine_furl_file', help = 'file to store the FURL for engines to connect with')
  180.     parser.add_option('-l', '--logfile', type = 'string', dest = 'logfile', help = 'log file name (default is stdout)')
  181.     parser.add_option('-r', action = 'store_true', dest = 'reuse_furls', help = 'try to reuse all furl files')
  182.     (options, args) = parser.parse_args()
  183.     config = kernel_config_manager.get_config_obj()
  184.     if options.client_ip is not None:
  185.         config['controller']['client_tub']['ip'] = options.client_ip
  186.     
  187.     if options.client_port is not None:
  188.         config['controller']['client_tub']['port'] = options.client_port
  189.     
  190.     if options.client_location is not None:
  191.         config['controller']['client_tub']['location'] = options.client_location
  192.     
  193.     if options.client_secure is not None:
  194.         config['controller']['client_tub']['secure'] = options.client_secure
  195.     
  196.     if options.client_cert_file is not None:
  197.         config['controller']['client_tub']['cert_file'] = options.client_cert_file
  198.     
  199.     if options.task_furl_file is not None:
  200.         config['controller']['controller_interfaces']['task']['furl_file'] = options.task_furl_file
  201.     
  202.     if options.multiengine_furl_file is not None:
  203.         config['controller']['controller_interfaces']['multiengine']['furl_file'] = options.multiengine_furl_file
  204.     
  205.     if options.engine_ip is not None:
  206.         config['controller']['engine_tub']['ip'] = options.engine_ip
  207.     
  208.     if options.engine_port is not None:
  209.         config['controller']['engine_tub']['port'] = options.engine_port
  210.     
  211.     if options.engine_location is not None:
  212.         config['controller']['engine_tub']['location'] = options.engine_location
  213.     
  214.     if options.engine_secure is not None:
  215.         config['controller']['engine_tub']['secure'] = options.engine_secure
  216.     
  217.     if options.engine_cert_file is not None:
  218.         config['controller']['engine_tub']['cert_file'] = options.engine_cert_file
  219.     
  220.     if options.engine_furl_file is not None:
  221.         config['controller']['engine_furl_file'] = options.engine_furl_file
  222.     
  223.     if options.reuse_furls is not None:
  224.         config['controller']['reuse_furls'] = options.reuse_furls
  225.     
  226.     if options.logfile is not None:
  227.         config['controller']['logfile'] = options.logfile
  228.     
  229.     kernel_config_manager.update_config_obj(config)
  230.  
  231.  
  232. def main():
  233.     init_config()
  234.     start_controller()
  235.  
  236. if __name__ == '__main__':
  237.     main()
  238.  
  239.