Skip to content

Commit 88f531d

Browse files
bjlittlepp-mo
authored andcommitted
fix setup.py std_names building (SciTools#4952)
* fix setup.py std_names building * refactor
1 parent f6003ea commit 88f531d

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

setup.py

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
from setuptools import Command, setup
55
from setuptools.command.build_py import build_py
6-
from setuptools.command.develop import develop as develop_cmd
6+
from setuptools.command.develop import develop
77

88

99
class BaseCommand(Command):
10-
"""A valid no-op command for setuptools & distutils."""
10+
"""A minimal no-op setuptools command."""
1111

12-
description = "A no-op command."
13-
user_options = []
12+
description: str = "A no-op command."
13+
user_options: list = []
1414

1515
def initialize_options(self):
1616
pass
@@ -22,51 +22,64 @@ def run(self):
2222
pass
2323

2424

25-
def build_std_names(cmd, directory):
26-
# Call out to tools/generate_std_names.py to build std_names module.
25+
def custom_command(cmd, help=""):
26+
"""
27+
Factory function to generate a custom command that adds additional
28+
behaviour to build the CF standard names module.
2729
28-
script_path = os.path.join("tools", "generate_std_names.py")
29-
xml_path = os.path.join("etc", "cf-standard-name-table.xml")
30-
module_path = os.path.join(directory, "iris", "std_names.py")
31-
args = (sys.executable, script_path, xml_path, module_path)
32-
cmd.spawn(args)
30+
"""
3331

32+
class CustomCommand(cmd):
33+
description = help or cmd.description
3434

35-
def custom_cmd(command_to_override, functions, help_doc=""):
36-
"""
37-
Allows command specialisation to include calls to the given functions.
35+
def _build_std_names(self, directory):
36+
# Call out to tools/generate_std_names.py to build std_names module.
3837

39-
"""
38+
script_path = os.path.join("tools", "generate_std_names.py")
39+
xml_path = os.path.join("etc", "cf-standard-name-table.xml")
40+
module_path = os.path.join(directory, "iris", "std_names.py")
41+
args = (sys.executable, script_path, xml_path, module_path)
42+
self.spawn(args)
43+
44+
def finalize_options(self):
45+
# Execute the parent "cmd" class method.
46+
cmd.finalize_options(self)
4047

41-
class ExtendedCommand(command_to_override):
42-
description = help_doc or command_to_override.description
48+
if (
49+
not hasattr(self, "editable_mode")
50+
or self.editable_mode is None
51+
):
52+
# Default to editable i.e., applicable to "std_names" and
53+
# and "develop" commands.
54+
self.editable_mode = True
4355

4456
def run(self):
45-
# Run the original command first to make sure all the target
46-
# directories are in place.
47-
command_to_override.run(self)
57+
# Execute the parent "cmd" class method.
58+
cmd.run(self)
4859

60+
# Determine the target root directory
4961
if self.editable_mode:
50-
print(" [Running in-place]")
5162
# Pick the source dir instead (currently in the sub-dir "lib").
52-
dest = "lib"
63+
target = "lib"
64+
msg = "in-place"
5365
else:
5466
# Not editable - must be building.
55-
dest = self.build_lib
67+
target = self.build_lib
68+
msg = "as-build"
69+
70+
print(f"\n[Running {msg}]")
5671

57-
for func in functions:
58-
func(self, dest)
72+
# Build the CF standard names.
73+
self._build_std_names(target)
5974

60-
return ExtendedCommand
75+
return CustomCommand
6176

6277

6378
custom_commands = {
64-
"develop": custom_cmd(develop_cmd, [build_std_names]),
65-
"build_py": custom_cmd(build_py, [build_std_names]),
66-
"std_names": custom_cmd(
67-
BaseCommand,
68-
[build_std_names],
69-
help_doc="generate CF standard name module",
79+
"develop": custom_command(develop),
80+
"build_py": custom_command(build_py),
81+
"std_names": custom_command(
82+
BaseCommand, help="generate CF standard names"
7083
),
7184
}
7285

0 commit comments

Comments
 (0)