|
1 | 1 | import subprocess |
2 | 2 | import tempfile |
3 | | -import logging |
4 | 3 | import os |
5 | 4 | import platform |
| 5 | +import logging |
6 | 6 | import zipfile |
7 | 7 | import tarfile |
8 | 8 | from pathlib import Path |
|
14 | 14 |
|
15 | 15 |
|
16 | 16 | class XmlPowerToolsEngine(object): |
17 | | - def __init__(self, reload_binary: bool = False): |
18 | | - self.extracted_binaries_path = self._unzip_binary() |
19 | | - self._reload_binary = reload_binary |
| 17 | + def __init__(self): |
| 18 | + self.extracted_binaries_path = self.__unzip_binary() |
20 | 19 |
|
21 | | - def _unzip_binary(self): |
| 20 | + def __unzip_binary(self): |
22 | 21 | """ |
23 | 22 | Unzips the appropriate C# binary for the current platform. |
24 | 23 | """ |
25 | 24 | base_path = os.path.dirname(__file__) |
26 | | - logger.debug(f"Python redlining base path: {base_path}") |
27 | | - |
28 | 25 | binaries_path = os.path.join(base_path, 'dist') |
29 | | - logger.debug(f"Python redlining binaries path: {binaries_path}") |
| 26 | + target_path = os.path.join(base_path, 'bin') |
| 27 | + |
| 28 | + if not os.path.exists(target_path): |
| 29 | + os.makedirs(target_path) |
| 30 | + |
| 31 | + # Get the binary name and zip name based on the OS and architecture |
| 32 | + binary_name, zip_name = self.__get_binaries_info() |
30 | 33 |
|
| 34 | + # Check if the binary already exists. If not, extract it. |
| 35 | + full_binary_path = os.path.join(target_path, binary_name) |
| 36 | + |
| 37 | + if not os.path.exists(full_binary_path): |
| 38 | + zip_path = os.path.join(binaries_path, zip_name) |
| 39 | + self.__extract_binary(zip_path, target_path) |
| 40 | + |
| 41 | + return os.path.join(target_path, binary_name) |
| 42 | + |
| 43 | + def __extract_binary(self, zip_path: str, target_path: str): |
| 44 | + """ |
| 45 | + Extracts the binary from the zip file based on the extension. Supports .zip and .tar.gz files |
| 46 | + :parameter |
| 47 | + zip_path: str - The path to the zip file |
| 48 | + target_path: str - The path to extract the binary to |
| 49 | + """ |
| 50 | + print(f"") |
| 51 | + if zip_path.endswith('.zip'): |
| 52 | + with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
| 53 | + zip_ref.extractall(target_path) |
| 54 | + |
| 55 | + elif zip_path.endswith('.tar.gz'): |
| 56 | + with tarfile.open(zip_path, 'r:gz') as tar_ref: |
| 57 | + tar_ref.extractall(target_path) |
| 58 | + |
| 59 | + def __get_binaries_info(self): |
| 60 | + """ |
| 61 | + Returns the binary name and zip name based on the OS and architecture |
| 62 | + :return |
| 63 | + binary_name: str - The name of the binary file |
| 64 | + zip_name: str - The name of the zip file |
| 65 | + """ |
31 | 66 | os_name = platform.system().lower() |
32 | 67 | arch = 'x64' # Assuming x64 architecture |
33 | 68 |
|
34 | 69 | if os_name == 'linux': |
35 | 70 | zip_name = f"linux-{arch}-{__version__}.tar.gz" |
36 | 71 | binary_name = 'linux-x64/redlines' |
37 | 72 |
|
38 | | - elif os_name == "windows": |
| 73 | + elif os_name == 'windows': |
39 | 74 | zip_name = f"win-{arch}-{__version__}.zip" |
40 | 75 | binary_name = 'win-x64/redlines.exe' |
41 | 76 |
|
42 | | - elif os_name == "darwin": |
| 77 | + elif os_name == 'darwin': |
43 | 78 | zip_name = f"osx-{arch}-{__version__}.tar.gz" |
44 | 79 | binary_name = 'osx-x64/redlines' |
45 | | - else: |
46 | | - raise EnvironmentError("Unsupported OS") |
47 | | - |
48 | | - target_path = os.path.join(base_path, 'bin') |
49 | | - logger.debug(f"Target path: {target_path}") |
50 | 80 |
|
51 | | - # If target folder doesn't exist... created |
52 | | - if not os.path.exists(target_path): |
53 | | - os.makedirs(target_path) |
54 | | - else: |
55 | | - # If we don't want to reload the binary and it already exists... just return path |
56 | | - if not self._reload_binary: |
57 | | - return os.path.join(target_path, binary_name) |
58 | | - |
59 | | - # Otherwise, go ahead and unzip... and this may vary depending on the architecture & env |
60 | | - if os_name == 'linux': |
61 | | - zip_path = os.path.join(binaries_path, zip_name) |
62 | | - if os.path.exists(zip_path): |
63 | | - with tarfile.open(zip_path, 'r:gz') as tar_ref: |
64 | | - tar_ref.extractall(target_path) |
65 | | - |
66 | | - elif os_name == 'windows': |
67 | | - zip_path = os.path.join(binaries_path, zip_name) |
68 | | - if os.path.exists(zip_path): |
69 | | - with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
70 | | - zip_ref.extractall(target_path) |
71 | | - |
72 | | - elif os_name == 'darwin': |
73 | | - zip_path = os.path.join(binaries_path, zip_name) |
74 | | - if os.path.exists(zip_path): |
75 | | - with tarfile.open(zip_path, 'r:gz') as tar_ref: |
76 | | - tar_ref.extractall(target_path) |
77 | | - # This is redundant given above test... but I'm leaving it here. |
78 | 81 | else: |
79 | 82 | raise EnvironmentError("Unsupported OS") |
80 | 83 |
|
81 | | - return os.path.join(target_path, binary_name) |
| 84 | + return binary_name, zip_name |
82 | 85 |
|
83 | 86 | def run_redline(self, author_tag: str, original: Union[bytes, Path], modified: Union[bytes, Path]) \ |
84 | 87 | -> Tuple[bytes, Optional[str], Optional[str]]: |
|
0 commit comments