1
2 """This module contains functions to be called from console script entry points.
3 """
4
5 import sys
6 import optparse
7
8 from os import getcwd
9 from os.path import dirname, exists, join
10
11 import pkg_resources
12 try:
13 pkg_resources.require("TurboGears>=1.1b1")
14 except pkg_resources.DistributionNotFound:
15 print """\
16 This is a TurboGears (http://www.turbogears.org) application. It seems that
17 you either don't have TurboGears installed or it can not be found.
18
19 Please check if your PYTHONPATH is set correctly. To install TurboGears, go to
20 http://docs.turbogears.org/Install and follow the instructions there. If you
21 are stuck, visit http://docs.turbogears.org/GettingHelp for support options."""
22 sys.exit(1)
23 try:
24 pkg_resources.require("SQLAlchemy>=0.4.0")
25 except pkg_resources.DistributionNotFound:
26 from turbogears.util import missing_dependency_error
27 print missing_dependency_error('SQLAlchemy')
28 sys.exit(1)
29
30 import cherrypy
31 import turbogears
32
33 cherrypy.lowercase_api = True
34
35
38
39
41 """Read deployment configuration file.
42
43 First looks on the command line for a desired config file, if it's not on
44 the command line, then looks for 'setup.py' in the parent of the directory
45 where this module is located.
46
47 If 'setup.py' is there, assumes that the application is started from
48 the project directory and should run in development mode and so loads the
49 configuration from a file called 'dev.cfg' in the current directory.
50
51 If 'setup.py' is not there, the project is probably installed and the code
52 looks first for a file called 'prod.cfg' in the current directory and, if
53 this isn't found either, for a default config file called 'default.cfg'
54 packaged in the egg.
55
56 """
57 setupdir = dirname(dirname(__file__))
58 curdir = getcwd()
59
60 if args:
61 configfile = args[0]
62 elif exists(join(setupdir, "setup.py")):
63 configfile = join(setupdir, "dev.cfg")
64 elif exists(join(curdir, "prod.cfg")):
65 configfile = join(curdir, "prod.cfg")
66 else:
67 try:
68 configfile = pkg_resources.resource_filename(
69 pkg_resources.Requirement.parse("spammcan"),
70 "config/default.cfg")
71 except pkg_resources.DistributionNotFound:
72 raise ConfigurationError("Could not find default configuration.")
73
74 turbogears.update_config(configfile=configfile,
75 modulename="spammcan.config")
76
78 """Example function for loading bootstrap data into the database
79
80 Adapt this to your needs and also uncomment/add a line in your project's
81 'setup.py' file that says::
82
83 'bootstrap-spammcan = spammcan.commands:bootstrap',
84
85 """
86
87 optparser = optparse.OptionParser(
88 description="Load bootstrap data into the database.")
89 optparser.add_option('-C', '--clean', dest="clean", action="store_true",
90 help="Purge all data in the database before loading the bootrap data.")
91 optparser.add_option('-u', '--user', dest="user", metavar="USERNAME",
92 help="Create a default user USERNAME (prompts for password).")
93 options, args = optparser.parse_args()
94 _read_config(args)
95 from bootstrap import bootstrap_model
96 bootstrap_model(options.clean, options.user)
97
104