def _Update(self):
    """Update() helper method. Returns the number of changed help text files."""
    with file_utils.TemporaryDirectory() as temp_dir:
      walker_util.HelpTextGenerator(self._cli, temp_dir).Walk(hidden=True)
      diff = HelpTextAccumulator()
      DirDiff(self._help_dir, temp_dir, diff)
      if diff.invalid_file_count:
        # Bail out early on invalid content errors. These must be corrected
        # before proceeding.
        raise HelpTextUpdateError(
            '{0} help text {1} with invalid content must be fixed.'.format(
                diff.invalid_file_count,
                text.Pluralize(diff.invalid_file_count, 'file')))

      ops = {}
      for op in ['add', 'delete', 'edit']:
        ops[op] = []

      changes = 0
      for op, path in sorted(diff.GetChanges()):
        changes += 1
        if not self._test or changes < TEST_CHANGES_DISPLAY_MAX:
          log.status.Print('{0} {1}'.format(op, path))
        ops[op].append(path)

      if self._test:
        if changes:
          if changes >= TEST_CHANGES_DISPLAY_MAX:
            log.status.Print('...')
          log.status.Print('{0} help test {1} changed'.format(
              changes, text.Pluralize(changes, 'file')))
        return changes

      op = 'add'
      if ops[op]:
        for path in ops[op]:
          dest_path = os.path.join(self._help_dir, path)
          subdir = os.path.dirname(dest_path)
          if subdir:
            file_utils.MakeDir(subdir)
          temp_path = os.path.join(temp_dir, path)
          shutil.copyfile(temp_path, dest_path)

      op = 'edit'
      if ops[op]:
        for path in ops[op]:
          dest_path = os.path.join(self._help_dir, path)
          temp_path = os.path.join(temp_dir, path)
          shutil.copyfile(temp_path, dest_path)

      op = 'delete'
      if ops[op]:
        for path in ops[op]:
          dest_path = os.path.join(self._help_dir, path)
          try:
            os.remove(dest_path)
          except OSError:
            pass

      return changes
 def Run(self, args):
   if args.devsite_dir:
     walker_util.DevSiteGenerator(self._cli_power_users_only,
                                  args.devsite_dir).Walk(
                                      args.hidden, args.restrict)
   if args.help_text_dir:
     walker_util.HelpTextGenerator(
         self._cli_power_users_only, args.help_text_dir).Walk(args.hidden,
                                                              args.restrict)
   if args.html_dir:
     walker_util.HtmlGenerator(
         self._cli_power_users_only, args.html_dir).Walk(args.hidden,
                                                         args.restrict)
     tree = walker_util.CommandTreeGenerator(
         self._cli_power_users_only).Walk(args.hidden, args.restrict)
     with io.open(os.path.join(args.html_dir, '_menu_.html'), 'wt') as out:
       WriteHtmlMenu(tree, out)
     for file_name in _HELP_HTML_DATA_FILES:
       with io.open(os.path.join(args.html_dir, file_name), 'wb') as out:
         file_contents = pkg_resources.GetResource(
             'googlecloudsdk.api_lib.meta.help_html_data.', file_name)
         out.write(file_contents)
   if args.manpage_dir:
     walker_util.ManPageGenerator(
         self._cli_power_users_only, args.manpage_dir).Walk(args.hidden,
                                                            args.restrict)
   if args.update_help_text_dir:
     # The help text golden files are always ascii.
     console_attr.ResetConsoleAttr(encoding='ascii')
     changes = help_util.HelpTextUpdater(
         self._cli_power_users_only, args.update_help_text_dir,
         test=args.test).Update(args.restrict)
     if changes and args.test:
       raise HelpTextOutOfDateError('Help text files must be updated.')
 def Run(self, args):
     if args.devsite_dir:
         walker_util.DevSiteGenerator(self.cli, args.devsite_dir).Walk(
             args.hidden, args.restrict)
     if args.help_text_dir:
         walker_util.HelpTextGenerator(self.cli, args.help_text_dir).Walk(
             args.hidden, args.restrict)
     if args.html_dir:
         walker_util.HtmlGenerator(self.cli, args.html_dir).Walk(
             args.hidden, args.restrict)
         tree = walker_util.CommandTreeGenerator(self.cli).Walk(
             args.hidden, args.restrict)
         with open(os.path.join(args.html_dir, '_menu_.html'), 'w') as out:
             WriteHtmlMenu(tree, out)
         for file_name in _HELP_HTML_DATA_FILES:
             with open(os.path.join(args.html_dir, file_name), 'wb') as out:
                 file_contents = pkg_resources.GetResource(
                     'googlecloudsdk.api_lib.meta.help_html_data.',
                     file_name)
                 out.write(file_contents)
     if args.manpage_dir:
         walker_util.ManPageGenerator(self.cli, args.manpage_dir).Walk(
             args.hidden, args.restrict)
     if args.update_help_text_dir:
         changes = help_util.HelpTextUpdater(self.cli,
                                             args.update_help_text_dir,
                                             test=args.test).Update()
         if changes and args.test:
             raise HelpTextOutOfDateError(
                 'Help text files must be updated.')
Exemple #4
0
 def testHelpTextGeneratorHidden(self):
   """Tests the hidden help text generated directory file names and sizes."""
   help_directory = os.path.join(self.temp_path, 'help')
   walker_util.HelpTextGenerator(self.cli, help_directory).Walk(hidden=True)
   self.AssertDirectoryIsGolden(
       help_directory, __file__, 'walker_util', 'help-hidden.dir')