diff options
Diffstat (limited to 'setup.py')
| -rw-r--r-- | setup.py | 232 |
1 files changed, 232 insertions, 0 deletions
diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..b59e23f --- /dev/null +++ b/setup.py | |||
| @@ -0,0 +1,232 @@ | |||
| 1 | import os | ||
| 2 | import platform | ||
| 3 | import shutil | ||
| 4 | import sys | ||
| 5 | import traceback | ||
| 6 | import typing as p | ||
| 7 | |||
| 8 | term = shutil.get_terminal_size() | ||
| 9 | sep = "=" * term.columns | ||
| 10 | |||
| 11 | venv = False | ||
| 12 | default = 1 | ||
| 13 | |||
| 14 | if os.name == "nt": | ||
| 15 | print(f"Setup not supported on {platform.system()}") | ||
| 16 | exit() | ||
| 17 | |||
| 18 | |||
| 19 | def _opt_selector(options: p.Dict[str, p.Any], default: p.Optional[int] = None, pmt: str = "") -> p.Tuple[int, p.Any]: | ||
| 20 | _options = {} | ||
| 21 | print("Choose variant") | ||
| 22 | print(sep) | ||
| 23 | |||
| 24 | num = 1 | ||
| 25 | for opt in options: | ||
| 26 | if options[opt] is None: | ||
| 27 | print() | ||
| 28 | continue | ||
| 29 | |||
| 30 | d = "*" if default == num else " " | ||
| 31 | |||
| 32 | print(f" {d}{num} - {opt}") | ||
| 33 | _options[num] = options[opt] | ||
| 34 | num += 1 | ||
| 35 | print(sep) | ||
| 36 | |||
| 37 | while True: | ||
| 38 | opt = input(f"{pmt}-> ") | ||
| 39 | if opt.isdigit(): | ||
| 40 | opt = int(opt) | ||
| 41 | if opt in _options: | ||
| 42 | return opt, _options[opt] | ||
| 43 | if opt == "" and default is not None: | ||
| 44 | return default, _options[default] | ||
| 45 | |||
| 46 | print("Incorrect answer") | ||
| 47 | pmt = "" | ||
| 48 | |||
| 49 | |||
| 50 | def _yes(default: bool = True) -> bool: | ||
| 51 | while True: | ||
| 52 | if default: | ||
| 53 | it = "[Y|n]" | ||
| 54 | else: | ||
| 55 | it = "[y|N]" | ||
| 56 | |||
| 57 | i = input(f"{it} -> ") | ||
| 58 | if i.lower() in ["y", "n"]: | ||
| 59 | return i.lower() == "y" | ||
| 60 | if i == "": | ||
| 61 | return default | ||
| 62 | |||
| 63 | print("Incorrect answer") | ||
| 64 | |||
| 65 | |||
| 66 | def _input(prompt: str, default: str = None, required: bool = True) -> str: | ||
| 67 | if default: | ||
| 68 | print(f"Default {default}") | ||
| 69 | while True: | ||
| 70 | result = input(f"{prompt} -> ") | ||
| 71 | if not result: | ||
| 72 | if default: | ||
| 73 | result = default | ||
| 74 | break | ||
| 75 | elif required: | ||
| 76 | print("Parameter required") | ||
| 77 | else: | ||
| 78 | break | ||
| 79 | |||
| 80 | return result | ||
| 81 | |||
| 82 | |||
| 83 | def _cmd(cmd: str, show_cmd: bool = True) -> bool: | ||
| 84 | if show_cmd: | ||
| 85 | input(f"Press enter for execute {cmd}") | ||
| 86 | result = os.system(cmd) | ||
| 87 | return True if result == 0 else False | ||
| 88 | |||
| 89 | |||
| 90 | def _clear(): | ||
| 91 | print() | ||
| 92 | print('\033c', end="") | ||
| 93 | |||
| 94 | |||
| 95 | def _enter(): | ||
| 96 | input("Press enter, to continue ...") | ||
| 97 | |||
| 98 | |||
| 99 | def systemd_unit_generator(): | ||
| 100 | while True: | ||
| 101 | username = _input("Your username", os.environ["USER"]) | ||
| 102 | path = _input("Path to root of project", os.path.dirname(os.path.abspath(__file__))) | ||
| 103 | py_path = _input("Path to python 3.9", sys.executable) | ||
| 104 | |||
| 105 | print("Send kill (if SIGTERM Timeout)") | ||
| 106 | send_kill = "off" | ||
| 107 | if _yes(True): | ||
| 108 | send_kill = "on" | ||
| 109 | |||
| 110 | with open("FunnyPineappleBot.sample.service", "r") as file: | ||
| 111 | sample = file.read().format(username=username, path=path, py_path=py_path, send_kill=send_kill) | ||
| 112 | |||
| 113 | _clear() | ||
| 114 | print( | ||
| 115 | f"{sep}", | ||
| 116 | f"{sample}", | ||
| 117 | f"{sep}", | ||
| 118 | f"Your username - {username}", | ||
| 119 | f"Path to root of project - {path}", | ||
| 120 | f"Path to python 3.9 - {py_path}", | ||
| 121 | f"Send kill - {send_kill}", | ||
| 122 | f"{sep}", | ||
| 123 | "", | ||
| 124 | "All correct ?", | ||
| 125 | sep="\n" | ||
| 126 | ) | ||
| 127 | if _yes(False): | ||
| 128 | with open("FunnyPineappleBot.service", "w") as file: | ||
| 129 | file.write(sample) | ||
| 130 | _clear() | ||
| 131 | |||
| 132 | print("Link unit from /etc/systemd/system/ ?") | ||
| 133 | if _yes(): | ||
| 134 | _cmd("sudo mv ToolKit.service /etc/systemd/system/") | ||
| 135 | _cmd("sudo ln /etc/systemd/system/ToolKit.service ./ -s") | ||
| 136 | break | ||
| 137 | else: | ||
| 138 | _clear() | ||
| 139 | print(sep) | ||
| 140 | _enter() | ||
| 141 | return True | ||
| 142 | |||
| 143 | |||
| 144 | def config_generator(): | ||
| 145 | try: | ||
| 146 | import config | ||
| 147 | except ImportError: | ||
| 148 | config = object() | ||
| 149 | while True: | ||
| 150 | main_token = _input("Main bot token", getattr(config, "main_token", None)) | ||
| 151 | test_token = _input("Test bot token", getattr(config, "test_token", None)) | ||
| 152 | |||
| 153 | with open("shared/config.sample.py", "r") as file: | ||
| 154 | sample = file.read().format(main_token=main_token, test_token=test_token) | ||
| 155 | |||
| 156 | print( | ||
| 157 | f"{sep}", | ||
| 158 | f"{sample}", | ||
| 159 | f"{sep}", | ||
| 160 | "All correct ?", | ||
| 161 | sep="\n" | ||
| 162 | ) | ||
| 163 | |||
| 164 | if _yes(False): | ||
| 165 | with open("shared/config.py", "w") as file: | ||
| 166 | file.write(sample) | ||
| 167 | break | ||
| 168 | else: | ||
| 169 | _clear() | ||
| 170 | return True | ||
| 171 | |||
| 172 | |||
| 173 | def install_dependencies(): | ||
| 174 | with open("dependencies", "r") as file: | ||
| 175 | dependencies = file.read() | ||
| 176 | print( | ||
| 177 | "Install this ?", | ||
| 178 | f"{sep}", | ||
| 179 | f"{dependencies}", | ||
| 180 | f"{sep}", | ||
| 181 | sep="\n" | ||
| 182 | ) | ||
| 183 | _cmd("pip install -U -r dependencies", False) | ||
| 184 | print(f"{sep}\nSuccessfully installed") | ||
| 185 | _enter() | ||
| 186 | return True | ||
| 187 | |||
| 188 | |||
| 189 | if __name__ == '__main__': | ||
| 190 | opts = { | ||
| 191 | "Setup systemd unit": systemd_unit_generator, | ||
| 192 | "Setup config.py": config_generator, | ||
| 193 | "Install or Update dependencies": install_dependencies, | ||
| 194 | "Exit": exit, | ||
| 195 | } | ||
| 196 | exit_index = list(opts.keys()).index("Exit") + 1 | ||
| 197 | default = 1 | ||
| 198 | pmt = "" | ||
| 199 | |||
| 200 | _clear() | ||
| 201 | while True: | ||
| 202 | if default >= exit_index: | ||
| 203 | default = exit_index | ||
| 204 | term = shutil.get_terminal_size() | ||
| 205 | sep = "=" * term.columns | ||
| 206 | try: | ||
| 207 | _clear() | ||
| 208 | num, opt = _opt_selector(opts, default, pmt) | ||
| 209 | _clear() | ||
| 210 | res = opt() | ||
| 211 | _clear() | ||
| 212 | |||
| 213 | if num != default: | ||
| 214 | default = num + 1 | ||
| 215 | elif res is True: | ||
| 216 | default += 1 | ||
| 217 | |||
| 218 | if res is not True: | ||
| 219 | pmt = f"{res!r} " | ||
| 220 | if num == default: | ||
| 221 | default = num + 1 | ||
| 222 | else: | ||
| 223 | pmt = "" | ||
| 224 | |||
| 225 | except KeyboardInterrupt: | ||
| 226 | break | ||
| 227 | except Exception as e: | ||
| 228 | pmt = f"An error has occurred ({e.__class__.__name__}:{e.args[0]})" | ||
| 229 | trc = traceback.format_exc() | ||
| 230 | print(trc) | ||
| 231 | _enter() | ||
| 232 | _clear() | ||
