Example #1
0
    def __init__(self, name, password='', password2=None, key=None):
        """
        Constructor.

        The authorization password is only required on hosts that
        separate the authentication from the authorization procedure.
        If an authorization password is not given, it defaults to the
        same value as the authentication password.

        :type  name: string
        :param name: A username.
        :type  password: string
        :param password: The authentication password.
        :type  password2: string
        :param password2: The authorization password, if required.
        :type  key: PrivateKey
        :param key: A private key, if required.
        """
        self.acquired_event = Event()
        self.released_event = Event()
        self.changed_event = Event()
        self.name = name
        self.password = password
        self.authorization_password = password2
        self.key = key
        self.synclock = multiprocessing.Condition(multiprocessing.Lock())
        self.lock = multiprocessing.Lock()
Example #2
0
 def __init__(self, workqueue):
     self.done_event = Event()
     self.workqueue = workqueue
     self.job_ids = set()
     self.completed = 0
     self.workqueue.job_succeeded_event.listen(self._on_job_done)
     self.workqueue.job_aborted_event.listen(self._on_job_done)
Example #3
0
    def __init__(self, name, password = '', password2 = None, key = None):
        """
        Constructor.

        The authorization password is only required on hosts that
        separate the authentication from the authorization procedure.
        If an authorization password is not given, it defaults to the
        same value as the authentication password.

        @type  name: string
        @param name: A username.
        @type  password: string
        @param password: The authentication password.
        @type  password2: string
        @param password2: The authorization password, if required.
        @type  key: PrivateKey
        @param key: A private key, if required.
        """
        self.acquired_event         = Event()
        self.released_event         = Event()
        self.changed_event          = Event()
        self.name                   = name
        self.password               = password
        self.authorization_password = password2
        self.key                    = key
        self.synclock               = threading.Condition(threading.Lock())
        self.lock                   = threading.Lock()
Example #4
0
    def __init__(self,
                 collection=None,
                 debug=0,
                 max_threads=1,
                 mode='threading'):
        """
        Constructor.

        @type  debug: int
        @param debug: The debug level.
        @type  max_threads: int
        @param max_threads: The maximum number of concurrent threads.
        """
        if mode == 'threading':
            self.job_cls = Thread
        elif mode == 'multiprocessing':
            self.job_cls = Process
        else:
            raise TypeError('invalid "mode" argument: ' + repr(mode))
        if collection is None:
            self.collection = Pipeline(max_threads)
        else:
            self.collection = collection
            collection.set_max_working(max_threads)
        self.job_init_event = Event()
        self.job_started_event = Event()
        self.job_error_event = Event()
        self.job_succeeded_event = Event()
        self.job_aborted_event = Event()
        self.queue_empty_event = Event()
        self.debug = debug
        self.main_loop = None
        self._init()
Example #5
0
 def __init__(self, order_id, name):
     DBObject.__init__(self)
     self.id = None
     self.order_id = order_id
     self.job_id = None  # reference to Exscript.workqueue.Job.id
     self.name = name
     self.status = 'new'
     self.progress = .0
     self.started = datetime.utcnow()
     self.closed = None
     self.logfile = None
     self.tracefile = None
     self.vars = {}
     self.changed_event = Event()
Example #6
0
 def __init__(self,
              parent,
              name,
              logger,
              address = '',
              port    = 80):
     self.parent               = parent
     self.name                 = name
     self.logger               = logger
     self.order_incoming_event = Event()
     self.address              = address
     self.port                 = port
     addr                      = self.address, self.port
     self.server               = HTTPd(addr, HTTPHandler, self)
     self.parent.daemon_added(self)
Example #7
0
 def __init__(self, collection, job_cls):
     threading.Thread.__init__(self)
     self.job_init_event = Event()
     self.job_started_event = Event()
     self.job_error_event = Event()
     self.job_succeeded_event = Event()
     self.job_aborted_event = Event()
     self.queue_empty_event = Event()
     self.collection = collection
     self.job_cls = job_cls
     self.debug = 5
     self.daemon = True
Example #8
0
    def __init__(self,
                 sender=None,
                 to='',
                 cc='',
                 bcc='',
                 subject='',
                 body=''):
        """
        Creates a new email with the given values.
        If the given sender is None, one will be automatically chosen
        using getpass.getuser().

        :type  sender: string
        :param sender: The email address of the sender.
        :type  to: string|list(string)
        :param to: A list of email addresses, passed to set_to().
        :type  cc: string|list(string)
        :param cc: A list of email addresses, passed to set_cc().
        :type  bcc: string|list(string)
        :param bcc: A list of email addresses, passed to set_bcc().
        :type  subject: string
        :param subject: A subject line, passed to set_subject().
        :type  body: string
        :param body: The email body, passed to set_body().
        """
        self.changed_event = Event()
        self.files = []
        self.sender = None
        self.cc = None
        self.bcc = None
        self.to = None
        self.subject = None
        self.body = None
        if not sender:
            domain = socket.getfqdn('localhost')
            sender = getuser() + '@' + domain
        self.set_sender(sender)
        self.set_to(to)
        self.set_cc(cc)
        self.set_bcc(bcc)
        self.set_subject(subject)
        self.set_body(body)
