Skip to content

Commit 82beed8

Browse files
authored
Add flake8 and bug fix venv in the AC tool. (#1784)
* Update venv.sh to add a custom galaxy config to enable flake8 Signed-off-by: ddimatos <[email protected]> * update galaxy config with comments Signed-off-by: ddimatos <[email protected]> * Update ac export var for galaxy config Signed-off-by: ddimatos <[email protected]> * Update ac so that a venv will find the locally venv installed collection Signed-off-by: ddimatos <[email protected]> * fragment added Signed-off-by: ddimatos <[email protected]> * Change flake8 design Signed-off-by: ddimatos <[email protected]> * change flake8 design Signed-off-by: ddimatos <[email protected]> --------- Signed-off-by: ddimatos <[email protected]>
1 parent fc54021 commit 82beed8

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

ac

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ ENDC=$'\e[0m'
121121
# return codes.. Use this method for messages to the console.
122122
# ------------------------------------------------------------------------------
123123
message(){
124+
printf '%s\n' "" >&2
124125
printf '%s\n' "${GRN}${DIV}${ENDC}" >&2
125126
printf '%s\n' "${GRN}INFO:${ENDC} ${1}" >&2
126127
printf '%s\n' "${GRN}${DIV}${ENDC}" >&2
@@ -132,6 +133,7 @@ message(){
132133
# Use this method for error messages to the console.
133134
# ------------------------------------------------------------------------------
134135
message_error(){
136+
printf '%s\n' "" >&2
135137
printf '%s\n' "${RED}${DIV}${RED}" >&2
136138
printf '%s\n' "${RED}ERROR:${ENDC} ${1}" >&2
137139
printf '%s\n' "${RED}${DIV}${ENDC}" >&2
@@ -144,6 +146,7 @@ message_error(){
144146
# Use this method for error messages to the console.
145147
# ------------------------------------------------------------------------------
146148
message_warn(){
149+
printf '%s\n' "" >&2
147150
printf '%s\n' "${YEL}${DIV}${YEL}" >&2
148151
printf '%s\n' "${YEL}WARN:${ENDC} ${1}" >&2
149152
printf '%s\n' "${YEL}${DIV}${YEL}" >&2
@@ -369,11 +372,27 @@ ac_build(){
369372
## Example:
370373
## $ ac --ac-galaxy-importer
371374
ac_galaxy_importer(){
375+
# Extract the pytest ignore errors from `$VENV/galaxy-importer.cfg` and trim all white space
376+
FLAKE8_IGNORE=`cat $VENV/galaxy-importer.cfg| grep -i "ignore = " | cut -d "=" -f 2 | tr -d ' '`
377+
378+
# Path to the installed constants.py
379+
eval "ORIGINAL_CONSTANTS_DIR=(${VENV}/lib/*/site-packages/galaxy_importer/)"
380+
381+
# Backup up the constants.py file before editing it.
382+
cp $ORIGINAL_CONSTANTS_DIR/constants.py /tmp/
383+
384+
# Update constants.py with our choice of FLAKE8_IGNORE entry
385+
sed "s/E402/$FLAKE8_IGNORE/" ${ORIGINAL_CONSTANTS_DIR}/constants.py > ${ORIGINAL_CONSTANTS_DIR}/constants.py.tmp
386+
387+
mv ${ORIGINAL_CONSTANTS_DIR}/constants.py.tmp ${ORIGINAL_CONSTANTS_DIR}/constants.py
388+
372389
message "Creating 'ibm_zos_core' collection with branch: '$GH_BRANCH'."
373390
. $VENV_BIN/activate && collection_name=$($VENV_BIN/ansible-galaxy collection build --force | awk -F/ '{print $NF}') && ls -la $collection_name
374391

375392
message "Running Galaxy Importer for collection $collection_name"
376-
. $VENV_BIN/activate && python3 -m galaxy_importer.main $collection_name
393+
. $VENV_BIN/activate && export GALAXY_IMPORTER_CONFIG=$VENV/galaxy-importer.cfg; python3 -m galaxy_importer.main $collection_name
394+
395+
mv /tmp/constants.py ${ORIGINAL_CONSTANTS_DIR}/constants.py
377396
}
378397

379398
# ------------------------------------------------------------------------------
@@ -1711,8 +1730,14 @@ venv_start(){
17111730
fi
17121731

17131732
message "Starting managed python virtual environment: $VENV_BASENAME"
1733+
# Original , see comments below.
17141734
#. $VENV_BIN/activate; exec /bin/sh -i
1715-
/bin/bash -c ". $VENV_BIN/activate; exec /bin/sh -i;"
1735+
1736+
# TODO: Document wny /bin/bash -c was introduced , the shebang already forces this, could have been because of original CE design which should not depend on bash.
1737+
# /bin/bash -c ". $VENV_BIN/activate; exec /bin/sh -i;"
1738+
1739+
# Invoke venv with epxorts that can find the ansible collection within the venv.
1740+
/bin/bash -c ". $VENV_BIN/activate && export ANSIBLE_LIBRARY=$VENV/ansible_collections/ibm/ibm_zos_core/plugins/modules;export ANSIBLE_CONFIG=$VENV/ansible.cfg; exec /bin/sh -i;"
17161741
}
17171742

17181743
# ------------------------------------------------------------------------------
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
trivial:
2+
- ac - Enabled flake8 support through galaxy-importer, updates
3+
venv.sh to create the galaxy-importer.cfg needed to activate
4+
flake8.
5+
(https://github.com/ansible-collections/ibm_zos_core/pull/1784).
6+
7+
- ac - Enabled managed venv's ability to detect locally installed
8+
collections.
9+
(https://github.com/ansible-collections/ibm_zos_core/pull/1784).

scripts/venv.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,37 @@ write_ansible_cfg(){
175175
unset ansible_cfg
176176
}
177177

178+
# Customized galaxy-importer.cfg for each managed venv, ./ac will know how to source this so its used during execution.
179+
write_galaxy_cfg(){
180+
galaxy_cfg=${galaxy_cfg}"[galaxy-importer]\\n"
181+
galaxy_cfg=${galaxy_cfg}"LOG_LEVEL_MAIN = INFO\\n"
182+
galaxy_cfg=${galaxy_cfg}"RUN_ANSIBLE_TEST = False\\n"
183+
galaxy_cfg=${galaxy_cfg}"ANSIBLE_LOCAL_TMP = '~/.ansible/tmp'\\n"
184+
galaxy_cfg=${galaxy_cfg}"RUN_FLAKE8 = True\\n\\n"
185+
186+
galaxy_cfg=${galaxy_cfg}"[flake8]\\n"
187+
galaxy_cfg=${galaxy_cfg}"exclude =\\n"
188+
galaxy_cfg=${galaxy_cfg}" ./galaxy_importer.egg-info/*,\\n"
189+
galaxy_cfg=${galaxy_cfg}" ./build/*,\\n"
190+
galaxy_cfg=${galaxy_cfg}" ./dist/*\\n"
191+
galaxy_cfg=${galaxy_cfg}" ./.git/*\\n"
192+
galaxy_cfg=${galaxy_cfg}" ./.env/*,\\n"
193+
galaxy_cfg=${galaxy_cfg}" ./.venv/*,\\n"
194+
galaxy_cfg=${galaxy_cfg}" ./.pytest_cache/*,\\n\\n"
195+
196+
galaxy_cfg=${galaxy_cfg}"ignore = E402,W503,W504\\n\\n"
197+
198+
galaxy_cfg=${galaxy_cfg}"# Flake8 codes\\n"
199+
galaxy_cfg=${galaxy_cfg}"# --------------------\\n"
200+
galaxy_cfg=${galaxy_cfg}"# W503: This enforces operators before line breaks which is not pep8 or black compatible.\\n"
201+
galaxy_cfg=${galaxy_cfg}"# W504: This enforces operators after line breaks which is not pep8 or black compatible.\\n"
202+
galaxy_cfg=${galaxy_cfg}"# E402: This enforces module level imports at the top of the file.\\n\\n"
203+
204+
echo -e "${galaxy_cfg}">"${VENV_HOME_MANAGED}"/"${venv_name}"/galaxy-importer.cfg
205+
unset galaxy_cfg
206+
207+
}
208+
178209
# Lest normalize the version from 3.10.2 to 3010002000
179210
# Do we we need that 4th octet?
180211
normalize_version() {
@@ -321,6 +352,7 @@ write_requirements(){
321352
cp ce.py "${VENV_HOME_MANAGED}"/"${venv_name}"/
322353
cp -R modules "${VENV_HOME_MANAGED}"/"${venv_name}"/
323354
write_ansible_cfg
355+
write_galaxy_cfg
324356

325357
# Decrypt file
326358
if [ "$option_pass" ]; then

0 commit comments

Comments
 (0)