setuptools for openpower-hw-diags PEL parser data

This repo currently contains the chip data files used for isolation.
These data files will also be used for creating data files for the
openpower-hw-diags PEL parsers. This commit includes the necessary
setuptools files to install the PEL parser data in the appropriate
python modules.

Signed-off-by: Zane Shelley <zshelle@us.ibm.com>
Change-Id: Id93a56748b12aa0c3ce9a286324a952779d7041b
diff --git a/xml/setup.py b/xml/setup.py
new file mode 100644
index 0000000..1ae5d2f
--- /dev/null
+++ b/xml/setup.py
@@ -0,0 +1,61 @@
+
+from setuptools import setup
+from setuptools.command.build_py import build_py
+
+import os
+import subprocess
+
+# Typically in files like this we'd use find_packages() to traverse directories
+# for any static packages. However, we are trying to add data to a package that
+# will actually exist in another repository. Therefore, we have to explicitly
+# list out the package name, directory, and data information.
+
+# We are building data for the following module:
+package_name = 'pel.hwdiags'
+
+# Since we are not using find_packages() we have to provide a package directory,
+# but in this case nothing exists because there are no static package
+# directories. Therefore, we will just use the empty string.
+package_dir = ''
+
+# Split the package data directory into its components.
+data_dir_components = [ *package_name.split('.'), 'data' ]
+
+# It is important to note that '/' must be used as the path separator, even on
+# Windows. Setuptools will automatically convert the slashes where appropriate.
+package_data_glob = '/'.join( data_dir_components )
+
+# This is a custom build class that is used to dynamically build the data files.
+class my_build_py(build_py):
+    def run(self):
+        if not self.dry_run: # honor --dry-run flag
+
+            # Make sure the build directory for the data exists.
+            # Yes, os.path.join() is necessary in this case, which is different
+            # that what is stated above regarding package_data_glob.
+            data_dir = os.path.join(self.build_lib, *data_dir_components)
+            self.mkpath(data_dir)
+
+            # Generate the PEL parser data JSON from the Chip Data XML.
+            # TODO: The current tool to build the PEL parser JSON files is a
+            #       perl script. Eventually, it will be converted to a python
+            #       module.
+            # TODO: The list of data file directories will need to be
+            #       configurable via the package config in the bitbake recipes.
+            for chip in ('p10', 'explorer'):
+                subprocess.run([ './parse_chip_data_xml', '--json',
+                                 '-i', chip, '-o', data_dir ])
+
+        # Call the superclass run() to ensure everything else builds.
+        super().run()
+
+
+setup(
+    name         = 'openpower-hw-diags-pel-parser-data',
+    version      = '0.1',
+    classifiers  = [ 'License :: OSI Approved :: Apache Software License' ],
+    cmdclass     = { 'build_py': my_build_py }, # register custom build class
+    packages     = [ package_name ],
+    package_dir  = { package_name: package_dir },
+    package_data = { package_name: [ package_data_glob ] },
+)