-
Notifications
You must be signed in to change notification settings - Fork 0
/
nipy.py
150 lines (119 loc) · 4.53 KB
/
nipy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# NiPy - [Nipe] re-coded in Python
# An engine to make Tor network your default gateway
# Nipe: https://github.com/htrgouvea/nipe
# Standard library imports
import os
import sys
import platform
from typing import NoReturn, Tuple
# Local application/library specific imports
from core.install import Install
from core.device import Device
from core.status import Status
from engine.start import Start
from engine.stop import Stop
# NiPy Class
class NiPy:
def __init__(self, argument: str) -> NoReturn:
"""
This method will handle argument and do specific process
based on given argument
Parameters:
argument (str): argument to be processed. one of:
- install
- start
- stop
- restart
- status
- help
Returns:
None (typing.NoReturn)
"""
# Set the argument
self.argument: str = argument.strip().lower()
# Initialize device info variables
self.username: str = ""
self.distribution: str = ""
# Initialize colors for pretty printing
self.red: str = "\33[1;31m"
self.green: str = "\33[1;32m"
self.blue: str = "\33[1;34m"
self.reset: str = "\33[m"
def process(self) -> NoReturn:
"""
Process method to do process with given argument
Parameters:
None
Returns:
None (typing.NoReturn)
"""
# Get argument
argument: str = self.argument
# Print Device info (will update self.username & self.distribution)
info: Tuple[str, str] = Device().get_device_info()
if info:
self.username, self.distribution = info
else:
raise SystemExit(f"[x] {self.red}Could not get system information!{self.reset}")
# Process [status] argument
if argument == "install":
print(f"\n [*] {self.blue}Installing NiPy... Be patient.{self.reset}\n")
Install(self.username,self.distribution).execute_installation()
# Process [status] argument
elif argument == "status":
status: str = Status().check_tor()
raise SystemExit(status)
# Process [start] argument
elif argument == "start":
if Start(self.argument, self.distribution).start_nipy():
raise SystemExit(f"\n [*]{self.green} Nipy Started.{self.reset}\n")
# Process [stop] argument
elif argument == "stop":
if Stop(self.argument, self.distribution).stop_nipy():
raise SystemExit(f"\n [*]{self.red} Nipy Stopped.{self.reset}\n")
# Process [restart] argument
elif argument == "restart":
# Stop the service
if Stop(self.argument, self.distribution).stop_nipy():
# Start the service again
Start(self.argument, self.distribution).start_nipy()
raise SystemExit(f"\n [*]{self.blue} Nipy Re started!{self.reset}\n")
# Process [help] argument
elif argument == "help":
raise SystemExit(
f"\n [!] Usage: python nipy.py [argument]\n\n"
f" - install | Install NiPy\n"
f" - start | Start the NiPy\n"
f" - stop | Stop the NiPy\n"
f" - restart | Re start the niPy\n"
f" - status | Show NiPy status\n"
f" - help | Show this message\n"
)
# Handle invalid argument
else:
raise SystemExit(f"\n [*]{self.red} Invalid argument!{self.reset}\n")
def main() -> NoReturn:
"""
Main function to handle NiPy with argument. This will also checks root
privilege (which is required), System (must be Linux in this case).
Parameters:
None
Returns:
NoReturn
"""
# Check argument
if len(sys.argv) < 2:
raise SystemExit("\n [!]\33[1;35m Usage: python nipy.py help\33[m\n")
# Check Platform system
if platform.system() != "Linux":
raise SystemExit("\n [x]\33[1;31m Sorry: Only for Linux Distributions!\33[m\n")
# Check Sudo privilege
if os.geteuid() != 0:
raise SystemExit("\n [x]\33[1;31m Error: Sudo privilege required!\33[m\n")
# Run NiPy
NiPy(sys.argv[1]).process()
# Run main program
if __name__ == "__main__":
main()