Runs command inside venv and returns termination code. Args: args: List of args. path: Name of venv. recreate: If false we do not run ` -m venv ` if already exists. This avoids a delay
(args, path, recreate=True, clean=False)
| 1561 | |
| 1562 | |
| 1563 | def venv_run(args, path, recreate=True, clean=False): |
| 1564 | ''' |
| 1565 | Runs command inside venv and returns termination code. |
| 1566 | |
| 1567 | Args: |
| 1568 | args: |
| 1569 | List of args. |
| 1570 | path: |
| 1571 | Name of venv. |
| 1572 | recreate: |
| 1573 | If false we do not run `<sys.executable> -m venv <path>` if <path> |
| 1574 | already exists. This avoids a delay in the common case where <path> |
| 1575 | is already set up, but fails if <path> exists but does not contain |
| 1576 | a valid venv. |
| 1577 | clean: |
| 1578 | If true we first delete <path>. |
| 1579 | ''' |
| 1580 | if clean: |
| 1581 | log(f'Removing any existing venv {path}.') |
| 1582 | assert path.startswith('venv-') |
| 1583 | shutil.rmtree(path, ignore_errors=1) |
| 1584 | if recreate or not os.path.isdir(path): |
| 1585 | run(f'{sys.executable} -m venv {path}') |
| 1586 | if platform.system() == 'Windows': |
| 1587 | command = f'{path}\\Scripts\\activate && python' |
| 1588 | # shlex not reliable on Windows. |
| 1589 | # Use crude quoting with "...". Seems to work. |
| 1590 | for arg in args: |
| 1591 | assert '"' not in arg |
| 1592 | command += f' "{arg}"' |
| 1593 | else: |
| 1594 | command = f'. {path}/bin/activate && python {shlex.join(args)}' |
| 1595 | e = run(command, check=0) |
| 1596 | return e |
| 1597 | |
| 1598 | |
| 1599 | if __name__ == '__main__': |