-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathutils.py
More file actions
206 lines (160 loc) · 5.72 KB
/
Copy pathutils.py
File metadata and controls
206 lines (160 loc) · 5.72 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from subprocess import Popen, PIPE
from mr.developer.compat import s
import os
import sys
import threading
def tee(process, filter_func):
"""Read lines from process.stdout and echo them to sys.stdout.
Returns a list of lines read. Lines are not newline terminated.
The 'filter_func' is a callable which is invoked for every line,
receiving the line as argument. If the filter_func returns True, the
line is echoed to sys.stdout.
"""
# We simply use readline here, more fancy IPC is not warranted
# in the context of this package.
lines = []
while True:
line = process.stdout.readline()
if line:
stripped_line = line.rstrip()
if filter_func(stripped_line):
sys.stdout.write(s(line))
lines.append(stripped_line)
elif process.poll() is not None:
break
return lines
def tee2(process, filter_func):
"""Read lines from process.stderr and echo them to sys.stderr.
The 'filter_func' is a callable which is invoked for every line,
receiving the line as argument. If the filter_func returns True, the
line is echoed to sys.stderr.
"""
while True:
line = process.stderr.readline()
if line:
stripped_line = line.rstrip()
if filter_func(stripped_line):
sys.stderr.write(s(line))
elif process.poll() is not None:
break
class background_thread(object):
"""Context manager to start and stop a background thread."""
def __init__(self, target, args):
self.target = target
self.args = args
def __enter__(self):
self._t = threading.Thread(target=self.target, args=self.args)
self._t.start()
return self._t
def __exit__(self, *ignored):
self._t.join()
def popen(cmd, echo=True, echo2=True, env=None, cwd=None):
"""Run 'cmd' and return a two-tuple of exit code and lines read.
If 'echo' is True, the stdout stream is echoed to sys.stdout.
If 'echo2' is True, the stderr stream is echoed to sys.stderr.
The 'echo' and 'echo2' arguments may also be callables, in which
case they are used as tee filters.
The 'env' argument allows to pass a dict replacing os.environ.
if 'cwd' is not None, current directory will be changed to cwd before execution.
"""
if not callable(echo):
if echo:
echo = On()
else:
echo = Off()
if not callable(echo2):
if echo2:
echo2 = On()
else:
echo2 = Off()
process = Popen(
cmd,
shell=True,
stdout=PIPE,
stderr=PIPE,
env=env,
cwd=cwd
)
bt = background_thread(tee2, (process, echo2))
bt.__enter__()
try:
lines = tee(process, echo)
finally:
bt.__exit__(None, None, None)
return process.returncode, lines
class On(object):
"""A tee filter printing all lines."""
def __call__(self, line):
return True
class Off(object):
"""A tee filter suppressing all lines."""
def __call__(self, line):
return False
class Process(object):
"""Process related functions using the tee module."""
def __init__(self, quiet=False, env=None, cwd=None):
self.quiet = quiet
self.env = env
self.cwd = cwd
def popen(self, cmd, echo=True, echo2=True, cwd=None):
# env *replaces* os.environ
if self.quiet:
echo = echo2 = False
return popen(cmd, echo, echo2, env=self.env, cwd=self.cwd or cwd)
def check_call(self, cmd, **kw):
rc, lines = self.popen(cmd, **kw)
assert rc == 0
return lines
class MockConfig(object):
def __init__(self):
self.buildout_args = []
self.develop = {}
self.rewrites = []
def save(self):
pass
class MockDevelop(object):
def __init__(self):
from mr.developer.develop import ArgumentParser
self.always_accept_server_certificate = True
self.always_checkout = False
self.auto_checkout = ''
self.update_git_submodules = 'always'
self.develeggs = ''
self.config = MockConfig()
self.parser = ArgumentParser()
self.parsers = self.parser.add_subparsers(title="commands", metavar="")
self.threads = 1
class GitRepo(object):
def __init__(self, base):
self.base = base
self.url = 'file:///%s' % self.base
self.process = Process(cwd=self.base)
def __call__(self, cmd, **kw):
return self.process.check_call(cmd, **kw)
def init(self):
os.mkdir(self.base)
self("git init")
self('git config --global init.defaultBranch master')
self('git config --global protocol.file.allow always')
def setup_user(self):
self('git config user.email "florian.schulze@gmx.net"')
self('git config user.name "Florian Schulze"')
self('git config commit.gpgsign false')
def add_file(self, fname, msg=None):
repo_file = self.base[fname]
repo_file.create_file(fname)
self("git add %s" % repo_file, echo=False)
if msg is None:
msg = fname
self("git commit %s -m %s" % (repo_file, msg), echo=False)
def add_dir(self, dirname):
repo_dir = self.base[dirname]
repo_dir.create_dir()
def add_submodule(self, submodule, submodule_name):
assert isinstance(submodule, GitRepo)
self("git -c protocol.file.allow=always submodule add %s %s" % (submodule.url, submodule_name))
self("git add .gitmodules")
self("git add %s" % submodule_name)
self("git commit -m 'Add submodule %s'" % submodule_name)
def add_branch(self, bname, msg=None):
self("git checkout -b %s" % bname)