Ejemplo n.º 1
0
def index(datapath, dbpath):
    """Create or update the index stored in database <dbpath>, using
    the music file/directory structure in <datapath>."""
    # Create or open the database we're going to be writing to.
    db = xapian.WritableDatabase(dbpath, xapian.DB_CREATE_OR_OPEN)

    # Make sure all songs in the directory are in the database.
    for filePath in dt.get_files(datapath):
        if not pathInDB(db, filePath):
            addSong(db, dt.parseFile(filePath))
        else:
            mtimeFile = os.path.getmtime(filePath)
            dbEntry = search(dbpath, "path:" + filePath)[0]
            mtimeDB = time.mktime(time.strptime(dbEntry['data']['mtime']))
            if mtimeFile > mtimeDB:
                logging.warning("File %s has changed." % filePath)
                addSong(db, mergeSongs(dbEntry['data'], dt.parseFile(filePath)))
            else:
                logging.info("File %s hasn't changed." % filePath)

    # Now, make sure no songs have disappeared.
    songFiles = dt.get_files(datapath)
    for song in all_songs(dbpath):
        songPath = song['data']['path']
        if not songPath in songFiles:
            logging.warning("Song file %s has disappeared!" % songPath)
Ejemplo n.º 2
0
    def test_all_songs(db):
        files = [f for f in get_files(os.path.join(TESTDIR, "music_dir"))]
        song_paths = [song["data"]["path"] for song in all_songs(db)]

        # Did we get all songs?
        print song_paths
        assert len(song_paths) == len(files)

        # ...and did we get the RIGHT songs?
        for f in files:
            print f
            assert f in song_paths