def runNBConvert(notebookFilePath): import music21 runDirectoryPath = common.getBuildDocFilePath() pathParts = music21.__path__ + [ 'ext', 'nbconvert', 'nbconvert.py', ] nbconvertPath = os.path.join(*pathParts) nbconvertCommand = '{executable} rst {notebook}'.format( executable=os.path.relpath(nbconvertPath, runDirectoryPath), notebook=os.path.relpath(notebookFilePath, runDirectoryPath), ) print nbconvertCommand subprocess.call(nbconvertCommand, shell=True, cwd=runDirectoryPath)
def convertAllNotebooks(): rstDirectoryPath = common.getBuildDocRstFilePath() notebookDirectoryPath = os.path.join( common.getBuildDocFilePath(), 'ipythonNotebooks', ) for notebookFileName in [x for x in os.listdir(notebookDirectoryPath) \ if x.endswith('.ipynb')]: notebookFilePath = os.path.join( notebookDirectoryPath, notebookFileName, ) try: rstFileName, imageFileDirectoryName = convertOneNotebook(notebookFilePath) except IOError as e: print "Problem with converting notebook %s; skipped." % notebookFileName print str(e) continue oldRstFilePath = os.path.join( notebookDirectoryPath, rstFileName, ) newRstFilePath = os.path.join( rstDirectoryPath, rstFileName, ) os.rename(oldRstFilePath, newRstFilePath) oldImageFileDirectoryPath = os.path.join( notebookDirectoryPath, imageFileDirectoryName, ) # Remove all unnecessary *.text files. for fileName in os.listdir(oldImageFileDirectoryPath): if fileName.endswith('.text'): filePath = os.path.join( oldImageFileDirectoryPath, fileName, ) os.remove(filePath) newImageFileDirectoryPath = os.path.join( rstDirectoryPath, imageFileDirectoryName, ) # Only move the image folder if it will not overwrite anything. if not os.path.exists(newImageFileDirectoryPath): os.rename(oldImageFileDirectoryPath, newImageFileDirectoryPath) # Otherwise, copy its contents and delete the emptied folder. else: for fileName in os.listdir(oldImageFileDirectoryPath): oldFilePath = os.path.join( oldImageFileDirectoryPath, fileName, ) newFilePath = os.path.join( newImageFileDirectoryPath, fileName, ) os.rename(oldFilePath, newFilePath) os.rmdir(oldImageFileDirectoryPath)
def __init__(self): self.srcDirRst = os.path.join(common.getBuildDocFilePath(), 'rst') self.dstDirRst = os.path.join(common.getTestDocsFilePath(), 'testRst') self.testConfigDir = os.path.join(common.getTestDocsFilePath())