def test_cache_load_bougus_data(): """ load will exit if bogus data provided """ fn = tempfile.NamedTemporaryFile(delete=False) with open(fn.name, "w") as f: f.write("lsdkjfalskdjf") with pytest.raises(SystemExit): cache.load(fn.name) os.remove(fn.name)
def test_cache_load_permission_denied(data): """ load should exit the cache """ fn = tempfile.NamedTemporaryFile(delete=False) os.remove(fn.name) cache.save("product", fn.name, data["product"]) os.chmod(fn.name, stat.S_IWUSR) with pytest.raises(SystemExit): cache.load(fn.name) os.remove(fn.name)
def get(pattern, cache_filename, no_cache, update_cache): cache_path = Path(cache_filename) data = {} fetch_from_scc = False if cache_path.exists() and cache_path.is_file() and not no_cache: data = cache.load(cache_filename) try: data["product"] except KeyError as err: fetch_from_scc = True else: fetch_from_scc = True if fetch_from_scc: response = request.fetch("https://scc.suse.com/api/package_search/products") try: data["product"] = response["data"] except KeyError as err: print_err(f"Error: No data key found in scc api response, {err}") sys.exit(1) ret = [] if not pattern: ret = data["product"] else: for product in data["product"]: for k in product.keys(): if pattern in str(product[k]): ret.append(product) break if update_cache: cache.save("product", cache_filename, ret) return ret
def test_cache_save_load(data): """ saves and loads content from a file-like source """ fn = tempfile.NamedTemporaryFile(delete=False) os.remove(fn.name) assert cache.save("product", fn.name, data["product"]) == None data["age"] = {"product": datetime.now().strftime("%Y-%m-%d")} assert cache.load(fn.name) == data os.remove(fn.name)
def get(pattern, cachefile, no_cache, update_cache) -> List: """Fetch the products used by SCC Patches Parameters ---------- pattern: str Only return products where 'pattern' matches product name cachefile: str Patch to cache file no_cache: bool If true the cache file will not be used update_cache: bool if true the cache file will be updated Returns: List A list of Dict representing the products Raises: SystemExit if response from SUSE Customer Center is not parsable """ data = {} if not update_cache and ( Path(cachefile).exists() and Path(cachefile).is_file() and not no_cache ): data = cache.load(cachefile) try: data["patchproducts"] except KeyError as err: print_err("no patch product key found in cachefile") sys.exit(1) else: response = request.fetch("https://scc.suse.com/patches", "html") matches = re.findall(".*productsData=(.*)[^ \t\n\r\f\v]+.*", response) if len(matches) != 1: if len(matches) > 1: print_err("to many matches in response from SCC") else: print_err("no matches in response from SCC") exit(1) try: data["patchproducts"] = json.loads(matches[0]) except json.decoder.JSONDecodeError as err: print_err(f"Couldn't parse json response {err}") sys.exit(1) ret = [] if not pattern: ret = data["patchproducts"] else: for product in data["patchproducts"]: for k in product.keys(): if pattern in str(product[k]): ret.append(product) break if update_cache: cache.save("patchproducts", cachefile, data["patchproducts"]) return ret
def get(cachefile, shell=None): """Generates a shell completion script Parameters ---------- cachefile: str Path to cache file shell: str, optional Name of the shell to generate completion script for if not provided the function will try to use the SHELL environment varable Raises ------ SystemExit if an unknown shell is provided if shell is not provided and SHELL environ is not set if completion generation file is not readable """ if shell == None: try: os.environ["SHELL"] except KeyError: print_err( "Couldn't determin shell, you need to specify shell or set $SHELL environment variable", ) sys.exit(1) shell = os.path.basename(os.environ.get("SHELL")) if shell == "bash": filename = f"{os.path.dirname(os.path.realpath(__file__))}/completion.sh" else: print_err(f"Unsupported shell specified '{shell}'") sys.exit(1) try: with open(filename, "r") as f: # pragma: no cover fc = f.read() except FileNotFoundError as err: print_err({err}) sys.exit(2) data = cache.load(cachefile) sps_patch_product_complete = "" sps_patch_version_complete = "" sps_patch_arch_complete = "" try: sps_patch_product_complete = " ".join( patchproducts.short(data["patchproducts"], "name")) sps_patch_version_complete = " ".join( patchproducts.short(data["patchproducts"], "version")) sps_patch_arch_complete = " ".join( patchproducts.short(data["patchproducts"], "architecture")) except KeyError: print_warn( "Patch product information not found in cache file\nTo enable completion for patch products run: sps patchproduct --update-cache" ) products = [] try: for product in data["product"]: products.append(product["identifier"]) except KeyError: print_warn( "Product information not found in cache file\nTo enable completion for products run: sps product --update-cache" ) sps_package_product_complete = " ".join(products) fc = fc.replace("{sps_patch_product_complete}", sps_patch_product_complete) fc = fc.replace("{sps_patch_version_complete}", sps_patch_version_complete) fc = fc.replace("{sps_patch_arch_complete}", sps_patch_arch_complete) fc = fc.replace("{sps_package_product_complete}", sps_package_product_complete) return fc
def test_cache_load_file_not_found(data): assert cache.load("file-not-found") == {}