]> scripts.mit.edu Git - wizard.git/blob - wizard/command/install.py
tests/all-versions.sh runs all tests for all versions of the specified app.
[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     input = prompt.make(old_options.prompt, old_options.non_interactive)
14
15     appstr = args[0]
16     dir = os.path.abspath(args[1])
17
18     if not old_options.retry and not old_options.help and os.path.exists(dir) and os.listdir(dir):
19         raise DirectoryExistsError
20
21     appname, _, version = appstr.partition('-')
22     application = app.applications()[appname]
23
24     # get configuration
25     schema = application.install_schema
26     schema.commit(application, dir)
27     options = None
28     opthandler = installopt.Controller(dir, schema)
29     parser = command.WizardOptionParser("""usage: %%prog install %s DIR [ -- SETUPARGS ]
30
31 Autoinstalls the application %s in the directory DIR.""" % (appname, appname))
32     configure_parser(parser, baton)
33     opthandler.push(parser)
34     if old_options.help:
35         parser.print_help()
36         sys.exit(1)
37     ihandler = interactive.Controller(dir, schema, input)
38     options, _ = parser.parse_all(args[2:] + command.make_base_args(old_options))
39     if old_options.non_interactive:
40         opthandler.handle(options)
41     else:
42         ihandler.ask(options)
43
44     if not os.path.exists(dir) or not os.listdir(dir):
45         input.infobox("Copying files (this may take a while)...")
46         shell.call("git", "clone", "-q", "--shared", application.repository(old_options.srv_path), dir)
47     else:
48         logging.info("Skipped clone")
49     with util.ChangeDirectory(dir):
50         if not old_options.retry and version and version != "head-scripts": # for ease in testing
51             shell.call("git", "reset", "-q", "--hard", appstr)
52         input.infobox("Installing...")
53         application.install(distutils.version.LooseVersion(version), options)
54         if not old_options.no_commit:
55             git.commit_configure()
56     if not hasattr(options, "web_inferred"):
57         open(os.path.join(dir, ".scripts/url"), "w").write("http://%s%s" % (options.web_host, options.web_path)) # XXX: no support for https yet!
58     input.infobox("Congratulations, your new install is now accessible at:\n\nhttp://%s%s" % (options.web_host, options.web_path), width=80)
59
60 def configure_parser(parser, baton):
61     parser.add_option("--prompt", dest="prompt", action="store_true",
62             default=False, help="Force to use non-ncurses interactive interface")
63     parser.add_option("--non-interactive", dest="non_interactive", action="store_true",
64             default=False, help="Force program to be non-interactive and use SETUPARGS.  Use --help with APP to find argument names.")
65     parser.add_option("--no-commit", dest="no_commit", action="store_true",
66             default=command.boolish(os.getenv("WIZARD_NO_COMMIT")), help="Do not generate an 'installation commit' after configuring the application. Envvar is WIZARD_NO_COMMIT")
67     parser.add_option("--retry", dest="retry", action="store_true",
68             default=False, help="Do not complain if directory already exists and reinstall application.")
69     baton.push(parser, "srv_path")
70
71 def parse_args(argv, baton):
72     usage = """usage: %prog install APP DIR [ -- SETUPARGS ]
73
74 Autoinstalls the application APP in the directory DIR.
75 This command will interactively ask for information to
76 complete the autoinstall.
77
78 You can also use --help with APP and DIR to find out what
79 are required SETUPARGS if you want to run this non-interactively
80 (the distribution of required and optional arguments may change
81 depending on what directory you are installing to.)"""
82     parser = command.WizardOptionParser(usage, store_help=True)
83     configure_parser(parser, baton)
84     options, args = parser.parse_all(argv)
85     if options.help:
86         if len(args) == 0:
87             parser.print_help()
88             sys.exit(1)
89         elif len(args) == 1:
90             args.append(os.getcwd())
91     else:
92         if len(args) < 2:
93             parser.error("not enough arguments")
94     return options, args
95
96 class DirectoryExistsError(wizard.Error):
97     def __str__(self):
98         return "Directory already exists and is not empty"