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_age_not_old(data): """ """ fn = tempfile.NamedTemporaryFile(delete=False) os.remove(fn.name) cache.save("product", fn.name, data["product"]) assert cache.age(fn.name, 60) == {} os.remove(fn.name)
def test_cache_save_bogus_data(): """ save will exit if bogus data provided """ fn = tempfile.NamedTemporaryFile(delete=False) with pytest.raises(SystemExit): cache.save("product", fn.name, "{age:100}") os.remove(fn.name)
def test_cache_lookup_product_found(data): """ lookup will not exit if identifier is found """ fn = tempfile.NamedTemporaryFile(delete=False) os.remove(fn.name) cache.save("product", fn.name, data["product"]) assert cache.lookup_product("SLED/15.2/x86_64", fn.name) == 1935 os.remove(fn.name)
def test_cache_lookup_product_notfound(data): """ lookup will exit if identifier is not found """ fn = tempfile.NamedTemporaryFile(delete=False) os.remove(fn.name) cache.save("product", fn.name, data["product"]) with pytest.raises(SystemExit): cache.lookup_product("Bogus product name", fn.name) os.remove(fn.name)
def test_cache_save_permission_denied(data): """ save 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_IRUSR) with pytest.raises(SystemExit): cache.save("product", fn.name, data["product"]) os.remove(fn.name)
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 test_cache_save_key_load_old(data): fn = tempfile.NamedTemporaryFile(delete=False) existing_data = {"testing": "testing", "product": "product"} with open(fn.name, "w") as f: json.dump(existing_data, f) cache.save("product", fn.name, data["product"]) with open(fn.name, "r") as f: cachedata = json.load(f) new_data = { "testing": "testing", "age": { "product": datetime.now().strftime("%Y-%m-%d") }, "product": data["product"], } assert new_data == cachedata
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 test_cache_save_bogus_key(): with pytest.raises(SystemExit): cache.save("fake-cache-key", "fake-file-name", [])