-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.py
248 lines (201 loc) · 11.3 KB
/
create.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import sys
import os
import webbrowser
# from path import BASE_DIR
import platform, shutil, subprocess
# Importing settings_rewriter from utils module
from utils.settings_writter import settings_rewriter, static_writer
import random
import time
# Loading function
def Loading(value=True):
loading = ['|||||', '/////', '\\\\\\']
# loading = ['🌟🌟🌟🌟🌟', '💫💫💫💫💫', '✨✨✨✨✨', '🌈🌈🌈🌈🌈', '🌸🌸🌸🌸🌸', '🌼🌼🌼🌼🌼', '🌞🌞🌞🌞🌞', '🌛🌛🌛🌛🌛', '🌟💫✨💫🌟']
while value:
random.shuffle(loading) # Shuffle the loading icons randomly
for i in loading:
# print(f'Please wait...{i}', end="\r")
print(f'\rPlease wait...{i}', end="")
time.sleep(0.1)
value = False
global BASE_DIR # Declare BASE_DIR as a global variable
def main():
# projectname Missing from Command Line Arguments - solver
while True:
if len(sys.argv) <= 1:
project_name = input('You missed entering your project name. Please enter it here: ')
if project_name.strip():
sys.argv.append(project_name)
break
else:
continue
else:
break
# global BASE_DIR # Declare BASE_DIR as a global variable
# path selection section
while True:
project_loc = input("\nPlease select your project directory, enter one of the following options:\n'c' to create the project in the current directory\n'p' to create project with your custom path: ")
if project_loc.lower() == 'c':
BASE_DIR = os.path.join(os.getcwd(), '') # Combine the current working directory with an empty string to append the path separator
print('BASE_DIR: ', BASE_DIR)
break
elif project_loc.lower() == 'p':
# Create directory and its intermediate directories if they do not exist
try:
import tkinter
print("Please select your path..")
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", "tk"])
print("Please select your path..")
import tkinter as tk
from tkinter.filedialog import askdirectory
# Create a root window and hide it
root = tk.Tk()
root.withdraw()
# Ask for directory path
path = askdirectory()
BASE_DIR = path
if BASE_DIR.strip():
os.makedirs(BASE_DIR, exist_ok=True) # This will create "/path/to/new/directory" along with any intermediate directories
if not BASE_DIR.endswith(os.path.sep):
BASE_DIR = BASE_DIR + os.path.sep
print('BASE_DIR: ', BASE_DIR)
# Destroy the root window after getting the path
root.destroy()
break
else:
continue
Loading()
if BASE_DIR != "":
if not len(sys.argv) <= 1:
project_name = sys.argv[1]
# print(project_name)
project_name = project_name.replace(' ', '_')
# apps = sys.argv[2:]
system = platform.system()
base_dir = BASE_DIR
# project folder exist solver section
while True:
if os.path.exists(base_dir + project_name):
# value = input(f'Project "{project_name}" already exists in the directory. \nPlease enter (y) to delete the folder and create new project, \n(n) to recreate with new project name, \n(e) to exit from program')
value = input(f'\nThe project named "{project_name}" already exists within the directory, enter one of the options below:\n'
f"'d' to delete the folder and create a new project,\n"
f"'n' to recreate with a new project name, or\n"
f"'e' to exit from the program: ")
if value.lower() == 'd':
Loading()
shutil.rmtree(base_dir + project_name) # Remove directory and its contents
break
elif value.lower() == 'n':
project_name = input("Please enter a new project name: ")
project_name = project_name.replace(' ', '_')
break
elif value.lower() == 'e':
print("Your program stoped successfully")
sys.exit(0)
else:
# print("Wrong input you entered")
# sys.exit(0)
continue
break
apps = []
# admin app_name error solver
while True:
app_input = input("\nPlease enter your app names separated by space or Press 'Enter' to leave app creation: ")
if app_input.strip(): # Check if input is not empty or just whitespace
app_list = app_input.split()
if 'admin' in app_list:
exact_admin = False
for app in app_list:
if app == 'admin':
exact_admin = True
break
if exact_admin:
# app_conflict = input('\nThe app name "admin" will conflict with the default Django admin ("django.contrib.admin"). \nDo you want to re-enter the app names again? (y) to continue and (n) to leave app creation: ')
app_conflict = input("\nThe app name 'admin' will clash with the default Django admin ('django.contrib.admin'). \nTherefore, you need to modify the name 'admin' to something like 'admin_panel', 'admin_section', 'admin_profile', etc. \nWould you like to enter the app names again? Enter 'y' to continue or 'n' to leave app creation: ")
if not app_conflict.strip() or app_conflict.lower() == 'y':
continue
else:
break
else:
apps = app_list
break
else:
apps = app_list
break
else:
break
print(f'\nCreating your project on "{base_dir}" directory')
Loading()
# main project creation section
if system == "Windows" or system == "Linux" or system == "Darwin":
try:
# Creating a new directory
os.chdir(base_dir) # change directy
os.mkdir(project_name) # make a directy with user input
# virtual Environment setup, activtivating , installing django and required libraries etc......
os.chdir(base_dir + project_name)
os.system('pip install virtualenv')
os.system('virtualenv venv')
activate_script = ""
# checking which operating system
print(system, os.getcwd())
if system == "Windows":
activate_script = os.path.join(base_dir, project_name, 'venv', 'Scripts', 'activate')
Loading()
# Activate the virtual environment and install Django
subprocess.run(f'"{activate_script}" && pip install django', shell=True, check=True)
Loading()
subprocess.run(f'django-admin startproject {project_name} .', shell=True, check=True)
# Running python manage.py startapp command within the activated virtual environment
# print('length or apps: ############# ',len(apps))
Loading()
if len(apps) > 0:
for app in apps[::-1]:
subprocess.run(f'python manage.py startapp {app}', shell=True, check=True)
settings_rewriter(os.path.join(base_dir, project_name, project_name, 'settings.py'), app)
elif system == "Linux" or system == "Darwin":
activate_script = os.path.join(base_dir, project_name, 'venv', 'bin', 'activate')
# Install Django within the virtual environment
Loading()
subprocess.run(f'bash -c "source {activate_script} && pip install django"', shell=True, check=True)
Loading()
# Run django-admin startproject command within the activated virtual environment
subprocess.run(f'bash -c "source {activate_script} && django-admin startproject {project_name} ."', shell=True, check=True)
# Running python manage.py startapp command within the activated virtual environment
# print('length or apps: ############# ',len(apps))
Loading()
if len(apps) > 0:
for app in apps[::-1]:
subprocess.run(f'bash -c "source {activate_script} && python manage.py startapp {app}"', shell=True, check=True)
settings_rewriter(os.path.join(base_dir, project_name, project_name, 'settings.py'), app)
Loading()
static_writer(os.path.join(base_dir, project_name, project_name, 'settings.py'))
# Change directory to the project directory
Loading()
os.chdir(os.path.join(base_dir, project_name))
# Create static and template directories
Loading()
os.mkdir('static')
os.mkdir('template')
# Run the Django development server within the activated virtual environment
if system == "Windows":
Loading()
activate_script = os.path.join(base_dir, project_name, 'venv', 'Scripts', 'activate')
# Activate the virtual environment and install Django
subprocess.run(f'{activate_script}', shell=True, check=True)
subprocess.run(f'python manage.py migrate && python manage.py runserver', shell=True, check=True)
elif system == "Linux" or system == "Darwin":
Loading()
subprocess.run(f'bash -c "source {activate_script} && python manage.py migrate && python manage.py runserver"', shell=True)
except FileExistsError:
print(f'{project_name} folder already exits.')
else:
print("Unsupported operating system")
else:
print(f"Project 'name' required,\nplease follow these format 'python create.py <project_name>' ")
else:
# print('Please set BASE_DIR in path.py file and run the script again')
print('\nPlease select the path, and try again!')
if __name__ == "__main__":
main()