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