Skip to content

Instantly share code, notes, and snippets.

@chicks-net
Created May 20, 2019 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chicks-net/76dab50c9f10564f5560e024a52756b2 to your computer and use it in GitHub Desktop.
Save chicks-net/76dab50c9f10564f5560e024a52756b2 to your computer and use it in GitHub Desktop.
Verify that a pipenv works
#!/usr/bin/env python3
"""
Verify that a pipenv works with all of the top-level packages defined in it.
Invocation: python3 check_pipenv.py DIRNAME
It will go to DIRNAME, look for a Pipfile and a .venv and proceed to test
all of the non-development dependancies.
"""
# standard
import sys # argv
import os # chdir
import subprocess # run
import pprint # pprint
import argparse # ArgumentParser
# not standard
import toml # load
# package map
PKG_MAP = {
"avro-python3": "avro",
"confluent-kafka": "confluent_kafka",
"mem-top": "mem_top",
"nested-lookup": "nested_lookup",
"pyyaml": "yaml",
"pyzmq": "zmq",
"onvif-zeep": "onvif",
"pyopenssl": "OpenSSL",
}
# function
def check_pipenv():
"""Make sure all of the expected python modules work here."""
# read Pipfile
if not os.path.exists("Pipfile"):
print("no Pipfile, exiting")
sys.exit(1)
pipfile = toml.load("Pipfile")
# check that virtualenv is here
if not os.path.exists(".venv"):
print("no pipenv, exiting")
sys.exit(2)
fail_packages = []
for package in pipfile["packages"]:
# renames
if package in PKG_MAP.keys():
package = PKG_MAP[package]
completed = subprocess.run('pipenv run python -c "import %s"' % package, shell=True)
if completed.returncode > 0:
print("%s returncode: %d" % (package, completed.returncode))
fail_packages.append(package)
if fail_packages:
printer = pprint.PrettyPrinter(indent=4)
printer.pprint(fail_packages)
sys.exit(3)
else:
print("no failed packages, yay!")
sys.exit(0)
# process args
PARSER = argparse.ArgumentParser()
PARSER.add_argument("py_dir", help="directory with python project and Pipfile.lock")
ARGS = PARSER.parse_args()
print("going to " + str(ARGS.py_dir) + " ...")
os.chdir(ARGS.py_dir)
check_pipenv()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment