command line
The python will be based on the version that you have installed as "python".
If you have multiple versions installed you can use the install path instead of the keyword
# Create .venv dir
python -m venv ./.venv
# Activate using powershell
.venv\Scripts\Activate.ps1
# Activate using cmd
.venv\Scripts\activate.bat
#Install the test requirements
pip install -r .\tests\requirements.txt
VSCode: https://code.visualstudio.com/docs/python/environments
command line
python -m pytest -s ./tests
VSCode: https://code.visualstudio.com/docs/python/testing#_configure-tests
Command line
#requirements
pip install pylint
#running for a specific directory
pylint <dir(source and tests for example)>
VSCode
Download the Pylint extension
To force scan click ctrl+shift+p then select "Pylint:Restart Server"
You can view issues in the Problems
If something should not trigger a problem you can disable scan for that line by adding # pylint: disable=name-of-issue at the end of line
If something should not trigger a problem for the whole project you can add it to the .pylintrc disable list
PEP8 https://peps.python.org/pep-0008/
VSCode: comes with Black so that is what I am currently using. If you want to use it outside VSCode you need to pip install it
To run Black, right click on the file and click Format file or
shift + alt + f
Pycharm : https://blog.jetbrains.com/pycharm/2023/07/2023-2-eap-5/
- go to https://github.com/github/codeql-cli-binaries/releases
- download the package for your platform
- extract to a directory of your choice (not the project dir)
- add the directory to your systems PATH so you can run codeql
- create a codeql database
codeql database create path/todatabase --language=javascript --source-root path/to/your/project
codeql database create path/todatabase --language=python --source-root path/to/your/project
codeql query run path/to/code/ql/query/pack --database path/to/database
pip install bandit
bandit -r path/to/code
For example:
cd source/lambda_functions
bandit -r ./
pip install coverage
coverage run -m pytest .\tests\
then get report
coverage report -m
this can be exported to a text file via '> coverage.txt' at the end of the report line
first time setup (already done)
npm install --save-dev eslint eslint-plugin-react eslint-plugin-react-hooks globals @eslint/js typescript-eslint eslint-plugin-import
or
npm install --dev
npx eslint path/to/code
For example:
npx eslint ./frontend
or
npm run lint
fix specific file
npx eslint path/to/code --fix
first time setup (already done)
npm install --save-dev prettier
or
npm install --dev
to check file
npx prettier --check path/to/file
to format
npx prettier --write path/to/code
VSCode: use the prettier extension
first time setup (already done)
npm install --save-dev eslint-config-prettier eslint-plugin-prettier
or
npm install --dev
npm run lint
VSCode: eslint-prettier extension check config to point it at the one we are using
-
Create a new Lambda function in the EV-ChART development account with a unique name.
- Proceed with the default choice
Author from scratch. - Chose the latest Python runtime.
- Expand
Change default execution role.- Choose
Use an existing role. - Chose the existing role that is prefixed with
LambdaExecution-Nevi....
- Choose
- Click
Create function.
- Proceed with the default choice
-
Add the
AWSSDKPandas-Python3xxfunction layer.- In the new function, scroll down to the bottom of the Console and click the
Add a layerbutton for theLayersheader to the right. - Proceed with the default
Layer source:AWS layers. - Choose
AWSSDKPandas-Python3xx(xxbeing the latest supported Python minor version) for theAWS layersdropdown. - Choose the only version available for
Version. - Click
Add.
- In the new function, scroll down to the bottom of the Console and click the
-
Add the
EV-ChART_Python_Layerfunction layer.- Reopen the Lambda layer configuration page.
- Select
Custom layersforLayer source. - Choose the layer that is prefixed with
EV-ChART_Python_Layer...for theCustom layersdropdown. - Choose the only version available for
Version. - Click
Add.
-
Increase the function timeout.
- In the new function, select the
Configurationtab. - Select
General configurationin the left navigation. - Click the
Editbutton for theGeneral configurationheader to the right. - Increase the function timeout to something higher than the default 3 seconds.
- 30 seconds is a good number.
- Click
Save.
- In the new function, select the
-
Associate to the account VPC.
- In the new function, select the
Configurationtab. - Select
VPCin the left navigation. - Click the
Editbutton for theVPCbox or header to the right. - Select the only available VPC:
vpc-05a349f3444076a6d. - Select a subnet in the
172.28.7.0/24CIDR address space. - Select the security group with ID
sg-033d88f0ef1672caa. - Click
Save.- It can take some time before the Lambda function will execute successfully after this change.
- In the new function, select the
-
Update the function code to connect to Aurora.
- Import:
from evchart_helper import aurora - Utilize your
auroravariable to establish a database connection and use as you would with anypymysqlconnection.connection = aurora.get_connection() cursor = connection.cursor() cursor.execute(""" SELECT @@version """, []) output = cursor.fetchall() connection.commit() # Remember to commit if adding data to the database. - When done, close the connection:
Or:
aurora.close_connection()connection.close() - For ease of management, set up the
connectionandcursorusingwith:This removes the need to explicitly close the connection as it will be done automatically. The importedwith ( aurora.get_connection() as connection, connection.cursor() as cursor ): cursor.execute(""" SELECT @@version """, []) output = cursor.fetchall() connection.commit()aurorawill clean up itself as needed.
- Import:
- Determine the environment moniker (e.g.
qa,preprod) and which primary environment (dev,test, orprod) it will be related to.- Replace all instances of
MONIKERin the subsequent instructions with the identified new environment moniker.
- Replace all instances of
- Make a new repository branch that will be identified as managing the new environment.
- Create a new deployment configuration for that moniker.
- Copy the baseline configuration (
devops/baseline.configuration.json) and rename the new filebaseline.MONIKER.configuration.json.- Edit the new file with the below content:
[ { "ParameterKey": "SubEnvironment", "ParameterValue": "MONIKER" } ]
- Edit the new file with the below content:
- Copy the configuration of the related primary environment (e.g.
devops/deploy.dev.configuration.json) and rename the new filedeploy.MONIKER.configuration.json.- Edit the new file and add the below to the list of parameters:
Ensure commas are added as necessary for valid JSON structure.
{ "ParameterKey": "SubEnvironment", "ParameterValue": "MONIKER" }
- Edit the new file and add the below to the list of parameters:
- Copy the baseline configuration (
- Create a new deployment workflow for that moniker.
-
Copy the workflow of the related primary environment (e.g.
.github/workflows/develop_deploy.yaml) and rename the new fileMONIKER_deploy.yaml.- Edit the new file making the below changes:
- Set the
on.push.brancheslist to contain only the name of the new environment branch.- Only if the primary environment is not
prod.
- Only if the primary environment is not
- Set
nametodeploy MONIKER. - Set
jobs.environment-restrictions.environmentto the environment moniker. - Set
jobs.get-sha.steps.0.with.refto the name of the new environment branch. - Set
jobs.trigger-tests.with.BRANCH_NAMEto the name of the new environment branch. - Set
jobs.trigger-deploy.with.ENV_NAMEto the environment moniker. - Set
jobs.trigger-deploy.with.BRANCH_NAMEto the name of the new environment branch.
- Set the
This new workflow file will need to be present on the default repository branch (
develop) before workflows can be run manually. - Edit the new file making the below changes:
-
- Create a new React configuration for that moniker.
- Copy the configuration of the related primary environment (e.g.
frontend/.env.dev) and rename the new file.env.MONIKER.- Edit the new file making the below changes:
- Set
PUBLIC_URLandREACT_APP_API_URLto the identified hostname (generallyhttps://evchart-MONIKER.driveelectric.gov) for the new environment. - Take note of
REACT_APP_CLIENTID; this value will need to be revisited in a future step.
- Set
- Edit the new file making the below changes:
- Edit
frontend/package.jsonand add the below to thescriptsattribute:Ensure commas are added as necessary for valid JSON structure."build:MONIKER": "env-cmd -f .env.MONIKER react-scripts build"
- Copy the configuration of the related primary environment (e.g.
- Deploy the new environment.
- Access the relevant AWS account and identify the new AWS Cognito user pool client; take note of the client ID.
- Update the value for
REACT_APP_CLIENTIDin the React configuration for the new environment.
- with no way to test sql when creating your query you can unit test using cursor.mogrify()
- to set this up in the test use the following
instead of executing the query usefrom pymysql.connections import Connection conn = Connection( host="localhost", user="fake", password="fake", database="fake", defer_connect=True ) conn.encoding = "utf8mb4" conn.client_flag = 0 conn.server_status = 0 conn._sock = True cursor = connection.cursor()cursor.mogrify(query=query, args=data)