How to use list_downloaded_images method in avocado

Best Python code snippet using avocado_python

vmimage.py

Source:vmimage.py Github

copy

Full Screen

...5from avocado.core.output import LOG_UI6from avocado.core.plugin_interfaces import CLICmd7from avocado.core.settings import settings8from avocado.utils import astring, vmimage9def list_downloaded_images():10 """11 List the available Image inside avocado cache12 :return: list with image's parameters13 :rtype: list of dicts14 """15 images = []16 for cache_dir in data_dir.get_cache_dirs():17 for root, _, files in os.walk(cache_dir):18 if files:19 metadata_files = [pos_json for pos_json in files20 if pos_json.endswith('_metadata.json')]21 files = list(set(files) - set(metadata_files))22 for metadata_file in metadata_files:23 with open(os.path.join(root, metadata_file), 'r') as data:24 metadata = json.loads(data.read())25 if isinstance(metadata, dict):26 if metadata.get("type", None) == "vmimage":27 provider = None28 for p in vmimage.IMAGE_PROVIDERS:29 if p.name == metadata["name"]:30 provider = p(metadata["version"],31 metadata["build"],32 metadata["arch"])33 break34 if provider is not None:35 for image in files:36 if re.match(provider.file_name, image):37 data = {"name": provider.name,38 "version": provider.version,39 "arch": provider.arch,40 "file": os.path.join(root, image)}41 images.append(data)42 break43 return images44def download_image(distro, version=None, arch=None):45 """46 Downloads the vmimage to the cache directory if doesn't already exist47 :param distro: Name of image distribution48 :type distro: str49 :param version: Version of image50 :type version: str51 :param arch: Architecture of image52 :type arch: str53 :raise AttributeError: When image can't be downloaded54 :return: Information about downloaded image55 :rtype: dict56 """57 cache_dir = data_dir.get_cache_dirs()[0]58 image_info = vmimage.get(name=distro, version=version, arch=arch,59 cache_dir=cache_dir)60 file_path = image_info.base_image61 image = {'name': distro, 'version': image_info.version,62 'arch': image_info.arch, 'file': file_path}63 return image64def display_images_list(images):65 """66 Displays table with information about images67 :param images: list with image's parameters68 :type images: list of dicts69 """70 image_matrix = [[image['name'], image['version'], image['arch'],71 image['file']] for image in images]72 header = (output.TERM_SUPPORT.header_str('Provider'),73 output.TERM_SUPPORT.header_str('Version'),74 output.TERM_SUPPORT.header_str('Architecture'),75 output.TERM_SUPPORT.header_str('File'))76 for line in astring.iter_tabular_output(image_matrix, header=header,77 strip=True):78 LOG_UI.debug(line)79class VMimage(CLICmd):80 """81 Implements the avocado 'vmimage' subcommand82 """83 name = 'vmimage'84 description = 'Provides VM images acquired from official repositories'85 def configure(self, parser):86 parser = super(VMimage, self).configure(parser)87 subcommands = parser.add_subparsers(dest='vmimage_subcommand')88 subcommands.required = True89 subcommands.add_parser('list', help='List of all downloaded images')90 get_parser = subcommands.add_parser('get',91 help="Downloads chosen VMimage if "92 "it's not already in the "93 "cache")94 help_msg = 'Name of image distribution'95 settings.register_option(section='vmimage.get',96 key='distro',97 default=None,98 help_msg=help_msg,99 key_type=str,100 parser=get_parser,101 long_arg='--distro',102 required=True)103 help_msg = 'Image version'104 settings.register_option(section='vmimage.get',105 key='version',106 default=None,107 help_msg=help_msg,108 key_type=str,109 parser=get_parser,110 long_arg='--distro-version')111 help_msg = 'Image architecture'112 settings.register_option(section='vmimage.get',113 key='arch',114 default=None,115 help_msg=help_msg,116 key_type=str,117 parser=get_parser,118 long_arg='--arch')119 def run(self, config):120 subcommand = config.get("vmimage_subcommand")121 if subcommand == 'list':122 images = list_downloaded_images()123 display_images_list(images)124 elif subcommand == 'get':125 name = config.get('vmimage.get.distro')126 version = config.get('vmimage.get.version')127 arch = config.get('vmimage.get.arch')128 try:129 image = download_image(name, version, arch)130 except AttributeError:131 LOG_UI.debug("The requested image could not be downloaded")132 return exit_codes.AVOCADO_FAIL133 LOG_UI.debug("The image was downloaded:")134 display_images_list([image])...

Full Screen

Full Screen

verify_wget_download.py

Source:verify_wget_download.py Github

copy

Full Screen

...7 within the working directory.8 """9 data_dir = get_args()10 expected_images = read_archive_table_export(data_dir)11 downloaded_images = list_downloaded_images(data_dir)12 check_for_missing_images(expected_images, downloaded_images)13def get_args():14 """Function to request or gather the required arguments"""15 if len(sys.argv) == 1:16 data_dir = raw_input('Please enter the path to the working data directory: ')17 else:18 data_dir = sys.argv[1]19 return data_dir20def read_archive_table_export(data_dir):21 """Function to read the table of images produced by the archive.22 This table file should be produced using the TXT formatting option.23 """24 table_file = os.path.join(data_dir,'tableExport.txt')25 if not os.path.isfile(table_file):26 print('ERROR: Cannot find the archive-generated list of expected images')27 sys.exit()28 f = open(table_file,'r')29 flines = f.readlines()30 f.close()31 image_list = []32 for line in flines[1:]:33 entries = line.replace('\n','').split(',')34 image_list.append(entries[2])35 print(str(len(image_list))+' image(s) expected from the archive')36 return image_list37def list_downloaded_images(data_dir):38 """Function to build a list of image files that were actually downloaded from those available in the39 working directory, stripping the file extensions since these are not included in the archives data list."""40 file_list = glob.glob(os.path.join(data_dir,'*.fits.fz'))41 if len(file_list) == 0:42 file_list = glob.glob(os.path.join(data_dir,'*.fits'))43 if len(file_list) == 0:44 print('ERROR: No frames downloaded')45 sys.exit()46 image_list = []47 for f in file_list:48 image_list.append(os.path.basename(f).split('.')[0])49 print(str(len(image_list))+' image(s) downloaded')50 return image_list51def check_for_missing_images(expected_images, downloaded_images):...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful