Exemple #1
0
    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)
Exemple #2
0
    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)