2 # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
3 """List model names from mapfile.csv files."""
8 from typing
import List
11 def dir_path(path
: str) -> str:
12 """Validate path is a directory for argparse."""
13 if os
.path
.isdir(path
):
15 raise argparse
.ArgumentTypeError(f
'\'{path}\' is not a valid directory')
17 def find_archs(start_dir
: str, arch
: str) -> List
[str]:
19 for item
in os
.scandir(start_dir
):
22 if arch
in (item
.name
, 'all'):
23 archs
.append(item
.name
)
26 raise IOError(f
'Missing architecture directory \'{arch}\'')
30 def find_mapfiles(start_dir
: str, archs
: List
[str]) -> List
[str]:
33 for item
in os
.scandir(f
'{start_dir}/{arch}'):
36 if item
.name
== 'mapfile.csv':
37 result
.append(f
'{start_dir}/{arch}/mapfile.csv')
40 def find_cpuids(mapfiles
: List
[str], cpuids
: str) -> List
[str]:
42 for mapfile
in mapfiles
:
43 with
open(mapfile
, encoding
='utf-8') as csvfile
:
45 table
= csv
.reader(csvfile
)
47 if not first
or len(row
) == 0 or row
[0].startswith('#'):
50 # Python regular expressions don't handle xdigit.
51 regex
= row
[0].replace('[[:xdigit:]]', '[0-9a-fA-F]')
52 for cpuid
in cpuids
.split(','):
53 if re
.match(regex
, cpuid
):
57 ap
= argparse
.ArgumentParser()
58 ap
.add_argument('arch', help='Architecture name like x86')
59 ap
.add_argument('cpuid', default
='all', help='List of cpuids to convert to model names')
63 help='Root of tree containing architecture directories containing json files'
65 args
= ap
.parse_args()
67 archs
= find_archs(args
.starting_dir
, args
.arch
)
68 mapfiles
= find_mapfiles(args
.starting_dir
, archs
)
69 models
= find_cpuids(mapfiles
, args
.cpuid
)
70 print(','.join(models
))
72 if __name__
== '__main__':