-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathsetup.py
111 lines (86 loc) · 4.25 KB
/
setup.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
import argparse
import os
import platform
import shutil
import subprocess
class Setup:
def __init__(self, args):
self.currentOS = platform.system()
self.args = args
self.command = None
self.pythonPrefix = "python3" if self.currentOS != "Windows" else "python"
self.pipPrefix = "pip3" if self.currentOS != "Windows" else "pip"
self.rootDir = os.path.dirname(os.path.realpath(__file__))
self.envPath = os.path.join(self.rootDir, "env")
self.envActivation = ". env/bin/activate" if self.currentOS != "Windows" else r"env\Scripts\activate.bat"
self.educativeScraperFilePath = os.path.join(self.rootDir, "EducativeScraper.py")
self.tempSetupFilePath = os.path.join(self.rootDir, "tempDir", "EducativeScraper.py")
self.tempDirPath = os.path.join(self.rootDir, "tempDir")
self.iconRoot = os.path.join(self.rootDir, "src", "Common")
os.chdir(self.rootDir)
def installVirtualEnvInLinux(self):
self.command = f"sudo apt-get install python3-virtualenv -y && exit"
subprocess.run(self.command, shell=True)
def installTkinterInLinux(self):
self.command = f"sudo apt-get install python3-tk -y && exit"
subprocess.run(self.command, shell=True)
def installPython3DevInLinux(self):
self.command = f"sudo apt-get install python3-dev -y && exit"
subprocess.run(self.command, shell=True)
def installDependencies(self):
self.removeFolderIfExists(self.envPath)
self.command = f"{self.pythonPrefix} -m venv env && {self.envActivation} && {self.pipPrefix} install -r requirements.txt && exit"
if self.currentOS == "Linux":
self.installTkinterInLinux()
self.installPython3DevInLinux()
self.installVirtualEnvInLinux()
self.command = f"virtualenv env && {self.envActivation} && {self.pipPrefix} install -r requirements.txt && exit"
subprocess.run(self.command, shell=True)
def createExecutable(self):
self.createFolderIfNotExists(self.tempDirPath)
self.createTempExecutableSetupFile()
self.getIconPath()
self.command = f"{self.envActivation} && pyinstaller --clean --noconfirm --onefile --console --icon \"{self.iconRoot}\" \"{self.tempSetupFilePath}\" && exit"
subprocess.run(self.command, shell=True)
self.removeFolderIfExists(self.tempDirPath)
def runScraper(self):
if os.path.isdir(self.envPath):
self.command = f"{self.envActivation} && {self.pythonPrefix} \"{self.educativeScraperFilePath}\" && exit"
subprocess.run(self.command, shell=True)
else:
print(f"Please run '{self.pythonPrefix} setup.py --install' to install the virtual env and dependencies")
def createTempExecutableSetupFile(self):
with open(self.tempSetupFilePath, "w") as f:
f.write(rf"""
import subprocess
import os
os.chdir(r"{self.rootDir}")
command = rf"{self.envActivation} && {self.pythonPrefix} \"{self.educativeScraperFilePath}\" && exit"
subprocess.call(command, shell=True)
""")
def getIconPath(self):
if self.currentOS == "Windows":
self.iconRoot = os.path.join(self.iconRoot, "icon.ico")
else:
self.iconRoot = os.path.join(self.iconRoot, "icon.png")
def createFolderIfNotExists(self, path):
if not os.path.isdir(path):
os.makedirs(path)
def removeFolderIfExists(self, path):
if os.path.isdir(path):
shutil.rmtree(path)
def generateAndExecuteCommand(self):
if self.args.install:
self.installDependencies()
elif self.args.create:
self.createExecutable()
elif self.args.run:
self.runScraper()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Educative Scraper Setup")
parser.add_argument("--install", action='store_true', default=False, help="Install required dependencies")
parser.add_argument("--run", action='store_true', default=True, help="Run the scraper")
parser.add_argument("--create", action='store_true', default=False, help="Create an executable file of the scraper")
args = parser.parse_args()
setup = Setup(args)
setup.generateAndExecuteCommand()