Example #9
0
    def __init__(self,
                 driver=None,
                 stdout=None,
                 stderr=None,
                 debug=0,
                 connect_timeout=30,
                 timeout=30,
                 logfile=None,
                 termtype='dumb',
                 verify_fingerprint=True,
                 account_factory=None):
        """
        Constructor.
        The following events are provided:

          - data_received_event: A packet was received from the connected host.
          - otp_requested_event: The connected host requested a
          one-time-password to be entered.

        :keyword driver: Driver()|str
        :keyword stdout: Where to write the device response. Defaults to
            os.devnull.
        :keyword stderr: Where to write debug info. Defaults to stderr.
        :keyword debug: An integer between 0 (no debugging) and 5 (very
            verbose debugging) that specifies the amount of debug info
            sent to the terminal. The default value is 0.
        :keyword connect_timeout: Timeout for the initial TCP connection attempt
        :keyword timeout: See set_timeout(). The default value is 30.
        :keyword logfile: A file into which a log of the conversation with the
            device is dumped.
        :keyword termtype: The terminal type to request from the remote host,
            e.g. 'vt100'.
        :keyword verify_fingerprint: Whether to verify the host's fingerprint.
        :keyword account_factory: A function that produces a new :class:`Account`.
        """
        self.data_received_event = Event()
        self.otp_requested_event = Event()
        self.os_guesser = OsGuesser()
        self.auto_driver = driver_map[self.guess_os()]
        self.proto_authenticated = False
        self.app_authenticated = False
        self.app_authorized = False
        self.manual_user_re = None
        self.manual_password_re = None
        self.manual_prompt_re = None
        self.manual_error_re = None
        self.manual_login_error_re = None
        self.driver_replaced = False
        self.host = None
        self.port = None
        self.last_account = None
        self.termtype = termtype
        self.verify_fingerprint = verify_fingerprint
        self.manual_driver = None
        self.debug = debug
        self.connect_timeout = connect_timeout
        self.timeout = timeout
        self.logfile = logfile
        self.response = None
        self.buffer = MonitoredBuffer()
        self.account_factory = account_factory
        self.send_data = None
        if stdout is None:
            self.stdout = open(os.devnull, 'w')
        else:
            self.stdout = stdout
        if stderr is None:
            self.stderr = sys.stderr
        else:
            self.stderr = stderr
        if logfile is None:
            self.log = None
        else:
            self.log = open(logfile, 'a')

        # set manual_driver
        if driver is not None:
            if isinstance(driver, str):
                if driver in driver_map:
                    self.manual_driver = driver_map[driver]
                else:
                    self._dbg(1, 'Invalid driver string given. Ignoring...')
            elif isinstance(driver, Driver):
                self.manual_driver = driver
            else:
                self._dbg(1, 'Invalid driver given. Ignoring...')
Example #10
0
class eventTest(unittest.TestCase):
    CORRELATE = Event

    def setUp(self):
        self.event  = Event()
        self.args   = None
        self.kwargs = None

    def callback(self, *args, **kwargs):
        self.args   = args
        self.kwargs = kwargs

    def callback2(self, *args, **kwargs):
        self.callback(*args, **kwargs)

    def testConstructor(self):
        event = Event()

    def testConnect(self):
        self.event.connect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.assertRaises(AttributeError, self.event.connect, self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)

    def testListen(self):
        import gc
        from Exscript.util.weakmethod import WeakMethod
        def thefunction():
            pass
        ref = self.event.listen(thefunction)
        self.assert_(isinstance(ref, WeakMethod))
        self.assertEqual(self.event.n_subscribers(), 1)
        self.assertRaises(AttributeError, self.event.listen, thefunction)
        del thefunction
        gc.collect()
        self.assertEqual(self.event.n_subscribers(), 0)

    def testNSubscribers(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.event.listen(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)

    def testIsConnected(self):
        self.assertEqual(self.event.is_connected(self.callback), False)
        self.event.connect(self.callback)
        self.assertEqual(self.event.is_connected(self.callback), True)

        self.assertEqual(self.event.is_connected(self.callback2), False)
        self.event.listen(self.callback2)
        self.assertEqual(self.event.is_connected(self.callback2), True)

    def testEmit(self):
        self.event.connect(self.callback)
        self.assertEqual(self.args,   None)
        self.assertEqual(self.kwargs, None)

        self.event.emit()
        self.assertEqual(self.args,   ())
        self.assertEqual(self.kwargs, {})

        self.event.emit('test')
        self.assertEqual(self.args,   ('test',))
        self.assertEqual(self.kwargs, {})

        self.event.emit('test', foo = 'bar')
        self.assertEqual(self.args,   ('test',))
        self.assertEqual(self.kwargs, {'foo': 'bar'})
        self.event.disconnect(self.callback)

        self.event.listen(self.callback)
        self.args   = None
        self.kwargs = None

        self.event.emit()
        self.assertEqual(self.args,   ())
        self.assertEqual(self.kwargs, {})

        self.event.emit('test')
        self.assertEqual(self.args,   ('test',))
        self.assertEqual(self.kwargs, {})

        self.event.emit('test', foo = 'bar')
        self.assertEqual(self.args,   ('test',))
        self.assertEqual(self.kwargs, {'foo': 'bar'})
        self.event.disconnect(self.callback)

    def testDisconnect(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)
        self.event.disconnect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.event.disconnect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 0)

    def testDisconnectAll(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)
        self.event.disconnect_all()
        self.assertEqual(self.event.n_subscribers(), 0)
