Created
April 15, 2017 00:40
-
-
Save aballano/eb52be71dfd69d8171c829d872343124 to your computer and use it in GitHub Desktop.
Functions to install required dependencies for a python script.
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
#!/usr/bin/env python | |
import subprocess | |
import sys | |
def run_command(command): | |
return subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() | |
def get_dependencies(): | |
return run_command("pip list --format=columns") | |
def check_pip(): | |
result = run_command("easy_install") | |
if "command not found" not in result: | |
print "easy_install found, installing pip..." | |
run_command("sudo easy_install pip") | |
return | |
print "No quick tool found to install dependencies. Please check if you have Python 2.7 installed and then " \ | |
"install Brew (https://brew.sh/) and run `brew install python`." | |
print "Alternatively, if Python is installed, consider manually installing pip as described in " \ | |
"https://pip.readthedocs.io/en/stable/installing/" | |
def upgrade_dependency(dependency): | |
run_command("pip install {} -U".format(dependency)) | |
print "Done." | |
def check_dependencies(dependency, dependency_version): | |
result = get_dependencies() | |
# Check pip | |
lib_version = None | |
if "command not found" in result: | |
print "Pip not found" | |
check_pip() | |
result = get_dependencies() | |
if "command not found" in result: | |
sys.exit(2) | |
for line in result.splitlines(): | |
if dependency in line: | |
lib_version = line | |
# Check lib installed | |
if lib_version is None: | |
print "No version of {} found, installing...".format(dependency) | |
upgrade_dependency(dependency) | |
# Check lib updated | |
elif lib_version.split()[1] != dependency_version: | |
print "Wrong version of {} found: {}, upgrading...".format(dependency, lib_version.split()[1]) | |
upgrade_dependency(dependency) | |
else: | |
print dependency + " up to date." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment