Created
August 13, 2018 11:58
-
-
Save cecedille1/737a877ac4b115761040cc4eafea8d72 to your computer and use it in GitHub Desktop.
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 sys | |
import shlex | |
import os | |
import os.path | |
import argparse | |
import base64 | |
import subprocess | |
SHAKE_ROOT = os.path.join(os.path.dirname(__file__), '.shake') | |
parser = argparse.ArgumentParser() | |
parser.add_argument('requirements', type=open) | |
parser.add_argument('command') | |
options = parser.parse_args() | |
try: | |
os.mkdir(SHAKE_ROOT) | |
except FileExistsError: | |
pass | |
candidates = [] | |
lines = list(options.requirements) | |
command = shlex.split(options.command) | |
for i, removed in enumerate(lines): | |
dep_name = removed.strip() | |
if '#shake: skip' in dep_name: | |
continue | |
if not dep_name or dep_name.startswith('#'): | |
continue | |
name = base64.b64encode(removed.encode('utf-8')).decode('utf-8') | |
base_dir = os.path.join(SHAKE_ROOT, 'd-' + name.rstrip('=')) | |
req_lines = list(lines) | |
req_lines.pop(i) | |
try: | |
os.mkdir(base_dir) | |
except FileExistsError: | |
pass | |
with open(os.path.join(base_dir, 'missing.txt'), 'w') as missing: | |
missing.write(removed) | |
with open(os.path.join(base_dir, 'requirements.txt'), 'w') as requirements: | |
requirements.write(''.join(req_lines)) | |
candidates.append((base_dir, dep_name)) | |
for candidate, removed in candidates: | |
print('Examine', removed, 'in', candidate) | |
venv_path = os.path.join(candidate, 'env') | |
subprocess.run([sys.executable, '-m', 'virtualenv', venv_path], check=True, stdout=subprocess.DEVNULL) | |
subprocess.run([ | |
os.path.join(venv_path, 'bin/python'), | |
'-m', 'pip', 'install', | |
# '--extra-index-url', 'https://dana.weezevent.net/pypi/...', | |
'-r', os.path.join(candidate, 'requirements.txt'), | |
], check=True, stdout=subprocess.DEVNULL) | |
print('Run') | |
env = os.environ.copy() | |
env.update({ | |
'PATH': '{}/bin/:{}'.format(venv_path, os.environ['PATH']), | |
}) | |
check = subprocess.run(command, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
print('Result', check.returncode) | |
with open(os.path.join(candidate, 'unrequired' if check.returncode == 0 else 'required'), 'w') as fate: | |
fate.write(removed + '\n') | |
with open(os.path.join(candidate, 'check-stdout'), 'wb') as stdout: | |
stdout.write(check.stdout) | |
with open(os.path.join(candidate, 'check-stderr'), 'wb') as stderr: | |
stderr.write(check.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment