From e5e302e5f62011f2b324b7b48c3546a78be0da51 Mon Sep 17 00:00:00 2001 From: nolan Date: Thu, 28 Mar 2024 19:26:21 -0700 Subject: [PATCH] add more microsite actions --- lib_afc_unify/microsite.py | 90 ++++++++++++++++++++++++-------------- setup.py | 2 +- 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/lib_afc_unify/microsite.py b/lib_afc_unify/microsite.py index e4c53c1..815c71b 100644 --- a/lib_afc_unify/microsite.py +++ b/lib_afc_unify/microsite.py @@ -49,14 +49,15 @@ def upload_website(bucket_name, website_dir, aws_access_key, aws_secret_key): logging.error("[FILE UPLOAD] [%s] [%s] [%s]", bucket_name, file, str(ex)) -def download_website_gzip(bucket_name, region_name, website_gzip_name, - aws_access_key, aws_secret_key): +def get_template(bucket_name, region_name, template_name, template_dir, + aws_access_key, aws_secret_key): """ - download_website_gzip + get_template :param bucket_name: The name of the bucket to work with :param region_name: The region of the bucket - :param website_gzip_name: Name of file to download + :param template_name: Name of template_name + :param template_dir: Name of template_dir :param aws_access_key: Authentication access key :param aws_secret_key: Authentication secret key @@ -69,36 +70,18 @@ def download_website_gzip(bucket_name, region_name, website_gzip_name, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key) - try: - # Download file from S3 - s3.download_file(bucket_name, - website_gzip_name, - website_gzip_name) - return True - except Exception as ex: - logging.error("[WEBSITE GZIP DOWNLOAD] [%s] [%s] [%s]", bucket_name, - website_gzip_name, - str(ex)) + # Download file from S3 + full_path_template = os.path.join(template_dir, f"{template_name}.tar.gz") + s3.download_file(bucket_name, + f"{template_name}.tar.gz", + full_path_template) - return False + # Extract template to directory + with tarfile.open(full_path_template, 'r:gz') as tar: + tar.extractall(template_dir) - -def extract_website(website_gzip): - """ - extract_website - - :param website_gzip: - - :return Folder of extracted website - """ - - # Create a temporary directory - extract_path = tempfile.mkdtemp() - - with tarfile.open(website_gzip, 'r:gz') as tar: - tar.extractall(extract_path) - - return extract_path + # Delete the tar file + os.remove(full_path_template) def apply_template_website(website_dir, template_dict): @@ -114,6 +97,9 @@ def apply_template_website(website_dir, template_dict): # Setup skip dirs skip_dirs = [os.path.join(website_dir, "img")] + # Prepare common context + common_context = template_dict.get('common', {}).get('context', {}) + # Loop all the files for root, _, files in os.walk(website_dir): # Skip certain dirs @@ -128,13 +114,51 @@ def apply_template_website(website_dir, template_dict): try: # Build file path local_path = os.path.join(root, file) + config_key = root.replace(website_dir, "") + config_key = "/" if config_key == "" else config_key # Get jinja2 template template = env.get_template(file) - rendered_content = template.render(template_data=template_dict) + stem_context = template_dict.get(config_key, {}).get('context', {}) + context = {**common_context, **stem_context} + rendered_content = template.render(context) # Write the rendered file with open(local_path, 'w', encoding='utf-8') as outfile: outfile.write(rendered_content) except Exception as ex: logging.error("[FILE JINJA] [%s] [%s]", file, str(ex)) + + +def get_images(website_dir, image_list, campaign_key, bucket_name, + region_name, aws_access_key, aws_secret_key): + """ + get_images + + :param website_dir: Folder of website files + :param image_list: List of images to pull + :param campaign_key: + :param bucket_name: + :param region_name: + :param aws_access_key: + :param aws_secret_key: + + :return + """ + + # Create client connection + s3 = boto3.client('s3', + endpoint_url=f'https://{region_name}.linodeobjects.com', + aws_access_key_id=aws_access_key, + aws_secret_access_key=aws_secret_key) + + # Loop and download all the images + for image in image_list: + # Check that the campaign key is valid + if image.split('/')[1] != campaign_key: + logging.error("[BAD IMAGE MATCH] [%s] [%s]", image.split('/')[1], campaign_key) + continue + + file_name = os.path.basename(image) + download_file = os.path.join(website_dir, "img", file_name) + s3.download_file(bucket_name, image, download_file) diff --git a/setup.py b/setup.py index 3203d44..f14791d 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ with open('README.md', 'r') as f: setup( name='lib_afc_unify', - version='0.1.1', + version='0.1.2', author='', author_email='', description='',