Пример #1
0
    def removeCorpus(self, fp):
        '''
        Remove the corpus from a compressed file (.tar.gz) and
        create a new music21-noCorpus version.

        Return the completed file path of the newly created edition.

        NOTE: this function works only with Posix systems.
        '''
        TAR = 'TAR'
        # EGG = 'EGG'
        if fp.endswith('.tar.gz'):
            mode = TAR
            modeExt = '.tar.gz'
        else:
            raise Exception('incorrect source file path')

        fpDir, fn = os.path.split(fp)

        # this has .tar.gz extension; this is the final completed package
        fnDst = fn.replace('music21', 'music21-noCorpus')
        fpDst = os.path.join(fpDir, fnDst)
        # remove file extnesions
        fnDstDir = fnDst.replace(modeExt, '')
        fpDstDir = os.path.join(fpDir, fnDstDir)

        # get the name of the dir after decompression
        fpSrcDir = os.path.join(fpDir, fn.replace(modeExt, ''))

        # remove old dir if it exists
        if os.path.exists(fpDst):
            # can use shutil.rmtree
            os.system('rm -r %s' % fpDst)
        if os.path.exists(fpDstDir):
            # can use shutil.rmtree
            os.system('rm -r %s' % fpDstDir)

        if mode == TAR:
            tf = tarfile.open(fp, "r:gz")
            # the path here is the dir into which to expand,
            # not the name of that dir
            tf.extractall(path=fpDir)
            os.system('mv %s %s' % (fpSrcDir, fpDstDir))
            tf.close()  # done after extraction

        # elif mode == EGG:
        #    os.system('mkdir %s' % fpDstDir)
        #    # need to create dst dir to unzip into
        #    tf = zipfile.ZipFile(fp, 'r')
        #    tf.extractall(path=fpDstDir)

        # remove files, updates manifest
        for fn in common.getCorpusContentDirs():
            fp = os.path.join(fpDstDir, 'music21', 'corpus', fn)
            os.system('rm -r %s' % fp)

        fp = os.path.join(fpDstDir, 'music21', 'corpus', '_metadataCache')
        os.system('rm -r %s' % fp)

        # adjust the sources Txt file
        # if mode == TAR:
        sourcesTxt = os.path.join(fpDstDir, 'music21.egg-info', 'SOURCES.txt')
        # else:
        #    raise Exception('invalid mode')

        # elif mode == EGG:
        #    sourcesTxt = os.path.join(fpDstDir, 'EGG-INFO', 'SOURCES.txt')

        # files will look like 'music21/corpus/haydn' in SOURCES.txt
        post = []
        f = open(sourcesTxt, 'r')
        corpusContentDirs = common.getCorpusContentDirs()
        for l in f:
            match = False
            if 'corpus' in l:
                for fn in corpusContentDirs:
                    # these are relative paths
                    fp = os.path.join('music21', 'corpus', fn)
                    if l.startswith(fp):
                        match = True
                        break
            if not match:
                post.append(l)
        f.close()
        f = open(sourcesTxt, 'w')
        f.writelines(post)
        f.close()

        if mode == TAR:
            # compress dst dir to dst file path name
            # need the -C flag to set relative dir
            # just name of dir
            cmd = 'tar -C %s -czf %s %s/' % (fpDir, fpDst, fnDstDir)
            os.system(cmd)
        # elif mode == EGG:
        #    # zip and name with egg: give dst, then source
        #    cmd = 'cd %s; zip -r %s %s' % (fpDir, fnDst, fnDstDir)
        #    os.system(cmd)

        # remove directory that was compressed
        if os.path.exists(fpDstDir):
            # can use shutil.rmtree
            os.system('rm -r %s' % fpDstDir)

        return fpDst  # full path with extension
Пример #2
0
    def removeCorpus(self, fp):
        '''
        Remove the corpus from a compressed file (.tar.gz or .egg) and 
        create a new music21-noCorpus version.

        Return the completed file path of the newly created edition.
    
        NOTE: this function works only with Posix systems. 
        '''
        TAR = 'TAR'
        EGG = 'EGG'
        if fp.endswith('.tar.gz'):
            mode = TAR
            modeExt = '.tar.gz'
        elif fp.endswith('.egg'):
            mode = EGG
            modeExt = '.egg'
        else:
            raise Exception('incorrect source file path')
    
        fpDir, fn = os.path.split(fp)
    
        # this has .tar.gz extension; this is the final completed package
        fnDst = fn.replace('music21', 'music21-noCorpus')
        fpDst = os.path.join(fpDir, fnDst)
        # remove file extnesions
        fnDstDir = fnDst.replace(modeExt, '')
        fpDstDir = os.path.join(fpDir, fnDstDir)
        
        # get the name of the dir after decompression
        fpSrcDir = os.path.join(fpDir, fn.replace(modeExt, ''))
            
        # remove old dir if it exists
        if os.path.exists(fpDst):
            # can use shutil.rmtree
            os.system('rm -r %s' % fpDst)
        if os.path.exists(fpDstDir):
            # can use shutil.rmtree
            os.system('rm -r %s' % fpDstDir)
    
        if mode == TAR:
            tf = tarfile.open(fp, "r:gz")
            # the path here is the dir into which to expand, 
            # not the name of that dir
            tf.extractall(path=fpDir)
            os.system('mv %s %s' % (fpSrcDir, fpDstDir))
    
        elif mode == EGG:
            os.system('mkdir %s' % fpDstDir)
            # need to create dst dir to unzip into
            tf = zipfile.ZipFile(fp, 'r')
            tf.extractall(path=fpDstDir)
    
        tf.close() # done after extraction
    
        # remove files, updates manifest
        for fn in common.getCorpusContentDirs():
            fp = os.path.join(fpDstDir, 'music21', 'corpus', fn)
            os.system('rm -r %s' % fp)
        
        fp = os.path.join(fpDstDir, 'music21', 'corpus', 'metadataCache')
        os.system('rm -r %s' % fp)
        
    
        # adjust the sources Txt file
        if mode == TAR:
            sourcesTxt = os.path.join(fpDstDir, 'music21.egg-info', 'SOURCES.txt')
        elif mode == EGG:
            sourcesTxt = os.path.join(fpDstDir, 'EGG-INFO', 'SOURCES.txt')
    
        # files will look like 'music21/corpus/haydn' in SOURCES.txt
        post = []
        f = open(sourcesTxt, 'r')
        corpusContentDirs = common.getCorpusContentDirs()
        for l in f:
            match = False
            if 'corpus' in l:
                for fn in corpusContentDirs:
                    # these are relative paths
                    fp = os.path.join('music21', 'corpus', fn)
                    if l.startswith(fp):
                        match = True
                        break
            if not match: 
                post.append(l)
        f.close()
        f = open(sourcesTxt, 'w')
        f.writelines(post)
        f.close()
    
        if mode == TAR:
            # compress dst dir to dst file path name
            # need the -C flag to set relative dir
            # just name of dir
            cmd = 'tar -C %s -czf %s %s/' % (fpDir, fpDst, fnDstDir) 
            os.system(cmd)
        elif mode == EGG:
            # zip and name with egg: give dst, then source
            cmd = 'cd %s; zip -r %s %s' % (fpDir, fnDst, fnDstDir) 
            os.system(cmd)
    
        # remove directory that was compressed
        if os.path.exists(fpDstDir):
            # can use shutil.rmtree
            os.system('rm -r %s' % fpDstDir)

        return fpDst # full path with extension