Skip to content

Commit

Permalink
Fix: Clean up generated parser files
Browse files Browse the repository at this point in the history
The setup.py file overrides the build step to generate parsetab.py and
lextab.py, but does not clean them up. This adds an override to the
clean step to remove them again.

Note that this uses the distutils version of the command as a basis,
since (unlike the build_py step) setuptools does not provide its own
version of it but just uses the distutils version.
  • Loading branch information
matthijskooijman authored and glx22 committed Dec 1, 2023
1 parent 649623c commit fed1a02
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from setuptools import Distribution, Extension, find_packages, setup

import contextlib
import os

try:
# Update the version by querying git if possible.
from nml import version_update
Expand All @@ -16,6 +19,7 @@

default_dist = Distribution()
default_build_py = default_dist.get_command_class('build_py')
default_clean = default_dist.get_command_class('clean')


class NMLBuildPy(default_build_py):
Expand All @@ -28,6 +32,18 @@ def run(self):
super().run()


class NMLClean(default_clean):
def run(self):
# Remove python files generated by custom build command above
with contextlib.suppress(FileNotFoundError):
os.remove('nml/generated/parsetab.py')
with contextlib.suppress(FileNotFoundError):
os.remove('nml/generated/lextab.py')

# Then continue with the normal setuptools build.
super().run()


setup(
name="nml",
version=NML_VERSION,
Expand Down Expand Up @@ -62,5 +78,5 @@ def run(self):
"Pillow>=3.4",
"ply",
],
cmdclass={"build_py": NMLBuildPy},
cmdclass={"build_py": NMLBuildPy, 'clean': NMLClean},
)

0 comments on commit fed1a02

Please sign in to comment.