Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,19 @@ Make sure you have the name of the package containing all of the slides you want

## Example
``` node generateDPRCommands.js 8712fa8e833798924c5c6205acdcd2d0```

# remove_slide.py

## How does this work?
When the script is executed, it makes a connection to the local MongoDB database. Then it searches the database for a matching slide. Then the script will remove any symbolic links that are linked to the slide that is being removed. Then the script will edit a link.sh file and remove the if block associated with the slide you are removing. Finally the slide will be removed from the MongoDB. This will completely remove the slide from the DPR and the only way to get the slide back is to run a slide processing script (see above).

## Prerequisites
Must have python 3 installed and the pymongo dependency installed. This can be installed using the requirements.txt file in this directory. It can be installed like this `pip3 install -r requirements.txt`

## How to run this script
Make sure you have the name of the slide you want to remove

## Example
`python3 remove_slide.py <slide_name>`

`python3 remove_slide.py S-1234-56789_SIL_2of2`
92 changes: 92 additions & 0 deletions scripts/remove_slide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import sys
from pymongo import MongoClient
import os


def remove_links(slide_id):
targets = [
f'/data/deepZoomImages/{slide_id}_files',
f'/data/deepZoomImages/{slide_id}.dzi',
f'/data/deepZoomImages/tn_{slide_id}.jpeg'
]
for target in targets:
if os.path.islink(target) or os.path.exists(target):
try:
os.remove(target)
print(f"Removed link or file: {target}")
except Exception as e:
print(f"Failed to remove {target}: {e}")

def remove_if_block_from_linksh(slide_id, link_sh_path):
if not os.path.exists(link_sh_path):
print(f"link.sh not found at {link_sh_path}")
return
with open(link_sh_path, "r") as f:
lines = f.readlines()

new_lines = []
inside_if = False
block_buffer = []

for line in lines:
if line.strip().startswith("if ! [ -L "):
inside_if = True
block_buffer = [line]
continue
if inside_if:
block_buffer.append(line)
if line.strip() == "fi":
block_str = "".join(block_buffer)
if slide_id in block_str:
inside_if = False
block_buffer = []
continue
else:
new_lines.extend(block_buffer)
inside_if = False
block_buffer = []
continue
new_lines.append(line)

with open(link_sh_path, "w") as f:
f.writelines(new_lines)
print(f"Removed if block containing slide id '{slide_id}' from {link_sh_path}")


if len(sys.argv) < 2:
print("Usage: python removeSlide.py <slideName>")
sys.exit(1)

slide_name = sys.argv[1]
uri = "mongodb://localhost:27017"
db_name = "knowledgeEnvironment"
collection_name = "patients"
link_sh_path = "/data/deepZoomImages/link.sh"

client = MongoClient(uri)
db = client[db_name]
collection = db[collection_name]

query = {"slides.slideName": slide_name}
found_docs = list(collection.find(query))

if len(found_docs) == 1:
for doc in found_docs:
kpmp_id = doc.get("kpmp_id")
slides = doc.get("slides", [])
for slide in slides:
if slide.get("slideName") == slide_name:
slide_id = slide.get("_id")
if kpmp_id and slide_id:
remove_links(slide_id)
remove_if_block_from_linksh(slide_id, link_sh_path)

update = {"$pull": {"slides": {"slideName": slide_name}}}
result = collection.update_many(query, update)
print(f"\nRemoved slide '{slide_name}' from {result.modified_count} document(s).")
elif len(found_docs) > 1:
print(f"Error: Found multiple documents with slide name '{slide_name}'.")
else:
print(f"No documents found with slide name '{slide_name}'.")

client.close()
1 change: 1 addition & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pymongo
Loading