11import os
22import click
3+ import boto3
4+ import glob
5+ import shutil
6+ import tarfile
37
48from datetime import datetime
59from joblib import Parallel , delayed
610from deepracer_race_stats .constants import (
711 LEADERBOARDS_CSV_FILEPATH ,
812 LEADERBOARDS_FOLDER_ASSETS ,
913 LEADERBOARDS_FOLDER ,
14+ SIMAPP_TAR_GZ ,
1015 TRACK_CSV_FILEPATH ,
1116 TRACK_FOLDER_ASSETS ,
17+ TRACK_FOLDER_ROUTES ,
1218)
1319
1420from deepracer_race_stats .util .csv_util import boto_response_to_csv
1723 list_leaderboards ,
1824 list_tracks ,
1925)
20- from deepracer_race_stats .util .media import fetch_media_assets
26+ from deepracer_race_stats .util .assets import fetch_assets , extract_asset_paths
2127
2228
2329@click .group ()
@@ -43,11 +49,59 @@ def track_update(ctx, output_folder):
4349
4450 boto_response_to_csv (response , output_path )
4551
46- asset_map = {r ["TrackArn" ]: r ["TrackPicture" ] for r in response }
52+ asset_map = {}
53+
54+ for r in response :
55+ asset_map .update (extract_asset_paths (r , arn_key = "TrackArn" ))
4756
4857 output_assets_folder = os .path .join (output_folder , TRACK_FOLDER_ASSETS )
58+ fetch_assets (asset_map , output_assets_folder )
59+
60+
61+ @cli .command ()
62+ @click .option ("-o" , "--output-folder" , required = True )
63+ @click .option ("-b" , "--simapp-bucket" , default = "deepracer-managed-resources-us-east-1" )
64+ @click .option ("-k" , "--simapp-key" , default = SIMAPP_TAR_GZ )
65+ @click .pass_context
66+ def simapp_update (ctx , output_folder , simapp_bucket , simapp_key ):
67+ # Download simapp bundle.
68+ tmp_folder = "simapp_tmp"
69+ route_prefix = "opt/install/deepracer_simulation_environment/share/deepracer_simulation_environment/routes"
70+
71+ if not os .path .exists (tmp_folder ):
72+ os .makedirs (tmp_folder )
73+
74+ s3 = boto3 .resource ("s3" )
75+ s3 .Bucket (simapp_bucket ).download_file (Key = simapp_key , Filename = os .path .join ("simapp_tmp" , SIMAPP_TAR_GZ ))
76+
77+ # Specify subfolders to extract.
78+ def subfolders (tf ):
79+ for m in tf .getmembers ():
80+ # Routes folder with the track numpy files.
4981
50- fetch_media_assets (asset_map , output_assets_folder )
82+ if m .path .startswith (route_prefix ):
83+ yield m
84+
85+ with tarfile .open (os .path .join (tmp_folder , SIMAPP_TAR_GZ )) as f :
86+ f .extractall (path = tmp_folder )
87+
88+ # Extract specific parts of the simapp we want to store.
89+ with tarfile .open (os .path .join (tmp_folder , "bundle.tar" ), mode = "r" ) as f :
90+ f .extractall (path = tmp_folder , members = subfolders (f ))
91+
92+ # Move the files we want to the raw_data folder.
93+ output_track_folder = os .path .join (output_folder , TRACK_FOLDER_ROUTES )
94+
95+ if not os .path .exists (output_track_folder ):
96+ os .makedirs (output_track_folder )
97+
98+ for route in glob .glob (os .path .join (tmp_folder , route_prefix , "*.npy" )):
99+ output_path = os .path .join (output_track_folder , os .path .basename (route ))
100+
101+ shutil .copy (route , output_path )
102+
103+ # Remove temporary folder
104+ shutil .rmtree (tmp_folder )
51105
52106
53107@cli .command ()
@@ -65,10 +119,12 @@ def leaderboard_update(ctx, output_folder):
65119
66120 boto_response_to_csv (response , output_path )
67121
68- asset_map = {r ["Arn" ]: r ["ImageUrl" ] for r in response if "ImageUrl" in r }
69- output_assets_folder = os .path .join (output_folder , LEADERBOARDS_FOLDER_ASSETS )
122+ asset_map = {}
123+ for r in response :
124+ asset_map .update (extract_asset_paths (r ))
70125
71- fetch_media_assets (asset_map , output_assets_folder )
126+ output_assets_folder = os .path .join (output_folder , LEADERBOARDS_FOLDER_ASSETS )
127+ fetch_assets (asset_map , output_assets_folder )
72128
73129 # Now do an update for each unique ARN:
74130 # - If OPEN: We collect a snapshot and save it under the current data and time.
0 commit comments