Skip to content

Commit 0ad1075

Browse files
authored
feat(ostags): apple silicon (#139)
* add OS tag for apple silicon macs: macarm * remove OSTags class and OSTagCvt enum * refactor OSTags.convert() -> convert_ostag() * update docs and tests
1 parent c3ec63e commit 0ad1075

3 files changed

Lines changed: 48 additions & 56 deletions

File tree

autotest/test_ostags.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from platform import system
1+
from platform import processor, system
22

33
import pytest
44

55
from modflow_devtools.ostags import (
6-
OSTag,
6+
convert_ostag,
77
get_binary_suffixes,
88
get_github_ostag,
99
get_modflow_ostag,
1010
)
1111

1212
_system = system()
13+
_processor = processor()
1314

1415

1516
def test_get_modflow_ostag():
@@ -19,7 +20,7 @@ def test_get_modflow_ostag():
1920
elif _system == "Linux":
2021
assert t == "linux"
2122
elif _system == "Darwin":
22-
assert t == "mac"
23+
assert t == "macarm" if _processor == "arm" else "mac"
2324
else:
2425
pytest.skip(reason="Unsupported platform")
2526

@@ -35,17 +36,17 @@ def test_get_github_ostag():
3536

3637

3738
@pytest.mark.parametrize(
38-
"cvt,tag,exp",
39+
"map,tag,exp",
3940
[
4041
("py2mf", "Windows", "win64"),
4142
("mf2py", "win64", "Windows"),
42-
("py2mf", "Darwin", "mac"),
43+
("py2mf", "Darwin", "macarm" if _processor == "arm" else "mac"),
4344
("mf2py", "mac", "Darwin"),
4445
("py2mf", "Linux", "linux"),
4546
("mf2py", "linux", "Linux"),
4647
("gh2mf", "Windows", "win64"),
4748
("mf2gh", "win64", "Windows"),
48-
("gh2mf", "macOS", "mac"),
49+
("gh2mf", "macOS", "macarm" if _processor == "arm" else "mac"),
4950
("mf2gh", "mac", "macOS"),
5051
("gh2mf", "Linux", "linux"),
5152
("mf2gh", "linux", "Linux"),
@@ -57,8 +58,8 @@ def test_get_github_ostag():
5758
("gh2py", "Linux", "Linux"),
5859
],
5960
)
60-
def test_ostag_convert(cvt, tag, exp):
61-
assert OSTag.convert(tag, cvt) == exp
61+
def test_convert_ostag(map, tag, exp):
62+
assert convert_ostag(tag, map) == exp
6263

6364

6465
def test_get_binary_suffixes():

docs/md/ostags.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Python3's `platform.system()` returns "Linux", "Darwin", and "Windows", respecti
1414

1515
GitHub Actions (e.g. `runner.os` context) use "Linux", "macOS" and "Windows".
1616

17-
MODFLOW 6 release asset names end with "linux", "mac" or "win64".
17+
MODFLOW 6 release asset names end with "linux", "mac" (Intel), "macarm", "win32", or "win64".
1818

1919
## Getting tags
2020

@@ -37,7 +37,8 @@ Conversion functions are available for each direction:
3737
Alternatively:
3838

3939
```python
40-
OSTag.convert(platform.system(), "py2mf")
40+
convert_ostag(platform.system(), "py2mf") # prints linux, mac, macarm, win32, or win64
41+
convert_ostag(platform.system(), "py2mf") # prints Linux, macOS, or Windows
4142
```
4243

4344
The second argument specifies the mapping in format `<source>2<target>`, where `<source>` and `<target>` may take values `py`, `mf`, or `gh`.

modflow_devtools/ostags.py

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
"""
55

66
import sys
7-
from enum import Enum
8-
from platform import system
7+
from platform import processor, system
98
from typing import Tuple
109

1110
_system = system()
11+
_processor = processor()
12+
13+
SUPPORTED_OSTAGS = ["linux", "mac", "macarm", "win32", "win64"]
1214

1315

1416
def get_modflow_ostag() -> str:
@@ -17,7 +19,7 @@ def get_modflow_ostag() -> str:
1719
elif _system == "Linux":
1820
return "linux"
1921
elif _system == "Darwin":
20-
return "mac"
22+
return "macarm" if _processor == "arm" else "mac"
2123
else:
2224
raise NotImplementedError(f"Unsupported system: {_system}")
2325

@@ -31,6 +33,15 @@ def get_github_ostag() -> str:
3133
raise NotImplementedError(f"Unsupported system: {_system}")
3234

3335

36+
def get_ostag(kind: str = "modflow") -> str:
37+
if kind == "modflow":
38+
return get_modflow_ostag()
39+
elif kind == "github":
40+
return get_github_ostag()
41+
else:
42+
raise ValueError(f"Invalid kind: {kind}")
43+
44+
3445
def get_binary_suffixes(ostag: str = None) -> Tuple[str, str]:
3546
"""
3647
Returns executable and library suffixes for the given OS tag, if provided,
@@ -55,10 +66,10 @@ def _suffixes(tag):
5566
return ".exe", ".dll"
5667
elif tag == "linux":
5768
return "", ".so"
58-
elif tag == "mac" or tag == "darwin":
69+
elif tag == "darwin" or "mac" in tag:
5970
return "", ".dylib"
6071
else:
61-
raise KeyError(f"unrecognized OS tag: {tag!r}")
72+
raise KeyError(f"Invalid OS tag: {tag!r}")
6273

6374
try:
6475
return _suffixes(ostag.lower())
@@ -89,9 +100,9 @@ def python_to_modflow_ostag(tag: str) -> str:
89100
elif tag == "Linux":
90101
return "linux"
91102
elif tag == "Darwin":
92-
return "mac"
103+
return "macarm" if _processor == "arm" else "mac"
93104
else:
94-
raise ValueError(f"Invalid or unsupported tag: {tag}")
105+
raise ValueError(f"Invalid tag: {tag}")
95106

96107

97108
def modflow_to_python_ostag(tag: str) -> str:
@@ -112,18 +123,18 @@ def modflow_to_python_ostag(tag: str) -> str:
112123
return "Windows"
113124
elif tag == "linux":
114125
return "Linux"
115-
elif tag == "mac":
126+
elif "mac" in tag:
116127
return "Darwin"
117128
else:
118-
raise ValueError(f"Invalid or unsupported tag: {tag}")
129+
raise ValueError(f"Invalid tag: {tag}")
119130

120131

121132
def modflow_to_github_ostag(tag: str) -> str:
122133
if tag == "win64":
123134
return "Windows"
124135
elif tag == "linux":
125136
return "Linux"
126-
elif tag == "mac":
137+
elif "mac" in tag:
127138
return "macOS"
128139
else:
129140
raise ValueError(f"Invalid modflow os tag: {tag}")
@@ -135,7 +146,7 @@ def github_to_modflow_ostag(tag: str) -> str:
135146
elif tag == "Linux":
136147
return "linux"
137148
elif tag == "macOS":
138-
return "mac"
149+
return "macarm" if _processor == "arm" else "mac"
139150
else:
140151
raise ValueError(f"Invalid github os tag: {tag}")
141152

@@ -148,39 +159,18 @@ def github_to_python_ostag(tag: str) -> str:
148159
return modflow_to_python_ostag(github_to_modflow_ostag(tag))
149160

150161

151-
def get_ostag(kind: str = "modflow") -> str:
152-
if kind == "modflow":
153-
return get_modflow_ostag()
154-
elif kind == "github":
155-
return get_github_ostag()
162+
def convert_ostag(tag: str, mapping: str) -> str:
163+
if mapping == "py2mf":
164+
return python_to_modflow_ostag(tag)
165+
elif mapping == "mf2py":
166+
return modflow_to_python_ostag(tag)
167+
elif mapping == "gh2mf":
168+
return github_to_modflow_ostag(tag)
169+
elif mapping == "mf2gh":
170+
return modflow_to_github_ostag(tag)
171+
elif mapping == "py2gh":
172+
return python_to_github_ostag(tag)
173+
elif mapping == "gh2py":
174+
return github_to_python_ostag(tag)
156175
else:
157-
raise ValueError(f"Invalid kind: {kind}")
158-
159-
160-
class OSTagCvt(Enum):
161-
py2mf = "py2mf"
162-
mf2py = "mf2py"
163-
gh2mf = "gh2mf"
164-
mf2gh = "mf2gh"
165-
py2gh = "py2gh"
166-
gh2py = "gh2py"
167-
168-
169-
class OSTag:
170-
@staticmethod
171-
def convert(tag: str, cvt: str) -> str:
172-
cvt = OSTagCvt(cvt)
173-
if cvt == OSTagCvt.py2mf:
174-
return python_to_modflow_ostag(tag)
175-
elif cvt == OSTagCvt.mf2py:
176-
return modflow_to_python_ostag(tag)
177-
elif cvt == OSTagCvt.gh2mf:
178-
return github_to_modflow_ostag(tag)
179-
elif cvt == OSTagCvt.mf2gh:
180-
return modflow_to_github_ostag(tag)
181-
elif cvt == OSTagCvt.py2gh:
182-
return python_to_github_ostag(tag)
183-
elif cvt == OSTagCvt.gh2py:
184-
return github_to_python_ostag(tag)
185-
else:
186-
raise ValueError(f"Unsupported mapping: {cvt}")
176+
raise ValueError(f"Invalid mapping: {mapping}")

0 commit comments

Comments
 (0)