Best Python code snippet using avocado_python
assets.py
Source:assets.py
...244 """245 name = "assets"246 description = "Manage assets"247 @staticmethod248 def _count_filter_args(config):249 sub_command = config.get("assets_subcommand")250 args = [251 config.get(f"assets.{sub_command}.days"),252 config.get(f"assets.{sub_command}.size_filter"),253 config.get(f"assets.{sub_command}.overall_limit"),254 ]255 return len([a for a in args if a is not None])256 def configure(self, parser):257 """Add the subparser for the assets action.258 :param parser: The Avocado command line application parser259 :type parser: :class:`avocado.core.parser.ArgumentParser`260 """261 def register_filter_options(subparser, section):262 help_msg = (263 "Apply action based on a size filter (comparison "264 "operator + value) in bytes. Ex '>20', '<=200'. "265 "Supported operators: " + ", ".join(SUPPORTED_OPERATORS)266 )267 settings.register_option(268 section=section,269 key="size_filter",270 help_msg=help_msg,271 default=None,272 metavar="FILTER",273 key_type=str,274 long_arg="--by-size-filter",275 parser=subparser,276 )277 help_msg = "How old (in days) should Avocado look for assets?"278 settings.register_option(279 section=section,280 key="days",281 help_msg=help_msg,282 default=None,283 key_type=int,284 metavar="DAYS",285 long_arg="--by-days",286 parser=subparser,287 )288 help_msg = (289 "Filter will be based on a overall system limit "290 "threshold in bytes (with assets ordered by last "291 "access) or with a suffix unit. Valid suffixes are: "292 )293 help_msg += ",".join(DataSize.MULTIPLIERS.keys())294 settings.register_option(295 section=section,296 key="overall_limit",297 help_msg=help_msg,298 default=None,299 key_type=str,300 metavar="LIMIT",301 long_arg="--by-overall-limit",302 parser=subparser,303 )304 parser = super().configure(parser)305 subcommands = parser.add_subparsers(dest="assets_subcommand")306 subcommands.required = True307 fetch_subcommand_parser = subcommands.add_parser(308 "fetch",309 help="Fetch assets from test source or config file if it's not"310 " already in the cache",311 )312 help_msg = "Path to avocado instrumented test"313 settings.register_option(314 section="assets.fetch",315 key="references",316 help_msg=help_msg,317 default=[],318 metavar="AVOCADO_INSTRUMENTED",319 key_type=list,320 nargs="+",321 parser=fetch_subcommand_parser,322 positional_arg=True,323 )324 help_msg = "always return success for the fetch command."325 settings.register_option(326 section="assets.fetch",327 key="ignore_errors",328 help_msg=help_msg,329 default=False,330 key_type=bool,331 parser=fetch_subcommand_parser,332 long_arg="--ignore-errors",333 )334 help_msg = "Timeout to be used when download an asset."335 settings.register_option(336 section="assets.fetch",337 key="timeout",338 help_msg=help_msg,339 default=300,340 key_type=int,341 metavar="TIMEOUT",342 parser=fetch_subcommand_parser,343 long_arg="--timeout",344 )345 register_subcommand_parser = subcommands.add_parser(346 "register", help="Register an asset directly to the cacche"347 )348 help_msg = "Unique name to associate with this asset."349 settings.register_option(350 section="assets.register",351 key="name",352 help_msg=help_msg,353 default=None,354 key_type=str,355 parser=register_subcommand_parser,356 positional_arg=True,357 )358 help_msg = "Path to asset that you would like to register manually."359 settings.register_option(360 section="assets.register",361 key="url",362 help_msg=help_msg,363 default=None,364 key_type=str,365 parser=register_subcommand_parser,366 positional_arg=True,367 )368 help_msg = "SHA1 hash of this asset."369 settings.register_option(370 section="assets.register",371 key="sha1_hash",372 help_msg=help_msg,373 default=None,374 key_type=str,375 metavar="SHA1",376 long_arg="--hash",377 parser=register_subcommand_parser,378 )379 purge_subcommand_parser = subcommands.add_parser(380 "purge", help="Removes assets cached locally."381 )382 register_filter_options(purge_subcommand_parser, "assets.purge")383 help_msg = "List all cached assets."384 list_subcommand_parser = subcommands.add_parser("list", help=help_msg)385 register_filter_options(list_subcommand_parser, "assets.list")386 def handle_purge(self, config):387 days = config.get("assets.purge.days")388 size_filter = config.get("assets.purge.size_filter")389 overall_limit = config.get("assets.purge.overall_limit")390 if self._count_filter_args(config) != 1:391 msg = (392 "You should choose --by-size-filter or --by-days. "393 "For help, run: avocado assets purge --help"394 )395 LOG_UI.error(msg)396 return exit_codes.AVOCADO_FAIL397 cache_dirs = config.get("datadir.paths.cache_dirs")398 try:399 if days is not None:400 Asset.remove_assets_by_unused_for_days(days, cache_dirs)401 elif size_filter is not None:402 Asset.remove_assets_by_size(size_filter, cache_dirs)403 elif overall_limit is not None:404 try:405 size = DataSize(overall_limit).b406 Asset.remove_assets_by_overall_limit(size, cache_dirs)407 except InvalidDataSize:408 error_msg = "You are using an invalid suffix. "409 error_msg += "Use one of the following values: "410 error_msg += ",".join(DataSize.MULTIPLIERS.keys())411 LOG_UI.error(error_msg)412 return exit_codes.AVOCADO_FAIL413 except (FileNotFoundError, OSError) as e:414 LOG_UI.error("Could not remove asset: %s", e)415 return exit_codes.AVOCADO_FAIL416 return exit_codes.AVOCADO_ALL_OK417 def handle_list(self, config):418 days = config.get("assets.list.days")419 size_filter = config.get("assets.list.size_filter")420 if self._count_filter_args(config) == 2:421 msg = (422 "You should choose --by-size-filter or --by-days. "423 "For help, run: avocado assets list --help"424 )425 LOG_UI.error(msg)426 return exit_codes.AVOCADO_FAIL427 cache_dirs = config.get("datadir.paths.cache_dirs")428 try:429 assets = None430 if days is not None:431 assets = Asset.get_assets_unused_for_days(days, cache_dirs)432 elif size_filter is not None:433 assets = Asset.get_assets_by_size(size_filter, cache_dirs)434 elif assets is None:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!