Mercurial > templog
annotate py/utils.py @ 482:9950ffa9a79b
set nearly working
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 04 Feb 2014 22:49:16 +0800 |
parents | fe729664a5e6 |
children | 185621f47040 |
rev | line source |
---|---|
439 | 1 import os |
2 import sys | |
3 #import ctypes | |
4 import time | |
5 import select | |
6 import logging | |
7 | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
444
diff
changeset
|
8 D = logging.debug |
439 | 9 L = logging.info |
10 W = logging.warning | |
11 E = logging.error | |
12 | |
13 DEFAULT_TRIES = 3 | |
14 READLINE_SELECT_TIMEOUT = 1 | |
15 | |
443 | 16 def EX(msg, *args, **kwargs): |
17 kwargs['exc_info'] = True | |
18 logging.error(msg, *args, **kwargs) | |
19 | |
439 | 20 clock_gettime = None |
21 no_clock_gettime = True | |
22 def monotonic_time(): | |
23 global clock_gettime | |
24 global no_clock_gettime | |
25 if no_clock_gettime: | |
26 return time.time() | |
27 | |
28 class timespec(ctypes.Structure): | |
29 _fields_ = [ | |
30 ('tv_sec', ctypes.c_long), | |
31 ('tv_nsec', ctypes.c_long) | |
32 ] | |
33 if not clock_gettime: | |
34 try: | |
35 librt = ctypes.CDLL('librt.so.0', use_errno=True) | |
36 clock_gettime = librt.clock_gettime | |
37 clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] | |
38 except: | |
39 W("No clock_gettime(), using fake fallback.") | |
40 no_clock_gettime = True | |
41 return time.time() | |
42 | |
43 t = timespec() | |
44 CLOCK_MONOTONIC = 1 # see <linux/time.h> | |
45 | |
46 if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0: | |
47 errno_ = ctypes.get_errno() | |
48 raise OSError(errno_, os.strerror(errno_)) | |
49 return t.tv_sec + t.tv_nsec * 1e-9 | |
50 | |
51 # decorator, tries a number of times, returns None on failure, sleeps between | |
52 # Must be used as "@retry()" if arguments are defaulted | |
53 def retry(retries=DEFAULT_TRIES, try_time = 1): | |
54 def inner(func): | |
55 def new_f(*args, **kwargs): | |
56 for i in range(retries): | |
57 d = func(*args, **kwargs) | |
58 if d is not None: | |
59 return d | |
60 time.sleep(try_time) | |
61 return None | |
62 | |
63 new_f.func_name = func.func_name | |
64 return new_f | |
65 return inner | |
66 | |
67 def readline(sock): | |
68 timeout = READLINE_SELECT_TIMEOUT | |
69 buf = '' | |
70 while True: | |
71 (rlist, wlist, xlist) = select.select([sock], [], [], timeout) | |
72 if sock not in rlist: | |
73 # hit timeout | |
74 return None | |
75 | |
76 c = sock.recv(1) | |
77 if c == '': | |
78 # lightblue timeout | |
79 return None | |
80 if c == '\r': | |
81 continue | |
82 | |
83 buf += c | |
84 if c == '\n': | |
85 return buf | |
86 | |
87 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html | |
88 def crc16(buff, crc = 0, poly = 0x8408): | |
89 l = len(buff) | |
90 i = 0 | |
91 while i < l: | |
92 ch = ord(buff[i]) | |
93 uc = 0 | |
94 while uc < 8: | |
95 if (crc & 1) ^ (ch & 1): | |
96 crc = (crc >> 1) ^ poly | |
97 else: | |
98 crc >>= 1 | |
99 ch >>= 1 | |
100 uc += 1 | |
101 i += 1 | |
102 return crc | |
103 | |
104 def cheap_daemon(): | |
105 L("Daemonising.") | |
106 sys.stdout.flush() | |
107 sys.stderr.flush() | |
108 out = file('/dev/null', 'a+') | |
109 os.dup2(out.fileno(), sys.stdout.fileno()) | |
110 os.dup2(out.fileno(), sys.stderr.fileno()) | |
111 | |
112 try: | |
113 pid = os.fork() | |
114 if pid > 0: | |
115 sys.exit(0) | |
116 except OSError, e: | |
117 E("Bad fork()") | |
118 sys.exit(1) | |
119 | |
120 os.setsid() | |
121 | |
122 try: | |
123 pid = os.fork() | |
124 if pid > 0: | |
125 sys.exit(0) | |
126 except OSError, e: | |
127 E("Bad fork()") | |
128 sys.exit(1) | |
129 | |
444 | 130 def uptime(): |
131 try: | |
132 return float(open('/proc/uptime', 'r').read().split(' ', 1)[0]) | |
133 except Exception, e: | |
134 return -1 | |
439 | 135 |