add checking for packages

This commit is contained in:
Jeniamakarchik
2020-02-25 16:04:56 +03:00
parent e15dc9cd30
commit 9a89c0bb37
2 changed files with 43 additions and 1 deletions

View File

@@ -14,4 +14,4 @@ COPY . /app/
WORKDIR /app/
VOLUME /app/logs
CMD python /app/src/util/check_dirs.py && python /app/src/consumer.py
CMD python /app/src/util/check_packs.py && python /app/src/util/check_dirs.py && python /app/src/consumer.py

42
src/util/check_packs.py Normal file
View File

@@ -0,0 +1,42 @@
import argparse
import os
import subprocess
import sys
def parse_args():
parser = argparse.ArgumentParser(description="Utility for checking installed packages.")
parser.add_argument('-p', '--packages', type=str, nargs='*', help='Names of the packages.')
args = parser.parse_args()
return args
def check_packages(required_packs):
inst = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
installed_packages = [r.decode().split('==')[0] for r in inst.split()]
to_be_installed = []
for package in required_packs:
if package not in installed_packages:
to_be_installed.append(package)
return to_be_installed
if __name__ == '__main__':
required_packs = parse_args().packages
if not required_packs:
folder_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
req_path = os.path.join(folder_path, 'requirements.txt')
with open(req_path, 'r') as f:
packs = f.readlines()
required_packs = [pack.split('>=')[0] for pack in packs]
not_inst_packs = check_packages(required_packs)
if not_inst_packs:
raise Exception(f'{" ".join(not_inst_packs)} are not installed.')
else:
print('All required packages has been installed.')