source: rtems-tools/tester/rt/tftpy/TftpShared.py @ 3a867a4

5
Last change on this file since 3a867a4 was 3a867a4, checked in by Chris Johns <chrisj@…>, on 09/21/17 at 08:26:20

Add TFTP as a back end option for testing. Add telnet as a console option.

TFTP runs a local TFTP server on port 69 or another specified port and
serves each test for any requested file.

Telnet is now a console option.

  • Property mode set to 100644
File size: 2.8 KB
Line 
1"""This module holds all objects shared by all other modules in tftpy."""
2
3from __future__ import absolute_import, division, print_function, unicode_literals
4import logging
5from logging.handlers import RotatingFileHandler
6
7LOG_LEVEL = logging.NOTSET
8MIN_BLKSIZE = 8
9DEF_BLKSIZE = 512
10MAX_BLKSIZE = 65536
11SOCK_TIMEOUT = 5
12MAX_DUPS = 20
13TIMEOUT_RETRIES = 5
14DEF_TFTP_PORT = 69
15
16# A hook for deliberately introducing delay in testing.
17DELAY_BLOCK = 0
18
19# Initialize the logger.
20logging.basicConfig()
21
22# The logger used by this library. Feel free to clobber it with your own, if
23# you like, as long as it conforms to Python's logging.
24log = logging.getLogger('tftpy')
25
26def create_streamhandler():
27    """add create_streamhandler output logging.DEBUG msg to stdout.
28    """
29    console = logging.StreamHandler()
30    console.setLevel(logging.INFO)
31    formatter = logging.Formatter('%(levelname)-8s %(message)s')
32    console.setFormatter(formatter)
33    return console
34
35def create_rotatingfilehandler(path, maxbytes=10*1024*1024, count=20):
36    """
37    add create_rotatingfilehandler record the logging.DEBUG msg to logfile. you can change the maxsize (10*1024*1024)
38    and amount of the logfiles
39    """
40    Rthandler = RotatingFileHandler(path, maxbytes, count)
41    Rthandler.setLevel(logging.INFO)
42    formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
43    Rthandler.setFormatter(formatter)
44    return Rthandler
45
46def addHandler(hdlr):
47    """add handler methods
48    More details see the page:
49    https://docs.python.org/2/library/logging.handlers.html#module-logging.handlers
50    """
51    log.addHandler(hdlr)
52
53def tftpassert(condition, msg):
54    """This function is a simple utility that will check the condition
55    passed for a false state. If it finds one, it throws a TftpException
56    with the message passed. This just makes the code throughout cleaner
57    by refactoring."""
58    if not condition:
59        raise TftpException(msg)
60
61def setLogLevel(level):
62    """This function is a utility function for setting the internal log level.
63    The log level defaults to logging.NOTSET, so unwanted output to stdout is
64    not created."""
65    log.setLevel(level)
66
67class TftpErrors(object):
68    """This class is a convenience for defining the common tftp error codes,
69    and making them more readable in the code."""
70    NotDefined = 0
71    FileNotFound = 1
72    AccessViolation = 2
73    DiskFull = 3
74    IllegalTftpOp = 4
75    UnknownTID = 5
76    FileAlreadyExists = 6
77    NoSuchUser = 7
78    FailedNegotiation = 8
79
80class TftpException(Exception):
81    """This class is the parent class of all exceptions regarding the handling
82    of the TFTP protocol."""
83    pass
84
85class TftpTimeout(TftpException):
86    """This class represents a timeout error waiting for a response from the
87    other end."""
88    pass
Note: See TracBrowser for help on using the repository browser.