Package turbogears :: Package command :: Module quickstart
[hide private]

Source Code for Module turbogears.command.quickstart

  1  """Quickstart command to generate a new project. 
  2   
  3  Quickstart takes the files from turbogears.quickstart and processes them to produce 
  4  a new, ready-to-run project. 
  5   
  6  """ 
  7   
  8  import pkg_resources 
  9  import re 
 10  import os 
 11  import os.path 
 12  import shutil 
 13  import stat 
 14  import optparse 
 15  import sys 
 16  import turbogears 
 17  from paste.script import templates, create_distro 
 18   
 19  try: 
 20      set 
 21  except NameError: # Python 2.3 
 22      from sets import Set as set 
 23   
 24  beginning_letter = re.compile(r"^[^a-z]*") 
 25  valid_only = re.compile(r"[^a-z0-9_]") 
 26   
 27   
28 -class TGTemplate(templates.Template):
29 - def run(self, command, output_dirs, vars):
30 vars.setdefault('einame', vars['project'].replace('-', '_')) 31 vars.setdefault('turbogearsversion', 32 pkg_resources.get_distribution('turbogears').version) 33 vars.setdefault('sys_executable', os.path.normpath(sys.executable)) 34 # define version-compatible decorator syntax 35 if sys.version_info >= (2, 4): 36 vars['b'] = '@' 37 vars['e'] = '' 38 else: 39 vars['b'] = '[' 40 vars['e'] = ']' 41 super(TGTemplate, self).run(command, output_dirs, vars)
42 43
44 -class BaseTemplate(TGTemplate):
45 egg_plugins = ["TurboGears"] 46 _template_dir = pkg_resources.resource_filename( 47 "turbogears.qstemplates", 48 "qsbase" 49 ) 50 summary = "tg base template" 51 use_cheetah = True
52 53
54 -class TurbogearsTemplate(TGTemplate):
55 required_templates = ["tgbase"] 56 _template_dir = pkg_resources.resource_filename( 57 "turbogears.qstemplates", 58 "quickstart") 59 summary = "web framework" 60 use_cheetah = True
61 62
63 -class TGBig(TGTemplate):
64 required_templates = ["turbogears"] 65 _template_dir = pkg_resources.resource_filename( 66 "turbogears.qstemplates", 67 "quickstartbig") 68 summary = "For more complex projects" 69 use_cheetah = True 70
71 - def post(self, command, output_dir, vars):
72 packagedir = os.path.join(output_dir, vars["package"]) 73 controllersdir = os.path.join(packagedir, "controllers") 74 controllersfile = os.path.join(packagedir, "controllers.py") 75 rootfile = os.path.join(controllersdir, "root.py") 76 if os.path.exists(controllersdir) and \ 77 os.path.exists(controllersfile): 78 controllerstext = open(controllersfile).read() 79 roottext = open(rootfile).read() 80 from paste.script.copydir import query_interactive 81 thesame = controllerstext == roottext 82 if not thesame: 83 print "\nYou seem to be upgrading from a smaller quickstart." 84 print "There is currently a controllers package and a" 85 print "controllers module, which would get confusing." 86 if not command.simulate and (controllerstext == roottext 87 or query_interactive(controllersfile, rootfile, 88 controllerstext, roottext, False)): 89 shutil.copyfile(controllersfile, rootfile) 90 try: 91 if not os.path.exists(os.path.join(os.path.dirname( 92 os.path.abspath(controllersfile)), '.svn')): 93 raise OSError 94 command.run_command('svn', 'revert', controllersfile) 95 command.run_command('svn', 'delete', controllersfile) 96 except OSError: 97 os.unlink(controllersfile) 98 controllerspyc = controllersfile + "c" 99 if os.path.exists(controllerspyc): 100 os.unlink(controllerspyc)
101 102
103 -class TGWidgetTemplate(TGTemplate):
104 required_templates = ["tgbase"] 105 _template_dir = pkg_resources.resource_filename( 106 "turbogears.qstemplates", 107 "widget") 108 summary = "TurboGears widget projects"
109 110
111 -def get_requirement(name, pkg=None):
112 dist = pkg_resources.get_distribution("TurboGears") 113 for r in set(dist.requires((name,))) - set(dist.requires()): 114 if r.project_name.lower() == (pkg or name): 115 return r 116 raise ValueError("Did not find matching %s requirement" 117 " in the TurboGears setup.py:extras_require." % name)
118 119
120 -class quickstart:
121 "Implementation of quickstart." 122 123 desc = "Create a new TurboGears project" 124 125 name = None 126 package = None 127 templates = "turbogears" 128 svn_repository = None 129 sqlalchemy = False 130 sqlobject = False 131 elixir = False 132 identity = False 133
134 - def __init__(self, version):
135 parser = optparse.OptionParser( 136 usage="%prog quickstart [options] [project name]", 137 version="%prog " + version) 138 parser.add_option("-s", "--sqlalchemy", 139 help="use SQLAlchemy instead of SQLObject", 140 action="store_true", dest="sqlalchemy", default = False) 141 parser.add_option("-e", "--elixir", 142 help="use SQLAlchemy Elixir instead of SQLObject", 143 action="store_true", dest="elixir", default = False) 144 parser.add_option("-o", "--sqlobject", 145 help="use SQLObject instead of SQLAlchemy", 146 action="store_true", dest="sqlobject", default = False) 147 parser.add_option("-i", "--identity", 148 help="provide Identity support", 149 action="store_true", dest="identity", default = False) 150 parser.add_option("-p", "--package", 151 help="package name for the code", 152 dest="package") 153 parser.add_option("-t", "--templates", 154 help="user specific templates", 155 dest="templates", default = self.templates) 156 parser.add_option("-r", "--svn-repository", metavar="REPOS", 157 help="create project in given SVN repository", 158 dest="svn_repository", default = self.svn_repository) 159 parser.add_option("--dry-run", 160 help="dry run (don't actually do anything)", 161 action="store_true", dest="dry_run") 162 163 options, args = parser.parse_args() 164 self.__dict__.update(options.__dict__) 165 if not True in [self.elixir, self.sqlalchemy, self.sqlobject]: 166 self.sqlobject = True 167 if self.elixir: 168 self.sqlalchemy = True 169 170 if args: 171 self.name = args[0] 172 self.turbogearsversion = version
173
174 - def run(self):
175 "Quickstarts the new project." 176 177 while not self.name: 178 self.name = raw_input("Enter project name: ") 179 180 while not self.package: 181 package = self.name.lower() 182 package = beginning_letter.sub("", package) 183 package = valid_only.sub("", package) 184 self.package = raw_input("Enter package name [%s]: " % package) 185 if not self.package: 186 self.package = package 187 188 doidentity = self.identity 189 while not doidentity: 190 doidentity = raw_input("Do you need Identity " 191 "(usernames/passwords) in this project? [no] ") 192 doidentity = doidentity.lower() 193 if not doidentity or doidentity.startswith('n'): 194 self.identity="none" 195 break 196 if doidentity.startswith("y"): 197 doidentity = True 198 break 199 print "Please enter y(es) or n(o)." 200 doidentity = None 201 202 if doidentity is True: 203 if self.sqlalchemy or self.elixir: 204 self.identity = "sqlalchemy" 205 else: 206 self.identity = "sqlobject" 207 self.name = pkg_resources.safe_name(self.name) 208 209 env = pkg_resources.Environment() 210 if self.name.lower() in env: 211 print 'The name "%s" is already in use by' % self.