def files(self):
        sql = """
            SELECT
                file.id,
                file.md5,
                file.s3_key,
                CONCAT('%s_2', '/', property.entity_id, '/', property.id) AS new_key,
                file.filename,
                file.filesize
            FROM file
            LEFT JOIN property ON property.value_file = file.id
            WHERE file.url IS NULL
            AND file.s3_key IS NOT NULL
            -- AND file.md5 IS NOT NULL
            ORDER BY id
            -- LIMIT 1000;
        """ % self.db_name


        s3_conn   = S3Connection(AWS_ACCESS_KEY, AWS_SECRET_KEY)
        s3_bucket = s3_conn.get_bucket(AWS_BUCKET, validate=False)

        rows = self.db.query(sql)
        for r in rows:
            s3_key = s3_bucket.get_key(r.s3_key)
            if not s3_key:
                print 'ERROR: %s - %s no S3 key' % (r.id, r.s3_key)
                continue

            if not r.filename:
                continue

            # if r.filename:
            #     mimetypes.init()
            #     mime = mimetypes.types_map.get('.%s' % r.filename.lower().split('.')[-1], 'application/octet-stream')
            # else:
            #     mime = 'application/octet-stream'

            if s3_key.content_disposition != 'inline; filename*=UTF-8\'\'%s' % urllib.quote(r.filename.encode('utf-8')):
                # if r.filename:
                #     mimetypes.init()
                #     mime = mimetypes.types_map.get('.%s' % r.filename.lower().split('.')[-1], 'application/octet-stream')
                #     metadata = {
                #         'Content-Type': mime,
                #         'Content-Disposition': 'inline; filename*=UTF-8\'\'%s' % urllib.quote(r.filename.encode('utf-8'))
                #     }
                # else:
                #     metadata = {
                #         'Content-Type': 'application/octet-stream'
                #     }

                # new_key = s3_key.copy(
                #     dst_bucket   = s3_key.bucket.name,
                #     dst_key      = s3_key.name,
                #     metadata     = metadata,
                #     encrypt_key  = True
                # )

                print (r.id, r.s3_key, r.filename, s3_key.content_disposition) % 'ERROR: %s - %s - %s - %s'
                continue


            # if r.md5:
            #     if r.md5 != s3_key.etag[1:][:-1]:
            #         print 'ERROR: %s - %s - %s' % (r.id, r.md5, s3_key.etag[1:-1])

            # if r.filesize != s3_key.size:
            #     print 'ERROR: %s - %s - %s' % (r.id, r.filesize, s3_key.size)


            # if r.new_key:
            #     print 'ERROR: %s - %s new_key' % (r.id, r.s3_key)
            #     continue

            # old_file = os.path.join('/', 'entu', 'oldfiles', r.md5[0], r.md5)
            # if not os.path.exists(old_file):

            #     s3_key = s3_bucket.get_key(self.db_name + '/' + r.md5[0] + '/' + r.md5)
            #     if s3_key:

            #         if r.filename:
            #             mimetypes.init()
            #             mime = mimetypes.types_map.get('.%s' % r.filename.lower().split('.')[-1], 'application/octet-stream')
            #             metadata = {
            #                 'Content-Type': mime,
            #                 'Content-Disposition': 'inline; filename*=UTF-8\'\'%s' % urllib.quote(r.filename.encode('utf-8'))
            #             }

            #         else:
            #             metadata = {
            #                 'Content-Type': 'application/octet-stream'
            #             }

            #         new_key = s3_key.copy(
            #             dst_bucket   = 'entu-files',
            #             dst_key      = r.new_key,
            #             metadata     = metadata,
            #             encrypt_key  = True
            #         )
            #         new_key.set_acl('private')

            #         self.db.execute('UPDATE file set s3_key = %s, changed = NOW(), changed_by = \'s3copy3\' WHERE id = %s LIMIT 1;', r.new_key, r.id)

            #         print 'ERROR: %s - %s S3 file found' % (r.id, r.s3_key)
                    # continue

                # s3_file = Key(s3_bucket)
                # s3_file.key = r.new_key
                # if r.filename:
                #     mimetypes.init()
                #     s3_file.content_type = mimetypes.types_map.get('.%s' % r.filename.lower().split('.')[-1], 'application/octet-stream')
                #     s3_file.set_metadata('Content-Disposition', 'inline; filename*=UTF-8\'\'%s' % urllib.quote(r.filename.encode('utf-8')))
                # else:
                #     s3_file.content_type = 'application/octet-stream'
                # s3_file.set_contents_from_filename(old_file, encrypt_key=True)

                # self.db.execute('UPDATE file set s3_key = %s, changed = NOW(), changed_by = \'s3copy3\' WHERE id = %s LIMIT 1;', r.new_key, r.id)

                # new_dir = os.path.join('/', 'entu', 'oldfiles', r.md5[0])
                # new_file = os.path.join(new_dir, r.md5)

                # if not os.path.exists(new_dir):
                #     os.makedirs(new_dir)
                # os.rename(old_file, new_file)

                # print 'ERROR: %s - %s - %s' % (r.id, old_file, r.new_key)


            # old_file = os.path.join('/', 'entu', 'files', self.db_name, r.md5[0], r.md5)
            # if os.path.exists(os.path.join('/', 'entu', 'files', self.db_name, r.md5[0], r.md5)):
            #     print 'ERROR: %s - %s file not in S3' % (r.id, r.s3_key)

            # if not r.new_key:
            #     print 'ERROR: %s - %s new key not set' % (r.id, r.old_key)
            #     continue

            # if r.filename:
            #     mimetypes.init()
            #     mime = mimetypes.types_map.get('.%s' % r.filename.lower().split('.')[-1], 'application/octet-stream')
            #     metadata = {
            #         'Content-Type': mime,
            #         'Content-Disposition': 'inline; filename*=UTF-8\'\'%s' % urllib.quote(r.filename.encode('utf-8'))
            #     }
            # else:
            #     metadata = {
            #         'Content-Type': 'application/octet-stream'
            #     }

            # new_key = s3_key.copy(
            #     dst_bucket   = s3_key.bucket.name,
            #     dst_key      = r.new_key,
            #     metadata     = metadata,
            #     encrypt_key  = True
            # )
            # new_key.set_acl('private')

            # if not new_key:
            #     print 'ERROR: %s - %s copy failed' % (r.id, r.s3_key)
            #     continue

            # self.db.execute('UPDATE file set s3_key = %s, changed = NOW(), changed_by = \'s3copy2\' WHERE id = %s LIMIT 1;', r.new_key, r.id)

            # s3_key2 = s3_bucket.get_key(r.new_key)
            # if not s3_key2:
            #     print 'ERROR: %s - %s new S3 file not found' % (r.id, r.s3_key)
            #     continue

            # s3_key.delete()

            # self.db.execute('UPDATE file set s3_key = %s, changed = NOW(), changed_by = \'s3copy\' WHERE id = %s LIMIT 1;', r.new_key, r.id)

            # if not self.db.get('SELECT id FROM file WHERE md5 = %s AND s3_key IS NULL LIMIT 1;', r.md5):
            #     old_file = os.path.join('/', 'entu', 'files', self.db_name, r.md5[0], r.md5)
            #     new_dir = os.path.join('/', 'entu', 'oldfiles', r.md5[0])
            #     new_file = os.path.join(new_dir, r.md5)
            #     if os.path.exists(old_file):
            #         if not os.path.exists(new_dir):
            #             os.makedirs(new_dir)
            #         os.rename(old_file, new_file)
            #     else:
            #         print 'ERROR: %s - %s file not found' % (r.id, r.old_key)

            #     s3_key.delete()

            # if args.verbose > 3: print '%s -> %s -> %s' % (r.id, r.old_key, new_key.key)
dataset/ETHPy150Open argoroots/Entu/app/update/s3.py/S3files.files/swapped operands of `%`