100
100
101
101
try :
102
102
import os
103
+ import shutil
103
104
import platform
104
105
import subprocess
105
106
from sys import exit
106
107
from time import sleep
107
- from typing import List
108
+ from typing import List , NoReturn
108
109
except ModuleNotFoundError as import_error :
109
110
print (f"{ RED } [!] Error: python modules not found. Broken installation?:\n { import_error } { RESET } " )
110
111
@@ -122,6 +123,62 @@ def the_unix_manager_version() -> str:
122
123
return f"{ RED } [!] Error: 'VERSION.txt' file not found.\n Broken installation?{ RESET } "
123
124
124
125
126
+ def generate_configs (log : bool = None ) -> None :
127
+ """
128
+ Generates config files for theUnixManager.
129
+
130
+ Args:
131
+ log (bool): Enable/disable logging. None by default
132
+
133
+ Returns:
134
+ NoReturn: [null].
135
+ """
136
+
137
+ the_unix_manager_config_path : str = "/etc/theunixmanager/python"
138
+ the_unix_manager_version_path : str = "/etc/theunixmanager/python/VERSION.txt"
139
+ the_unix_manager_preferred_editor_path : str = "/etc/theunixmanager/python/EDITOR.txt"
140
+ the_unix_manager_root_command_path : str = "/etc/theunixmanager/python/COMMAND.txt"
141
+
142
+ try :
143
+ if not os .path .exists (the_unix_manager_config_path ):
144
+ if log :
145
+ print (f"{ RED } [!] Error: '/etc/theunixmanager/python' not found.{ RESET } " )
146
+ print (f"{ BLUE } [<==] Creating directories...{ RESET } " )
147
+ os .makedirs (the_unix_manager_config_path )
148
+ else :
149
+ if log :
150
+ print (f"{ GREEN } [*] theUnixManager config path found.{ RESET } " )
151
+
152
+ if not os .path .exists (the_unix_manager_version_path ):
153
+ if log :
154
+ print (f"{ RED } [!] Error: 'VERSION.txt' file not found.\n Broken installation?{ RESET } " )
155
+ print (f"{ BLUE } [<==] Creating file...{ RESET } " )
156
+ subprocess .run (["touch" , "/etc/theunixmanager/python/VERSION.txt" ], check = True )
157
+ else :
158
+ if log :
159
+ print (f"{ GREEN } [*] theUnixManager version path found.{ RESET } " )
160
+
161
+ if not os .path .exists (the_unix_manager_preferred_editor_path ):
162
+ if log :
163
+ print (f"{ RED } [!] Error: 'EDITOR.txt' not found.\n Broken installation?{ RESET } " )
164
+ print (f"{ BLUE } [<==] Creating file...{ RESET } " )
165
+ subprocess .run (["touch" , "/etc/theunixmanager/python/EDITOR.txt" ], check = True )
166
+ else :
167
+ if log :
168
+ print (f"{ GREEN } [*] theUnixManager preferred editor path found.{ RESET } " )
169
+
170
+ if not os .path .exists (the_unix_manager_root_command_path ):
171
+ if log :
172
+ print (f"{ RED } [!] Error: 'COMMAND.txt' not found.\n Broken installation?{ RESET } " )
173
+ print (f"{ BLUE } [<==] Creating file...{ RESET } " )
174
+ subprocess .run (["touch" , "/etc/theunixmanager/python/COMMAND.txt" ], check = True )
175
+ else :
176
+ if log :
177
+ print (f"{ GREEN } [*] theUnixManager root command path found.{ RESET } " )
178
+ except subprocess .CalledProcessError :
179
+ pass
180
+
181
+
125
182
def the_unix_manager_tester () -> None :
126
183
"""
127
184
Autotests.
@@ -528,58 +585,42 @@ def get_init_system() -> str:
528
585
529
586
def get_preferred_editor () -> str :
530
587
"""
531
- Gets user preffered text editor and stores it in '/etc/theunixmanager/bash /EDITOR.txt'.
588
+ Gets user preferred text editor and stores it in '/etc/theunixmanager/python /EDITOR.txt'.
532
589
533
590
Returns:
534
591
text_editor (str): User's preferred editor.
535
592
"""
536
593
537
594
editor_config_path : str = "/etc/theunixmanager/python/EDITOR.txt"
538
595
539
- if os .path .isfile (editor_file ):
540
- with open (editor_file , "r" ) as editor_config_file :
596
+ if os .path .isfile (editor_config_path ):
597
+ with open (editor_config_path , "r" ) as editor_config_file :
541
598
stored_editor : str = editor_config_file .read ().strip ()
542
599
if stored_editor :
543
600
return stored_editor
544
601
545
602
print (f"{ ORANGE } \n [!] Error: No preferred editor found at { editor_config_path } .{ RESET } " )
546
603
user_editor : str = input ("[==>] Enter your preferred editor manually: " )
547
- with open (editor_file , "w" ) as editor_config_file :
548
- file .write (user_editor )
604
+ with open (editor_config_path , "w" ) as editor_config_file :
605
+ editor_config_file .write (user_editor )
549
606
550
607
return user_editor
551
608
552
609
553
610
def sudo_or_doas () -> str | None :
554
611
"""
555
- Checks for the preferred command (either sudo or doas) in '/etc/theunixmanager/bash/COMMAND.txt'.
556
- If no preference is found, it checks whether sudo or doas is available on the system and stores
557
- the preference accordingly. If both are available, sudo is chosen.
612
+ Checks whether sudo or doas is available on the system and stores
613
+ the preference accordingly.
558
614
559
615
Returns:
560
- result (str): The preferred command ('sudo' or 'doas').
616
+ result (str | None ): The preferred command ('sudo' or 'doas').
561
617
"""
562
-
563
- command_file : str = "/etc/theunixmanager/python/EDITOR.txt"
564
-
565
- if os .path .isfile (command_file ):
566
- with open (command_file , "r" ) as command_config_file :
567
- stored_command : str = command_config_file .read ().strip ()
568
- if stored_command in ["sudo" , "doas" ]:
569
- return stored_command
570
618
571
- if which ("sudo" ):
572
- with open (command_file , "w" ) as command_config_file :
573
- command_config_file .write ("sudo" )
619
+ if shutil .which ("sudo" ):
574
620
return "sudo"
575
-
576
- elif which ("doas" ):
577
- with open (command_file , "w" ) as file :
578
- file .write ("doas" )
621
+ elif shutil .which ("doas" ):
579
622
return "doas"
580
-
581
623
else :
582
- print (f"{ RED } [!] Error: Neither sudo nor doas is available.{ RESET } " )
583
624
return None
584
625
585
626
@@ -2282,6 +2323,7 @@ def check_privileges(log: bool = False) -> bool:
2282
2323
exit (1 )
2283
2324
2284
2325
2285
- # if __name__ == "__main__":
2286
- # check_privileges()
2326
+ if __name__ == "__main__" :
2327
+ check_privileges ()
2328
+ generate_configs ()
2287
2329
# the_unix_manager_tester()
0 commit comments