Last active
August 22, 2019 20:05
-
-
Save aegis1980/256388312e02737979af5895434f7ddb to your computer and use it in GitHub Desktop.
Programmatic parsing of a requirements file and importing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import sys | |
from pip import _internal as pip #this seems a little dangerous. as-off | |
DEBUG = True | |
def myprint(string_to_print): | |
if DEBUG: | |
print(string_to_print) | |
class PackageImporter: | |
def __init__(self,app_name): | |
self._appname= app_name | |
self._pip_session = app_name +"session" | |
# from here: https://stackoverflow.com/questions/1051254/check-if-python-package-is-installed | |
reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze']) | |
self._installed_packages = [r.decode().split('==')[0] for r in reqs.split()] | |
self._incoming_reqs = [] | |
def run_me(self, incoming_req_txt_file): | |
self.__parse_requirements_file(incoming_req_txt_file) | |
for pkg in self._incoming_reqs: | |
self.__install_package(pkg) | |
def __parse_requirements_file(self,req_file): | |
for item in pip.req.parse_requirements(req_file, session=self._pip_session): | |
myprint("") | |
if isinstance(item, pip.req.InstallRequirement): | |
myprint("required package: {}".format(item.name)) | |
self._incoming_reqs.append(item.name) | |
if len(str(item.req.specifier)) > 0: | |
myprint(" " + str(item.req.specifier)) | |
if item.link is not None: | |
myprint(" from: " + item.link.url) | |
myprint(" filename: " + item.link.filename) | |
myprint(" egg: " + item.link.egg_fragment) | |
if len(item.options) > 0: | |
for opt_type,opts in item.options.iteritems(): | |
myprint(" {}:".format(opt_type)) | |
if type(opts) is list: | |
for opt in opts: | |
myprint(" " + opt) | |
elif type(opts) is dict: | |
for k,v in opts.iteritems(): | |
myprint(" {}: {}".format(k,v)) | |
def __install_package(self,package): | |
# see here : https://stackoverflow.com/questions/1051254/check-if-python-package-is-installed | |
if package not in self._installed_packages: | |
# see here http://jelly.codes/articles/python-pip-module/ | |
try: | |
pip.main(["install", package]) | |
#might be better doing this in separate process with 'subprocess' | |
except SystemExit as e: | |
pass | |
else: | |
myprint("required package: {} is already on the system :)".format(package)) | |
pkg_importer = PackageImporter('myapp') | |
pkg_importer.run_me('requirements_new.txt') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment