libinstaller/lib/installer.py

113 lines
4.3 KiB
Python
Raw Permalink Normal View History

import os
import sys
import subprocess
import time
import glob
import shutil
2022-09-22 01:51:47 +02:00
from lib.ui import bordered, underlined, progressbar
2022-09-22 01:51:47 +02:00
def install_lib(lib, src_dir, work_dir, inst_dir, comp_cc, comp_cxx, comp_fc, build_threads, disable_shared_libs, verbose):
2022-09-22 01:51:47 +02:00
print(bordered("Installing " + lib['name'] + " v." + lib['version']))
if not os.path.exists(src_dir):
os.makedirs(src_dir)
if not os.path.exists(work_dir):
os.makedirs(work_dir)
if not os.path.exists(inst_dir):
os.makedirs(inst_dir)
os.chdir(src_dir)
lib_name = lib['name']
lib_version = lib['version']
2022-09-22 01:51:47 +02:00
if not verbose:
logfile_path = work_dir + "/" + lib_name + "-" + lib_version + ".log"
if os.path.exists(logfile_path):
os.remove(logfile_path)
2022-09-22 01:51:47 +02:00
logfile = open(logfile_path, 'a')
else:
logfile = None
# download lib src if not present
if not os.path.exists(lib['name']+"-"+lib['version']):
print("Downloading Source Code")
load_command = lib['download'].replace("$VERSION_UNDERSCORE", lib_version.replace(".", "_")).replace("$VERSION", lib_version)
err = subprocess.call(load_command, stdout=logfile, stderr=logfile, shell=True)
if err != 0:
print("Error: Download of "+lib_name+" v."+lib_version+" failed!")
print("See "+logfile_path+" for details")
sys.exit(0)
2022-09-22 01:51:47 +02:00
# configure library
os.chdir(work_dir)
if os.path.exists(work_dir+"/"+lib_name+"-"+lib_version):
shutil.rmtree(work_dir+"/"+lib_name+"-"+lib_version)
2022-09-22 01:51:47 +02:00
shutil.copytree(src_dir+"/"+lib_name+"-"+lib_version, work_dir+"/"+lib_name+"-"+lib_version)
os.chdir(work_dir+"/"+lib_name+"-"+lib_version)
if disable_shared_libs:
shared_option = "--disable-shared"
else:
shared_option = ""
config_command = lib['configure'].replace("$PREFIX", inst_dir).replace("$SHARED",shared_option)
if verbose:
print(underlined("\nConfiguring Library"))
else:
print("Configuring library...")
2022-09-22 01:51:47 +02:00
err = subprocess.call(config_command, stdout=logfile, stderr=logfile, shell=True)
if err != 0:
print("Error: Configuring of "+lib_name+" v."+lib_version+" failed!")
print("See "+logfile_path+" for details")
sys.exit(1)
# build library
2022-09-22 01:51:47 +02:00
build_command = lib['build'].replace("$BUILDTHREADS", build_threads)
if verbose:
print(underlined("\nBuilding Library"))
2022-09-22 01:51:47 +02:00
err = subprocess.call(build_command, stdout=logfile, stderr=logfile, shell=True)
else:
2022-09-22 01:51:47 +02:00
build_task = subprocess.Popen(build_command, stdout=logfile, stderr=logfile, shell=True)
num_objects = int(lib['object files'])
finished = False
2022-09-22 01:51:47 +02:00
while build_task.poll() is None:
if not finished:
# bar_width = num_objects
# while (bar_width > 100):
# bar_width = int(bar_width/10)
bar_width = 50
2022-09-22 01:51:47 +02:00
for i in progressbar(range(num_objects), "Building library: ", bar_width):
while len(glob.glob('**/*.o', recursive=True)) < i:
time.sleep(0.5)
2022-09-22 01:51:47 +02:00
if build_task.poll is not None:
break
finished = True
build_task.wait()
err = build_task.returncode
if err != 0:
print("Error: Building "+lib_name+" v."+lib_version+" failed!")
print("See "+logfile_path+" for details")
sys.exit(1)
2022-09-22 01:51:47 +02:00
# install library
install_command = lib['install'].replace("$BUILDTHREADS", build_threads)
if verbose:
print(underlined("\nInstalling Library"))
else:
print("Installing library...")
2022-09-22 01:51:47 +02:00
err = subprocess.call(install_command, stdout=logfile, stderr=logfile, shell=True)
if err != 0:
print("Error: Installing "+lib_name+" v."+lib_version+" failed!")
print("See "+logfile_path+" for details")
sys.exit(1)
print("Library "+lib_name+" has been installed successfully!\n")
# copy logfile to install location
if not verbose:
build_output_path = inst_dir+"/"+"build_info"
if not os.path.exists(build_output_path):
os.makedirs(build_output_path)
shutil.copyfile(logfile_path, build_output_path+"/"+lib_name+".log")