Skip to content

Instantly share code, notes, and snippets.

@milindchawre
Last active October 21, 2020 07:36
Show Gist options
  • Save milindchawre/c423b9b62799b8996ddf1e1d742f9c4d to your computer and use it in GitHub Desktop.
Save milindchawre/c423b9b62799b8996ddf1e1d742f9c4d to your computer and use it in GitHub Desktop.
General steps while developing cli tools using python3 (following proper TT Test Driven Development)
Create project folder
mkdir -p code/fileops
Initialize virtualenv
cd code/fileops
pipenv -python 3.9
pipenv shell
cat Pipefile
git init
git status
Write readme file
Create Makefile
--------------------------------------
.PHONY: default install test
default: test
install:
pipenv install --dev --skip-lock
test:
PYTHONPATH=./src pytest
--------------------------------------
Create src code and test folder
mkdir -p src/fileops tests
touch src/fileops/__init__.py tests/.keep
Add .gitignore for python
https://github.com/github/gitignore/blob/master/Python.gitignore
https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore
Commit it: git commit -m "Initial Commit"
Start writing tests (for parser)
tests -> in tests/test_cli.py
install pytest as dev package we need it (pipenv install --dev pytest)
Also install pytest-mock incase you're using mocker in pytest
Run test using: make
Follow RED -> Green -> Refactor concept after adding each test (following the TDD test driven development)
Install flake8 and black for liniting and formatting python code (pipenv install --dev flake8 black)
Change makefile to include linting
-----------------------------
lint:
black .
flake8
lint-check:
flake8
black --check --diff .
-----------------------------
Might need to add .flake8 and pyproject.toml file
https://github.com/aquasecurity/kube-hunter/blob/master/.flake8
Write tests files and python code in src folder.
Write setup.py file with metadata info.
---------------------------------------------
from setuptools import setup, find_packages
with open('README.md', encoding='UTF-8') as f:
readme = f.read()
setup(
name='fileops',
version='0.1.0',
description='Utility to print content of specific line number from a given file.',
long_description=readme,
author='Milind',
author_email='example@gmail.com',
packages=find_packages('src'),
package_dir={'': 'src'},
install_requires=[],
entry_points={
'console_scripts': [
'fileops=fileops.cli:main',
],
}
)
---------------------------------------------
Install the developed package using: pipenv install -e .
test the package:
Uninstall package pipenv uninstall package-name
Edit/fix the code and reinstall the package and test it. Repeat.
Add setup.cfg file
--------
[bdist_wheel]
python-tag = py36
--------
Build a wheel distribution
python setup.py bdist_wheel
This will generate wheel file inside dist folder (dist/fileops-0.1.0-py36-none-any.whl)
Use this file with pip install to install our developed package.
Note the python-tag in setup.cfg says the min. version of python on which it will work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment