Skip to content
Snippets Groups Projects
Commit b4ad950c authored by jgoldste's avatar jgoldste
Browse files

SSA-6324: all new scode_updater tests that simulate command-line execution

parent 7c07fa54
No related branches found
No related tags found
No related merge requests found
......@@ -9,14 +9,14 @@ import warnings
from typing import List
from pymygdala import LogHandler, SendNRAOEvent
from s_code_project_updater import Telescope
from schema.model import Author, Project
from schema.pstmodel import Person, UserAuthentication
from sqlalchemy import exc as sa_exc, asc, desc
### pytest can find these modules just fine from command line
from support.capo import get_my_capo_config
from support.logging import get_console_logger, LOG_MESSAGE_FORMATTER
from schema import ArchiveDBSession
from schema import ArchiveDBSession, ExecutionBlock
from ._version import ___version___ as version
from .project_fetcher import ArchiveProjectFetcher
......@@ -59,11 +59,10 @@ class ScodeProjectUpdater:
self.set_minimum_properties_from_args(args_dict)
return
self.project_code = args_dict['project']
self.stored_project = None
_LOG.debug(f'{self.args}')
self.sc_project = self.scode_project_from_args(self.args)
self.capo_config = get_my_capo_config(profile=self.args.profile)
try:
self.archive_context = ArchiveDBSession('SDM', profile=self.capo_config.profile)
......@@ -269,9 +268,9 @@ class ScodeProjectUpdater:
(including authors) according to the arguments passed in
:return:
'''
fetcher = ArchiveProjectFetcher(self.profile)
fetcher = ArchiveProjectFetcher(self.args.profile)
self.project = fetcher.fetch_project(self.project_code)
if self.is_fetch_only():
self.project = fetcher.fetch_project(self.project_code)
output = fetcher.build_project_info()
try:
[_LOG.info(line) for line in output]
......@@ -296,13 +295,12 @@ class ScodeProjectUpdater:
if self.stored_project is None:
self.exit_with_error('No project found for the project_code provided', 3)
if fetcher.is_alma:
if self.is_alma():
raise ValueError(f'{self.stored_project.project_code} '
f'is an ALMA project; update not permitted')
if self.args.investigators:
proposed_investigators = self.get_pst_users(self.args.investigators)
self.sc_project.investigators = proposed_investigators
if len(proposed_investigators) == 0 or \
not len(self.args.investigators) == len(proposed_investigators):
self.exit_with_error('One or more of the investigators you entered was not '
......@@ -312,10 +310,8 @@ class ScodeProjectUpdater:
if self.args.title:
self.stored_project.title = self.args.title
self.sc_project.title = self.args.title
if self.args.abstract:
self.stored_project.abstract = self.args.abstract
self.sc_project.abstract = self.args.abstract
if not self.args.dry:
if not self.is_fetch_only():
......@@ -327,13 +323,28 @@ class ScodeProjectUpdater:
self.print_project()
return self.stored_project
def is_alma(self):
''' is this an alma project? '''
with warnings.catch_warnings():
# Suppress SQLAlchemy warnings
warnings.simplefilter("ignore", category=sa_exc.SAWarning)
exec_block = self.archive_context.session.query(ExecutionBlock) \
.filter(ExecutionBlock.project_code == self.project_code) \
.filter(ExecutionBlock.telescope == Telescope.ALMA.value) \
.first()
return exec_block is not None
def reindex_project(self):
"""
If we are not performing a dry run, and have made it this far without error, then we
re-index the project so the updates will show up in the profile-mapped archive.
:return: None
"""
if not self.args.dry:
if not self.args.dry and not self.is_fetch_only() \
and '_TEST_PROJECT' not in self.project_code:
_LOG.info(f'Re-indexing project {self.args.project} to make changes available....')
# Set up a LogHandler to record the fact we just made a change to this project.
# We're adding it here, instead of earlier, because nothing we log earlier should be
......@@ -377,7 +388,6 @@ class ArchiveProject:
self.title = title
self.abstract = abstract
self.investigators = author_pst_ids
self.is_alma = None
options = []
options.append('-C')
......@@ -392,9 +402,6 @@ class ArchiveProject:
options.append('--investigators')
self.options = options
def set_alma(self, is_alma):
self.is_alma = is_alma
def make_args(self, is_dry):
args = []
if is_dry:
......@@ -417,13 +424,6 @@ class ArchiveProject:
return args
@staticmethod
def from_schema_project(project: Project, is_alma: bool):
to_return = ArchiveProject(project.project_code, project.title,
project.abstract, project.authors)
to_return.set_alma(is_alma)
return to_return
def is_arg(self, arg):
return arg in self.options
......
......@@ -4,7 +4,6 @@ import warnings
from sqlalchemy import exc as sa_exc, asc, desc
## pytest can't find these
from schema import ArchiveDBSession, create_session, ExecutionBlock
from schema.model import Project, Author
from support.capo import get_my_capo_config
......@@ -30,10 +29,13 @@ class ArchiveProjectFetcher:
_LOG.error(f'An error occurred while creating a db context: {k_ex}')
sys.exit(1)
def __exit__(self, exc_type, exc_val, exc_tb):
self.archive_context.close()
self.pst_context.close()
def fetch_project(self, project_code: str):
with warnings.catch_warnings(): # , self.archive_session, \
# self.pst_session:
with warnings.catch_warnings():
# Suppress SQLAlchemy warnings
warnings.simplefilter("ignore", category=sa_exc.SAWarning)
"""
......@@ -48,7 +50,7 @@ class ArchiveProjectFetcher:
raise AttributeError(f'project {project_code} not found')
self.abstract = self.project.abstract
self.is_alma = self._is_alma()
# self.is_alma = self._is_alma()
return self.project
......@@ -99,7 +101,7 @@ class ArchiveProjectFetcher:
def _is_alma(self):
''' is this an alma project? '''
with warnings.catch_warnings():
with warnings.catch_warnings(), self.archive_context, self.pst_context:
# Suppress SQLAlchemy warnings
warnings.simplefilter("ignore", category=sa_exc.SAWarning)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment