Skip to content

Commit 3b2cfa3

Browse files
committed
Adding package structure for download tracker
lint Beginning to add code to update index Adding workflow and updating script to update index.html Commenting out branch for now and renaming workflow Adding id-token Only adding new files to index Adding dryrun and ability to create index file for first time Fixing format of index.html adding new line using actual s3 link
1 parent dbfd782 commit 3b2cfa3

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Publish Download Tracker
2+
on:
3+
release:
4+
types: [published]
5+
pull_request:
6+
workflow_dispatch:
7+
8+
jobs:
9+
publish-download-tracker:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
id-token: write
13+
steps:
14+
- uses: actions/checkout@v4
15+
# with:
16+
# ref: 'stable'
17+
- name: Set up latest Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version-file: 'pyproject.toml'
21+
- name: Install dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
python -m pip install .[dev]
25+
python -m pip install -r ./scripts/rdt-download-tracker/requirements.txt
26+
- name: Configure AWS Credentials
27+
uses: aws-actions/[email protected]
28+
with:
29+
aws-region: us-east-1
30+
role-to-assume: ${{ secrets.AWS_DOWNLOAD_TRACKER_ROLE }}
31+
role-session-name: UploadRDTDownloadTracker
32+
- name: Create and upload
33+
env:
34+
DOWNLOAD_TRACKER_BUCKET: ${{ secrets.DOWNLOAD_TRACKER_BUCKET }}
35+
run: |
36+
python -m scripts.create_download_tracker

scripts/create_download_tracker.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""Script that creates the rdt-download-tracker package."""
2+
3+
import argparse
4+
import logging
5+
import os
6+
7+
import boto3
8+
import build
9+
import tomli
10+
import tomli_w
11+
from botocore.exceptions import ClientError
12+
13+
import rdt
14+
15+
LOGGER = logging.getLogger(__name__)
16+
PROJECT_PATH = './scripts/rdt-download-tracker'
17+
BUCKET = os.getenv('DOWNLOAD_TRACKER_BUCKET', '')
18+
S3_PACKAGE_PATH = 'simple/rdt-download-tracker/'
19+
INDEX_FILE_NAME = 'index.html'
20+
21+
22+
def _set_version(version):
23+
toml_path = os.path.join(PROJECT_PATH, 'pyproject.toml')
24+
with open(toml_path, 'rb') as f:
25+
pyproject = tomli.load(f)
26+
pyproject['project']['version'] = version
27+
28+
with open(toml_path, 'wb') as f:
29+
tomli_w.dump(pyproject, f)
30+
31+
32+
def build_package():
33+
"""Builds the wheel and sdist for 'rdt-download-tracker'."""
34+
_set_version(rdt.__version__)
35+
build.ProjectBuilder(PROJECT_PATH).build('wheel', 'dist')
36+
build.ProjectBuilder(PROJECT_PATH).build('sdist', 'dist')
37+
38+
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('</body>')
60+
current_text = current_index_file[:insertion_point]
61+
text_list = [current_text]
62+
for file in files:
63+
download_link = f'https://{BUCKET}.s3.us-east-1.amazonaws.com/{S3_PACKAGE_PATH}{file}'
64+
new_link = f"<a href='{download_link}'>{file}</a>"
65+
if new_link not in current_text:
66+
text_list.append(new_link)
67+
text_list.append('<br>')
68+
69+
text_list.append(current_index_file[insertion_point:])
70+
new_index = '\n'.join(text_list)
71+
if dryrun:
72+
print('New index file:') # noqa: T201 `print` found
73+
print(new_index) # noqa: T201 `print` found
74+
else:
75+
s3_client.put_object(Bucket=BUCKET, Key=index_file_path, Body=new_index)
76+
77+
78+
def upload_package(dryrun=False):
79+
"""Uploads the built package to the S3 bucket.
80+
81+
Args:
82+
dryrun (bool):
83+
If true, skip the actual uploading and just print out which files would be uploaded.
84+
"""
85+
s3_client = boto3.client('s3')
86+
files = os.listdir('dist')
87+
for file_name in files:
88+
dest = os.path.join(S3_PACKAGE_PATH, file_name)
89+
if dryrun:
90+
print(f'Uploading {file_name} as {dest} to bucket {BUCKET}') # noqa: T201 `print` found
91+
else:
92+
s3_client.upload_file(os.path.join('dist', file_name), BUCKET, dest)
93+
94+
_update_index_html(files, s3_client, dryrun)
95+
96+
97+
if __name__ == '__main__':
98+
parser = argparse.ArgumentParser()
99+
parser.add_argument('-d', '--dryrun', action='store_true', help='Skip uploading built files.')
100+
args = parser.parse_args()
101+
build_package()
102+
upload_package(args.dryrun)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TODO: fill this in
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Links</title>
4+
</head>
5+
<body>
6+
<h1>Links</h1>
7+
</body>
8+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[project]
2+
name = "rdt-download-tracker"
3+
description = "Package to track downloads of RDT"
4+
version = "1.18.1.dev0"
5+
readme = "README.md"
6+
7+
[build-system]
8+
requires = [
9+
"setuptools",
10+
"wheel",
11+
]
12+
build-backend = "setuptools.build_meta"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Package for tracking downloads of RDT."""
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
boto3
2+
build
3+
tomli
4+
tomli-w

0 commit comments

Comments
 (0)