bad_clone_prediction/delete_handled_blobs.py

41 lines
1.1 KiB
Python

import os
import shutil
import stat
from MySQLOperator import MySQLOperator
def delete_handled_blobs():
handle_ids = []
repo_blobs_parent_path = "blobs"
mysqlOp: MySQLOperator = MySQLOperator(config_path="config.yml")
mysqlOp.cursor.execute("select id from repositories where handled = 1")
ids = mysqlOp.cursor.fetchall()
def file_remove_readonly(func, path, execinfo):
"""
Function: change the only-read file to writable file
"""
os.chmod(path, stat.S_IWUSR)
func(path)
for id in ids:
handle_ids.append(id.get("id"))
for handle_id in handle_ids:
repo_blobs_path = os.path.join(
repo_blobs_parent_path,
"{id}".format(id=handle_id),
)
if not os.path.exists(repo_blobs_path):
return False
else:
shutil.rmtree(repo_blobs_path, onerror=file_remove_readonly)
print(
"delete blobs of {id} successfully!".format(id=handle_id),
)
if __name__ == "__main__":
delete_handled_blobs()
print("finish")