forked from LiveCarta/BookConverter
add checking for packages
This commit is contained in:
42
src/util/check_packs.py
Normal file
42
src/util/check_packs.py
Normal 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.')
|
||||
Reference in New Issue
Block a user