launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/scripts/jujuman.py (about) 1 """Functions for generating the manpage using the juju command.""" 2 3 import subprocess 4 import textwrap 5 import time 6 7 8 class JujuMan(object): 9 10 def __init__(self): 11 self.version = self._version() 12 13 def get_filename(self, options): 14 """Provides name of manpage""" 15 return 'juju.1' 16 17 def run_juju(self, *args): 18 cmd = ['juju'] + list(args) 19 return subprocess.check_output(cmd).strip() 20 21 def _version(self): 22 juju_version = self.run_juju('version') 23 return juju_version.split('-')[0] 24 25 def commands(self): 26 commands = self.run_juju('help', 'commands') 27 result = [] 28 for line in commands.split('\n'): 29 name, short_help = line.split(' ', 1) 30 if 'alias for' in short_help: 31 continue 32 result.append((name, short_help.strip())) 33 return result 34 35 def write_documentation(self, options, outfile): 36 """Assembles a man page""" 37 t = time.time() 38 tt = time.gmtime(t) 39 params = { 40 "cmd": "juju", 41 "datestamp": time.strftime("%Y-%m-%d",tt), 42 "timestamp": time.strftime("%Y-%m-%d %H:%M:%S +0000",tt), 43 "version": self.version, 44 } 45 outfile.write(man_preamble % params) 46 outfile.write(man_escape(man_head % params)) 47 outfile.write(man_escape(self.getcommand_list(params))) 48 outfile.write("".join(environment_variables())) 49 outfile.write(man_escape(man_foot % params)) 50 51 def getcommand_list(self, params): 52 """Builds summary help for command names in manpage format""" 53 output = '.SH "COMMAND OVERVIEW"\n' 54 for cmd_name, short_help in self.commands(): 55 tmp = '.TP\n.B "%s %s"\n%s\n' % (params['cmd'], cmd_name, short_help) 56 output = output + tmp 57 return output 58 59 60 ENVIRONMENT = ( 61 ('JUJU_ENV', textwrap.dedent("""\ 62 Provides a way for the shell environment to specify the current Juju 63 environment to use. If the environment is specified explicitly using 64 -e ENV, this takes precedence. 65 """)), 66 ('JUJU_HOME', textwrap.dedent("""\ 67 Overrides the default Juju configuration directory of ~/.juju. 68 """)), 69 ('AWS_ACCESS_KEY_ID', textwrap.dedent("""\ 70 The access-key for your AWS account. 71 """)), 72 ('AWS_SECRET_ACCESS_KEY', textwrap.dedent("""\ 73 The secret-key for your AWS account. 74 """)), 75 ('OS_USERNAME', textwrap.dedent("""\ 76 Your openstack username. 77 """)), 78 ('OS_PASSWORD', textwrap.dedent("""\ 79 Your openstack password. 80 """)), 81 ('OS_TENANT_NAME', textwrap.dedent("""\ 82 Your openstack tenant name. 83 """)), 84 ('OS_REGION_NAME', textwrap.dedent("""\ 85 Your openstack region name. 86 """)), 87 ) 88 89 def man_escape(string): 90 """Escapes strings for man page compatibility""" 91 result = string.replace("\\","\\\\") 92 result = result.replace("`","\\'") 93 result = result.replace("'","\\*(Aq") 94 result = result.replace("-","\\-") 95 return result 96 97 98 def environment_variables(): 99 yield ".SH \"ENVIRONMENT\"\n" 100 101 for k, desc in ENVIRONMENT: 102 yield ".TP\n" 103 yield ".I \"%s\"\n" % k 104 yield man_escape(desc) + "\n" 105 106 107 man_preamble = """\ 108 .\\\"Man page for Juju (%(cmd)s) 109 .\\\" 110 .\\\" Large parts of this file are autogenerated from the output of 111 .\\\" \"%(cmd)s help commands\" 112 .\\\" \"%(cmd)s help <cmd>\" 113 .\\\" 114 .\\\" Generation time: %(timestamp)s 115 .\\\" 116 117 .ie \\n(.g .ds Aq \\(aq 118 .el .ds Aq ' 119 """ 120 121 man_head = """\ 122 .TH %(cmd)s 1 "%(datestamp)s" "%(version)s" "Juju" 123 .SH "NAME" 124 %(cmd)s - Juju -- devops distilled 125 .SH "SYNOPSIS" 126 .B "%(cmd)s" 127 .I "command" 128 [ 129 .I "command_options" 130 ] 131 .br 132 .B "%(cmd)s" 133 .B "help" 134 .br 135 .B "%(cmd)s" 136 .B "help" 137 .I "command" 138 .SH "DESCRIPTION" 139 140 Juju provides easy, intelligent service orchestration on top of environments 141 such as OpenStack, Amazon AWS, or bare metal. 142 """ 143 144 man_foot = """\ 145 .SH "FILES" 146 .TP 147 .I "~/.juju/environments.yaml" 148 This is the Juju config file, which you can use to specify multiple 149 environments in which to deploy. 150 151 A config file can be created using 152 .B juju init 153 which you can then edit to provide the secret keys, or use environment 154 variables to provide the secret values. 155 156 .SH "SEE ALSO" 157 .UR https://juju.ubuntu.com/ 158 .BR https://juju.ubuntu.com/ 159 """ 160