Skip to content

Commit 4ee2e99

Browse files
authored
Merge pull request #7 from SpotDraft/fix-binary-extraction-flow
Fix binary extraction flow
2 parents e362a65 + 342c214 commit 4ee2e99

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

docs/developer-guide.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ dotnet publish -c Release -r linux-x64 --self-contained
7878
dotnet publish -c Release -r win-x64 --self-contained
7979
```
8080

81-
3. Archive and package binaries into `./dist/`:
81+
3. Build a binary for MacOS:
82+
83+
```bash
84+
dotnet publish -c Release -r osx-x64 --self-contained
85+
```
86+
87+
4. Archive and package binaries into `./dist/`:
8288

8389

8490
## Running Tests

src/python_redlines/engines.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
class XmlPowerToolsEngine(object):
1414
def __init__(self):
15-
self.extracted_binaries_path = self._unzip_binary()
15+
self.extracted_binaries_path = self.__unzip_binary()
1616

17-
def _unzip_binary(self):
17+
def __unzip_binary(self):
1818
"""
1919
Unzips the appropriate C# binary for the current platform.
2020
"""
@@ -25,37 +25,60 @@ def _unzip_binary(self):
2525
if not os.path.exists(target_path):
2626
os.makedirs(target_path)
2727

28+
# Get the binary name and zip name based on the OS and architecture
29+
binary_name, zip_name = self.__get_binaries_info()
30+
31+
# Check if the binary already exists. If not, extract it.
32+
full_binary_path = os.path.join(target_path, binary_name)
33+
34+
if not os.path.exists(full_binary_path):
35+
zip_path = os.path.join(binaries_path, zip_name)
36+
self.__extract_binary(zip_path, target_path)
37+
38+
return os.path.join(target_path, binary_name)
39+
40+
def __extract_binary(self, zip_path: str, target_path: str):
41+
"""
42+
Extracts the binary from the zip file based on the extension. Supports .zip and .tar.gz files
43+
:parameter
44+
zip_path: str - The path to the zip file
45+
target_path: str - The path to extract the binary to
46+
"""
47+
48+
if zip_path.endswith('.zip'):
49+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
50+
zip_ref.extractall(target_path)
51+
52+
elif zip_path.endswith('.tar.gz'):
53+
with tarfile.open(zip_path, 'r:gz') as tar_ref:
54+
tar_ref.extractall(target_path)
55+
56+
def __get_binaries_info(self):
57+
"""
58+
Returns the binary name and zip name based on the OS and architecture
59+
:return
60+
binary_name: str - The name of the binary file
61+
zip_name: str - The name of the zip file
62+
"""
2863
os_name = platform.system().lower()
2964
arch = 'x64' # Assuming x64 architecture
3065

3166
if os_name == 'linux':
3267
zip_name = f"linux-{arch}-{__version__}.tar.gz"
3368
binary_name = 'linux-x64/redlines'
34-
zip_path = os.path.join(binaries_path, zip_name)
35-
if not os.path.exists(zip_path):
36-
with tarfile.open(zip_path, 'r:gz') as tar_ref:
37-
tar_ref.extractall(target_path)
3869

3970
elif os_name == 'windows':
4071
zip_name = f"win-{arch}-{__version__}.zip"
4172
binary_name = 'win-x64/redlines.exe'
42-
zip_path = os.path.join(binaries_path, zip_name)
43-
if not os.path.exists(zip_path):
44-
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
45-
zip_ref.extractall(target_path)
4673

4774
elif os_name == 'darwin':
4875
zip_name = f"osx-{arch}-{__version__}.tar.gz"
4976
binary_name = 'osx-x64/redlines'
50-
zip_path = os.path.join(binaries_path, zip_name)
51-
if not os.path.exists(zip_path):
52-
with tarfile.open(zip_path, 'r:gz') as tar_ref:
53-
tar_ref.extractall(target_path)
5477

5578
else:
5679
raise EnvironmentError("Unsupported OS")
5780

58-
return os.path.join(target_path, binary_name)
81+
return binary_name, zip_name
5982

6083
def run_redline(self, author_tag: str, original: Union[bytes, Path], modified: Union[bytes, Path]) \
6184
-> Tuple[bytes, Optional[str], Optional[str]]:

0 commit comments

Comments
 (0)