def test_asset_purge_by_overall_cache_size(self): """Make sure that we can set cache limits.""" # creates a single byte asset asset_file = tempfile.NamedTemporaryFile(dir=self.base_dir.name, delete=False) asset_file.write(b'\xff') asset_file.close() config = self.config_file.name url = asset_file.name name = "should-be-removed" cmd_line = "%s --config %s assets register %s %s" % (AVOCADO, config, name, url) result = process.run(cmd_line) self.assertIn("Now you can reference it by name {}".format(name), result.stdout_text) cmd_line = "%s --config %s assets purge --by-overall-limit 2" % (AVOCADO, config) process.run(cmd_line) cmd_line = "%s --config %s assets list" % (AVOCADO, config) result = process.run(cmd_line) self.assertIn(name, result.stdout_text) # Double check that the sum of assets is not bigger than 2bytes size_sum = 0 for asset in Asset.get_all_assets([self.mapping['cache_dir']]): size_sum += os.stat(asset).st_size self.assertLessEqual(size_sum, 2)
def test_asset_purge_by_overall_cache_size(self): """Make sure that we can set cache limits.""" # creates a single byte asset asset_file = tempfile.NamedTemporaryFile(dir=self.base_dir.name, delete=False) asset_file.write(b"\xff") asset_file.close() config = self.config_file.name url = asset_file.name name = "should-be-removed" cmd_line = f"{AVOCADO} --config {config} assets register {name} {url}" result = process.run(cmd_line) self.assertIn(f"Now you can reference it by name {name}", result.stdout_text) cmd_line = f"{AVOCADO} --config {config} assets purge " f"--by-overall-limit 2" process.run(cmd_line) cmd_line = f"{AVOCADO} --config {config} assets list" result = process.run(cmd_line) self.assertIn(name, result.stdout_text) # Double check that the sum of assets is not bigger than 2bytes size_sum = 0 for asset in Asset.get_all_assets([self.mapping["cache_dir"]]): size_sum += os.stat(asset).st_size self.assertLessEqual(size_sum, 2)
def handle_list(self, config): days = config.get("assets.list.days") size_filter = config.get("assets.list.size_filter") if self._count_filter_args(config) == 2: msg = ("You should choose --by-size-filter or --by-days. " "For help, run: avocado assets list --help") LOG_UI.error(msg) return exit_codes.AVOCADO_FAIL cache_dirs = config.get("datadir.paths.cache_dirs") try: assets = None if days is not None: assets = Asset.get_assets_unused_for_days(days, cache_dirs) elif size_filter is not None: assets = Asset.get_assets_by_size(size_filter, cache_dirs) elif assets is None: assets = Asset.get_all_assets(cache_dirs) except (FileNotFoundError, OSError) as e: LOG_UI.error("Could get assets: %s", e) return exit_codes.AVOCADO_FAIL matrix = [] for asset in assets: stat = os.stat(asset) basename = os.path.basename(asset) hash_path = f"{asset}-CHECKSUM" atime = datetime.fromtimestamp(stat.st_atime) _, checksum = Asset.read_hash_from_file(hash_path) matrix.append(( basename, str(checksum or "unknown")[:10], atime.strftime("%Y-%m-%d %H:%M:%S"), display_data_size(stat.st_size), )) header = ("asset", "checksum", "atime", "size") output = list(iter_tabular_output(matrix, header)) if len(output) == 1: LOG_UI.info("No asset found in your cache system.") else: for line in output: LOG_UI.info(line)
def handle_list(self, config): days = config.get('assets.list.days') size_filter = config.get('assets.list.size_filter') if (days is not None and size_filter is not None): msg = ("You should choose --by-size-filter or --by-days. " "For help, run: avocado assets list --help") LOG_UI.error(msg) return # IMO, this should get data from config instead. See #4443 cache_dirs = data_dir.get_cache_dirs() try: assets = None if days is not None: assets = Asset.get_assets_unused_for_days(days, cache_dirs) elif size_filter is not None: assets = Asset.get_assets_by_size(size_filter, cache_dirs) elif assets is None: assets = Asset.get_all_assets(cache_dirs) except (FileNotFoundError, OSError) as e: LOG_UI.error("Could get assets: %s", e) return matrix = [] for asset in assets: stat = os.stat(asset) basename = os.path.basename(asset) hash_path = "{}-CHECKSUM".format(asset) atime = datetime.fromtimestamp(stat.st_atime) _, checksum = Asset.read_hash_from_file(hash_path) matrix.append((basename, str(checksum or "unknown")[:10], atime.strftime('%Y-%m-%d %H:%M:%S'), display_data_size(stat.st_size))) header = ("asset", "checksum", "atime", "size") output = list(iter_tabular_output(matrix, header)) if len(output) == 1: LOG_UI.info("No asset found in your cache system.") else: for line in output: LOG_UI.info(line)