def warning(self, text): """ Convenient shortcut to main EDLM LOGGER """ if self.index_file: LOGGER.warning(f'"{self.index_file}": {text}') else: LOGGER.warning(f'"{self.source_folder}": {text}')
def cli(debug): """ Command line interface """ debug = debug or CFG.debug if debug: elib.custom_logging.set_handler_level('EDLM', 'ch', 'debug') else: elib.custom_logging.set_handler_level('EDLM', 'ch', 'info') LOGGER.info(__version__) PANDOC.setup() MIKTEX.setup()
def __init__(self, archive: STR_OR_PATH = None, install_dir: STR_OR_PATH = None): self.__check_values() if archive is None: archive = self.default_archive if install_dir is None: install_dir = self.default_install self.archive = elib.path.ensure_file(archive, must_exist=False).absolute() self.install_dir = elib.path.ensure_dir(install_dir, must_exist=False).absolute() LOGGER.debug(f'{self.__class__.__name__}: archive: {self.archive}') LOGGER.debug( f'{self.__class__.__name__}: install dir: {self.install_dir}') self._exe = None self._version = None
def make_pdf(ctx: Context, source_folder: Path): """ Makes a PDF document from a source folder Args: ctx: Context source_folder: source folder """ _remove_artifacts() source_folder = elib.path.ensure_dir(source_folder).absolute() LOGGER.info(f'analyzing folder: "{source_folder}"') if _is_source_folder(source_folder): ctx.source_folder = source_folder _build_folder(ctx) else: for child in source_folder.iterdir(): if _is_source_folder(child): ctx.source_folder = child.absolute() _build_folder(ctx)
def _download(self) -> bool: if self._archive_is_correct(): LOGGER.debug( '{self.__class__.__name__}: archive already downloaded') return True LOGGER.debug( f'{self.__class__.__name__}: downloading: {self.url} -> {self.archive}' ) if elib.downloader.download(url=self.url, outfile=self.archive.absolute(), hexdigest=self.hash): LOGGER.debug(f'{self.__class__.__name__}: download successful') return True else: LOGGER.error(f'{self.__class__.__name__}: download failed') exit(-1)
def _extract(self): LOGGER.info( f'{self.__class__.__name__}: extracting (this may take a while)') archive = pyunpack.Archive(self.archive) patool = _find_patool() archive.extractall(directory=self.install_dir, auto_create_dir=True, patool_path=str(patool)) LOGGER.debug(f'{self.__class__.__name__}: removing archive') # self.archive.unlink() LOGGER.info(f'{self.__class__.__name__}: successfully extracted')
def _is_installed(self) -> bool: LOGGER.debug(f'{self.__class__.__name__}: checking installation') if not self.get_exe().exists(): LOGGER.debug(f'{self.__class__.__name__}: executable not found') return False if not self.get_version() == self.expected_version: LOGGER.debug(f'{self.__class__.__name__}: wrong version: ' f'{self.get_version()} ' f'(expected {self.expected_version})') return False return True
def setup(self): """ Setup this tool """ if not self._is_installed(): LOGGER.debug(f'{self.__class__.__name__}: setting up') self._download() self._extract() LOGGER.debug( f'{self.__class__.__name__}: adding to PATH: {self.get_exe().parent.absolute()}' ) os.environ[ 'PATH'] = f'{self.get_exe().parent.absolute()};' + os.environ[ 'PATH'] LOGGER.debug(f'{self.__class__.__name__}: {self.get_version()}')
def __exit__(self, exc_type, exc_val, exc_tb): if not exc_type: if self.ctx.keep_temp_dir: LOGGER.info( f'"{self.ctx.source_folder}": build successful, keeping temp dir: "{self.path}"' ) else: LOGGER.debug( f'"{self.ctx.source_folder}": build successful, removing temp dir' ) shutil.rmtree(self.path) self.ctx.temp_dir = None else: LOGGER.warning( f'"{self.ctx.source_folder}": build failed, keeping temp dir: "{self.path}"' )
def _find_patool(path=sys.executable) -> Path: LOGGER.debug('looking for Patool') python_exe = Path(path).absolute() LOGGER.debug(f'python_exe: {python_exe}') patool_path = Path(python_exe.parent, 'patool') LOGGER.debug(f'looking at {patool_path}') if patool_path.exists(): LOGGER.debug(f'patool found: {patool_path}') return patool_path.absolute() scripts_path = Path(python_exe.parent, 'scripts') if scripts_path.exists() and scripts_path.is_dir(): patool_path = Path(scripts_path, 'patool') LOGGER.debug(f'looking at {patool_path}') if patool_path.exists(): LOGGER.debug(f'patool found: {patool_path}') return patool_path.absolute() raise FileNotFoundError('patool not found')
def __enter__(self): LOGGER.debug( f'"{self.ctx.source_folder}": using temporary folder: "{self.path}"' ) self.ctx.temp_dir = self.path