Merge pull request #748 from minrk/pipfile-toml

parse pipfiles to determine Python version
pull/752/head
Tim Head 2019-07-18 21:48:35 +02:00 zatwierdzone przez GitHub
commit 74af2ad83c
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 36 dodań i 27 usunięć

Wyświetl plik

@ -1,12 +1,20 @@
"""Buildpack for git repos with Pipfile.lock or Pipfile within them. `pipenv` """Buildpack for git repos with Pipfile.lock or Pipfile
will be used to install the dependencies but we will manually install declared
Python versions instead of using PyEnv."""
`pipenv` will be used to install the dependencies
conda will provide the base Python environment,
same as the Python or Conda build packs.
"""
import json
import os import os
import re import re
import toml
from ..conda import CondaBuildPack from ..conda import CondaBuildPack
VERSION_PAT = re.compile(r"\d+(\.\d+)*")
class PipfileBuildPack(CondaBuildPack): class PipfileBuildPack(CondaBuildPack):
"""Setup Python with pipfile for use with a repository.""" """Setup Python with pipfile for use with a repository."""
@ -23,38 +31,39 @@ class PipfileBuildPack(CondaBuildPack):
return self._python_version return self._python_version
files_to_search_in_order = [ files_to_search_in_order = [
{ self.binder_path("Pipfile.lock"),
"path": self.binder_path("Pipfile.lock"), self.binder_path("Pipfile"),
"pattern": r"\s*[\",\']python_(?:full_)?version[\",\']: [\",\']?([0-9a-z\.]*)[\",\']?", # ' "python_version": "3.6"'
},
{
"path": self.binder_path("Pipfile"),
"pattern": r"python_(?:full_)?version\s*=+\s*[\",\']?([0-9a-z\.]*)[\",\']?", # 'python_version = "3.6"'
},
{
"path": self.binder_path("runtime.txt"),
"pattern": r"\s*python-([0-9a-z\.]*)\s*", # 'python-3.6'
},
] ]
lockfile = self.binder_path("Pipfile.lock")
requires_sources = []
if os.path.exists(lockfile):
with open(lockfile) as f:
lock_info = json.load(f)
requires_sources.append(lock_info.get("_meta", {}).get("requires", {}))
pipfile = self.binder_path("Pipfile")
if os.path.exists(pipfile):
with open(pipfile) as f:
pipfile_info = toml.load(f)
requires_sources.append(pipfile_info.get("requires", {}))
py_version = None py_version = None
for file in files_to_search_in_order: for requires in requires_sources:
try: for key in ("python_full_version", "python_version"):
with open(file["path"]) as f: version_str = requires.get(key, None)
for line in f: if version_str:
match = re.match(file["pattern"], line) match = VERSION_PAT.match(version_str)
if not match: if match:
continue py_version = match.group()
py_version = match.group(1) if py_version:
break break
except FileNotFoundError:
pass
if py_version: if py_version:
break break
# extract major.minor # extract major.minor
if py_version: if py_version:
if len(py_version) == 1: if len(py_version.split(".")) == 1:
self._python_version = self.major_pythons.get(py_version[0]) self._python_version = self.major_pythons.get(py_version[0])
else: else:
# return major.minor # return major.minor