#
# This script checks to see if a project exists for the given
# app_env/team.
#
import os, sys, site
from optparse import OptionParser
from urllib import quote

# Add the project to the python path
activate_this = '<%= @path %>/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from sentry.utils.runner import configure
configure()

# Add in the sentry object models
from sentry.models import Organization, Project, ProjectKey, Team, User

def build_parser():
    parser = OptionParser()
    parser.add_option("-p", "--project", dest="project", help="Application/Project name.", type="string")
    parser.add_option("-l", "--platform", dest="platform", help="Application Language/Platform.", type="string")
    parser.add_option("-o", "--org", dest="org", help="Organization to own this project (<%= @organization %>)", type="string", default="<%= @organization %>")
    parser.add_option("-t", "--team", dest="team", help="Team to own this project (<%= @team %>)", type="string", default="<%= @team %>")
    parser.add_option("-v", "--verbose", dest="verbose", help="Verbose output", action="store_true")
    parser.add_option("-s", "--sentry-path", dest="sentry_path", help="Path to sentry project", action="store_true", required=True)
    return parser

def main():
  parser = build_parser()
  options, _args = parser.parse_args()

  os.environ['SENTRY_CONF'] = options.sentry_path

  if not options.project:
    parser.error("Project name required")
  if not options.platform:
    parser.error("Platform is required")

  # try to load the requested organization
  # and the admin user, who will own all new projects and teams
  e = False
  try:  
    o = Organization.objects.get(name=options.org)
    u = User.objects.get(email='<%= @admin_email %>')
  except:
    e = sys.exc_info()[0]
  if e:
    print "Error loading Sentry environment: %s" % (e)
    sys.exit(1)

  # try to load the requested team
  try:
    t = Team.objects.get(name=options.team,organization_id=o.id)
  except Team.DoesNotExist:
    # this team does not yet exist.  Create it.
    t = Team()
    t.name = options.team
    t.organization_id = o.id
    t.owner_id = u.id
    t.save()
    # reload the object
    t = Team.objects.get(name=options.team,organization_id=o.id)

  try:
    p = Project.objects.get(name=options.project,team_id=t.id)
  except:
    p = False

  if p:
    if options.verbose:
      print 'Project %s exists in team %s' % (options.project, t.name)
    sys.exit(1)

  # the project doesn't exist.  Create it!
  p = Project()
  # ensure all project names are in lowercase
  p.name = options.project.lower()
  p.team_id = t.id
  p.organization_id = o.id
  p.platform = options.platform
  try:
    p.save()
  except:
    e = sys.exc_info()[0]

  if e:
    # an error occured
    if options.verbose:
      print "Project save failed for %s: %s" % (options.project, e)
    sys.exit(1)

  # create a static file containing this application's DSN
  k = ProjectKey.objects.get(project_id=p.id).get_dsn()
  prefix = quote(o.name.lower() + "-" + t.name.lower() + "-")
  dsn_path = "%s/dsn/%s%s" % (options.sentry_path, prefix, p.name)
  dsn = open(dsn_path, 'a')
  dsn.write(k)
  dsn.close()

  if options.verbose:
    print "Project %s created in team %s." % (options.project, t.name)

if __name__ == "__main__":
  main()

@@ -160,152 +160,8 @@
 te%0A%0A
-# Add the project to the python path%0Aactivate_this = '%3C%25= @path %25%3E/bin/activate_this.py'%0Aexecfile(activate_this, dict(__file__=activate_this))%0A%0A
 from

Remove env activation