get_feat.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env python3
  2. # pylint: disable=R0902,R0911,R0912,R0914,R0915
  3. # Copyright(c) 2025: Mauro Carvalho Chehab <mchehab@kernel.org>.
  4. # SPDX-License-Identifier: GPL-2.0
  5. """
  6. Parse the Linux Feature files and produce a ReST book.
  7. """
  8. import argparse
  9. import os
  10. import subprocess
  11. import sys
  12. from pprint import pprint
  13. LIB_DIR = "../../tools/lib/python"
  14. SRC_DIR = os.path.dirname(os.path.realpath(__file__))
  15. sys.path.insert(0, os.path.join(SRC_DIR, LIB_DIR))
  16. from feat.parse_features import ParseFeature # pylint: disable=C0413
  17. SRCTREE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../..")
  18. DEFAULT_DIR = "Documentation/features"
  19. class GetFeature:
  20. """Helper class to parse feature parsing parameters"""
  21. @staticmethod
  22. def get_current_arch():
  23. """Detects the current architecture"""
  24. proc = subprocess.run(["uname", "-m"], check=True,
  25. capture_output=True, text=True)
  26. arch = proc.stdout.strip()
  27. if arch in ["x86_64", "i386"]:
  28. arch = "x86"
  29. elif arch == "s390x":
  30. arch = "s390"
  31. return arch
  32. def run_parser(self, args):
  33. """Execute the feature parser"""
  34. feat = ParseFeature(args.directory, args.debug, args.enable_fname)
  35. data = feat.parse()
  36. if args.debug > 2:
  37. pprint(data)
  38. return feat
  39. def run_rest(self, args):
  40. """
  41. Generate tables in ReST format. Three types of tables are
  42. supported, depending on the calling arguments:
  43. - neither feature nor arch is passed: generates a full matrix;
  44. - arch provided: generates a table of supported tables for the
  45. guiven architecture, eventually filtered by feature;
  46. - only feature provided: generates a table with feature details,
  47. showing what architectures it is implemented.
  48. """
  49. feat = self.run_parser(args)
  50. if args.arch:
  51. rst = feat.output_arch_table(args.arch, args.feat)
  52. elif args.feat:
  53. rst = feat.output_feature(args.feat)
  54. else:
  55. rst = feat.output_matrix()
  56. print(rst)
  57. def run_current(self, args):
  58. """
  59. Instead of using a --arch parameter, get feature for the current
  60. architecture.
  61. """
  62. args.arch = self.get_current_arch()
  63. self.run_rest(args)
  64. def run_list(self, args):
  65. """
  66. Generate a list of features for a given architecture, in a format
  67. parseable by other scripts. The output format is not ReST.
  68. """
  69. if not args.arch:
  70. args.arch = self.get_current_arch()
  71. feat = self.run_parser(args)
  72. msg = feat.list_arch_features(args.arch, args.feat)
  73. print(msg)
  74. def parse_arch(self, parser):
  75. """Add a --arch parsing argument"""
  76. parser.add_argument("--arch",
  77. help="Output features for an specific"
  78. " architecture, optionally filtering for a "
  79. "single specific feature.")
  80. def parse_feat(self, parser):
  81. """Add a --feat parsing argument"""
  82. parser.add_argument("--feat", "--feature",
  83. help="Output features for a single specific "
  84. "feature.")
  85. def current_args(self, subparsers):
  86. """Implementscurrent argparse subparser"""
  87. parser = subparsers.add_parser("current",
  88. formatter_class=argparse.RawTextHelpFormatter,
  89. description="Output table in ReST "
  90. "compatible ASCII format "
  91. "with features for this "
  92. "machine's architecture")
  93. self.parse_feat(parser)
  94. parser.set_defaults(func=self.run_current)
  95. def rest_args(self, subparsers):
  96. """Implement rest argparse subparser"""
  97. parser = subparsers.add_parser("rest",
  98. formatter_class=argparse.RawTextHelpFormatter,
  99. description="Output table(s) in ReST "
  100. "compatible ASCII format "
  101. "with features in ReST "
  102. "markup language. The "
  103. "output is affected by "
  104. "--arch or --feat/--feature"
  105. " flags.")
  106. self.parse_arch(parser)
  107. self.parse_feat(parser)
  108. parser.set_defaults(func=self.run_rest)
  109. def list_args(self, subparsers):
  110. """Implement list argparse subparser"""
  111. parser = subparsers.add_parser("list",
  112. formatter_class=argparse.RawTextHelpFormatter,
  113. description="List features for this "
  114. "machine's architecture, "
  115. "using an easier to parse "
  116. "format. The output is "
  117. "affected by --arch flag.")
  118. self.parse_arch(parser)
  119. self.parse_feat(parser)
  120. parser.set_defaults(func=self.run_list)
  121. def validate_args(self, subparsers):
  122. """Implement validate argparse subparser"""
  123. parser = subparsers.add_parser("validate",
  124. formatter_class=argparse.RawTextHelpFormatter,
  125. description="Validate the contents of "
  126. "the files under "
  127. f"{DEFAULT_DIR}.")
  128. parser.set_defaults(func=self.run_parser)
  129. def parser(self):
  130. """
  131. Create an arparse with common options and several subparsers
  132. """
  133. parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
  134. parser.add_argument("-d", "--debug", action="count", default=0,
  135. help="Put the script in verbose mode, useful for "
  136. "debugging. Can be called multiple times, to "
  137. "increase verbosity.")
  138. parser.add_argument("--directory", "--dir", default=DEFAULT_DIR,
  139. help="Changes the location of the Feature files. "
  140. f"By default, it uses the {DEFAULT_DIR} "
  141. "directory.")
  142. parser.add_argument("--enable-fname", action="store_true",
  143. help="Prints the file name of the feature files. "
  144. "This can be used in order to track "
  145. "dependencies during documentation build.")
  146. subparsers = parser.add_subparsers()
  147. self.current_args(subparsers)
  148. self.rest_args(subparsers)
  149. self.list_args(subparsers)
  150. self.validate_args(subparsers)
  151. args = parser.parse_args()
  152. return args
  153. def main():
  154. """Main program"""
  155. feat = GetFeature()
  156. args = feat.parser()
  157. if "func" in args:
  158. args.func(args)
  159. else:
  160. sys.exit(f"Please specify a valid command for {sys.argv[0]}")
  161. # Call main method
  162. if __name__ == "__main__":
  163. main()