1
+ import argparse
2
+ import fnmatch
3
+ import os
4
+ import shutil
5
+ import subprocess
6
+
7
+ from distutils import dir_util
8
+
9
+ if os .name == "nt" :
10
+ default_prefix = "C:\\ Program Files"
11
+ script_sufix = ".bat"
12
+ else :
13
+ default_prefix = "/usr/local"
14
+ script_sufix = ".sh"
15
+
16
+ def installUI (src_dir , build_dir , install_dir ):
17
+ ui_build_dir = os .path .join (build_dir , "ui" )
18
+ if os .path .exists (ui_build_dir ):
19
+ shutil .rmtree (ui_build_dir )
20
+
21
+ src_dir = os .path .join (src_dir , "src" , "ui" )
22
+ shutil .copytree (src_dir , ui_build_dir )
23
+
24
+ cmd_npm_install = "npm install"
25
+ cmd_npx_build = f"npx electron-packager . --out={ install_dir } --overwrite"
26
+
27
+ print (f"Installing OpenLeetCodeUI dependencies..." )
28
+ result = subprocess .run (cmd_npm_install ,
29
+ stdout = subprocess .PIPE ,
30
+ stderr = subprocess .STDOUT ,
31
+ shell = True ,
32
+ cwd = ui_build_dir )
33
+ if result .returncode != 0 :
34
+ print (result .stdout .decode ('utf-8' ))
35
+ raise RuntimeError (f"Error running the command: { cmd_npm_install } " )
36
+
37
+ print (f"Building OpenLeetCodeUI..." )
38
+ result = subprocess .run (cmd_npx_build ,
39
+ stdout = subprocess .PIPE ,
40
+ stderr = subprocess .STDOUT ,
41
+ shell = True ,
42
+ cwd = ui_build_dir )
43
+ if result .returncode != 0 :
44
+ print (result .stdout .decode ('utf-8' ))
45
+ raise RuntimeError (f"Error running the command: { cmd_npx_build } " )
46
+
47
+ openleetcode_ui_dir = os .path .join (
48
+ install_dir ,
49
+ [
50
+ d for d in os .listdir (install_dir )
51
+ if fnmatch .fnmatch (d , "OpenLeetCodeUI-*-*" )
52
+ ][0 ]
53
+ )
54
+
55
+ new_openleetcode_ui_dir = os .path .join (install_dir , "OpenLeetCodeUI" )
56
+ if os .path .exists (new_openleetcode_ui_dir ):
57
+ shutil .rmtree (new_openleetcode_ui_dir )
58
+
59
+ os .rename (openleetcode_ui_dir , new_openleetcode_ui_dir )
60
+
61
+ if os .name == "nt" :
62
+ script = "openleetcodeui.bat"
63
+ elif os .name == "posix" :
64
+ script = "openleetcodeui.sh"
65
+ else :
66
+ raise RuntimeError ("Unsupported OS" )
67
+
68
+ print (f"Installing OpenLeetCodeUI..." )
69
+ script = os .path .join (src_dir , script )
70
+ if not os .path .exists (script ):
71
+ raise FileNotFoundError (f"No file found at { script } " )
72
+ shutil .copy (script , install_dir )
73
+
74
+ print ("OpenLeetCodeUI installed at" , new_openleetcode_ui_dir )
75
+
76
+ def installOpenLeetCode (src_dir , install_dir ):
77
+ print (f"Installing OpenLeetCode..." )
78
+ data_dir = os .path .join (src_dir , 'data' )
79
+ if not os .path .exists (data_dir ):
80
+ raise FileNotFoundError (f"No data directory found at { data_dir } " )
81
+
82
+ dir_util .copy_tree (data_dir , install_dir )
83
+
84
+ schema_file = os .path .join (src_dir , "src" , "schema" , "results_validation_schema.json" )
85
+ if not os .path .exists (schema_file ):
86
+ raise FileNotFoundError (f"No schema file found at { schema_file } " )
87
+ shutil .copy (schema_file , install_dir )
88
+
89
+ if os .name == "nt" :
90
+ script = "openleetcode.bat"
91
+ elif os .name == "posix" :
92
+ script = "openleetcode.sh"
93
+ else :
94
+ raise RuntimeError ("Unsupported OS" )
95
+
96
+ app_files = [
97
+ "functionextractor.py" ,
98
+ "logger.py" ,
99
+ "openleetcode.py" ,
100
+ "resultsvalidator.py" ,
101
+ "testrunner.py" ,
102
+ script
103
+ ]
104
+
105
+ for file in app_files :
106
+ file = os .path .join (src_dir , "src" , "app" , file )
107
+ if not os .path .exists (file ):
108
+ raise FileNotFoundError (f"No file found at { file } " )
109
+ shutil .copy (file , install_dir )
110
+
111
+ print ("OpenLeetCode installed at" , install_dir )
112
+
113
+ def main ():
114
+ print ("OpenLeetCode Installer" )
115
+
116
+ parser = argparse .ArgumentParser (description = "Installer for OpenLeetCode" )
117
+ parser .add_argument ("--build_dir" , help = "Build directory" , default = "build" )
118
+ parser .add_argument ("--prefix" , help = "Installation prefix" , default = default_prefix )
119
+ parser .add_argument ("--enable_ui" , help = "Enable UI installation" , action = "store_true" , default = False )
120
+
121
+ args = parser .parse_args ()
122
+
123
+ src_dir = os .path .dirname (os .path .abspath (__file__ ))
124
+ build_dir = os .path .abspath (args .build_dir )
125
+ if not os .path .exists (build_dir ):
126
+ os .makedirs (build_dir )
127
+
128
+ install_dir = os .path .abspath (os .path .join (args .prefix , "OpenLeetCode" ))
129
+ if not os .path .exists (install_dir ):
130
+ os .makedirs (install_dir )
131
+
132
+ installOpenLeetCode (src_dir , install_dir )
133
+
134
+ if args .enable_ui :
135
+ installUI (src_dir , build_dir , install_dir )
136
+
137
+ print (
138
+ f"Installation complete!\n You can now run OpenLeetCode using "
139
+ f"openleetcode{ script_sufix } "
140
+ f"{ ' or openleetcodeui' + script_sufix if args .enable_ui else '' } ."
141
+ )
142
+
143
+ if __name__ == '__main__' :
144
+ main ()
0 commit comments