12
12
13
13
class XmlPowerToolsEngine (object ):
14
14
def __init__ (self ):
15
- self .extracted_binaries_path = self ._unzip_binary ()
15
+ self .extracted_binaries_path = self .__unzip_binary ()
16
16
17
- def _unzip_binary (self ):
17
+ def __unzip_binary (self ):
18
18
"""
19
19
Unzips the appropriate C# binary for the current platform.
20
20
"""
@@ -25,37 +25,60 @@ def _unzip_binary(self):
25
25
if not os .path .exists (target_path ):
26
26
os .makedirs (target_path )
27
27
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
+ """
28
63
os_name = platform .system ().lower ()
29
64
arch = 'x64' # Assuming x64 architecture
30
65
31
66
if os_name == 'linux' :
32
67
zip_name = f"linux-{ arch } -{ __version__ } .tar.gz"
33
68
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 )
38
69
39
70
elif os_name == 'windows' :
40
71
zip_name = f"win-{ arch } -{ __version__ } .zip"
41
72
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 )
46
73
47
74
elif os_name == 'darwin' :
48
75
zip_name = f"osx-{ arch } -{ __version__ } .tar.gz"
49
76
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 )
54
77
55
78
else :
56
79
raise EnvironmentError ("Unsupported OS" )
57
80
58
- return os . path . join ( target_path , binary_name )
81
+ return binary_name , zip_name
59
82
60
83
def run_redline (self , author_tag : str , original : Union [bytes , Path ], modified : Union [bytes , Path ]) \
61
84
-> Tuple [bytes , Optional [str ], Optional [str ]]:
0 commit comments