def ratetest(ratemin: int, token=None): """Tests available Github API rate. :exception ghau.errors.GithubRateLimitError: stops the update process if the available rates are below the ratemin.""" g = Github(token) rl = g.get_rate_limit() if rl.core.remaining <= ratemin: raise GithubRateLimitError(rl.core.reset.timestamp()) else: files.message("API requests remaining: " + str(rl.core.remaining), "info")
def cl_test(self): """Test the cleanlist and output what it will clean. Useful for testing your cleaning configuration.""" gf.message(self.cleanlist, "debug") cl = gf.load_dict("Cleanlist", self.program_dir, self.cleanlist, self.debug) gf.message(cl, "debug") if len(cl) == 0: gf.message("Nothing will be deleted during cleaning.", "info") else: gf.message("Cleaning will delete the following: ", "info") for path in cl: gf.message(path, "debug")
def wl_files(self, *args: str): """Add files to the whitelist. This protects any listed files from deletion during update installation. Each file should be a string referring to its name. :param args: list of files to protect. :type args: str""" if len(self.whitelist.keys()) == 1 and "!**" in self.whitelist.keys( ): # resets whitelist if not used yet. self.whitelist = {} gf.message("Reset whitelist for building.", "debug") for arg in args: self.whitelist[arg] = False gf.message("Loaded file {} into the whitelist.".format(arg), "debug")
def wl_exclude(self, *args: str): """Add directories here to exclude them in building the whitelist. Useful to fine-tune your whitelist if it's grabbing too many files. :param args: list of folders to exclude. :type args: str""" if len(self.whitelist.keys()) == 1 and "!**" in self.whitelist.keys( ): # resets whitelist if not used yet. self.whitelist = {} gf.message("Reset whitelist for building.", "debug") for arg in args: self.whitelist[arg] = True gf.message("Loaded file {} into the whitelist.".format(arg), "debug")
def wl_test(self): """Test the whitelist and output what's protected. Useful for testing your whitelist configuration.""" gf.message(self.whitelist, self.debug) wl = gf.load_dict("Whitelist", self.program_dir, self.whitelist, self.debug) gf.message(wl, "debug") if len(wl) == 0: gf.message("Nothing is protected by your whitelist.", "info") else: gf.message( "Whitelist will protect the following from being overwritten during installation: ", "info") for path in wl: gf.message(path, "info")
def _find_release_asset(release: GitRelease.GitRelease, asset: str, debug: bool) -> str: # TODO: detect asset use regex """Return the requested asset's download url from the given release. If no specific asset is requested, it will return the first one it comes across. :exception ghau.errors.ReleaseAssetError: No asset by given name was found. :exception ghau.errors.NoAssetsFoundError: No assets found for given release.""" al = release.get_assets() if al.totalCount == 0: # if there are no assets, abort. raise ge.NoAssetsFoundError(release.tag_name) if asset is None: # if no specific asset is requested, download the first it finds. return al[0].browser_download_url for item in al: # otherwise, look for the specific asset requested. if item.name == asset: gf.message( "Found asset {} with URL: {}".format( item.name, item.browser_download_url), "debug") return item.browser_download_url raise ge.ReleaseAssetError( release.tag_name, asset) # no asset found by requested name? abort.
def cl_files(self, *args): """List files here you would like to erase before installing an update. Each file should be a string referring to its name. :param args: list of files to delete. :type args: str :exception ghau.errors.NoPureWildcardsAllowedError: Found a "*" or "*.*" entry in the given arguments. Be more specific than that. This is to protect consumer devices should they have your program in a different environment. We don't want to wipe anyone's devices.""" try: if "*" in args or "*.*" in args: raise ge.NoPureWildcardsAllowedError( "cleanlist") # wildcard protection, no wiping devices. except ge.NoPureWildcardsAllowedError as e: gf.message(e.message, "info") raise if len(self.cleanlist.keys()) == 1 and "!**" in self.cleanlist.keys( ): # resets cleanlist if not used yet. self.cleanlist = {} gf.message("Reset cleanlist for building.", "debug") for arg in args: self.cleanlist[arg] = False gf.message("Loaded file {} into the cleanlist.".format(arg), "debug")
def _load_release(repo: str, pre_releases: bool, auth, debug: bool) -> GitRelease.GitRelease: """Returns the latest release (or pre_release if enabled) for the loaded repository. :exception ghau.errors.ReleaseNotFoundError: No releases found for given repository. :exception ghau.errors.GithubRateLimitError: Hit the rate limit in the process of loading the release. :exception ghau.errors.RepositoryNotFoundError: Given repository is not found.""" g = Github(auth) try: if g.get_repo( repo).get_releases().totalCount == 0: # no releases found gf.message("Release count is 0", "debug") raise ge.ReleaseNotFoundError(repo) if pre_releases: gf.message("Accepting pre-releases", "debug") return g.get_repo(repo).get_releases().reversed[0] elif not pre_releases: gf.message("Accepting full releases", "debug") for release in g.get_repo(repo).get_releases().reversed: gf.message("Checking release: {}".format(release.tag_name), "debug") if not release.prerelease: gf.message("Release found {}".format(release.tag_name), "debug") return release gf.message("Zero non-pre-release releases found", "debug") raise (ge.ReleaseNotFoundError(repo)) except RateLimitExceededException: reset_time = g.rate_limiting_resettime raise ge.GithubRateLimitError(reset_time) except UnknownObjectException: raise ge.RepositoryNotFoundError(repo)
def update(self): """Check for updates and install if an update is found. All expected exceptions triggered during the run of this method are automatically handled. They are intentionally raised to stop the update process should it be needed, not the entire program. An error message will be printed to the console summarizing what occurred when this happens. :exception ghau.errors.InvalidDownloadTypeError: an unexpected value was given to the download parameter of :class:`ghau.update.Update`.""" try: ge.argtest(sys.argv, "-ghau") ge.devtest(self.program_dir) ge.ratetest(self.ratemin, self.auth) wl = gf.load_dict("Whitelist", self.program_dir, self.whitelist, self.debug) cl = gf.load_dict("Cleanlist", self.program_dir, self.cleanlist, self.debug) latest_release = _load_release(self.repo, self.pre_releases, self.auth, self.debug) do_update = _update_check(self.version, latest_release.tag_name) if do_update: gf.clean_files(cl, self.debug) if self.download == "zip": gf.message("Downloading Zip", "debug") gf.download(latest_release.zipball_url, os.path.join(self.program_dir, "update.zip"), self.debug) gf.extract_zip( self.program_dir, os.path.join(self.program_dir, "update.zip"), wl, self.debug) gf.message( "Updated from {} to {}".format( self.version, latest_release.tag_name), "info") _run_cmd(self.reboot) sys.exit() if self.download == "asset": gf.message("Downloading Asset", "debug") asset_link = _find_release_asset(latest_release, self.asset, self.debug) gf.download(asset_link, self.asset, self.debug) gf.message( "Updated from {} to {}".format( self.version, latest_release.tag_name), "info") _run_cmd(self.reboot) sys.exit() else: raise ge.InvalidDownloadTypeError(self.download) else: gf.message("No update required.", True) except (ge.GithubRateLimitError, ge.GitRepositoryFoundError, ge.ReleaseNotFoundError, ge.ReleaseAssetError, ge.FileNotExeError, ge.FileNotScriptError, ge.NoAssetsFoundError, ge.InvalidDownloadTypeError, ge.LoopPreventionError) as e: gf.message(e.message, True) return