-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: prefix doi management commands
- prefix all one-off destructive DOI commands with `doi_` - add reset_staging to mint new DOIs on staging using the datacite sandbox, doi_reset_staging -> step 3, doi_mint_parent_codebase_dois - bump deps for datacite schema 4.5 and django cve
- Loading branch information
Showing
14 changed files
with
398 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 0 additions & 64 deletions
64
django/library/management/commands/clean_peer_reviewed_dois_02.py
This file was deleted.
Oops, something went wrong.
62 changes: 0 additions & 62 deletions
62
django/library/management/commands/delete_all_existing_codebase_dois_01.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
django/library/management/commands/doi_reset_production.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import csv | ||
import logging | ||
import sys | ||
from django.core.management.base import BaseCommand | ||
from library.doi import VERIFICATION_MESSAGE, get_welcome_message | ||
from library.models import Codebase, CodebaseRelease | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def cleanup_existing_dois(interactive=True, dry_run=True): | ||
print(get_welcome_message(dry_run)) | ||
codebases_with_dois = Codebase.objects.with_doi() | ||
|
||
logger.info("Removing all Codebase DOIs") | ||
if interactive and codebases_with_dois.exists(): | ||
confirm = input( | ||
"WARNING: this will remove all existing codebase DOIs and is unrecoverable. Type 'DELETE' to continue or Ctrl+C to quit: " | ||
) | ||
if not confirm.lower() == "delete": | ||
logger.info("Aborting.") | ||
sys.exit() | ||
|
||
""" | ||
assert correctness | ||
""" | ||
if not dry_run: | ||
print(VERIFICATION_MESSAGE) | ||
with open("codebases_with_dois.csv", "w") as f: | ||
writer = csv.writer(f) | ||
writer.writerow(["Codebase ID", "Codebase DOI"]) | ||
for codebase in codebases_with_dois: | ||
writer.writerow([codebase.pk, codebase.doi]) | ||
codebases_with_dois.update(doi=None) | ||
assert not Codebase.objects.with_doi().exists() | ||
logger.info("Success. All existing codebase DOIs deleted.") | ||
|
||
# clean up unreviewed release DOIs | ||
|
||
unreviewed_releases_with_dois = CodebaseRelease.objects.unreviewed().with_doi() | ||
total_unreviewed_releases_with_dois = unreviewed_releases_with_dois.count() | ||
logger.info( | ||
"Removing %s unreviewed CodebaseRelease DOIs", | ||
total_unreviewed_releases_with_dois, | ||
) | ||
if interactive: | ||
confirm = input( | ||
f"Deleting all DOIs for {total_unreviewed_releases_with_dois} unreviewed CodebaseReleases. Enter 'DELETE' to continue or CTRL+C to quit: " | ||
) | ||
if not confirm.lower() == "delete": | ||
logger.debug("Aborting...") | ||
sys.exit() | ||
|
||
if not dry_run: | ||
with open("unreviewed_releases_with_dois.csv", "w") as f: | ||
writer = csv.writer(f) | ||
writer.writerow(["CodebaseRelease ID", "CodebaseRelease DOI"]) | ||
for release in unreviewed_releases_with_dois: | ||
writer.writerow([release.pk, release.doi]) | ||
unreviewed_releases_with_dois.update(doi=None) | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--interactive", | ||
action="store_true", | ||
help="Wait for user to press enter to continue.", | ||
default=True, | ||
) | ||
parser.add_argument( | ||
"--dry-run", action="store_true", help="Output what would have happened." | ||
) | ||
|
||
def handle(self, *args, **options): | ||
interactive = options["interactive"] | ||
dry_run = options["dry_run"] | ||
cleanup_existing_dois(interactive, dry_run) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import csv | ||
import logging | ||
import sys | ||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from library.doi import VERIFICATION_MESSAGE, get_welcome_message, DataCiteApi | ||
from library.models import Codebase, CodebaseRelease | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def reset_all_dois(interactive=True, dry_run=True): | ||
print(get_welcome_message(dry_run)) | ||
if settings.DEPLOY_ENVIRONMENT.is_production: | ||
logger.error("This command is not allowed in production.") | ||
sys.exit() | ||
logger.info("(ENV: %s) Removing all DOIs", settings.DEPLOY_ENVIRONMENT) | ||
releases_with_dois = CodebaseRelease.objects.with_doi() | ||
codebases_with_dois = Codebase.objects.with_doi() | ||
confirm = input( | ||
"WARNING: this will remove ALL existing DOIs and is unrecoverable. Type 'DELETE' to continue or Ctrl+C to quit: " | ||
) | ||
if confirm.lower() == "delete": | ||
with open("deleted_codebase_dois.csv", "w") as f: | ||
writer = csv.writer(f) | ||
writer.writerow(["Codebase ID", "Codebase DOI"]) | ||
for codebase in codebases_with_dois: | ||
writer.writerow([codebase.pk, codebase.doi]) | ||
Codebase.objects.update(doi=None) | ||
with open("deleted_release_dois.csv", "w") as f: | ||
writer = csv.writer(f) | ||
writer.writerow(["CodebaseRelease ID", "CodebaseRelease DOI"]) | ||
for release in releases_with_dois: | ||
writer.writerow([release.pk, release.doi]) | ||
CodebaseRelease.objects.update(doi=None) | ||
else: | ||
logger.info("Aborting.") | ||
sys.exit() | ||
|
||
""" | ||
assert correctness | ||
""" | ||
if not dry_run: | ||
print(VERIFICATION_MESSAGE) | ||
assert Codebase.objects.with_doi().count() == 0 | ||
assert CodebaseRelease.objects.with_doi().count() == 0 | ||
logger.info("Success. All existing codebase DOIs deleted.") | ||
|
||
""" Mint DOIs for all new Peer Reviewed Releases""" | ||
peer_reviewed_releases = CodebaseRelease.objects.reviewed().public() | ||
datacite_api = DataCiteApi(dry_run=dry_run) | ||
invalid_releases = [] | ||
for release in peer_reviewed_releases: | ||
try: | ||
datacite_api.mint_new_doi_for_release(release) | ||
except Exception as e: | ||
logger.error("Error minting DOI for release %s", release) | ||
invalid_releases.append((release, e)) | ||
|
||
for release, error in invalid_releases: | ||
with open("invalid_releases.csv", "w") as f: | ||
writer = csv.writer(f) | ||
writer.writerow(["CodebaseRelease ID", "Reason", "Datacite Metadata"]) | ||
writer.writerow([release.pk, error, release.datacite.to_dict()]) | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--interactive", | ||
action="store_true", | ||
help="Wait for user to press enter to continue.", | ||
default=True, | ||
) | ||
parser.add_argument( | ||
"--dry-run", action="store_true", help="Output what would have happened." | ||
) | ||
|
||
def handle(self, *args, **options): | ||
interactive = options["interactive"] | ||
dry_run = options["dry_run"] | ||
reset_all_dois(interactive, dry_run) |
Oops, something went wrong.