Example #11
0
 def setUp(self):
     self.event  = Event()
     self.args   = None
     self.kwargs = None
Example #12
0
class Account(object):
    """
    This class represents a user account.
    """
    def __init__(self, name, password='', password2=None, key=None):
        """
        Constructor.

        The authorization password is only required on hosts that
        separate the authentication from the authorization procedure.
        If an authorization password is not given, it defaults to the
        same value as the authentication password.

        :type  name: string
        :param name: A username.
        :type  password: string
        :param password: The authentication password.
        :type  password2: string
        :param password2: The authorization password, if required.
        :type  key: PrivateKey
        :param key: A private key, if required.
        """
        self.acquired_event = Event()
        self.released_event = Event()
        self.changed_event = Event()
        self.name = name
        self.password = password
        self.authorization_password = password2
        self.key = key
        self.synclock = multiprocessing.Condition(multiprocessing.Lock())
        self.lock = multiprocessing.Lock()

    def __enter__(self):
        self.acquire()
        return self

    def __exit__(self, thetype, value, traceback):
        self.release()

    def context(self):
        """
        When you need a 'with' context for an already-acquired account.
        """
        return Context(self)

    def acquire(self, signal=True):
        """
        Locks the account.

        :type  signal: bool
        :param signal: Whether to emit the acquired_event signal.
        """
        with self.synclock:
            while not self.lock.acquire(False):
                self.synclock.wait()
            if signal:
                self.acquired_event(self)
            self.synclock.notify_all()

    def release(self, signal=True):
        """
        Unlocks the account.

        :type  signal: bool
        :param signal: Whether to emit the released_event signal.
        """
        with self.synclock:
            self.lock.release()
            if signal:
                self.released_event(self)
            self.synclock.notify_all()

    def set_name(self, name):
        """
        Changes the name of the account.

        :type  name: string
        :param name: The account name.
        """
        self.name = name
        self.changed_event.emit(self)

    def get_name(self):
        """
        Returns the name of the account.

        :rtype:  string
        :return: The account name.
        """
        return self.name

    def set_password(self, password):
        """
        Changes the password of the account.

        :type  password: string
        :param password: The account password.
        """
        self.password = password
        self.changed_event.emit(self)

    def get_password(self):
        """
        Returns the password of the account.

        :rtype:  string
        :return: The account password.
        """
        return self.password

    def set_authorization_password(self, password):
        """
        Changes the authorization password of the account.

        :type  password: string
        :param password: The new authorization password.
        """
        self.authorization_password = password
        self.changed_event.emit(self)

    def get_authorization_password(self):
        """
        Returns the authorization password of the account.

        :rtype:  string
        :return: The account password.
        """
        return self.authorization_password or self.password

    def get_key(self):
        """
        Returns the key of the account, if any.

        :rtype:  PrivateKey|None
        :return: A key object.
        """
        return self.key
Example #13
0
 def testConstructor(self):
     event = Event()
Example #14
0
 def setUp(self):
     self.event = Event()
     self.args = None
     self.kwargs = None
Example #15
0
class eventTest(unittest.TestCase):
    CORRELATE = Event

    def setUp(self):
        self.event = Event()
        self.args = None
        self.kwargs = None

    def callback(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs

    def callback2(self, *args, **kwargs):
        self.callback(*args, **kwargs)

    def testConstructor(self):
        event = Event()

    def testConnect(self):
        self.event.connect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.assertRaises(AttributeError, self.event.connect, self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)

    def testListen(self):
        import gc
        from Exscript.util.weakmethod import WeakMethod

        def thefunction():
            pass

        ref = self.event.listen(thefunction)
        self.assertIsInstance(ref, WeakMethod)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.assertRaises(AttributeError, self.event.listen, thefunction)
        del thefunction
        gc.collect()
        self.assertEqual(self.event.n_subscribers(), 0)

    def testNSubscribers(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.event.listen(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)

    def testIsConnected(self):
        self.assertEqual(self.event.is_connected(self.callback), False)
        self.event.connect(self.callback)
        self.assertEqual(self.event.is_connected(self.callback), True)

        self.assertEqual(self.event.is_connected(self.callback2), False)
        self.event.listen(self.callback2)
        self.assertEqual(self.event.is_connected(self.callback2), True)

    def testEmit(self):
        self.event.connect(self.callback)
        self.assertEqual(self.args, None)
        self.assertEqual(self.kwargs, None)

        self.event.emit()
        self.assertEqual(self.args, ())
        self.assertEqual(self.kwargs, {})

        self.event.emit('test')
        self.assertEqual(self.args, ('test', ))
        self.assertEqual(self.kwargs, {})

        self.event.emit('test', foo='bar')
        self.assertEqual(self.args, ('test', ))
        self.assertEqual(self.kwargs, {'foo': 'bar'})
        self.event.disconnect(self.callback)

        self.event.listen(self.callback)
        self.args = None
        self.kwargs = None

        self.event.emit()
        self.assertEqual(self.args, ())
        self.assertEqual(self.kwargs, {})

        self.event.emit('test')
        self.assertEqual(self.args, ('test', ))
        self.assertEqual(self.kwargs, {})

        self.event.emit('test', foo='bar')
        self.assertEqual(self.args, ('test', ))
        self.assertEqual(self.kwargs, {'foo': 'bar'})
        self.event.disconnect(self.callback)

    def testDisconnect(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)
        self.event.disconnect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.event.disconnect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 0)

    def testDisconnectAll(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)
        self.event.disconnect_all()
        self.assertEqual(self.event.n_subscribers(), 0)
Example #16
0
class Account(object):
    """
    This class represents a user account.
    """

    def __init__(self, name, password = '', password2 = None, key = None):
        """
        Constructor.

        The authorization password is only required on hosts that
        separate the authentication from the authorization procedure.
        If an authorization password is not given, it defaults to the
        same value as the authentication password.

        @type  name: string
        @param name: A username.
        @type  password: string
        @param password: The authentication password.
        @type  password2: string
        @param password2: The authorization password, if required.
        @type  key: PrivateKey
        @param key: A private key, if required.
        """
        self.acquired_event         = Event()
        self.released_event         = Event()
        self.changed_event          = Event()
        self.name                   = name
        self.password               = password
        self.authorization_password = password2
        self.key                    = key
        self.synclock               = threading.Condition(threading.Lock())
        self.lock                   = threading.Lock()

    def __enter__(self):
        self.acquire()
        return self

    def __exit__(self, thetype, value, traceback):
        self.release()

    def context(self):
        """
        When you need a 'with' context for an already-acquired account.
        """
        return Context(self)

    def acquire(self, signal = True):
        """
        Locks the account.

        @type  signal: bool
        @param signal: Whether to emit the acquired_event signal.
        """
        with self.synclock:
            while not self.lock.acquire(False):
                self.synclock.wait()
            if signal:
                self.acquired_event(self)
            self.synclock.notify_all()

    def release(self, signal = True):
        """
        Unlocks the account.

        @type  signal: bool
        @param signal: Whether to emit the released_event signal.
        """
        with self.synclock:
            self.lock.release()
            if signal:
                self.released_event(self)
            self.synclock.notify_all()

    def set_name(self, name):
        """
        Changes the name of the account.

        @type  name: string
        @param name: The account name.
        """
        self.name = name
        self.changed_event.emit(self)

    def get_name(self):
        """
        Returns the name of the account.

        @rtype:  string
        @return: The account name.
        """
        return self.name

    def set_password(self, password):
        """
        Changes the password of the account.

        @type  password: string
        @param password: The account password.
        """
        self.password = password
        self.changed_event.emit(self)

    def get_password(self):
        """
        Returns the password of the account.

        @rtype:  string
        @return: The account password.
        """
        return self.password

    def set_authorization_password(self, password):
        """
        Changes the authorization password of the account.

        @type  password: string
        @param password: The new authorization password.
        """
        self.authorization_password = password
        self.changed_event.emit(self)

    def get_authorization_password(self):
        """
        Returns the authorization password of the account.

        @rtype:  string
        @return: The account password.
        """
        return self.authorization_password or self.password

    def get_key(self):
        """
        Returns the key of the account, if any.

        @rtype:  PrivateKey|None
        @return: A key object.
        """
        return self.key