#!/usr/bin/env python from __future__ import print_function import os import shutil import argparse chapterNames = [] def isEquqalsSign(str): # Check if this line only contains '=' if str.strip() == '': return False for ch in str: if ch != '=': return False return True def isGuideHeader(title, equalsSign): if not isEquqalsSign(equalsSign.rstrip()): return False if len(title.rstrip()) == len(equalsSign.rstrip()): return True else: return False def getLines(lines, start, end): # Get the text of one chapter out = '' while(start < end): out += lines[start] start += 1 return out def getGuides(lines, lineNumbers): output = [] for i in range(0, len(lineNumbers)): if i != len(lineNumbers) - 1: start = lineNumbers[i] - 1 end = lineNumbers[i + 1] - 1 output.append(getLines(lines, start, end)) else: start = lineNumbers[i] - 1 end = len(lines) output.append(getLines(lines, start, end)) return output def printToFile(textList): chapterIndex = 0 for text in textList: try: file = open(outputPath + '/' + chapterNames[chapterIndex] + '.rst', 'w') except: file = open(outputPath + '/' + chapterNames[chapterIndex] + '.rst', 'w') file.write(text) file.close() chapterIndex += 1 return def copyDirectory(src, dest): try: shutil.copytree(src, dest) # Directories are the same except shutil.Error as e: print('Directory not copied. Error: %s' % e) # Any error saying that the directory doesn't exist except OSError as e: print('Directory not copied. Error: %s' % e) class FileWrapper(object): def __init__(self, f): self.f = f self.line = 0 def close(self): return self.f.close def readline(self): self.line += 1 return self.f.readline() def readlines(self): return self.f.readlines() # Obtain command line arguments parser = argparse.ArgumentParser( description='Obtain input/output and image directories') parser.add_argument('-i', '--input', help='Input reST file. ' + 'Default="developer-guide/index.rst"', default='developer-guide/index.rst') parser.add_argument('-r', '--resource', help='Image resource directory', default='argparse.SUPPRESS') parser.add_argument('-o', '--output', help='Output directory', default='argparse.SUPPRESS') args = parser.parse_args() inputFile = args.input inputPath = os.path.dirname(inputFile) if not hasattr(parser, 'resource'): imagePath = inputPath + '/images' else: imagePath = parser.resource if not hasattr(parser, 'output'): outputPath = inputPath + '-autogenerated' else: outputPath = parser.output # Create outputPath if necessary if not os.path.exists(outputPath): os.makedirs(outputPath) deGuide = FileWrapper(open(inputFile)) lineAbove = deGuide.readline() line = deGuide.readline() lineNumbers = [] while (line != ''): if isGuideHeader(lineAbove, line): lineNumbers.append(deGuide.line - 1) chapterNames.append(lineAbove.lower().replace(' ', '-'). replace(':', '_').rstrip()) # replace ':' in case sphinx builder can't process. lineAbove = line line = deGuide.readline() deGuide.close() deGuide = FileWrapper(open(inputFile)) lines = deGuide.readlines() deGuide.close() textList = getGuides(lines, lineNumbers) printToFile(textList) # Copy image path to developer-guide-autogenerated derectory copyDirectory(imagePath, outputPath + '/images') # Initialize the index.rst file # 1.rename opendaylight-developer-guide.rst(title + author) to index.rst # 2.write toctree info to index.rst indexPath = outputPath + '/' + 'index.rst' try: os.rename(outputPath + '/' + chapterNames[0] + '.rst', indexPath) except OSError: print(chapterNames[0] + '.rst ' + 'does not exists.') rstIndexText = open(indexPath, 'a') rstIndexText.write('.. toctree:: \n') rstIndexText.write('\t' + ':maxdepth: 1 \n') rstIndexText.write('\n') for i in range(1, len(chapterNames)): rstIndexText.write('\t' + chapterNames[i] + '\n') rstIndexText.close()