Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Always Try to Install psutil #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pyperformance/_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
OLD_SETUPTOOLS = '18.5'


def get_pkg_name(req):
"""Return the name of the package in the given requirement text."""
# strip env markers
req = req.partition(';')[0]
# strip version
req = req.partition('==')[0]
req = req.partition('>=')[0]
return req


def get_best_pip_version(python):
"""Return the pip to install for the given Python executable."""
if not python or isinstance(python, str):
Expand Down
60 changes: 24 additions & 36 deletions pyperformance/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@


REQUIREMENTS_FILE = os.path.join(pyperformance.DATA_DIR, 'requirements.txt')
PYPERF_OPTIONAL = ['psutil']


class Requirements(object):

@classmethod
def from_file(cls, filename, optional=None):
def from_file(cls, filename):
self = cls()
self._add_from_file(filename, optional)
self._add_from_file(filename)
return self

@classmethod
Expand All @@ -32,44 +33,25 @@ def __init__(self):
# requirements
self.specs = []

# optional requirements
self._optional = set()

def __len__(self):
return len(self.specs)

def iter_non_optional(self):
for spec in self.specs:
if spec in self._optional:
continue
yield spec

def iter_optional(self):
def __iter__(self):
for spec in self.specs:
if spec not in self._optional:
continue
yield spec

def _add_from_file(self, filename, optional=None):
def _add_from_file(self, filename):
if not os.path.exists(filename):
return
for line in _utils.iter_clean_lines(filename):
self._add(line, optional)
self._add(line)

def _add(self, line, optional=None):
def _add(self, line):
self.specs.append(line)
if optional:
# strip env markers
req = line.partition(';')[0]
# strip version
req = req.partition('==')[0]
req = req.partition('>=')[0]
if req in optional:
self._optional.add(line)

def get(self, name):
for req in self.specs:
if req.startswith(name):
if _pip.get_pkg_name(req) == name:
return req
return None

Expand Down Expand Up @@ -186,8 +168,10 @@ def install_pyperformance(self):
print("installing pyperformance in the venv at %s" % self.root)
# Install pyperformance inside the virtual environment.
if pyperformance.is_dev():
basereqs = Requirements.from_file(REQUIREMENTS_FILE, ['psutil'])
basereqs = Requirements.from_file(REQUIREMENTS_FILE)
self.ensure_reqs(basereqs)
if basereqs.get('pyperf'):
self._install_pyperf_optional_dependencies()

root_dir = os.path.dirname(pyperformance.PKG_ROOT)
ec, _, _ = _pip.install_editable(
Expand All @@ -202,9 +186,18 @@ def install_pyperformance(self):
python=self.info,
env=self._env,
)
self._install_pyperf_optional_dependencies()
if ec != 0:
sys.exit(ec)

def _install_pyperf_optional_dependencies(self):
for req in PYPERF_OPTIONAL:
try:
self.ensure_reqs([req])
except _venv.RequirementsInstallationFailedError:
print("WARNING: failed to install %s" % req)
pass

def ensure_reqs(self, requirements=None, *, exitonerror=False):
# parse requirements
bench = None
Expand All @@ -216,7 +209,7 @@ def ensure_reqs(self, requirements=None, *, exitonerror=False):

# Every benchmark must depend on pyperf.
if bench is not None and not requirements.get('pyperf'):
basereqs = Requirements.from_file(REQUIREMENTS_FILE, ['psutil'])
basereqs = Requirements.from_file(REQUIREMENTS_FILE)
pyperf_req = basereqs.get('pyperf')
if not pyperf_req:
raise NotImplementedError
Expand All @@ -229,21 +222,16 @@ def ensure_reqs(self, requirements=None, *, exitonerror=False):
# install requirements
try:
super().ensure_reqs(
*requirements.iter_non_optional(),
*requirements,
upgrade=False,
)
except _venv.RequirementsInstallationFailedError:
if exitonerror:
sys.exit(1)
raise # re-raise

# install optional requirements
for req in requirements.iter_optional():
try:
super().ensure_reqs(req, upgrade=True)
except _venv.RequirementsInstallationFailedError:
print("WARNING: failed to install %s" % req)
print()
if bench is not None:
self._install_pyperf_optional_dependencies()

# Dump the package list and their versions: pip freeze
_pip.run_pip('freeze', python=self.python, env=self._env)
Expand Down