[aur] generating the optimized makepkg file if it is not found during an installation

This commit is contained in:
Vinícius Moreira
2019-12-18 19:13:02 -03:00
parent 98ea92bc1f
commit ce1f30365d
11 changed files with 92 additions and 79 deletions

View File

@@ -8,7 +8,7 @@ from threading import Thread
import requests
from bauh.api.abstract.context import ApplicationContext
from bauh.gems.arch import pacman, disk, CUSTOM_MAKEPKG_PATH, CONFIG_DIR, BUILD_DIR, \
from bauh.gems.arch import pacman, disk, CUSTOM_MAKEPKG_FILE, CONFIG_DIR, BUILD_DIR, \
AUR_INDEX_FILE, config
URL_INDEX = 'https://aur.archlinux.org/packages.gz'
@@ -75,74 +75,77 @@ class ArchCompilationOptimizer(Thread):
super(ArchCompilationOptimizer, self).__init__(daemon=True)
self.logger = logger
def optimize(self):
try:
ncpus = os.cpu_count()
except:
self.logger.error('Could not determine the number of processors. Aborting...')
ncpus = None
if os.path.exists(GLOBAL_MAKEPKG):
self.logger.info("Verifying if it is possible to optimize Arch packages compilation")
with open(GLOBAL_MAKEPKG) as f:
global_makepkg = f.read()
Path(CONFIG_DIR).mkdir(parents=True, exist_ok=True)
custom_makepkg, optimizations = None, []
if ncpus:
makeflags = RE_MAKE_FLAGS.findall(global_makepkg)
if makeflags:
not_commented = [f for f in makeflags if not f.startswith('#')]
if not not_commented:
custom_makepkg = RE_MAKE_FLAGS.sub('', global_makepkg)
optimizations.append('MAKEFLAGS="-j$(nproc)"')
else:
self.logger.warning("It seems '{}' compilation flags are already customized".format(GLOBAL_MAKEPKG))
else:
optimizations.append('MAKEFLAGS="-j$(nproc)"')
compress_xz = RE_COMPRESS_XZ.findall(custom_makepkg if custom_makepkg else global_makepkg)
if compress_xz:
not_eligible = [f for f in compress_xz if not f.startswith('#') and '--threads' in f]
if not not_eligible:
custom_makepkg = RE_COMPRESS_XZ.sub('', global_makepkg)
optimizations.append('COMPRESSXZ=(xz -c -z - --threads=0)')
else:
self.logger.warning("It seems '{}' COMPRESSXZ is already customized".format(GLOBAL_MAKEPKG))
else:
optimizations.append('COMPRESSXZ=(xz -c -z - --threads=0)')
if optimizations:
generated_by = '# <generated by bauh>\n'
custom_makepkg = generated_by + custom_makepkg + '\n' + generated_by + '\n'.join(optimizations) + '\n'
with open(CUSTOM_MAKEPKG_FILE, 'w+') as f:
f.write(custom_makepkg)
self.logger.info("A custom optimized 'makepkg.conf' was generated at '{}'".format(CUSTOM_MAKEPKG_FILE))
else:
self.logger.info("No optimizations are necessary")
if os.path.exists(CUSTOM_MAKEPKG_FILE):
self.logger.info("Removing old optimized 'makepkg.conf' at '{}'".format(CUSTOM_MAKEPKG_FILE))
os.remove(CUSTOM_MAKEPKG_FILE)
self.logger.info('Finished')
def run(self):
local_config = config.read_config(update_file=True)
if not local_config['optimize']:
self.logger.info("Arch packages compilation optimizations are disabled")
if os.path.exists(CUSTOM_MAKEPKG_PATH):
self.logger.info("Removing custom 'makepkg.conf' -> '{}'".format(CUSTOM_MAKEPKG_PATH))
os.remove(CUSTOM_MAKEPKG_PATH)
if os.path.exists(CUSTOM_MAKEPKG_FILE):
self.logger.info("Removing custom 'makepkg.conf' -> '{}'".format(CUSTOM_MAKEPKG_FILE))
os.remove(CUSTOM_MAKEPKG_FILE)
self.logger.info('Finished')
else:
try:
ncpus = os.cpu_count()
except:
self.logger.error('Could not determine the number of processors. Aborting...')
ncpus = None
if os.path.exists(GLOBAL_MAKEPKG):
self.logger.info("Verifying if it is possible to optimize Arch packages compilation")
with open(GLOBAL_MAKEPKG) as f:
global_makepkg = f.read()
Path(CONFIG_DIR).mkdir(parents=True, exist_ok=True)
custom_makepkg, optimizations = None, []
if ncpus:
makeflags = RE_MAKE_FLAGS.findall(global_makepkg)
if makeflags:
not_commented = [f for f in makeflags if not f.startswith('#')]
if not not_commented:
custom_makepkg = RE_MAKE_FLAGS.sub('', global_makepkg)
optimizations.append('MAKEFLAGS="-j$(nproc)"')
else:
self.logger.warning("It seems '{}' compilation flags are already customized".format(GLOBAL_MAKEPKG))
else:
optimizations.append('MAKEFLAGS="-j$(nproc)"')
compress_xz = RE_COMPRESS_XZ.findall(custom_makepkg if custom_makepkg else global_makepkg)
if compress_xz:
not_eligible = [f for f in compress_xz if not f.startswith('#') and '--threads' in f]
if not not_eligible:
custom_makepkg = RE_COMPRESS_XZ.sub('', global_makepkg)
optimizations.append('COMPRESSXZ=(xz -c -z - --threads=0)')
else:
self.logger.warning("It seems '{}' COMPRESSXZ is already customized".format(GLOBAL_MAKEPKG))
else:
optimizations.append('COMPRESSXZ=(xz -c -z - --threads=0)')
if optimizations:
generated_by = '# <generated by bauh>\n'
custom_makepkg = generated_by + custom_makepkg + '\n' + generated_by + '\n'.join(optimizations) + '\n'
with open(CUSTOM_MAKEPKG_PATH, 'w+') as f:
f.write(custom_makepkg)
self.logger.info("A custom optimized 'makepkg.conf' was generated at '{}'".format(CUSTOM_MAKEPKG_PATH))
else:
self.logger.info("No optimizations are necessary")
if os.path.exists(CUSTOM_MAKEPKG_PATH):
self.logger.info("Removing old optimized 'makepkg.conf' at '{}'".format(CUSTOM_MAKEPKG_PATH))
os.remove(CUSTOM_MAKEPKG_PATH)
self.logger.info('Finished')
self.optimize()