Skip to content

Commit 0a3e043

Browse files
committed
Adding dryrun and ability to create index file for first time
1 parent e184aa6 commit 0a3e043

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

scripts/create_download_tracker.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
"""Script that creates the rdt-download-tracker package."""
22

3+
import argparse
4+
import logging
35
import os
46

57
import boto3
68
import build
79
import tomli
810
import tomli_w
11+
from botocore.exceptions import ClientError
912

1013
import rdt
1114

15+
LOGGER = logging.getLogger(__name__)
1216
PROJECT_PATH = './scripts/rdt-download-tracker'
13-
BUCKET = os.getenv('DOWNLOAD_TRACKER_BUCKET', '')
17+
BUCKET = os.getenv('DOWNLOAD_TRACKER_BUCKET', 'datacebo-download-tracking-pypi')
1418
S3_PACKAGE_PATH = 'simple/rdt-download-tracker/'
19+
INDEX_FILE_NAME = 'index.html'
1520

1621

1722
def _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

6295
if __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)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<head>
3+
<title>Links</title>
4+
</head>
5+
<body>
6+
<h1>Links</h1>
7+
<ul>
8+
</ul>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)