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

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


def is_initted(d):
8
    if not os.path.isdir(d):
9
        log.debug('%s not initialized: %s doesn\'t exist', d, d)
Matt Traudt's avatar
Matt Traudt committed
10
        return False
Matt Traudt's avatar
Matt Traudt committed
11
12
13
    conf_fnames = [os.path.join(d, 'config.ini'),
                   os.path.join(d, 'config.log.ini')]
    for fname in conf_fnames:
14
        if not os.path.isfile(fname):
15
            log.debug('%s not initialized: missing %s', d, fname)
Matt Traudt's avatar
Matt Traudt committed
16
            return False
17
    log.debug('%s seems initialized.', d)
Matt Traudt's avatar
Matt Traudt committed
18
    return True
19
20


21
def fail_hard(*a, **kw):
Matt Traudt's avatar
Matt Traudt committed
22
    ''' Log something ... and then exit as fast as possible '''
Matt Traudt's avatar
Matt Traudt committed
23
    log.critical(*a, **kw)
24
    exit(1)
Matt Traudt's avatar
Matt Traudt committed
25
26


27
28
29
30
31
32
33
34
35
36
37
38
39
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)