Blame googletest/scripts/release_docs.py

Packit bd1cd8
#!/usr/bin/env python
Packit bd1cd8
#
Packit bd1cd8
# Copyright 2013 Google Inc. All Rights Reserved.
Packit bd1cd8
#
Packit bd1cd8
# Redistribution and use in source and binary forms, with or without
Packit bd1cd8
# modification, are permitted provided that the following conditions are
Packit bd1cd8
# met:
Packit bd1cd8
#
Packit bd1cd8
#     * Redistributions of source code must retain the above copyright
Packit bd1cd8
# notice, this list of conditions and the following disclaimer.
Packit bd1cd8
#     * Redistributions in binary form must reproduce the above
Packit bd1cd8
# copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
# in the documentation and/or other materials provided with the
Packit bd1cd8
# distribution.
Packit bd1cd8
#     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
# contributors may be used to endorse or promote products derived from
Packit bd1cd8
# this software without specific prior written permission.
Packit bd1cd8
#
Packit bd1cd8
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
Packit bd1cd8
"""Script for branching Google Test/Mock wiki pages for a new version.
Packit bd1cd8
Packit bd1cd8
SYNOPSIS
Packit bd1cd8
       release_docs.py NEW_RELEASE_VERSION
Packit bd1cd8
Packit bd1cd8
       Google Test and Google Mock's external user documentation is in
Packit bd1cd8
       interlinked wiki files.  When we release a new version of
Packit bd1cd8
       Google Test or Google Mock, we need to branch the wiki files
Packit bd1cd8
       such that users of a specific version of Google Test/Mock can
Packit bd1cd8
       look up documenation relevant for that version.  This script
Packit bd1cd8
       automates that process by:
Packit bd1cd8
Packit bd1cd8
         - branching the current wiki pages (which document the
Packit bd1cd8
           behavior of the SVN trunk head) to pages for the specified
Packit bd1cd8
           version (e.g. branching FAQ.wiki to V2_6_FAQ.wiki when
Packit bd1cd8
           NEW_RELEASE_VERSION is 2.6);
Packit bd1cd8
         - updating the links in the branched files to point to the branched
Packit bd1cd8
           version (e.g. a link in V2_6_FAQ.wiki that pointed to
Packit bd1cd8
           Primer.wiki#Anchor will now point to V2_6_Primer.wiki#Anchor).
Packit bd1cd8
Packit bd1cd8
       NOTE: NEW_RELEASE_VERSION must be a NEW version number for
Packit bd1cd8
       which the wiki pages don't yet exist; otherwise you'll get SVN
Packit bd1cd8
       errors like "svn: Path 'V1_7_PumpManual.wiki' is not a
Packit bd1cd8
       directory" when running the script.
Packit bd1cd8
Packit bd1cd8
EXAMPLE
Packit bd1cd8
       $ cd PATH/TO/GTEST_SVN_WORKSPACE/trunk
Packit bd1cd8
       $ scripts/release_docs.py 2.6  # create wiki pages for v2.6
Packit bd1cd8
       $ svn status                   # verify the file list
Packit bd1cd8
       $ svn diff                     # verify the file contents
Packit bd1cd8
       $ svn commit -m "release wiki pages for v2.6"
Packit bd1cd8
"""
Packit bd1cd8
Packit bd1cd8
__author__ = 'wan@google.com (Zhanyong Wan)'
Packit bd1cd8
Packit bd1cd8
import os
Packit bd1cd8
import re
Packit bd1cd8
import sys
Packit bd1cd8
Packit bd1cd8
import common
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
# Wiki pages that shouldn't be branched for every gtest/gmock release.
Packit bd1cd8
GTEST_UNVERSIONED_WIKIS = ['DevGuide.wiki']
Packit bd1cd8
GMOCK_UNVERSIONED_WIKIS = [
Packit bd1cd8
    'DesignDoc.wiki',
Packit bd1cd8
    'DevGuide.wiki',
Packit bd1cd8
    'KnownIssues.wiki'
Packit bd1cd8
    ]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def DropWikiSuffix(wiki_filename):
Packit bd1cd8
  """Removes the .wiki suffix (if any) from the given filename."""
Packit bd1cd8
Packit bd1cd8
  return (wiki_filename[:-len('.wiki')] if wiki_filename.endswith('.wiki')
Packit bd1cd8
          else wiki_filename)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
