def _cache_categories_to_disk(self, categories_str: str, timestamp: float): self.logger.info(self._msg('Caching downloaded categories to disk')) try: Path(os.path.dirname(self.categories_path)).mkdir(parents=True, exist_ok=True) with open(self.categories_path, 'w+') as f: f.write(categories_str) self.logger.info( self._msg("Categories cached to file '{}'".format( self.categories_path))) categories_ts_path = map_timestamp_file(self.categories_path) with open(categories_ts_path, 'w+') as f: f.write(str(timestamp)) self.logger.info( self._msg( "Categories timestamp ({}) cached to file '{}'".format( timestamp, categories_ts_path))) except: self.logger.error( self._msg( "Could not cache categories to the disk as '{}'".format( self.categories_path))) traceback.print_exc()
def __init__(self, http_client: HttpClient, logger: Logger, i18n: I18n, file_url: Optional[str]): self.http_client = http_client self.logger = logger self.i18n = i18n if file_url: self._file_url = file_url else: self._file_url = "https://raw.githubusercontent.com/vinifmor/bauh-files/master/web/env/v2/suggestions.yml" self._cached_file_path = f'{WEB_CACHE_DIR}/suggestions.yml' self._cached_file_ts_path = map_timestamp_file(self._cached_file_path)
ENV_PATH = '{}/env'.format(WEB_PATH) FIXES_PATH = '{}/fixes'.format(WEB_PATH) NODE_DIR_PATH = '{}/node'.format(ENV_PATH) NODE_PATHS = {NODE_DIR_PATH + '/bin'} NODE_BIN_PATH = '{}/bin/node'.format(NODE_DIR_PATH) NPM_BIN_PATH = '{}/bin/npm'.format(NODE_DIR_PATH) NODE_MODULES_PATH = '{}/node_modules'.format(ENV_PATH) NATIVEFIER_BIN_PATH = '{}/.bin/nativefier'.format(NODE_MODULES_PATH) ELECTRON_PATH = '{}/.cache/electron'.format(str(Path.home())) ELECTRON_DOWNLOAD_URL = 'https://github.com/electron/electron/releases/download/v{version}/electron-v{version}-linux-{arch}.zip' ELECTRON_SHA256_URL = 'https://github.com/electron/electron/releases/download/v{version}/SHASUMS256.txt' ELECTRON_WIDEVINE_URL = 'https://github.com/castlabs/electron-releases/releases/download/v{version}-wvvmp/electron-v{version}-wvvmp-linux-{arch}.zip' ELECTRON_WIDEVINE_SHA256_URL = 'https://github.com/castlabs/electron-releases/releases/download/v{version}-wvvmp/SHASUMS256.txt' URL_ENVIRONMENT_SETTINGS = 'https://raw.githubusercontent.com/vinifmor/bauh-files/master/web/env/v1/environment.yml' DESKTOP_ENTRY_PATH_PATTERN = DESKTOP_ENTRIES_DIR + '/bauh.web.{name}.desktop' URL_FIX_PATTERN = "https://raw.githubusercontent.com/vinifmor/bauh-files/master/web/fix/{url}.js" URL_SUGGESTIONS = "https://raw.githubusercontent.com/vinifmor/bauh-files/master/web/env/v1/suggestions.yml" UA_CHROME = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36' TEMP_PATH = '{}/web'.format(TEMP_DIR) SEARCH_INDEX_FILE = '{}/index.yml'.format(WEB_CACHE_PATH) SUGGESTIONS_CACHE_FILE = '{}/suggestions.yml'.format(WEB_CACHE_PATH) SUGGESTIONS_CACHE_TS_FILE = map_timestamp_file(SUGGESTIONS_CACHE_FILE) CONFIG_FILE = '{}/web.yml'.format(CONFIG_PATH) URL_NATIVEFIER = 'https://github.com/jiahaog/nativefier/archive/v{version}.tar.gz' ENVIRONMENT_SETTINGS_CACHED_FILE = '{}/environment.yml'.format(WEB_CACHE_PATH) ENVIRONMENT_SETTINGS_TS_FILE = '{}/environment.ts'.format(WEB_CACHE_PATH) def get_icon_path() -> str: return resource.get_path('img/web.svg', ROOT_DIR)
def should_download(self) -> bool: if self.internet_connection is False or ( self.internet_connection is None and not self.internet_checker.is_available()): self.logger.warning( self._msg( "No internet connection. The categories file '{}' cannot be updated." .format(self.categories_path))) return False if self.expiration is None or self.expiration <= 0: self.logger.warning( self._msg( "No expiration set for the categories file '{}'. It should be downloaded" .format(self.categories_path))) return True if not os.path.exists(self.categories_path): self.logger.warning( self._msg( "Categories file '{}' does not exist. It should be downloaded." .format(self.categories_path))) return True categories_ts_path = map_timestamp_file(self.categories_path) if not os.path.exists(categories_ts_path): self.logger.warning( self._msg( "Categories timestamp file '{}' does not exist. The categories file should be re-downloaded." .format(categories_ts_path))) return True with open(categories_ts_path) as f: timestamp_str = f.read() try: categories_timestamp = datetime.fromtimestamp(float(timestamp_str)) except: self.logger.error( self._msg( "An exception occurred when trying to parse the categories file timestamp from '{}'. The categories file should be re-downloaded." .format(categories_ts_path))) traceback.print_exc() return True should_download = ( categories_timestamp + timedelta(hours=self.expiration) <= datetime.utcnow()) if should_download: self.logger.info( self._msg( "Cached categories file '{}' has expired. A new one should be downloaded." .format(self.categories_path))) return True else: self.logger.info( self._msg( "Cached categories file '{}' is up to date. No need to re-download it." .format(self.categories_path))) return False