Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Fix qrscp --clean #717

Merged
merged 1 commit into from
Dec 21, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Fixes
:meth:`ServiceUser.supported_contexts
<pynetdicom.association.ServiceUser.supported_contexts>` not returning the
expected contexts (:issue:`636`)
* Fixed `qrscp --clean` (:issue:`681`, :issue:`682`)

Enhancements
............
Expand Down
22 changes: 15 additions & 7 deletions pynetdicom/apps/qrscp/qrscp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import argparse
from configparser import ConfigParser
import os
from pathlib import Path
import sys

import pydicom.config
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from pynetdicom import AE, evt, AllStoragePresentationContexts, ALL_TRANSFER_SYNTAXES
from pynetdicom import _config, _handlers
Expand Down Expand Up @@ -48,7 +51,7 @@ def _dont_log(event):
_handlers._recv_c_store_rsp = _dont_log


__version__ = "1.0.0"
__version__ = "1.0.1"


def _log_config(config, logger):
Expand Down Expand Up @@ -212,14 +215,16 @@ def _setup_argparser():
return parser.parse_args()


def clean(db_path, logger):
def clean(db_path, instance_path, logger):
"""Remove all entries from the database and delete the corresponding
stored instances.

Parameters
----------
db_path : str
The database path to use with create_engine().
instance_path : str
The instance storage path.
logger : logging.Logger
The application logger.

Expand All @@ -233,21 +238,24 @@ def clean(db_path, logger):
with engine.connect() as conn:
Session = sessionmaker(bind=engine)
session = Session()

query_success = True
try:
fpaths = [ii.filename for ii in session.query(Instance).all()]
fpaths = [ii.filename for ii in session.query(db.Instance).all()]
except Exception as exc:
logger.error("Exception raised while querying the database")
logger.exception(exc)
session.rollback()
query_success = False
finally:
session.close()

if not query_success:
return False

storage_cleaned = True
for fpath in fpaths:
try:
os.remove(os.path.join(config.INSTANCE_LOCATION, fpath))
os.remove(os.path.join(instance_path, fpath))
except Exception as exc:
logger.error(f"Unable to delete the instance at '{fpath}'")
logger.exception(exc)
Expand All @@ -260,7 +268,7 @@ def clean(db_path, logger):

database_cleaned = False
try:
clear(session)
db.clear(session)
database_cleaned = True
logger.info("Database cleaned successfully")
except Exception as exc:
Expand Down Expand Up @@ -342,7 +350,7 @@ def main(args=None):
if response != "yes":
sys.exit()

if clean(db_path, APP_LOGGER):
if clean(db_path, instance_dir, APP_LOGGER):
sys.exit()
else:
sys.exit(1)
Expand Down