Python is like the pyramids it takes forever to get setup

Create a python module shell in under 5 minutes

Colin Bitterfield

--

I routine create python modules for security work and personal fun projects. Getting a directory structure prepared and prepped takes way too long manually. Recently I found a tool called “toptive python” that runs in Yeomn. It creates a shell, docker file, documentation and a complete skeleton for a well formed python module. Credit to “Franco Riberi

I use MacPorts to install NPM and yo.

$ sudo port install npm

$ sudo npm install -g yo

Then I installed Toptive-Python

$ npm install -g generator-toptive-python

For a little background, I use IntellJ Idea as my IDE but this will work for any IDE.

Now on to creating a shell repository in git hub.

Example:

Repository Name: medium-shell

Open your favorite browser and negotiate to Github

Example:

Create Repository
Repository Created

Open your terminal program

$ cd ${project_directory}

Run Topotive-Python

yo toptive-python

Creating the Shell

Uploading the initial shell to GitHub and creating the repository initialization.

  • See GitHub Instruction from initial creation for more information.

razzamataz:IdeaProjects colin$ cd medium-shell/
razzamataz:medium-shell colin$ git init
Initialized empty Git repository in /Users/colin/IdeaProjects/medium-shell/.git/
razzamataz:medium-shell colin$ git add *
razzamataz:medium-shell colin$ git commit -m “first commit medium shell”
[master (root-commit) f2088a8] first commit medium shell
31 files changed, 968 insertions(+)

create mode 100644 AUTHORS.rst
create mode 100644 CHANGELOG.rst
create mode 100644 CONTRIBUTING.rst
create mode 100644 Dockerfile
create mode 100644 LICENSE.rst
create mode 100644 MANIFEST.in
create mode 100644 Makefile
create mode 100644 README.rst
create mode 100644 docs/Makefile
create mode 100644 docs/authors.rst
create mode 100755 docs/conf.py
create mode 100644 docs/contributing.rst
create mode 100644 docs/history.rst

create mode 100644 docs/index.rst
create mode 100644 docs/installation.rst
create mode 100644 docs/make.bat
create mode 100644 docs/readme.rst
create mode 100644 docs/usage.rst
create mode 100644 medium-shell/__init__.py
create mode 100644 medium-shell/medium-shell.py
create mode 100644 requirements/dev.txt
create mode 100644 requirements/prod.txt
create mode 100644 requirements/test.txt
create mode 100644 setup.cfg
create mode 100644 setup.py
create mode 100644 stests/__init__.py
create mode 100644 stests/common_resources.robot

create mode 100644 stests/default_suite.robot
create mode 100644 tox.ini
create mode 100644 utests/__init__.py
create mode 100644 utests/test_python_boilerplate.py
razzamataz:medium-shell colin$ git branch -M main
razzamataz:medium-shell colin$ git remote add origin git@github.com:cbitterfield/medium-shell.git
razzamataz:medium-shell colin$ git push -u origin main
Enumerating objects: 37, done.
Counting objects: 100% (37/37), done.
Delta compression using up to 16 threads
Compressing objects: 100% (32/32), done.
Writing objects: 100% (37/37), 13.05 KiB | 2.61 MiB/s, done.
Total 37 (delta 0), reused 0 (delta 0)
To github.com:cbitterfield/medium-shell.git
* [new branch] main -> main

Branch ‘main’ set up to track remote branch ‘main’ from ‘origin’.

Repository is ready for use.

Link to the actual repository I created.

The python tool “Bumpversion” is very useful here as well.

This shell is well formed and has everything you need in place to integrate with Travis and CICD pipelines.

Installing Bumpversion with MacPort Python

$ sudo port install py38-pip

— -> Some of the ports you installed have notes:
py38-pip has the following notes:
To make the Python 3.8 version of pip the one that is run when you execute the commands without a version suffix, e.g. ‘pip’,
run:
sudo port select — set pip pip38
sudo port select — set pip3 pip38
$ sudo port select — set pip pip38
Selecting ‘pip38’ for ‘pip’ succeeded. ‘pip38’ is now active.
$ sudo port select — set pip3 pip38
Selecting ‘pip38’ for ‘pip3’ succeeded. ‘pip38’ is now active.

$ pip install — upgrade bumpversion
Defaulting to user installation because normal site-packages is not writeable
Collecting bumpversion
Using cached bumpversion-0.6.0-py2.py3-none-any.whl (8.4 kB)
Collecting bump2version
Using cached bump2version-1.0.1-py2.py3-none-any.whl (22 kB)
Installing collected packages: bump2version, bumpversion
WARNING: The scripts bump2version and bumpversion are installed in ‘/Users/colin/Library/Python/3.8/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use — no-warn-script-location.
Successfully installed bump2version-1.0.1 bumpversion-0.6.0

  • Note for a Mac with MacPorts and your local bin file to your .profile, .zprofile, .bashrc or similar file. (Not recommended to add to global profile)

I use bash even though OSX 11.0 has recommended Zshell. Too many years of BASH programming to relearn.

$ echo “/Users/${USER}/Library/Python/3.8/bin” >> ~/.profile

Install Python virtualenv if not already installed.

Example:

$ sudo port install py38-virtualenv
Password:
— -> Computing dependencies for py38-virtualenv
The following dependencies will be installed:
py38-appdirs
py38-distlib
py38-filelock
py38-six
virtualenv_select
Continue? [Y/n]: y

— -> Some of the ports you installed have notes:
py38-virtualenv has the following notes:
The executable is installed as ‘/opt/local/bin/virtualenv-3.8’. To symlink it to ‘/opt/local/bin/virtualenv’, run:

sudo port select — set virtualenv virtualenv38
$ sudo port select — set virtualenv virtualenv38
Selecting ‘virtualenv38’ for ‘virtualenv’ succeeded. ‘virtualenv38’ is now active.

Congratulations: You are now ready to code.

Time to code

--

--

Colin Bitterfield

NIST certified Security Professional | 10+ years experience in infrastructure security and compliance | Experienced in creating security programs.