2 from __future__ import print_function
10 def isEquqalsSign(str):
11 # Check if this line only contains '='
20 def isGuideHeader(title, equalsSign):
21 if not isEquqalsSign(equalsSign.rstrip()):
23 if len(title.rstrip()) == len(equalsSign.rstrip()):
29 def getLines(lines, start, end):
30 # Get the text of one chapter
38 def getGuides(lines, lineNumbers):
40 for i in range(0, len(lineNumbers)):
41 if i != len(lineNumbers) - 1:
42 start = lineNumbers[i] - 1
43 end = lineNumbers[i + 1] - 1
44 output.append(getLines(lines, start, end))
46 start = lineNumbers[i] - 1
48 output.append(getLines(lines, start, end))
52 def printToFile(textList):
57 file = open(outputPath + '/' +
58 chapterNames[chapterIndex] + '.rst', 'w')
60 file = open(outputPath + '/' +
61 chapterNames[chapterIndex] + '.rst', 'w')
68 def copyDirectory(src, dest):
70 shutil.copytree(src, dest)
71 # Directories are the same
72 except shutil.Error as e:
73 print('Directory not copied. Error: %s' % e)
74 # Any error saying that the directory doesn't exist
76 print('Directory not copied. Error: %s' % e)
79 class FileWrapper(object):
80 def __init__(self, f):
89 return self.f.readline()
92 return self.f.readlines()
94 # Obtain command line arguments
95 parser = argparse.ArgumentParser(
96 description='Obtain input/output and image directories')
97 parser.add_argument('-i', '--input', help='Input reST file. ' +
98 'Default="developer-guide/index.rst"',
99 default='developer-guide/index.rst')
100 parser.add_argument('-r', '--resource', help='Image resource directory',
101 default='argparse.SUPPRESS')
102 parser.add_argument('-o', '--output', help='Output directory',
103 default='argparse.SUPPRESS')
104 args = parser.parse_args()
105 inputFile = args.input
106 inputPath = os.path.dirname(inputFile)
107 if not hasattr(parser, 'resource'):
108 imagePath = inputPath + '/images'
110 imagePath = parser.resource
111 if not hasattr(parser, 'output'):
112 outputPath = inputPath + '-autogenerated'
114 outputPath = parser.output
116 # Create outputPath if necessary
117 if not os.path.exists(outputPath):
118 os.makedirs(outputPath)
120 deGuide = FileWrapper(open(inputFile))
122 lineAbove = deGuide.readline()
123 line = deGuide.readline()
128 if isGuideHeader(lineAbove, line):
129 lineNumbers.append(deGuide.line - 1)
130 chapterNames.append(lineAbove.lower().replace(' ', '-').
131 replace(':', '_').rstrip()) # replace ':' in case sphinx builder can't process.
133 line = deGuide.readline()
135 deGuide = FileWrapper(open(inputFile))
136 lines = deGuide.readlines()
138 textList = getGuides(lines, lineNumbers)
139 printToFile(textList)
141 # Copy image path to developer-guide-autogenerated derectory
142 copyDirectory(imagePath, outputPath + '/images')
144 # Initialize the index.rst file
145 # 1.rename opendaylight-developer-guide.rst(title + author) to index.rst
146 # 2.write toctree info to index.rst
147 indexPath = outputPath + '/' + 'index.rst'
149 os.rename(outputPath + '/' + chapterNames[0] + '.rst', indexPath)
151 print(chapterNames[0] + '.rst ' + 'does not exists.')
153 rstIndexText = open(indexPath, 'a')
154 rstIndexText.write('.. toctree:: \n')
155 rstIndexText.write('\t' + ':maxdepth: 1 \n')
156 rstIndexText.write('\n')
157 for i in range(1, len(chapterNames)):
158 rstIndexText.write('\t' + chapterNames[i] + '\n')