pdocToS3

pdocToS3

This script runs pdoc over a python library to generate html docs and pushes them to an s3 bucket.

Welcome users: run this script as

pdocToS3 [-p,--profile <aws_profile>] [-b,--bucket <s3_bucket>] [-h Help] [-v VERBOSE]
  1#!/usr/bin/env python3
  2"""
  3pdocToS3
  4
  5This script runs pdoc over a python library to generate html docs and pushes them to an s3 bucket.
  6
  7Welcome users: run this script as
  8
  9    pdocToS3 [-p,--profile <aws_profile>] [-b,--bucket <s3_bucket>] [-h Help] [-v VERBOSE]
 10
 11"""
 12
 13import argparse
 14import os
 15from subprocess_tee import run as _run
 16from utils.aws import ssm_get_parameter, cloudfront_create_invalidation
 17
 18REGION = os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')
 19
 20if __name__ == "__main__":
 21    print("pdocToS3.__main__(): BEGIN")
 22
 23    #
 24    # ArgParse
 25    #
 26    parser = argparse.ArgumentParser(description='Deploy a CDK to an AWS Environment.')
 27    parser.add_argument(
 28        '-p',
 29        '--profile',
 30        action='store',
 31        type=str,
 32        required=False,
 33        help='aws env to run against')
 34
 35    parser.add_argument(
 36        '-b',
 37        '--bucket',
 38        action='store',
 39        type=str,
 40        required=True,
 41        help='s3 bucket to copy files into')
 42
 43    parser.add_argument(
 44        '-d',
 45        '--distribution',
 46        action='store',
 47        type=str,
 48        required=False,
 49        default=None,
 50        help='SSM Parameter Path to a Cloudfront Distribution ID to invalidate cache. Not required.')
 51
 52    parser.add_argument(
 53        '-i',
 54        '--items',
 55        action='store',
 56        type=str,
 57        required=False,
 58        default=None,
 59        help='Space separated List of items to invalidate in the Cloudfront Distribution. (i.e. "/icons/* /css/*")')
 60
 61    parser.add_argument(
 62        '-l',
 63        '--logo_url',
 64        action='store',
 65        type=str,
 66        required=False,
 67        default="https://d2q79iu7y748jz.cloudfront.net/s/_squarelogo/96x96/6901bf333162261745be97d3f22a3445",
 68        help='logo image url to display')
 69
 70    parser.add_argument(
 71        '-u',
 72        '--link_url',
 73        action='store',
 74        type=str,
 75        required=False,
 76        default="https://gocd.cloudops.rekor.io/",
 77        help='link url to attach to logo')
 78
 79    parser.add_argument(
 80        '-v',
 81        '--verbose',
 82        action='count',
 83        default=0,
 84        required=False,
 85        help='verbosity level')
 86
 87    args = parser.parse_args()
 88
 89    _VERBOSE = args.verbose
 90    _AWS_ENV = args.profile
 91    _BUCKET = args.bucket
 92    _LOGO_URL = args.logo_url
 93    _LINK_URL = args.link_url
 94    _DISTRIBUTION = args.distribution
 95    _ITEMS = args.items
 96
 97    #
 98    # Grab any / all text files that have "env python" in the first line
 99    #
100    _file_list = []
101    for root, dirs, files in os.walk('.'):
102        for file in files:
103            try:
104                with open(os.path.join(root, file)) as f:
105                    first_line = f.readline()
106                    if "python" in first_line:
107                        _file_list.append(os.path.join(root, file))
108            except UnicodeDecodeError:
109                pass
110    # _file_list = ' '.join(glob.glob('**/*.py', recursive=True))
111    _proccess_command = "pdoc -o .pdocs --logo " + _LOGO_URL + " --logo-link " + _LINK_URL + " " + ' '.join(_file_list)
112    _process_output = _run(_proccess_command.split(' '), check=True)
113
114    with open('pdoc.output.txt', 'w') as file:
115        if _process_output.stderr is not None:
116            file.write(_process_output.stderr)
117
118        if _process_output.stdout is not None:
119            file.write(_process_output.stdout)
120
121    # pdoc -o hello --logo https://d2q79iu7y748jz.cloudfront.net/s/_squarelogo/96x96/6901bf333162261745be97d3f22a3445 --logo-link https://gocd.cloudops.rekor.io/ *.p
122
123    _profile = '' if _AWS_ENV is None else '--profile ' + _AWS_ENV
124    _proccess_command = "aws s3 " + _profile + "sync .pdocs/ s3://" + _BUCKET
125    _process_output = _run(_proccess_command.split(' '), check=True)
126
127    if _DISTRIBUTION is not None and _ITEMS is not None:
128        _dist_id = ssm_get_parameter(name=_DISTRIBUTION)
129        print(f"pdocToS3(): Found distribution to invalidate. {_dist_id}")
130        _invalidation = cloudfront_create_invalidation(dist=_dist_id, items=[_ITEMS])
131        print(f"pdocToS3.__main__(): CloudFront Invalidation ID: {_invalidation}")
132        with open('pdoc.output.txt', 'w') as file:
133            file.write(f"pdocToS3.__main__(): CloudFront Invalidation ID: {_invalidation}")
134
135    # sh "aws s3 cp .groovy.docs s3://${env.S3_BUCKET}/ --recursive"