11"""Script that creates the rdt-download-tracker package."""
22
3+ import argparse
4+ import logging
35import os
46
57import boto3
68import build
79import tomli
810import tomli_w
11+ from botocore .exceptions import ClientError
912
1013import rdt
1114
15+ LOGGER = logging .getLogger (__name__ )
1216PROJECT_PATH = './scripts/rdt-download-tracker'
13- BUCKET = os .getenv ('DOWNLOAD_TRACKER_BUCKET' , '' )
17+ BUCKET = os .getenv ('DOWNLOAD_TRACKER_BUCKET' , 'datacebo-download-tracking-pypi ' )
1418S3_PACKAGE_PATH = 'simple/rdt-download-tracker/'
19+ INDEX_FILE_NAME = 'index.html'
1520
1621
1722def _set_version (version ):
@@ -31,34 +36,65 @@ def build_package():
3136 build .ProjectBuilder (PROJECT_PATH ).build ('sdist' , 'dist' )
3237
3338
34- def _update_index_html (files , s3_client ):
35- index_file_path = os .path .join (S3_PACKAGE_PATH , 'index.html' )
36- response = s3_client .get_object (Bucket = BUCKET , Key = index_file_path )
37- current_index = response .get ('Body' ).read ().decode ('utf-8' )
38- insertion_point = current_index .find ('</ul>' )
39- current_text = current_index [:insertion_point ]
39+ def _load_local_index_file ():
40+ local_index_file_path = os .path .join (PROJECT_PATH , INDEX_FILE_NAME )
41+ with open (local_index_file_path , 'rb' ) as local_index_file :
42+ file = local_index_file .read ().decode ('utf-8' )
43+ return file
44+
45+
46+ def _update_index_html (files , s3_client , dryrun = False ):
47+ index_file_path = os .path .join (S3_PACKAGE_PATH , INDEX_FILE_NAME )
48+ if not dryrun :
49+ try :
50+ response = s3_client .get_object (Bucket = BUCKET , Key = index_file_path )
51+ current_index_file = response .get ('Body' ).read ().decode ('utf-8' )
52+ except ClientError as e :
53+ if e .response ['Error' ]['Code' ] == 'NoSuchKey' :
54+ LOGGER .info ('Index file does not exist yet. Using local one instead.' )
55+ current_index_file = _load_local_index_file ()
56+ else :
57+ current_index_file = _load_local_index_file ()
58+
59+ insertion_point = current_index_file .find ('</ul>' )
60+ current_text = current_index_file [:insertion_point ]
4061 text_list = [current_text ]
4162 for file in files :
4263 new_link = f"<a href='{ file } '>{ file } </a>"
4364 if new_link not in current_text :
4465 text_list .append (new_link )
4566
46- text_list .append (current_index [insertion_point :])
67+ text_list .append (current_index_file [insertion_point :])
4768 new_index = '' .join (text_list )
48- s3_client .put_object (Bucket = BUCKET , Key = index_file_path , Body = new_index )
69+ if dryrun :
70+ print ('New index file:' ) # noqa: T201 `print` found
71+ print (new_index ) # noqa: T201 `print` found
72+ else :
73+ s3_client .put_object (Bucket = BUCKET , Key = index_file_path , Body = new_index )
4974
5075
51- def upload_package ():
52- """Uploads the built package to the S3 bucket."""
76+ def upload_package (dryrun = False ):
77+ """Uploads the built package to the S3 bucket.
78+
79+ Args:
80+ dryrun (bool):
81+ If true, skip the actual uploading and just print out which files would be uploaded.
82+ """
5383 s3_client = boto3 .client ('s3' )
5484 files = os .listdir ('dist' )
5585 for file_name in files :
5686 dest = os .path .join (S3_PACKAGE_PATH , file_name )
57- s3_client .upload_file (os .path .join ('dist' , file_name ), BUCKET , dest )
87+ if dryrun :
88+ print (f'Uploading { file_name } as { dest } to bucket { BUCKET } ' ) # noqa: T201 `print` found
89+ else :
90+ s3_client .upload_file (os .path .join ('dist' , file_name ), BUCKET , dest )
5891
59- _update_index_html (files , s3_client )
92+ _update_index_html (files , s3_client , dryrun )
6093
6194
6295if __name__ == '__main__' :
96+ parser = argparse .ArgumentParser ()
97+ parser .add_argument ('-d' , '--dryrun' , action = 'store_true' , help = 'Skip uploading built files.' )
98+ args = parser .parse_args ()
6399 build_package ()
64- upload_package ()
100+ upload_package (args . dryrun )
0 commit comments