mirror of
https://github.com/kennethreitz/pipenv.git
synced 2026-06-05 22:50:18 +00:00
Replace test PYPI_VENDOR_DIR envvar with explicit package prep
This commit is contained in:
@@ -9,6 +9,7 @@ from pipenv.vendor import delegator
|
||||
from pipenv.vendor import requests
|
||||
from pipenv.vendor import six
|
||||
from pipenv.vendor import toml
|
||||
from pytest_pypi.app import prepare_packages as prepare_pypi_packages
|
||||
|
||||
if six.PY2:
|
||||
class ResourceWarning(Warning):
|
||||
@@ -30,6 +31,8 @@ def check_internet():
|
||||
WE_HAVE_INTERNET = check_internet()
|
||||
|
||||
TESTS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
PYPI_VENDOR_DIR = os.path.join(TESTS_ROOT, 'pypi')
|
||||
prepare_pypi_packages(PYPI_VENDOR_DIR)
|
||||
|
||||
|
||||
def pytest_runtest_setup(item):
|
||||
@@ -68,7 +71,6 @@ class _PipenvInstance(object):
|
||||
os.environ['PIPENV_DONT_USE_PYENV'] = '1'
|
||||
os.environ['PIPENV_IGNORE_VIRTUALENVS'] = '1'
|
||||
os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
|
||||
os.environ['PYPI_VENDOR_DIR'] = os.path.join(TESTS_ROOT, 'pypi')
|
||||
if self.chdir:
|
||||
os.chdir(self.path)
|
||||
return self
|
||||
|
||||
@@ -4,9 +4,6 @@ import json
|
||||
import requests
|
||||
from flask import Flask, redirect, abort, render_template, send_file, jsonify
|
||||
|
||||
PYPI_VENDOR_DIR = os.environ.get('PYPI_VENDOR_DIR', './pypi')
|
||||
PYPI_VENDOR_DIR = os.path.abspath(PYPI_VENDOR_DIR)
|
||||
|
||||
app = Flask(__name__)
|
||||
session = requests.Session()
|
||||
|
||||
@@ -14,41 +11,49 @@ packages = {}
|
||||
|
||||
|
||||
class Package(object):
|
||||
"""docstring for Package"""
|
||||
"""Package represents a collection of releases from one or more directories"""
|
||||
|
||||
def __init__(self, name):
|
||||
super(Package, self).__init__()
|
||||
self.name = name
|
||||
self._releases = []
|
||||
self.releases = {}
|
||||
self._package_dirs = set()
|
||||
|
||||
@property
|
||||
def releases(self):
|
||||
r = []
|
||||
for release in self._releases:
|
||||
release = release[len(PYPI_VENDOR_DIR):].replace('\\', '/')
|
||||
r.append(release)
|
||||
return r
|
||||
def json(self):
|
||||
for path in self._package_dirs:
|
||||
try:
|
||||
with open(os.path.join(path, 'api.json')) as f:
|
||||
return json.load(f)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
def __repr__(self):
|
||||
return "<Package name={0!r} releases={1!r}".format(self.name, len(self.releases))
|
||||
|
||||
def add_release(self, path_to_binary):
|
||||
self._releases.append(path_to_binary)
|
||||
path_to_binary = os.path.abspath(path_to_binary)
|
||||
path, release = os.path.split(path_to_binary)
|
||||
self.releases[release] = path_to_binary
|
||||
self._package_dirs.add(path)
|
||||
|
||||
|
||||
def prepare_packages():
|
||||
for root, dirs, files in os.walk(os.path.abspath(PYPI_VENDOR_DIR)):
|
||||
def prepare_packages(path):
|
||||
"""Add packages in path to the registry."""
|
||||
path = os.path.abspath(path)
|
||||
if not (os.path.exists(path) and os.path.isdir(path)):
|
||||
raise ValueError("{} is not a directory!".format(path))
|
||||
if not (os.path.exists(path) and os.path.isdir(path)):
|
||||
raise ValueError("{} is not a directory!".format(path))
|
||||
for root, dirs, files in os.walk(path):
|
||||
for file in files:
|
||||
if not file.startswith('.') and not file.endswith('.json'):
|
||||
package_name = root.split(os.path.sep)[-1]
|
||||
package_name = os.path.basename(root)
|
||||
|
||||
if package_name not in packages:
|
||||
packages[package_name] = Package(package_name)
|
||||
|
||||
packages[package_name].add_release(os.path.sep.join([root, file]))
|
||||
|
||||
|
||||
prepare_packages()
|
||||
packages[package_name].add_release(os.path.join(root, file))
|
||||
|
||||
|
||||
@app.route('/')
|
||||
@@ -74,22 +79,26 @@ def serve_package(package, release):
|
||||
if package in packages:
|
||||
package = packages[package]
|
||||
|
||||
for _release in package.releases:
|
||||
if _release.endswith(release):
|
||||
return send_file(os.path.sep.join([PYPI_VENDOR_DIR, _release]))
|
||||
if release in package.releases:
|
||||
return send_file(package.releases[release])
|
||||
|
||||
abort(404)
|
||||
|
||||
|
||||
@app.route('/pypi/<package>/json')
|
||||
def json_for_package(package):
|
||||
try:
|
||||
with open(os.path.sep.join([PYPI_VENDOR_DIR, package, 'api.json'])) as f:
|
||||
return jsonify(json.load(f))
|
||||
return jsonify(packages[package].json)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
r = session.get('https://pypi.org/pypi/{0}/json'.format(package))
|
||||
return jsonify(r.json())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
PYPI_VENDOR_DIR = os.environ.get('PYPI_VENDOR_DIR', './pypi')
|
||||
PYPI_VENDOR_DIR = os.path.abspath(PYPI_VENDOR_DIR)
|
||||
prepare_packages(PYPI_VENDOR_DIR)
|
||||
|
||||
app.run()
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
<body>
|
||||
<h1>Links for {{ package.name }}</h1>
|
||||
{% for release in package.releases %}
|
||||
<a href="{{ release }}">{{ release }}</a>
|
||||
<a href="/{{ package.name }}/{{ release }}">{{ release }}</a>
|
||||
<br>
|
||||
{% endfor %}
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user