share.py :  » Network » Chestnut-Dialer » chestnut-dialer-0.3.3 » chestnut_dialer » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Chestnut Dialer 
Chestnut Dialer » chestnut dialer 0.3.3 » chestnut_dialer » share.py
import socket
import select
import os
import glob
import time
import chestnut_dialer
import chestnut_dialer.config
import chestnut_dialer.dobj as dobj
from chestnut_dialer import _
from chestnut_dialer import debug_msg

def wait_session_server(child = None):
  filename = chestnut_dialer.session_wait_base + str(os.getpid())
  try:
    os.mkfifo(filename)
    f = open(filename, 'r')
  except OSError: return False
  result = False
  for i in range(0, 12):
    (r, w, x) = select.select([f], [], [f], 5)
    if r:
      result = True
      break
    if x: break
    if child != None:
      try: os.waitpid(child, os.WNOHANG)
      except OSError: break
    elif not filter(lambda x: x != filename,
        glob.glob(chestnut_dialer.session_wait_base + '*')): break
  f.close()
  os.remove(filename)
  return result

def is_session_server_running():
  if not os.access(chestnut_dialer.session_socket, os.F_OK):
    if glob.glob(chestnut_dialer.session_wait_base + '*'):
      return wait_session_server()
    else: return False
  try: pid = int(open(chestnut_dialer.session_pid_file, 'r').read())
  except: pass
  else:
    try: os.kill(pid, 0)
    except OSError: pass
    else: return True
  try: os.remove(chestnut_dialer.session_socket)
  except OSError:
    if os.access(chestnut_dialer.session_socket, os.F_OK):
      debug_msg(_("cannot remove session socket: %s") %
          chestnut_dialer.session_socket, 0)
    else: time.sleep(1)
  return False

def start_session_server(args = []):
  p = os.fork()
  if p < 0:
    debug_msg(_("fork() failed"), 0)
    return False
  if p == 0:
    os.setsid()
    try: path = os.environ['PYTHONPATH'] + ":"
    except KeyError: path = ""
    os.execve(chestnut_dialer.config.python,
        [chestnut_dialer.session_server,
        chestnut_dialer.session_server] + args,
        {'PYTHONPATH': path + os.path.dirname(os.path.dirname(__file__))})
    debug_msg(_("execve() failed"), 0)
    sys.exit(chestnut_dialer.EX_SOFTWARE)  
  return wait_session_server(p)

class Share:
  def __init__(self):
    if not is_session_server_running():
      if not start_session_server():
        debug_msg(_("unable to start session server"), 0)
        return None
    self._dobj_connection = dobj.DObjConnection(
        dobj.AF_UNIX, chestnut_dialer.session_socket)
    self._share = self._dobj_connection.get_object()
  def __getattr__(self, name):
    try: return getattr(self._share, name)
    except AttributeError:
      return getattr(self._dobj_connection, name)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.