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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import sys
  9. from threading import Thread
  10. from calibre.library.server import server_config as config
  11. from calibre.library.server.base import LibraryServer
  12. from calibre.constants import iswindows
  13. import cherrypy
  14.  
  15. def start_threaded_server(db, opts):
  16.     server = LibraryServer(db, opts, embedded = True)
  17.     server.thread = Thread(target = server.start)
  18.     server.thread.setDaemon(True)
  19.     server.thread.start()
  20.     return server
  21.  
  22.  
  23. def stop_threaded_server(server):
  24.     server.exit()
  25.     server.thread = None
  26.  
  27.  
  28. def option_parser():
  29.     parser = config().option_parser('%prog ' + _('[options]\n\nStart the calibre content server.'))
  30.     parser.add_option('--with-library', default = None, help = _('Path to the library folder to serve with the content server'))
  31.     parser.add_option('--pidfile', default = None, help = _('Write process PID to the specified file'))
  32.     parser.add_option('--daemonize', default = False, action = 'store_true', help = 'Run process in background as a daemon. No effect on windows.')
  33.     return parser
  34.  
  35.  
  36. def daemonize(stdin = '/dev/null', stdout = '/dev/null', stderr = '/dev/null'):
  37.     
  38.     try:
  39.         pid = os.fork()
  40.         if pid > 0:
  41.             sys.exit(0)
  42.     except OSError:
  43.         e = None
  44.         print >>sys.stderr, 'fork #1 failed: %d (%s)' % (e.errno, e.strerror)
  45.         sys.exit(1)
  46.  
  47.     os.chdir('/')
  48.     os.setsid()
  49.     os.umask(0)
  50.     
  51.     try:
  52.         pid = os.fork()
  53.         if pid > 0:
  54.             sys.exit(0)
  55.     except OSError:
  56.         e = None
  57.         print >>sys.stderr, 'fork #2 failed: %d (%s)' % (e.errno, e.strerror)
  58.         sys.exit(1)
  59.  
  60.     si = file(stdin, 'r')
  61.     so = file(stdout, 'a+')
  62.     se = file(stderr, 'a+', 0)
  63.     os.dup2(si.fileno(), sys.stdin.fileno())
  64.     os.dup2(so.fileno(), sys.stdout.fileno())
  65.     os.dup2(se.fileno(), sys.stderr.fileno())
  66.  
  67.  
  68. def main(args = sys.argv):
  69.     LibraryDatabase2 = LibraryDatabase2
  70.     import calibre.library.database2
  71.     parser = option_parser()
  72.     (opts, args) = parser.parse_args(args)
  73.     if opts.daemonize and not iswindows:
  74.         daemonize()
  75.     
  76.     cherrypy.log.screen = True
  77.     prefs = prefs
  78.     import calibre.utils.config
  79.     if opts.with_library is None:
  80.         opts.with_library = prefs['library_path']
  81.     
  82.     db = LibraryDatabase2(opts.with_library)
  83.     server = LibraryServer(db, opts)
  84.     server.start()
  85.     return 0
  86.  
  87. if __name__ == '__main__':
  88.     sys.exit(main())
  89.  
  90.