Best Python code snippet using autotest_python
fsinfo.py
Source:fsinfo.py  
...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 = re.search('^([-\w]+)', line)183        if m:184            main_tag = m.group(1)185        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] = value262    # '-j' option will add 'has_journal' feature.263    if '-j' in needed_opt and 'has_journal' not in needed_opt['-O']:264        needed_opt['-O'].append('has_journal')265    # 'extents' will be shown as 'extent' in the outcome of tune2fs266    if 'extents' in needed_opt['-O']:267        needed_opt['-O'].append('extent')268        needed_opt['-O'].remove('extents')269    # large_file is a byproduct of resize_inode.270    if 'large_file' not in needed_opt['-O'] and (271        'resize_inode' in needed_opt['-O']):272        needed_opt['-O'].append('large_file')273    current_opt = {}...fsinfo_unittest.py
Source:fsinfo_unittest.py  
...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):102        f1 = ['sparse_super', 'filetype', 'resize_inode', 'dir_index']103        f2 = ['filetype', 'resize_inode', 'dir_index', 'large_file']104        self.assertTrue(fsinfo.compare_features(f1, f1))105        self.assertFalse(fsinfo.compare_features(f1, f2))106    def test_mke2fs_conf(self):107        content = ('[defaults]\n'108                   'base_features = sparse_super,filetype,resize_inode\n'109                   '       blocksize = 4096 \n'110                   '       inode_ratio = 8192  \n'111                   '\n [fs_types]\n'112                   '       small = {\n'113                   '                         blocksize = 1024\n'...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!!
