Skip to content

Commit a9de882

Browse files
committed
PB-2094: Use CustomBaseCommand for all management commands
1 parent 62d3c58 commit a9de882

12 files changed

Lines changed: 41 additions & 63 deletions

app/stac_api/management/commands/dummy_asset.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import hashlib
2-
import logging
32
import random
43
import uuid
54
from io import BytesIO
65

76
from django.conf import settings
8-
from django.core.management.base import BaseCommand
97

108
from stac_api.utils import CommandHandler
9+
from stac_api.utils import CustomBaseCommand
1110
from stac_api.utils import get_s3_resource
1211
from stac_api.utils import get_sha256_multihash
1312
from stac_api.validators import MEDIA_TYPES
1413

15-
logger = logging.getLogger(__name__)
16-
1714
PREFIX = 'dummy-obj-'
1815

1916

@@ -66,7 +63,7 @@ def upload(self):
6663
self.print_success('Done')
6764

6865

69-
class Command(BaseCommand):
66+
class Command(CustomBaseCommand):
7067
help = f"""Upload dummy asset file on S3 for testing.
7168
7269
The command upload dummy asset file with random data on S3 for testing.
@@ -80,6 +77,7 @@ class Command(BaseCommand):
8077
"""
8178

8279
def add_arguments(self, parser):
80+
super().add_arguments(parser)
8381
parser.add_argument(
8482
'action',
8583
type=str,

app/stac_api/management/commands/dummy_asset_upload.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
import logging
21
import os
32

43
from django.conf import settings
5-
from django.core.management.base import BaseCommand
64

75
from stac_api.models.general import BaseAssetUpload
86
from stac_api.models.item import Asset
97
from stac_api.models.item import AssetUpload
108
from stac_api.s3_multipart_upload import MultipartUpload
119
from stac_api.utils import AVAILABLE_S3_BUCKETS
1210
from stac_api.utils import CommandHandler
11+
from stac_api.utils import CustomBaseCommand
1312
from stac_api.utils import get_asset_path
1413
from stac_api.utils import get_sha256_multihash
1514

16-
logger = logging.getLogger(__name__)
17-
1815

1916
class DummyAssetUploadHandler(CommandHandler):
2017

@@ -59,7 +56,7 @@ def start(self):
5956

6057
def list(self):
6158
for upload in AssetUpload.objects.filter(status=BaseAssetUpload.Status.IN_PROGRESS):
62-
print(f"> {upload.upload_id} (asset: {upload.asset.name})")
59+
self.print(f"> {upload.upload_id} (asset: {upload.asset.name})")
6360

6461
def complete(self):
6562
try:
@@ -78,11 +75,12 @@ def abort(self):
7875
self.print_error(f"upload_id {self.options['upload_id']} doesn't exist")
7976

8077

81-
class Command(BaseCommand):
78+
class Command(CustomBaseCommand):
8279
help = """Start dummy Multipart upload for asset file on S3 for testing.
8380
"""
8481

8582
def add_arguments(self, parser):
83+
super().add_arguments(parser)
8684

8785
subparsers = parser.add_subparsers(
8886
dest='action',

app/stac_api/management/commands/dummy_data.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import datetime
2-
import logging
32
import random
43
import string
54
import time
@@ -11,16 +10,14 @@
1110

1211
from django.contrib.gis.geos import Polygon
1312
from django.core.files.uploadedfile import SimpleUploadedFile
14-
from django.core.management.base import BaseCommand
1513

1614
from stac_api.models.collection import Collection
1715
from stac_api.models.item import Asset
1816
from stac_api.models.item import Item
1917
from stac_api.utils import CommandHandler
18+
from stac_api.utils import CustomBaseCommand
2019
from stac_api.validators import MEDIA_TYPES
2120

22-
logger = logging.getLogger(__name__)
23-
2421
# Min/Max extent (roughly) of CH in LV95
2522
XMIN = 2570000
2623
XMAX = 2746000
@@ -252,7 +249,7 @@ def create_asset(self, item, asset_id):
252249
self.print('Asset %s/%s/%s created', item.collection.name, item.name, asset_id, level=3)
253250

254251

255-
class Command(BaseCommand):
252+
class Command(CustomBaseCommand):
256253
help = """Manage dummy data for performance testing.
257254
258255
The command populates the database by default with
@@ -264,6 +261,7 @@ class Command(BaseCommand):
264261
"""
265262

266263
def add_arguments(self, parser):
264+
super().add_arguments(parser)
267265
parser.add_argument(
268266
'action',
269267
type=str,

app/stac_api/management/commands/list_asset_uploads.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import json
2-
import logging
32

4-
from django.core.management.base import BaseCommand
53
from django.core.serializers.json import DjangoJSONEncoder
64

75
from stac_api.models.item import AssetUpload
86
from stac_api.s3_multipart_upload import MultipartUpload
97
from stac_api.serializers.upload import AssetUploadSerializer
108
from stac_api.utils import CommandHandler
9+
from stac_api.utils import CustomBaseCommand
1110
from stac_api.utils import get_asset_path
1211

13-
logger = logging.getLogger(__name__)
14-
1512

1613
class ListAssetUploadsHandler(CommandHandler):
1714

@@ -98,7 +95,7 @@ def are_uploads_equal(s3_upload, db_upload):
9895
elif self.options['s3_only']:
9996
only_s3_uploads = s3_uploads
10097

101-
print(
98+
self.print(
10299
json.dumps(
103100
{
104101
'uploads': uploads,
@@ -119,7 +116,7 @@ def are_uploads_equal(s3_upload, db_upload):
119116
)
120117

121118

122-
class Command(BaseCommand):
119+
class Command(CustomBaseCommand):
123120
help = """List all asset uploads object (DB and/or S3)
124121
125122
This checks for all asset uploads object in DB (by default only returning the `in-progress`
@@ -138,6 +135,7 @@ class Command(BaseCommand):
138135

139136
def add_arguments(self, parser):
140137
self.prog = parser.prog # pylint: disable=attribute-defined-outside-init
138+
super().add_arguments(parser)
141139

142140
parser.add_argument(
143141
'--status',

app/stac_api/management/commands/populate_testdb.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import logging
21
import os
32

43
from django.conf import settings
5-
from django.core.management.base import BaseCommand
64

75
from stac_api.sample_data import importer
86
from stac_api.utils import CommandHandler
7+
from stac_api.utils import CustomBaseCommand
98

109
# path definition relative to the directory that contains manage.py
1110
DATADIR = settings.BASE_DIR / 'app/stac_api/sample_data/'
12-
logger = logging.getLogger(__name__)
1311

1412

1513
class Handler(CommandHandler):
@@ -25,7 +23,7 @@ def populate(self):
2523
self.print_success('Done')
2624

2725

28-
class Command(BaseCommand):
26+
class Command(CustomBaseCommand):
2927
help = """Populates the local test database with sample data
3028
3129
The sample data has to be located in stac_api/management/sample_data and

app/stac_api/management/commands/profile_cursor_paginator.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import cProfile
2-
import logging
32
import os
43
import pstats
54

65
from django.conf import settings
7-
from django.core.management.base import BaseCommand
86

97
from rest_framework.pagination import CursorPagination
108
from rest_framework.request import Request
119
from rest_framework.test import APIRequestFactory
1210

1311
from stac_api.models.item import Item
1412
from stac_api.utils import CommandHandler
15-
16-
logger = logging.getLogger(__name__)
13+
from stac_api.utils import CustomBaseCommand
1714

1815
STAC_BASE_V = f'{settings.STAC_BASE}/v1'
1916

@@ -44,7 +41,7 @@ def profiling(self):
4441
self.print_success('Done')
4542

4643

47-
class Command(BaseCommand):
44+
class Command(CustomBaseCommand):
4845
help = """Paginator paginate_queryset() profiling command
4946
5047
Profiling of the method paginator.paginate_queryset(qs, request)
@@ -53,6 +50,7 @@ class Command(BaseCommand):
5350
"""
5451

5552
def add_arguments(self, parser):
53+
super().add_arguments(parser)
5654
parser.add_argument(
5755
'--collection',
5856
type=str,

app/stac_api/management/commands/profile_item_serializer.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import cProfile
2-
import logging
32
import os
43
import pstats
54

65
from django.conf import settings
7-
from django.core.management.base import BaseCommand
86

97
from rest_framework.test import APIRequestFactory
108

119
from stac_api.models.item import Item
1210
from stac_api.utils import CommandHandler
13-
14-
logger = logging.getLogger(__name__)
11+
from stac_api.utils import CustomBaseCommand
1512

1613
STAC_BASE_V = f'{settings.STAC_BASE}/v1'
1714

@@ -40,7 +37,7 @@ def profiling(self):
4037
self.print_success('Done')
4138

4239

43-
class Command(BaseCommand):
40+
class Command(CustomBaseCommand):
4441
help = """ItemSerializer profiling command
4542
4643
Profiling of the serialization of many items.
@@ -49,6 +46,7 @@ class Command(BaseCommand):
4946
"""
5047

5148
def add_arguments(self, parser):
49+
super().add_arguments(parser)
5250
parser.add_argument(
5351
'--collection',
5452
type=str,

app/stac_api/management/commands/profile_serializer_vs_no_drf.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import json
2-
import logging
32
from timeit import timeit
43

54
from django.conf import settings
6-
from django.core.management.base import BaseCommand
75

86
from rest_framework.test import APIRequestFactory
97

108
from stac_api.models.item import Item
119
from stac_api.utils import CommandHandler
12-
13-
logger = logging.getLogger(__name__)
10+
from stac_api.utils import CustomBaseCommand
1411

1512
STAC_BASE_V = f'{settings.STAC_BASE}/v1'
1613

@@ -85,7 +82,7 @@ def serialize(qs):
8582
self.print_success('NO DRF time: %fms', no_drf_time / self.options['repeat'] * 1000)
8683

8784

88-
class Command(BaseCommand):
85+
class Command(CustomBaseCommand):
8986
help = """ItemSerializer vs simple serializer profiling command
9087
9188
Profiling of the serialization of many items using DRF vs using a simple function.
@@ -94,6 +91,7 @@ class Command(BaseCommand):
9491
"""
9592

9693
def add_arguments(self, parser):
94+
super().add_arguments(parser)
9795
parser.add_argument(
9896
'--collection',
9997
type=str,

app/stac_api/management/commands/reset_counter_tables.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import time
22

3-
from django.core.management.base import BaseCommand
43
from django.db import connection
54

65
from stac_api.utils import CommandHandler
6+
from stac_api.utils import CustomBaseCommand
77

88

99
class Handler(CommandHandler):
@@ -59,7 +59,7 @@ def run(self):
5959
)
6060

6161

62-
class Command(BaseCommand):
62+
class Command(CustomBaseCommand):
6363
help = """Reset the summary counter tables.
6464
6565
Truncates all the summary counter tables and repopulates with current data to make sure they are

app/stac_api/management/commands/update_asset_file_size.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import logging
22

3-
from django.core.management.base import BaseCommand
43
from django.core.management.base import CommandParser
54

65
from stac_api.models.collection import CollectionAsset
76
from stac_api.models.item import Asset
87
from stac_api.utils import CommandHandler
9-
10-
logger = logging.getLogger(__name__)
8+
from stac_api.utils import CustomBaseCommand
119

1210
# increase the log level so boto3 doesn't spam the output
1311
logging.getLogger('boto3').setLevel(logging.WARNING)
@@ -17,15 +15,15 @@
1715
class Handler(CommandHandler):
1816

1917
def update(self):
20-
self.print_success('Running command to update file size')
18+
self.print('Running command to update file size')
2119

2220
asset_limit = self.options['count']
2321

2422
asset_qs = Asset.objects.filter(file_size=0, is_external=False)
2523
total_asset_count = asset_qs.count()
2624
assets = asset_qs.all()[:asset_limit]
2725

28-
self.print_success(f'Update file size for {len(assets)} assets out of {total_asset_count}')
26+
self.print(f'Update file size for {len(assets)} assets out of {total_asset_count}')
2927

3028
for asset in assets:
3129
try:
@@ -45,14 +43,13 @@ def update(self):
4543
asset.file_size = None
4644
asset.save()
4745
print("_", end="", flush=True)
48-
logger.error('file %s could not be found', asset.file)
49-
print()
46+
self.print_error('file %s could not be found', asset.file)
5047

5148
collection_asset_qs = CollectionAsset.objects.filter(file_size=0)
5249
total_asset_count = collection_asset_qs.count()
5350
collection_assets = collection_asset_qs.all()[:asset_limit]
5451

55-
self.print_success(
52+
self.print(
5653
f"Update file size for {len(collection_assets)} collection assets out of "
5754
f"{total_asset_count}"
5855
)
@@ -73,14 +70,12 @@ def update(self):
7370
# bucket.
7471
collection_asset.file_size = None
7572
collection_asset.save()
76-
print("_", end="", flush=True)
77-
logger.error('file %s could not be found', collection_asset.file)
73+
self.print_error('file %s could not be found', collection_asset.file)
7874

79-
print()
8075
self.print_success('Update completed')
8176

8277

83-
class Command(BaseCommand):
78+
class Command(CustomBaseCommand):
8479
help = """Requests the file size of every asset / collection asset from the s3 bucket and
8580
updates the value in the database"""
8681

0 commit comments

Comments
 (0)