Skip to content

Commit

Permalink
Fix qrscp --clean
Browse files Browse the repository at this point in the history
  • Loading branch information
scaramallion committed Dec 21, 2021
1 parent 142d4e4 commit 13ba58f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
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

0 comments on commit 13ba58f

Please sign in to comment.