globals.py 2.19 KB
Newer Older
Matt Traudt's avatar
Matt Traudt committed
1
import os
2
import time
3
import logging
Matt Traudt's avatar
Matt Traudt committed
4

5
log = logging.getLogger(__name__)
Matt Traudt's avatar
Matt Traudt committed
6
7
8

G_PKG_DIR = os.path.abspath(os.path.dirname(__file__))

Matt Traudt's avatar
Matt Traudt committed
9
# Minimum and maximum number of bytes a scanner is allowed to request from a
10
11
# server. If these are changed, a WIRE_PROTO_VER bump is required, which also
# happens to require an sbws major version bump.
12
#
Matt Traudt's avatar
Matt Traudt committed
13
# Note for smart people and people who pull out Wireshark: Even if the scanner
14
15
16
17
18
19
# requests 1 byte, that request and the 1 byte response will each be carried
# over the Internet in 514 byte Tor cells. Theoretically we could bump the
# minimum request size up to ~498 bytes, but I see no reason why we should.
# Trying to hit the maximum cell size just makes sbws server send more, us read
# more, and it runs the risk of standards changing underneath us and sbws
# suddenly creating more than one cell.
20
MIN_REQ_BYTES = 1
21
MAX_REQ_BYTES = 1 * 1024 * 1024 * 1024  # 1 GiB
22
SOCKET_TIMEOUT = 60  # seconds
23

24
SPEC_VERSION = '1.1.0'
Matt Traudt's avatar
Matt Traudt committed
25

juga's avatar
juga committed
26

Matt Traudt's avatar
Matt Traudt committed
27
def is_initted(d):
28
    if not os.path.isdir(d):
29
        log.debug('%s not initialized: %s doesn\'t exist', d, d)
Matt Traudt's avatar
Matt Traudt committed
30
        return False
Matt Traudt's avatar
Matt Traudt committed
31
32
33
    conf_fnames = [os.path.join(d, 'config.ini'),
                   os.path.join(d, 'config.log.ini')]
    for fname in conf_fnames:
34
        if not os.path.isfile(fname):
35
            log.debug('%s not initialized: missing %s', d, fname)
Matt Traudt's avatar
Matt Traudt committed
36
            return False
37
    log.debug('%s seems initialized.', d)
Matt Traudt's avatar
Matt Traudt committed
38
    return True
39
40


41
def fail_hard(*a, **kw):
Matt Traudt's avatar
Matt Traudt committed
42
    ''' Log something ... and then exit as fast as possible '''
Matt Traudt's avatar
Matt Traudt committed
43
    log.critical(*a, **kw)
44
    exit(1)
Matt Traudt's avatar
Matt Traudt committed
45
46


47
48
49
50
51
52
53
54
55
56
def time_now():
    '''
    Return the current time in seconds since 1970. This function exists to
    make testing easier

    :returns: Unix timestamp as a float
    '''
    return time.time()


57
58
59
60
61
62
63
64
65
66
67
68
69
def touch_file(fname, times=None):
    '''
    If **fname** exists, update its last access and modified times to now. If
    **fname** does not exist, create it. If **times** are specified, pass them
    to os.utime for use.

    :param str fname: Name of file to update or create
    :param tuple times: 2-tuple of floats for access time and modified time
        respectively
    '''
    log.debug('Touching %s', fname)
    with open(fname, 'a') as fd:
        os.utime(fd.fileno(), times=times)