-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
90 lines (69 loc) · 1.89 KB
/
Copy pathutil.py
File metadata and controls
90 lines (69 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
"""
util.py
"""
from __future__ import print_function
import os
import sys
PID = os.getpid()
# HACK TO SAVE TERMINAL
def HackyLogRedirect():
os.dup2(2, 99)
DEBUG = os.getenv('DEBUG')
def log(msg, *args):
if not DEBUG:
return
if args:
msg = msg % args
#print('[%d] %s' % (PID, msg), file=sys.stderr)
os.write(99, '[%d] %s\n' % (PID, msg))
def read_length(read_func):
# TODO: We should also prevent the loop from going too many times?
buf = ''
while True:
c = read_func(1)
if not c: # EOF
raise EOFError
if not c.isdigit():
if buf:
if c != ':':
raise ValueError("Expected ':', got %r" % c)
else: # didn't get a number
raise ValueError("Expected chunk length, got %r" % c)
break
buf += c
return buf
def _netstring_read(read_func, max_length):
"""Helper for netstring_read and netstring_readfd."""
length_str = read_length(read_func)
length = int(length_str)
if max_length and length > max_length:
raise ValueError("Payload is too large: %s" % length)
payload = read_func(length)
tag_byte = read_func(1)
# dump_line can emit a newline.
if tag_byte != ',' and tag_byte != '\n':
raise ValueError('Got tag %r, expected comma or newline' % tag_byte)
return payload
def netstring_read(f, max_length=0):
"""Read a byte string.
Returns:
The bytes
Raises:
ValueError: if the value is not a byte string (comma or newline)
"""
read_func = f.read
return _netstring_read(read_func, max_length)
def netstring_readfd(fd, max_length=0):
"""Read a byte string from a file descriptor.
Raises:
ValueError: if the value is not a byte string (comma or newline)
"""
read_func = lambda length: os.read(fd, length)
return _netstring_read(read_func, max_length)
def netstring_encode(s):
"""
Args:
s: A byte string to encode
"""
return '%d:%s,' % (len(s), s)