def clean_ssl_keyring(keyring, session):
    old = open(keyring, 'r+')
    fcntl.lockf(old, fcntl.LOCK_EX)

    new = open(keyring + '.tmp', 'w')
    fcntl.lockf(new, fcntl.LOCK_EX)

    for line in old:
        if "-BEGIN CERTIFICATE-" in line:
            der = b""
            pem = line
        elif "-END CERTIFICATE-" in line:
            pem += line
            fingerprint = sha1(der).hexdigest().upper()
            builder = session.query(Builder).filter_by(ssl=fingerprint).first()
            user = session.query(Person).filter_by(ssl=fingerprint).first()
            if builder or user:
                new.write(pem)
        else:
            der += b64decode(line.strip())
            pem += line

    new.close()
    os.rename(keyring + '.tmp', keyring)
    old.close()
dataset/ETHPy150Open Debian/debile/debile/master/keyrings.py/clean_ssl_keyring/original