class WikiBrancher(object):
Packit bd1cd8
  """Branches ..."""
Packit bd1cd8
Packit bd1cd8
  def __init__(self, dot_version):
Packit bd1cd8
    self.project, svn_root_path = common.GetSvnInfo()
Packit bd1cd8
    if self.project not in ('googletest', 'googlemock'):
Packit bd1cd8
      sys.exit('This script must be run in a gtest or gmock SVN workspace.')
Packit bd1cd8
    self.wiki_dir = svn_root_path + '/wiki'
Packit bd1cd8
    # Turn '2.6' to 'V2_6_'.
Packit bd1cd8
    self.version_prefix = 'V' + dot_version.replace('.', '_') + '_'
Packit bd1cd8
    self.files_to_branch = self.GetFilesToBranch()
Packit bd1cd8
    page_names = [DropWikiSuffix(f) for f in self.files_to_branch]
Packit bd1cd8
    # A link to Foo.wiki is in one of the following forms:
Packit bd1cd8
    #   [Foo words]
Packit bd1cd8
    #   [Foo#Anchor words]
Packit bd1cd8
    #   [http://code.google.com/.../wiki/Foo words]
Packit bd1cd8
    #   [http://code.google.com/.../wiki/Foo#Anchor words]
Packit bd1cd8
    # We want to replace 'Foo' with 'V2_6_Foo' in the above cases.
Packit bd1cd8
    self.search_for_re = re.compile(
Packit bd1cd8
        # This regex matches either
Packit bd1cd8
        #   [Foo
Packit bd1cd8
        # or
Packit bd1cd8
        #   /wiki/Foo
Packit bd1cd8
        # followed by a space or a #, where Foo is the name of an
Packit bd1cd8
        # unversioned wiki page.
Packit bd1cd8
        r'(\[|/wiki/)(%s)([ #])' % '|'.join(page_names))
Packit bd1cd8
    self.replace_with = r'\1%s\2\3' % (self.version_prefix,)
Packit bd1cd8
Packit bd1cd8
  def GetFilesToBranch(self):
Packit bd1cd8
    """Returns a list of .wiki file names that need to be branched."""
Packit bd1cd8
Packit bd1cd8
    unversioned_wikis = (GTEST_UNVERSIONED_WIKIS if self.project == 'googletest'
Packit bd1cd8
                         else GMOCK_UNVERSIONED_WIKIS)
Packit bd1cd8
    return [f for f in os.listdir(self.wiki_dir)
Packit bd1cd8
            if (f.endswith('.wiki') and
Packit bd1cd8
                not re.match(r'^V\d', f) and  # Excluded versioned .wiki files.
Packit bd1cd8
                f not in unversioned_wikis)]
Packit bd1cd8
Packit bd1cd8
  def BranchFiles(self):
Packit bd1cd8
    """Branches the .wiki files needed to be branched."""
Packit bd1cd8
Packit bd1cd8
    print 'Branching %d .wiki files:' % (len(self.files_to_branch),)
Packit bd1cd8
    os.chdir(self.wiki_dir)
Packit bd1cd8
    for f in self.files_to_branch:
Packit bd1cd8
      command = 'svn cp %s %s%s' % (f, self.version_prefix, f)
Packit bd1cd8
      print command
Packit bd1cd8
      os.system(command)
Packit bd1cd8
Packit bd1cd8
  def UpdateLinksInBranchedFiles(self):
Packit bd1cd8
Packit bd1cd8
    for f in self.files_to_branch:
Packit bd1cd8
      source_file = os.path.join(self.wiki_dir, f)
Packit bd1cd8
      versioned_file = os.path.join(self.wiki_dir, self.version_prefix + f)
Packit bd1cd8
      print 'Updating links in %s.' % (versioned_file,)
Packit bd1cd8
      text = file(source_file, 'r').read()
Packit bd1cd8
      new_text = self.search_for_re.sub(self.replace_with, text)
Packit bd1cd8
      file(versioned_file, 'w').write(new_text)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def main():
Packit bd1cd8
  if len(sys.argv) != 2:
Packit bd1cd8
    sys.exit(__doc__)
Packit bd1cd8
Packit bd1cd8
  brancher = WikiBrancher(sys.argv[1])
Packit bd1cd8
  brancher.BranchFiles()
Packit bd1cd8
  brancher.UpdateLinksInBranchedFiles()
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
if __name__ == '__main__':
Packit bd1cd8
  main()