Zane Shelley | 2215d23 | 2023-04-07 14:43:40 -0500 | [diff] [blame] | 1 | import os |
| 2 | |
| 3 | from parse_chip_data import gen_peltool_json |
| 4 | from setuptools import setup |
| 5 | from setuptools.command.build_py import build_py |
| 6 | |
| 7 | # Typically in files like this we'd use find_packages() to traverse directories |
| 8 | # for any static packages. However, we are trying to add data to a package that |
| 9 | # will actually exist in another repository. Therefore, we have to explicitly |
| 10 | # list out the package name, directory, and data information. |
| 11 | |
| 12 | # We are building data for the following module: |
| 13 | package_name = "pel.hwdiags" |
| 14 | |
| 15 | # Since we are not using find_packages() we have to provide a package directory, |
| 16 | # but in this case nothing exists because there are no static package |
| 17 | # directories. Therefore, we will just use the empty string. |
| 18 | package_dir = "" |
| 19 | |
| 20 | # Split the package data directory into its components. |
| 21 | data_dir_components = [*package_name.split("."), "data"] |
| 22 | |
| 23 | # It is important to note that '/' must be used as the path separator, even on |
| 24 | # Windows. Setuptools will automatically convert the slashes where appropriate. |
| 25 | package_data_glob = "/".join(data_dir_components) |
| 26 | |
| 27 | |
| 28 | # This is a custom build class that is used to dynamically build the data files. |
| 29 | class my_build_py(build_py): |
Caleb Palmer | ae60d8e | 2024-08-28 16:09:20 -0500 | [diff] [blame^] | 30 | user_options = build_py.user_options + [ |
| 31 | ("chipConfig=", None, "List of chip IDs to build PEL parser JSON") |
| 32 | ] |
| 33 | |
| 34 | def initialize_options(self): |
| 35 | # Default chip_config option is all valid chip types |
| 36 | self.chipConfig = ["p10_10", "p10_20", "explorer", "odyssey"] |
| 37 | return super().initialize_options() |
| 38 | |
| 39 | def finalize_options(self): |
| 40 | if not isinstance(self.chipConfig, list): |
| 41 | self.chipConfig = self.chipConfig.split(",") |
| 42 | return super().finalize_options() |
| 43 | |
Zane Shelley | 2215d23 | 2023-04-07 14:43:40 -0500 | [diff] [blame] | 44 | def run(self): |
| 45 | if not self.dry_run: # honor --dry-run flag |
| 46 | # Make sure the build directory for the data exists. |
| 47 | # Yes, os.path.join() is necessary in this case, which is different |
| 48 | # that what is stated above regarding package_data_glob. |
| 49 | data_dir = os.path.join(self.build_lib, *data_dir_components) |
| 50 | self.mkpath(data_dir) |
| 51 | |
| 52 | # Generate the PEL parser data JSON from the Chip Data XML. |
Caleb Palmer | ae60d8e | 2024-08-28 16:09:20 -0500 | [diff] [blame^] | 53 | for chip in self.chipConfig: |
Zane Shelley | 2215d23 | 2023-04-07 14:43:40 -0500 | [diff] [blame] | 54 | gen_peltool_json(chip, data_dir) |
| 55 | |
| 56 | # Call the superclass run() to ensure everything else builds. |
| 57 | super().run() |
| 58 | |
| 59 | |
| 60 | setup( |
| 61 | name="openpower-hw-diags-pel-parser-data", |
| 62 | version=os.getenv("PELTOOL_VERSION", "1.0"), |
| 63 | classifiers=["License :: OSI Approved :: Apache Software License"], |
| 64 | cmdclass={"build_py": my_build_py}, # register custom build class |
| 65 | packages=[package_name], |
| 66 | package_dir={package_name: package_dir}, |
| 67 | package_data={package_name: [package_data_glob]}, |
| 68 | ) |