Created
July 3, 2013 19:23
-
-
Save cladley/5921901 to your computer and use it in GitHub Desktop.
Script to setup a django project and create any app that have been passed in. Creates the templates,static/js,static/img, static/css in the root and each app. Also edits the settings.py file and adds a few default properties that I always use.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import sys | |
| import subprocess | |
| class FileHeadRule: | |
| def __init__(self): | |
| self.once = False | |
| def condition(self,line): | |
| if not self.once: | |
| self.once = True | |
| return True | |
| def action(self,line): | |
| str = "import os \n" | |
| str += 'PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))\n' | |
| return str | |
| class StaticDirsRule: | |
| def condition(self,line): | |
| return line.startswith('STATICFILES_DIRS = (') | |
| def action(self,line): | |
| str = '\tos.path.join(PROJECT_PATH,"static")\n' | |
| return str | |
| class TemplateDirsRule: | |
| def condition(self,line): | |
| return line.startswith('TEMPLATE_DIRS = (') | |
| def action(self,line): | |
| str = '\tos.path.join(PROJECT_PATH,"templates")\n' | |
| return str | |
| class SimpleParser: | |
| def __init__(self): | |
| self.rules = [] | |
| def addRule(self,rule): | |
| self.rules.append(rule) | |
| def parse(self,infile,outfile): | |
| for line in infile.readlines(): | |
| outfile.write(line) | |
| for rule in self.rules: | |
| if rule.condition(line): | |
| output = rule.action(line) | |
| outfile.write(output) | |
| continue | |
| def create_django_project(projectName): | |
| try: | |
| subprocess.call(['django-admin.py','startproject', projectName],shell=True) | |
| print("Created django project : ", projectName) | |
| except: | |
| print("Error occured trying to create project ", projectName) | |
| else: | |
| root_path = os.path.join(os.getcwd(), projectName) | |
| config_path = os.path.join(root_path,projectName) | |
| return (root_path,config_path) | |
| def create_django_apps(root_path,*apps): | |
| manage = os.path.join(root_path,'manage.py') | |
| os.chdir(root_path) | |
| for app in apps: | |
| try: | |
| subprocess.call([manage, 'startapp', app], shell=True) | |
| print("Created django app : ", app) | |
| except: | |
| print("Error creating app " , app) | |
| def write_settings(path): | |
| os.chdir(path) | |
| try: | |
| settingsFile = open('settings.py', 'r') | |
| tempFile = open('temp_settings.py', 'w') | |
| parser = SimpleParser() | |
| parser.addRule(FileHeadRule()) | |
| parser.addRule(StaticDirsRule()) | |
| parser.addRule(TemplateDirsRule()) | |
| parser.parse(settingsFile,tempFile) | |
| print("Edited settings file.") | |
| except: | |
| print("Some error occured trying to read settings.py") | |
| else: | |
| settingsFile.close() | |
| tempFile.close() | |
| os.remove('settings.py') | |
| os.rename('temp_settings.py', 'settings.py') | |
| def create_dir_tree(path): | |
| print('Created directories from : ', path) | |
| os.chdir(path) | |
| os.makedirs('templates') | |
| os.makedirs('static/js') | |
| os.makedirs('static/img') | |
| os.makedirs('static/css') | |
| def create_directories(root_path,root_config,*apps): | |
| create_dir_tree(root_config) | |
| for app in apps: | |
| create_dir_tree(os.path.join(root_path,app)) | |
| if __name__=='__main__': | |
| if len(sys.argv) == 1: | |
| print('Usage: setup_django.py projectname [appname1,appname2,...]') | |
| else: | |
| args = sys.argv[1:] | |
| projectName = args[0] | |
| if len(args) > 1: | |
| appNames = args[1:] | |
| else: | |
| appNames = [] | |
| root_path, root_config = create_django_project(projectName) | |
| if appNames: | |
| create_django_apps(root_path,*appNames) | |
| write_settings(root_config) | |
| create_directories(root_path, root_config,*appNames) | |
| print("Finished") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment