How to use opt_string2dict method in autotest

Best Python code snippet using autotest_python Github


Full Screen

...4"""5import os, re, tempfile6import common7from autotest_lib.client.common_lib import error, utils8def opt_string2dict(opt_string):9 """Breaks the mkfs.ext* option string into dictionary."""10 # Example string: '-j -q -i 8192 -b 4096'. There may be extra whitespaces.11 opt_dict = {}12 for item in opt_string.split('-'):13 item = item.strip()14 if ' ' in item:15 (opt, value) = item.split(' ', 1)16 opt_dict['-%s' % opt] = value17 elif item != '':18 opt_dict['-%s' % item] = None19 # Convert all the digit strings to int.20 for key, value in opt_dict.iteritems():21 if value and value.isdigit():22 opt_dict[key] = int(value)23 return opt_dict24def parse_mke2fs_conf(fs_type, conf_file='/etc/mke2fs.conf'):25 """Parses mke2fs config file for default settings."""26 # Please see /ect/mke2fs.conf for an example.27 default_opt = {}28 fs_opt = {}29 current_fs_type = ''30 current_section = ''31 f = open(conf_file, 'r')32 for line in f:33 if '[defaults]' == line.strip():34 current_section = '[defaults]'35 elif '[fs_types]' == line.strip():36 current_section = '[fs_types]'37 elif current_section == '[defaults]':38 components = line.split('=', 1)39 if len(components) == 2:40 default_opt[components[0].strip()] = components[1].strip()41 elif current_section == '[fs_types]':42 m ='(\w+) = {', line)43 if m:44 current_fs_type = else:46 components = line.split('=', 1)47 if len(components) == 2 and current_fs_type == fs_type:48 default_opt[components[0].strip()] = components[1].strip()49 f.close()50 # fs_types options override the defaults options51 for key, value in fs_opt.iteritems():52 default_opt[key] = value53 # Convert all the digit strings to int.54 for key, value in default_opt.iteritems():55 if value and value.isdigit():56 default_opt[key] = int(value)57 return default_opt58def convert_conf_opt(default_opt):59 conf_opt_mapping = {'blocksize': '-b',60 'inode_ratio': '-i',61 'inode_size': '-I'}62 mkfs_opt = {}63 # Here we simply concatenate the feature string while we really need64 # to do the better and/or operations.65 if 'base_features' in default_opt:66 mkfs_opt['-O'] = default_opt['base_features']67 if 'default_features' in default_opt:68 mkfs_opt['-O'] += ',%s' % default_opt['default_features']69 if 'features' in default_opt:70 mkfs_opt['-O'] += ',%s' % default_opt['features']71 for key, value in conf_opt_mapping.iteritems():72 if key in default_opt:73 mkfs_opt[value] = default_opt[key]74 if '-O' in mkfs_opt:75 mkfs_opt['-O'] = mkfs_opt['-O'].split(',')76 return mkfs_opt77def merge_ext_features(conf_feature, user_feature):78 user_feature_list = user_feature.split(',')79 merged_feature = []80 # Removes duplicate entries in conf_list.81 for item in conf_feature:82 if item not in merged_feature:83 merged_feature.append(item)84 # User options override config options.85 for item in user_feature_list:86 if item[0] == '^':87 if item[1:] in merged_feature:88 merged_feature.remove(item[1:])89 else:90 merged_feature.append(item)91 elif item not in merged_feature:92 merged_feature.append(item)93 return merged_feature94def ext_tunables(dev):95 """Call tune2fs -l and parse the result."""96 cmd = 'tune2fs -l %s' % dev97 try:98 out = utils.system_output(cmd)99 except error.CmdError:100 tools_dir = os.path.join(os.environ['AUTODIR'], 'tools')101 cmd = '%s/tune2fs.ext4dev -l %s' % (tools_dir, dev)102 out = utils.system_output(cmd)103 # Load option mappings104 tune2fs_dict = {}105 for line in out.splitlines():106 components = line.split(':', 1)107 if len(components) == 2:108 value = components[1].strip()109 option = components[0]110 if value.isdigit():111 tune2fs_dict[option] = int(value)112 else:113 tune2fs_dict[option] = value114 return tune2fs_dict115def ext_mkfs_options(tune2fs_dict, mkfs_option):116 """Map the tune2fs options to mkfs options."""117 def __inode_count(tune_dict, k):118 return (tune_dict['Block count']/tune_dict[k] + 1) * (119 tune_dict['Block size'])120 def __block_count(tune_dict, k):121 return int(100*tune_dict[k]/tune_dict['Block count'] + 1)122 def __volume_name(tune_dict, k):123 if tune_dict[k] != '<none>':124 return tune_dict[k]125 else:126 return ''127 # mappings between fs features and mkfs options128 ext_mapping = {'Blocks per group': '-g',129 'Block size': '-b',130 'Filesystem features': '-O',131 'Filesystem OS type': '-o',132 'Filesystem revision #': '-r',133 'Filesystem volume name': '-L',134 'Flex block group size': '-G',135 'Fragment size': '-f',136 'Inode count': '-i',137 'Inode size': '-I',138 'Journal inode': '-j',139 'Reserved block count': '-m'}140 conversions = {141 'Journal inode': lambda d, k: None,142 'Filesystem volume name': __volume_name,143 'Reserved block count': __block_count,144 'Inode count': __inode_count,145 'Filesystem features': lambda d, k: re.sub(' ', ',', d[k]),146 'Filesystem revision #': lambda d, k: d[k][0]}147 for key, value in ext_mapping.iteritems():148 if key not in tune2fs_dict:149 continue150 if key in conversions:151 mkfs_option[value] = conversions[key](tune2fs_dict, key)152 else:153 mkfs_option[value] = tune2fs_dict[key]154def xfs_tunables(dev):155 """Call xfs_grow -n to get filesystem tunables."""156 # Have to mount the filesystem to call xfs_grow.157 tmp_mount_dir = tempfile.mkdtemp()158 cmd = 'mount %s %s' % (dev, tmp_mount_dir)159 utils.system_output(cmd)160 xfs_growfs = os.path.join(os.environ['AUTODIR'], 'tools', 'xfs_growfs')161 cmd = '%s -n %s' % (xfs_growfs, dev)162 try:163 out = utils.system_output(cmd)164 finally:165 # Clean.166 cmd = 'umount %s' % dev167 utils.system_output(cmd, ignore_status=True)168 os.rmdir(tmp_mount_dir)169 ## The output format is given in report_info (xfs_growfs.c)170 ## "meta-data=%-22s isize=%-6u agcount=%u, agsize=%u blks\n"171 ## " =%-22s sectsz=%-5u attr=%u\n"172 ## "data =%-22s bsize=%-6u blocks=%llu, imaxpct=%u\n"173 ## " =%-22s sunit=%-6u swidth=%u blks\n"174 ## "naming =version %-14u bsize=%-6u\n"175 ## "log =%-22s bsize=%-6u blocks=%u, version=%u\n"176 ## " =%-22s sectsz=%-5u sunit=%u blks, lazy-count=%u\n"177 ## "realtime =%-22s extsz=%-6u blocks=%llu, rtextents=%llu\n"178 tune2fs_dict = {}179 # Flag for extracting naming version number180 keep_version = False181 for line in out.splitlines():182 m ='^([-\w]+)', line)183 if m:184 main_tag = pairs = line.split()186 for pair in pairs:187 # naming: version needs special treatment188 if pair == '=version':189 # 1 means the next pair is the version number we want190 keep_version = True191 continue192 if keep_version:193 tune2fs_dict['naming: version'] = pair194 # Resets the flag since we have logged the version195 keep_version = False196 continue197 # Ignores the strings without '=', such as 'blks'198 if '=' not in pair:199 continue200 key, value = pair.split('=')201 tagged_key = '%s: %s' % (main_tag, key)202 if re.match('[0-9]+', value):203 tune2fs_dict[tagged_key] = int(value.rstrip(','))204 else:205 tune2fs_dict[tagged_key] = value.rstrip(',')206 return tune2fs_dict207def xfs_mkfs_options(tune2fs_dict, mkfs_option):208 """Maps filesystem tunables to their corresponding mkfs options."""209 # Mappings210 xfs_mapping = {'meta-data: isize': '-i size',211 'meta-data: agcount': '-d agcount',212 'meta-data: sectsz': '-s size',213 'meta-data: attr': '-i attr',214 'data: bsize': '-b size',215 'data: imaxpct': '-i maxpct',216 'data: sunit': '-d sunit',217 'data: swidth': '-d swidth',218 'data: unwritten': '-d unwritten',219 'naming: version': '-n version',220 'naming: bsize': '-n size',221 'log: version': '-l version',222 'log: sectsz': '-l sectsize',223 'log: sunit': '-l sunit',224 'log: lazy-count': '-l lazy-count',225 'realtime: extsz': '-r extsize',226 'realtime: blocks': '-r size',227 'realtime: rtextents': '-r rtdev'}228 mkfs_option['-l size'] = tune2fs_dict['log: bsize'] * (229 tune2fs_dict['log: blocks'])230 for key, value in xfs_mapping.iteritems():231 mkfs_option[value] = tune2fs_dict[key]232def compare_features(needed_feature, current_feature):233 """Compare two ext* feature lists."""234 if len(needed_feature) != len(current_feature):235 return False236 for feature in current_feature:237 if feature not in needed_feature:238 return False239 return True240def match_ext_options(fs_type, dev, needed_options):241 """Compare the current ext* filesystem tunables with needed ones."""242 # mkfs.ext* will load default options from /etc/mke2fs.conf243 conf_opt = parse_mke2fs_conf(fs_type)244 # We need to convert the conf options to mkfs options.245 conf_mkfs_opt = convert_conf_opt(conf_opt)246 # Breaks user mkfs option string to dictionary.247 needed_opt_dict = opt_string2dict(needed_options)248 # Removes ignored options.249 ignored_option = ['-c', '-q', '-E', '-F']250 for opt in ignored_option:251 if opt in needed_opt_dict:252 del needed_opt_dict[opt]253 # User options override config options.254 needed_opt = conf_mkfs_opt255 for key, value in needed_opt_dict.iteritems():256 if key == '-N' or key == '-T':257 raise Exception('-N/T is not allowed.')258 elif key == '-O':259 needed_opt[key] = merge_ext_features(needed_opt[key], value)260 else:261 needed_opt[key] = value...

Full Screen

Full Screen Github


Full Screen

...81 mkfs_option = {}82 fsinfo.xfs_mkfs_options(tune2fs_dict, mkfs_option)83 for option, value in expected_option.iteritems():84 self.assertEqual(value, mkfs_option[option])85 def test_opt_string2dict(self):86 test_string = '-q -b 1234 -O fdasfa,fdasfdas -l adfas -k -L'87 result = fsinfo.opt_string2dict(test_string)88 expected_result = {'-q': None,89 '-b': 1234,90 '-O': 'fdasfa,fdasfdas',91 '-l': 'adfas',92 '-k': None,93 '-L': None}94 self.assertEqual(expected_result, result)95 def test_merge_ext_features(self):96 conf = 'a,b,d,d,d,d,d,e,e,a,f'.split(',')97 user = '^e,a,^f,g,h,i'98 expected_result = ['a', 'b', 'd', 'g', 'h', 'i']99 result = fsinfo.merge_ext_features(conf, user)100 self.assertEqual(expected_result, result)101 def test_compare_features(self):...

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:


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

Run autotest 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?