Skip to content

Commit

Permalink
Merge pull request #401 from fyvon/improve/set_release_date
Browse files Browse the repository at this point in the history
Set specific optional release date
  • Loading branch information
fyvon authored Dec 30, 2024
2 parents 210213a + 16e9451 commit 4519dcc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
67 changes: 45 additions & 22 deletions release/scripts/CreateRelease.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import re
from catalog.models import *
from datetime import date,timedelta
from datetime import date, timedelta
from ftplib import FTP
from typing import Self


class CreateRelease:
Expand All @@ -13,36 +14,39 @@ class CreateRelease:
new_performances = {}
new_traits = set()

def __init__(self, release_tomorrow=None):
self.release_tomorrow = release_tomorrow
def __init__(self, release_date=date.today()):
self.release_date = release_date
self.new_publications = Publication.objects.filter(date_released__isnull=True, curation_status="C")

@classmethod
def tomorrow(cls) -> Self:
return cls.on(date.today() + timedelta(days=1))

def get_release_date(self):
release_date = date.today()
if self.release_tomorrow:
release_date = release_date + timedelta(days=1)
self.new_release_date = release_date
@classmethod
def on(cls, release_date: date) -> Self:
return cls(release_date=release_date)

def get_release_date(self):
return self.release_date

def update_data_to_release(self):
"""
Update data ready for the release (scores, performances and publications)
by adding a date in the 'date_released' columns
"""
self.get_release_date()
release_date = self.get_release_date()
previous_traits = set(Score.objects.values_list('trait_efo__id', flat=True).exclude(date_released__isnull=True).distinct())
#### Add release date for each publications and dependent models ####
#### Add release date for each publication and dependent models ####
for publication in self.new_publications:
publication.date_released = self.new_release_date
publication.date_released = release_date
publication.save()

# Scores
scores_list = Score.objects.filter(date_released__isnull=True, publication=publication)
for score in scores_list:
self.new_scores[score.id] = 1
# Update date_release
score.date_released = self.new_release_date
score.date_released = release_date
score.save()
# Get new traits
score_traits = {efotrait.id for efotrait in score.trait_efo.all()}
Expand All @@ -53,16 +57,15 @@ def update_data_to_release(self):
for performance in performances_list:
self.new_performances[performance.id] = 1
# Update date_release
performance.date_released = self.new_release_date
performance.date_released = release_date
performance.save()


def create_new_release(self):
""" Create new release instance and save it in the database """
#### Create new release instance ####
release_notes = 'This release contains {} new Score(s), {} new Publication(s), {} new Performance metric(s) and {} new Trait(s)'.format(len(self.new_scores.keys()), len(self.new_publications), len(self.new_performances.keys()), len(self.new_traits))
release = Release.objects.create(
date=self.new_release_date,
date=self.get_release_date(),
performance_count=len(self.new_performances.keys()),
publication_count=len(self.new_publications),
score_count=len(self.new_scores.keys()),
Expand All @@ -71,7 +74,6 @@ def create_new_release(self):
)
return release


def check_ftp(self):
""" Check that all the PGSs have a corresponding Scoring file in the PGS FTP. """
files_list = []
Expand Down Expand Up @@ -113,17 +115,38 @@ def check_ftp(self):

################################################################################

def run():
def parse_arguments(args):
"""Parse runscript --script-args arguments and return them as a dictionary
Must be a list of key=value pairs.
eg: python manage.py runscript CreateRelease --script-args date=2024-12-25"""
try:
return dict(map(lambda s: s.split('='), args))
except ValueError:
raise Exception("Invalid arguments: must be key=value pairs")


def create_new_release_from_args(args) -> CreateRelease:
"""Parse the runscript arguments for the CreateRelease command.
Expected argument: date=<iso-format-date YYYY-MM-DD>, to choose a particular date for the release.
If no argument is given, the release will be planned for tomorrow."""
parsed_args = parse_arguments(args)

if 'date' in parsed_args:
release = CreateRelease.on(date.fromisoformat(parsed_args['date']))
else:
release = CreateRelease.tomorrow()
release.update_data_to_release()
return release

lastest_release = Release.objects.latest('date').date

release = CreateRelease(release_tomorrow=1)
release.update_data_to_release()
new_release = release.create_new_release()
def run(*args):

latest_release = Release.objects.latest('date').date
release = create_new_release_from_args(args)
new_release = release.create_new_release()

# Just a bunch of prints
print("Latest release: "+str(lastest_release))
print("Latest release: "+str(latest_release))
print("New release: "+str(new_release.date))
print("Number of new Scores: "+str(new_release.score_count))
print(', '.join(release.new_scores.keys()))
Expand Down
22 changes: 10 additions & 12 deletions release/scripts/run_release_script.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import sys, os.path, shutil, glob
from datetime import date
from catalog.models import *
from release.scripts.UpdateGwasStudies import UpdateGwasStudies
from release.scripts.CreateRelease import CreateRelease
from release.scripts.CreateRelease import create_new_release_from_args
from release.scripts.EuropePMCLinkage import EuropePMCLinkage
from curation_tracker.models import CurationPublicationAnnotation

Expand All @@ -11,10 +9,12 @@
output_prefix = ' > '


def run():
def run(*args):
"""
Main method executed by the Django command:
`python manage.py runscript run_release_script`
To choose a particular release date, use the command:
`python manage.py runscript run_release_script --script-args date=YYYY-MM-DD`
"""

#-------------#
Expand Down Expand Up @@ -45,8 +45,8 @@ def run():

print("\n\n#### Start the database release ####")

# Create release
call_create_release()
# Create release (default date is tomorrow, otherwise the value of the runscript argument 'date')
call_create_release(args)

# Update Curation Tracker
update_curation_tracker()
Expand Down Expand Up @@ -164,18 +164,16 @@ def update_gwas_studies():
gwas_studies.update_studies()


def call_create_release():
def call_create_release(args):
""" Create a new PGS Catalog release """
report_header("Create a new PGS Catalog release")

lastest_release = Release.objects.latest('date').date

release = CreateRelease(release_tomorrow=1)
release.update_data_to_release()
latest_release = Release.objects.latest('date').date
release = create_new_release_from_args(args)
new_release = release.create_new_release()

# Just a bunch of prints
output_report("Latest release: "+str(lastest_release))
output_report("Latest release: "+str(latest_release))
output_report("New release: "+str(new_release.date))
output_report("Number of new Scores: "+str(new_release.score_count))
output_report(', '.join(release.new_scores.keys()))
Expand Down

0 comments on commit 4519dcc

Please sign in to comment.