Beispiel #1
0
    def do_server(self, server):
        """Specify STC server.

        If a port is not specified, then the value of the STC_SERVER_PORT
        environment variable is used.  If that is not set, then the default
        port 80 is tried.  If a connection cannot be make to the default, then
        port 8888 is used.

        Synopsis:
            server server_address [port]

        Example:
            server 10.109.120.84 80

        """
        server_ok = False
        while not server_ok:
            while not server:
                server = input('Enter server: ')
            try:
                socket.gethostbyname(server)
                server_ok = True
            except socket.gaierror as e:
                print(e)
                server = None

        try:
            self._stc = stchttp.StcHttp(server,
                                        debug_print=self._stc.debug_print())
        except RuntimeError as e:
            print(e)
            return

        self._server = server
        self._sessions = []
def stc_system_info(stc_addr):
    """Return dictionary of STC and API information.

    If a session already exists, then use it to get STC information and avoid
    taking the time to start a new session.  A session is necessary to get
    STC information.

    """
    stc = stchttp.StcHttp(stc_addr)
    sessions = stc.sessions()
    if sessions:
        # If a session already exists, use it to get STC information.
        stc.join_session(sessions[0])
        sys_info = stc.system_info()
    else:
        # Create a new session to get STC information.
        stc.new_session('anonymous')
        try:
            sys_info = stc.system_info()
        finally:
            # Make sure the temporary session in terminated.
            stc.end_session()

    return sys_info
Beispiel #3
0
def main():
    import argparse
    ap = argparse.ArgumentParser(
        prog='python -m stcrestclient.tccsh',
        usage='python -m stcrestclient.tccsh server [options]',
        description='Command shell that provides STC Automation API '
        'functionality using an interactive command line interface, or '
        'command file.')
    ap.add_argument('server',
                    nargs='?',
                    help='Address of TestCenter server to connect to.')
    ap.add_argument('-c',
                    metavar='COMMAND_STRING',
                    dest='cmd_str',
                    help='Command string.  Multiple commands are separated '
                    'by ";" (semicolon).')
    ap.add_argument('--debug',
                    '-d',
                    action='store_true',
                    help='Enable debug output.')
    ap.add_argument('--file',
                    '-f',
                    metavar='FILE_PATH',
                    dest='cmd_file',
                    help='Read commands from the specified file.')
    ap.add_argument('--port',
                    '-p',
                    metavar='PORT',
                    type=int,
                    help='Server TCP port to connect to (default %s).' %
                    (stchttp.DEFAULT_PORT, ))
    ap.add_argument('--version',
                    '-V',
                    action='store_true',
                    help='Print stcrestclient version number and exit.')
    args = ap.parse_args()

    if args.version:
        import pkg_resources
        pkg_name = 'stcrestclient'
        print(pkg_name, pkg_resources.get_distribution(pkg_name).version)
        return 0

    server_ok = False
    while not server_ok:
        while not args.server:
            args.server = input('Enter server: ')

        try:
            socket.gethostbyname(args.server)
            server_ok = True
        except socket.gaierror as e:
            print('hostname not known')
            args.server = None

    if args.debug:
        print('===> connecting to', args.server, end=' ')
        if args.port:
            print('on port', args.port)
        else:
            print()

    cmds = []
    if args.cmd_file:
        with open(args.cmd_file) as cf:
            for c in cf:
                if not c:
                    continue
                c = c.strip()
                if not c or c[0] == '#':
                    continue
                cmds.append(c)
    elif args.cmd_str:
        for c in args.cmd_str.split(';'):
            if not c:
                continue
            c = c.strip()
            if not c or c[0] == '#':
                continue
            cmds.append(c)

    if cmds:
        tccsh = TestCenterCommandShell(None)
        tccsh.use_rawinput = False
        tccsh.intro = None
    else:
        tccsh = TestCenterCommandShell()

    tccsh._server = args.server
    tccsh._port = args.port
    try:
        tccsh._stc = stchttp.StcHttp(args.server,
                                     args.port,
                                     debug_print=args.debug)
        if cmds:
            tccsh.preloop()
            for c in cmds:
                if tccsh.onecmd(c):
                    break
        else:
            tccsh.cmdloop()
    except KeyboardInterrupt:
        print('\nGot keyboard interrupt. Exiting...')
    except Exception as e:
        print(e)
        return 1

    return 0
Beispiel #4
0
    def new_session(self,
                    server=None,
                    session_name=None,
                    user_name=None,
                    existing_session=None):
        """Create a new session or attach to existing.

        Normally, this function is called automatically, and gets its parameter
        values from the environment.  It is provided as a public function for
        cases when extra control over session creation is required in an
        automation script that is adapted to use ReST.

        WARNING:  This function is not part of the original StcPython.py and if
        called directly by an automation script, then that script will not be
        able to revert to using the non-ReST API until the call to this
        function is removed.

        Arguments:
        server           -- STC server (Lab Server) address.  If not set get
                            value from STC_SERVER_ADDRESS environment variable.
        session_name     -- Name part of session ID.  If not set get value from
                            STC_SESSION_NAME environment variable.
        user_name        -- User portion of session ID.  If not set get name of
                            user this script is running as.
        existing_session -- Behavior when session already exists.  Recognized
                            values are 'kill' and 'join'.  If not set get value
                            from EXISTING_SESSION environment variable.  If not
                            set to recognized value, raise exception if session
                            already exists.

        See also: stchttp.StcHttp(), stchttp.new_session()

        Return:
        The internal StcHttp object that is used for this session.  This allows
        the caller to perform additional interactions with the STC ReST API
        beyond what the adapter provides.

        """
        if not server:
            server = os.environ.get('STC_SERVER_ADDRESS')
            if not server:
                raise EnvironmentError('STC_SERVER_ADDRESS not set')
        self._stc = stchttp.StcHttp(server)
        if not session_name:
            session_name = os.environ.get('STC_SESSION_NAME')
            if not session_name or session_name == '__NEW_TEST_SESSION__':
                session_name = None
        if not user_name:
            try:
                # Try to get the name of the current user.
                user_name = getpass.getuser()
            except:
                pass

        if not existing_session:
            # Try to get existing_session from environ if not passed in.
            existing_session = os.environ.get('EXISTING_SESSION')

        if existing_session:
            existing_session = existing_session.lower()
            if existing_session == 'kill':
                # Kill any existing session and create a new one.
                self._stc.new_session(user_name, session_name, True)
                return self._stc
            if existing_session == 'join':
                # Create a new session, or join if already exists.
                try:
                    self._stc.new_session(user_name, session_name, False)
                except RuntimeError as e:
                    if str(e).find('already exists') >= 0:
                        sid = ' - '.join((session_name, user_name))
                        self._stc.join_session(sid)
                    else:
                        raise
                return self._stc

        # Create a new session, raise exception if session already exists.
        self._stc.new_session(user_name, session_name, False)
        return self._stc