github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/scripts/jujuman.py (about)

     1  # Copyright 2013 Canonical Ltd.
     2  # Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  """Functions for generating the manpage using the juju command."""
     5  
     6  import subprocess
     7  import textwrap
     8  import time
     9  
    10  
    11  class JujuMan(object):
    12  
    13      def __init__(self):
    14          self.version = self._version()
    15  
    16      def get_filename(self, options):
    17          """Provides name of manpage"""
    18          return 'juju.1'
    19  
    20      def run_juju(self, *args):
    21          cmd = ['juju'] + list(args)
    22          return subprocess.check_output(cmd).strip()
    23  
    24      def _version(self):
    25          juju_version = self.run_juju('version')
    26          return juju_version.split('-')[0]
    27  
    28      def commands(self):
    29          commands = self.run_juju('help', 'commands')
    30          result = []
    31          for line in commands.split('\n'):
    32              name, short_help = line.split(' ', 1)
    33              if 'alias for' in short_help:
    34                  continue
    35              result.append((name, short_help.strip()))
    36          return result
    37  
    38      def write_documentation(self, options, outfile):
    39          """Assembles a man page"""
    40          t = time.time()
    41          tt = time.gmtime(t)
    42          params = {
    43              "cmd": "juju",
    44              "datestamp": time.strftime("%Y-%m-%d",tt),
    45              "timestamp": time.strftime("%Y-%m-%d %H:%M:%S +0000",tt),
    46              "version": self.version,
    47              }
    48          outfile.write(man_preamble % params)
    49          outfile.write(man_escape(man_head % params))
    50          outfile.write(man_escape(self.getcommand_list(params)))
    51          outfile.write("".join(environment_variables()))
    52          outfile.write(man_escape(man_foot % params))
    53  
    54      def getcommand_list(self, params):
    55          """Builds summary help for command names in manpage format"""
    56          output = '.SH "COMMAND OVERVIEW"\n'
    57          for cmd_name, short_help in self.commands():
    58              tmp = '.TP\n.B "%s %s"\n%s\n' % (params['cmd'], cmd_name, short_help)
    59              output = output + tmp
    60          return output
    61  
    62  
    63  ENVIRONMENT = (
    64      ('JUJU_MODEL', textwrap.dedent("""\
    65          Provides a way for the shell environment to specify the current Juju
    66          model to use.  If the model is specified explicitly using
    67          -m MODEL, this takes precedence.
    68          """)),
    69      ('JUJU_DATA', textwrap.dedent("""\
    70          Overrides the default Juju configuration directory of $XDG_DATA_HOME/juju or ~/.local/share/juju
    71          if $XDG_DATA_HOME is not defined.
    72          """)),
    73      ('AWS_ACCESS_KEY_ID', textwrap.dedent("""\
    74          The access-key for your AWS account.
    75          """)),
    76      ('AWS_SECRET_ACCESS_KEY', textwrap.dedent("""\
    77          The secret-key for your AWS account.
    78          """)),
    79      ('OS_USERNAME', textwrap.dedent("""\
    80          Your OpenStack username.
    81          """)),
    82      ('OS_PASSWORD', textwrap.dedent("""\
    83          Your OpenStack password.
    84          """)),
    85      ('OS_TENANT_NAME', textwrap.dedent("""\
    86          Your OpenStack tenant name.
    87          """)),
    88      ('OS_REGION_NAME', textwrap.dedent("""\
    89          Your OpenStack region name.
    90          """)),
    91  )
    92  
    93  def man_escape(string):
    94      """Escapes strings for man page compatibility"""
    95      result = string.replace("\\","\\\\")
    96      result = result.replace("`","\\'")
    97      result = result.replace("'","\\*(Aq")
    98      result = result.replace("-","\\-")
    99      return result
   100  
   101  
   102  def environment_variables():
   103      yield ".SH \"ENVIRONMENT\"\n"
   104  
   105      for k, desc in ENVIRONMENT:
   106          yield ".TP\n"
   107          yield ".I \"%s\"\n" % k
   108          yield man_escape(desc) + "\n"
   109  
   110  
   111  man_preamble = """\
   112  .\\\"Man page for Juju (%(cmd)s)
   113  .\\\"
   114  .\\\" Large parts of this file are autogenerated from the output of
   115  .\\\"     \"%(cmd)s help commands\"
   116  .\\\"     \"%(cmd)s help <cmd>\"
   117  .\\\"
   118  .\\\" Generation time: %(timestamp)s
   119  .\\\"
   120  
   121  .ie \\n(.g .ds Aq \\(aq
   122  .el .ds Aq '
   123  """
   124  
   125  man_head = """\
   126  .TH %(cmd)s 1 "%(datestamp)s" "%(version)s" "Juju"
   127  .SH "NAME"
   128  %(cmd)s - Juju -- devops distilled
   129  .SH "SYNOPSIS"
   130  .B "%(cmd)s"
   131  .I "command"
   132  [
   133  .I "command_options"
   134  ]
   135  .br
   136  .B "%(cmd)s"
   137  .B "help"
   138  .br
   139  .B "%(cmd)s"
   140  .B "help"
   141  .I "command"
   142  .SH "DESCRIPTION"
   143  
   144  Juju is model & service management software designed to leverage the power 
   145  of existing resource pools, particularly cloud-based ones. It has built-in 
   146  support for cloud providers such as Amazon EC2, Google GCE, Microsoft Azure,
   147  OpenStack, and Rackspace. It also works very well with MAAS and LXD. 
   148  """
   149  
   150  man_foot = """\
   151  .SH "FILES"
   152  .TP
   153  .I "~/.local/share/juju/accounts.yaml"
   154  Records the current authorised Juju users and their 
   155  passwords.
   156  .TP
   157  .I "~/.local/share/juju/bootstrap-config.yaml"
   158  Records any configuration values which were used in the 
   159  creation of running controllers.
   160  .TP
   161  .I "~/.local/share/juju/clouds.yaml"
   162  Records any user-specified clouds which have been 
   163  added to Juju.
   164  .TP
   165  .I "~/.local/share/juju/controllers.yaml"
   166  Records all the running controllers, their UUIDS, 
   167  api-endpoints and CA certificates.
   168  .TP
   169  .I "~/.local/share/juju/credentials.yaml"
   170  Records all the cloud credentials known to Juju.
   171  .TP
   172  .I "~/.local/share/juju/models.yaml"
   173  Records the UUIDs of all models known to Juju.
   174  .TP
   175  .I "~/.local/share/juju/ssh/"
   176  A directory containing the SSH credentials for the Juju client.
   177  .SH "SEE ALSO"
   178  .UR https://jujucharms.com/docs
   179  .BR https://jujucharms.com/docs
   180  """
   181