setup.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import os
  2. import re
  3. from optparse import OptionParser
  4. from django.core import management
  5. def validate_project_name(parser, project_name):
  6. if project_name[0].isdigit():
  7. parser.error("project_name cannot start with digit")
  8. if project_name.startswith("-"):
  9. parser.error("project_name cannot start with '-'")
  10. if re.search("[^0-9a-zA-Z]", project_name):
  11. parser.error("project_name cannot contain special characters")
  12. # Ensure the given directory name doesn't clash with an existing
  13. # Python package/module.
  14. try:
  15. __import__(project_name)
  16. except ImportError:
  17. pass
  18. else:
  19. parser.error(
  20. (
  21. "'%s' conflicts with the name of an existing "
  22. "Python module and cannot be used as a project "
  23. "name. Please try another name."
  24. )
  25. % project_name
  26. )
  27. return project_name
  28. def get_misago_project_template():
  29. misago_path = os.path.dirname(os.path.dirname(__file__))
  30. return os.path.join(misago_path, "project_template")
  31. def start_misago_project():
  32. parser = OptionParser(usage="usage: %prog project_name [directory]")
  33. _, args = parser.parse_args()
  34. if len(args) < 1:
  35. parser.error("project_name must be specified")
  36. directory = None
  37. if len(args) == 2:
  38. directory = args[1]
  39. project_name = validate_project_name(parser, args[0])
  40. argv = [
  41. "misago-start.py",
  42. "startproject",
  43. project_name,
  44. directory,
  45. "--template=%s" % get_misago_project_template(),
  46. ]
  47. management.execute_from_command_line(argv)