Chad Duffin | ac9ac06 | 2019-07-23 10:06:45 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import logging |
| 4 | from distutils.spawn import find_executable |
| 5 | |
| 6 | from tools.wpt.utils import call |
| 7 | |
| 8 | logger = logging.getLogger(__name__) |
| 9 | |
| 10 | class Virtualenv(object): |
| 11 | def __init__(self, path): |
| 12 | self.path = path |
| 13 | self.virtualenv = find_executable("virtualenv") |
| 14 | if not self.virtualenv: |
| 15 | raise ValueError("virtualenv must be installed and on the PATH") |
| 16 | |
| 17 | @property |
| 18 | def exists(self): |
| 19 | return os.path.isdir(self.path) |
| 20 | |
| 21 | def create(self): |
| 22 | if os.path.exists(self.path): |
| 23 | shutil.rmtree(self.path) |
| 24 | call(self.virtualenv, self.path) |
| 25 | |
| 26 | @property |
| 27 | def bin_path(self): |
| 28 | if sys.platform in ("win32", "cygwin"): |
| 29 | return os.path.join(self.path, "Scripts") |
| 30 | return os.path.join(self.path, "bin") |
| 31 | |
| 32 | @property |
| 33 | def pip_path(self): |
| 34 | path = find_executable("pip", self.bin_path) |
| 35 | if path is None: |
| 36 | raise ValueError("pip not found") |
| 37 | return path |
| 38 | |
| 39 | def activate(self): |
| 40 | path = os.path.join(self.bin_path, "activate_this.py") |
| 41 | execfile(path, {"__file__": path}) |
| 42 | |
| 43 | def start(self): |
| 44 | if not self.exists: |
| 45 | self.create() |
| 46 | self.activate() |
| 47 | |
| 48 | def install(self, *requirements): |
| 49 | call(self.pip_path, "install", *requirements) |
| 50 | |
| 51 | def install_requirements(self, requirements_path): |
| 52 | call(self.pip_path, "install", "-r", requirements_path) |