MCPcopy Index your code
hub / github.com/pyinvoke/invoke / prefix

Method prefix

invoke/context.py:281–334  ·  view source on GitHub ↗

Prefix all nested `run`/`sudo` commands with given command plus ``&&``. Most of the time, you'll want to be using this alongside a shell script which alters shell state, such as ones which export or alter shell environment variables. For example, one of the

(self, command: str)

Source from the content-addressed store, hash-verified

279
280 @contextmanager
281 def prefix(self, command: str) -> Generator[None, None, None]:
282 """
283 Prefix all nested `run`/`sudo` commands with given command plus ``&&``.
284
285 Most of the time, you'll want to be using this alongside a shell script
286 which alters shell state, such as ones which export or alter shell
287 environment variables.
288
289 For example, one of the most common uses of this tool is with the
290 ``workon`` command from `virtualenvwrapper
291 <https://virtualenvwrapper.readthedocs.io/en/latest/>`_::
292
293 with c.prefix('workon myvenv'):
294 c.run('./manage.py migrate')
295
296 In the above snippet, the actual shell command run would be this::
297
298 $ workon myvenv && ./manage.py migrate
299
300 This context manager is compatible with `cd`, so if your virtualenv
301 doesn&#x27;t ``cd`` in its ``postactivate`` script, you could do the
302 following::
303
304 with c.cd('/path/to/app'):
305 with c.prefix('workon myvenv'):
306 c.run('./manage.py migrate')
307 c.run('./manage.py loaddata fixture')
308
309 Which would result in executions like so::
310
311 $ cd /path/to/app && workon myvenv && ./manage.py migrate
312 $ cd /path/to/app && workon myvenv && ./manage.py loaddata fixture
313
314 Finally, as alluded to above, `prefix` may be nested if desired, e.g.::
315
316 with c.prefix('workon myenv'):
317 c.run('ls')
318 with c.prefix('source /some/script'):
319 c.run('touch a_file')
320
321 The result::
322
323 $ workon myenv && ls
324 $ workon myenv && source /some/script && touch a_file
325
326 Contrived, but hopefully illustrative.
327
328 .. versionadded:: 1.0
329 """
330 self.command_prefixes.append(command)
331 try:
332 yield
333 finally:
334 self.command_prefixes.pop()
335
336 @property
337 def cwd(self) -> str:

Calls 1

popMethod · 0.45

Tested by

no test coverage detected