]> scripts.mit.edu Git - wizard.git/blob - wizard/command/install.py
Fix bug where php.ini not being rewritten for MediaWiki.
[wizard.git] / wizard / command / install.py
1 import os
2 import sys
3 import distutils
4 import logging
5
6 import wizard
7 from wizard import app, command, git, prompt, shell, util
8 from wizard.install import installopt, interactive
9
10 def main(argv, baton):
11     old_options, args = parse_args(argv, baton)
12
13     appstr = args[0]
14     dir = os.path.abspath(args[1])
15     web_stub_path = old_options.web_stub_path
16
17     if not old_options.retry and not old_options.help and os.path.exists(dir) and os.listdir(dir):
18         raise DirectoryExistsError(dir)
19
20     appname, _, version = appstr.partition('-')
21     application = app.getApplication(appname)
22
23     if application.needs_web_stub and web_stub_path is None:
24         raise NeedsWebStubError
25
26     # get configuration
27     schema = application.install_schema
28     schema.commit(application, dir, web_stub_path)
29     options = None
30     opthandler = installopt.Controller(dir, schema)
31     parser = command.WizardOptionParser("""usage: %%prog install %s DIR [ -- SETUPARGS ]
32
33 Autoinstalls the application %s in the directory DIR.""" % (appname, appname))
34     configure_parser(parser, baton)
35     opthandler.push(parser)
36     if old_options.help:
37         parser.print_help()
38         sys.exit(1)
39     input = prompt.make(old_options.prompt, old_options.non_interactive)
40     ihandler = interactive.Controller(dir, schema, input)
41     options, _ = parser.parse_all(args[2:] + command.make_base_args(old_options))
42     if old_options.non_interactive:
43         opthandler.handle(options)
44     else:
45         ihandler.ask(options)
46
47     if not os.path.exists(dir) or not os.listdir(dir):
48         input.infobox("Copying files (this may take a while)...")
49         shell.call("git", "clone", "-q", "--shared", application.repository(old_options.srv_path), dir)
50     else:
51         logging.info("Skipped clone")
52     wizard_dir = ".wizard"
53     with util.ChangeDirectory(dir):
54         util.init_wizard_dir()
55         if not old_options.retry and version:
56             shell.call("git", "reset", "-q", "--hard", appstr)
57         input.infobox("Installing...")
58         if not version: # figure out what version we're installing
59             v = application.detectVersionFromGit(appname + "-*", appname + "-")
60             logging.info("Installing latest version: %s", version)
61         else:
62             v = distutils.version.LooseVersion(version)
63         if application.needs_web_stub:
64             application.install(v, options, web_stub_path)
65         else:
66             application.install(v, options)
67         if not old_options.no_commit:
68             git.commit_configure()
69     # This should be on a per-application basis
70     #if not hasattr(options, "web_inferred"):
71     #    open(os.path.join(dir, os.path.join(wizard_dir, "url")), "w") \
72     #        .write("http://%s%s" % (options.web_host, options.web_path)) # XXX: no support for https yet!
73
74     input.infobox("Congratulations, your new install is now accessible at:\n\nhttp://%s%s" \
75             % (options.web_host, options.web_path), width=80)
76
77 def configure_parser(parser, baton):
78     parser.add_option("--prompt", dest="prompt", action="store_true",
79             default=False, help="Force to use non-ncurses interactive interface")
80     parser.add_option("--non-interactive", dest="non_interactive", action="store_true",
81             default=False, help="Force program to be non-interactive and use SETUPARGS.  Use --help with APP to find argument names.")
82     parser.add_option("--no-commit", dest="no_commit", action="store_true",
83             default=util.boolish(os.getenv("WIZARD_NO_COMMIT")), help="Do not generate an 'installation commit' after configuring the application. Envvar is WIZARD_NO_COMMIT")
84     parser.add_option("--retry", dest="retry", action="store_true",
85             default=False, help="Do not complain if directory already exists and reinstall application.")
86     baton.push(parser, "srv_path")
87
88 def parse_args(argv, baton):
89     usage = """usage: %prog install APP DIR [ -- SETUPARGS ]
90
91 Autoinstalls the application APP in the directory DIR.
92 This command will interactively ask for information to
93 complete the autoinstall.
94
95 You can also use --help with APP and DIR to find out what
96 are required SETUPARGS if you want to run this non-interactively
97 (the distribution of required and optional arguments may change
98 depending on what directory you are installing to.)"""
99     parser = command.WizardOptionParser(usage, store_help=True)
100     configure_parser(parser, baton)
101     parser.add_option("--web-stub-path", dest="web_stub_path",
102             default=None, help="Used on certain installations for indicating"
103             "where to place stub files for a web path.")
104     options, args = parser.parse_all(argv)
105     if options.help:
106         if len(args) == 0:
107             parser.print_help()
108             sys.exit(1)
109         elif len(args) == 1:
110             args.append(os.getcwd())
111     else:
112         if len(args) < 2:
113             parser.error("not enough arguments")
114     return options, args
115
116 class DirectoryExistsError(wizard.Error):
117     def __init__(self, dir=None, ):
118         self.dir = dir
119     def __str__(self):
120         return "Directory (%s) already exists and is not empty" % self.dir
121
122 class NeedsWebStubError(wizard.Error):
123     def __str__(self):
124         return "You need to specify a web stub directory for this application"