コード例 #1
0
ファイル: archive.py プロジェクト: amoghravish/openlibrary
def archive():
    """Move files from local disk to tar files and update the paths in the db."""
    tar_manager = TarManager()
    
    _db = db.getdb()

    try:
        covers = _db.select('cover', where='archived=$f', order='id', vars={'f': False})
        for cover in covers:
            id = "%010d" % cover.id

            print 'archiving', cover
            
            files = {
                'filename': web.storage(name=id + '.jpg', filename=cover.filename),
                'filename_s': web.storage(name=id + '-S.jpg', filename=cover.filename_s),
                'filename_m': web.storage(name=id + '-M.jpg', filename=cover.filename_m),
                'filename_l': web.storage(name=id + '-L.jpg', filename=cover.filename_l),
            }
            # required until is coverstore is completely migrated to new code.
            ensure_thumbnail_created(cover.id, find_image_path(cover.filename))
            
            for d in files.values():
                d.path = d.filename and os.path.join(config.data_root, "localdisk", d.filename)
                    
            if any(d.path is None or not os.path.exists(d.path) for d in files.values()):
                print >> web.debug, "Missing image file for %010d" % cover.id
                continue
            
            if isinstance(cover.created, basestring):
                from infogami.infobase import utils
                cover.created = utils.parse_datetime(cover.created)    
            
            timestamp = time.mktime(cover.created.timetuple())
                
            for d in files.values():
                d.newname = tar_manager.add_file(d.name, open(d.path), timestamp)
                
            _db.update('cover', where="id=$cover.id",
                archived=True, 
                filename=files['filename'].newname,
                filename_s=files['filename_s'].newname,
                filename_m=files['filename_m'].newname,
                filename_l=files['filename_l'].newname,
                vars=locals()
            )
        
            for d in files.values():
                print 'removing', d.path            
                os.remove(d.path)
    finally:
        #logfile.close()
        tar_manager.close()
コード例 #2
0
def _test_write_image(prefix, path):
    data = open(path).read()
    assert coverlib.write_image(data, prefix) != None
    
    def exists(filename):
        return os.path.exists(coverlib.find_image_path(filename))
    
    assert exists(prefix + '.jpg')
    assert exists(prefix + '-S.jpg')
    assert exists(prefix + '-M.jpg')
    assert exists(prefix + '-L.jpg')
    
    assert open(coverlib.find_image_path(prefix + '.jpg')).read() == data
コード例 #3
0
def test_write_image(prefix, path, image_dir):
    """Test writing jpg, gif and png images"""
    data = open(join(static_dir, path)).read()
    assert coverlib.write_image(data, prefix) is not None

    def _exists(filename):
        return exists(coverlib.find_image_path(filename))

    assert _exists(prefix + '.jpg')
    assert _exists(prefix + '-S.jpg')
    assert _exists(prefix + '-M.jpg')
    assert _exists(prefix + '-L.jpg')

    assert open(coverlib.find_image_path(prefix + '.jpg')).read() == data
コード例 #4
0
 def _exists(filename):
     return exists(coverlib.find_image_path(filename))
コード例 #5
0
def test_image_path(image_dir):
    assert coverlib.find_image_path(
        'a.jpg') == config.data_root + '/localdisk/a.jpg'
    assert (coverlib.find_image_path('covers_0000_00.tar:1234:10') ==
            config.data_root + '/items/covers_0000/covers_0000_00.tar:1234:10')
コード例 #6
0
 def exists(filename):
     return os.path.exists(coverlib.find_image_path(filename))
コード例 #7
0
def test_image_path():
    assert coverlib.find_image_path('a.jpg') == config.data_root + '/localdisk/a.jpg'
    assert coverlib.find_image_path('covers_0000_00.tar:1234:10') == config.data_root + '/items/covers_0000/covers_0000_00.tar:1234:10'
コード例 #8
0
 def _exists(filename):
     return exists(coverlib.find_image_path(filename))