Trying the Python Secrets Module and a Non-Secret Gist

I have been aware of GitHub Gists for a while, but haven’t used them until now. This post is mainly to see how they work in this WordPress blog. I created a public gist and embedded the URL below.

#!/usr/bin/env python3
# https://docs.python.org/3/library/secrets.html
import secrets
s = secrets.token_urlsafe(15)
n1 = secrets.randbelow(len(s))
n2 = secrets.randbelow(len(s))
a = '~!@#$%^&*()-_+=:;'
a1 = secrets.choice(a)
a2 = secrets.choice(a)
t = s[:n1] + a1 + s[n1:]
t = t[:n2] + a2 + t[n2:]
print(t)
view raw try_secrets.py hosted with ❤ by GitHub

For a code sample, I picked a short Python script from when I was looking at the secrets module. This script generates a random password with some hard-coded attributes. It starts by getting a 15 character string from token_urlsafe (though the final result is not URL-safe). It then inserts two special characters, chosen at random from a sequence, at random positions in the string. If I want different attributes for the password (such as a different length, or different token) I just edit the script.

Looks like a secret gist can be embedded too, but that sure doesn’t keep it secret:

# https://docs.pytest.org/en/stable/reference/reference.html#pytest-raises
import pytest
def f():
raise SystemExit(1)
def test_mytest():
with pytest.raises(SystemExit):
f()
view raw test_sysexit.py hosted with ❤ by GitHub

This code sample is from trying the pytest.raises method.

Installing Git From Source On Ubuntu

Git is available from the Ubuntu repositories in the git-core, git-doc, and git-gui packages (there are other Git-related packages available but I think those three make up a basic installation). The package maintainer’s version tends to be behind the currently available version of Git. To have the latest features and fixes in Git it is necessary to install it from source. This is the process I use to install Git from source on Ubuntu 9.10 (karmic) and 10.04 (lucid):

Download the source archive from http://git-scm.com and extract it (I just extract it where it lands in my Downloads directory). Open a terminal in (or cd to) the extracted Git source directory (~/Downloads/git-1.7.2.1 as of my latest install).

Install required packages:

sudo apt-get install libcurl4-gnutls-dev libexpat1-dev libssl-dev gettext libz-dev asciidoc

Build and install Git and its documentation:

make prefix=/usr/local all doc
sudo make prefix=/usr/local install install-doc

If you don’t include the doc and install-doc tasks in the installation then the Git documentation will not be available via man pages or the git help command. If you don’t want the documentation then you can leave out those tasks and you won’t need to install the asciidoc package either. I prefer to have the documentation.

Tweaking the Bash Prompt

A little Saturday morning tweaking.

Based on this post at railstips.org, I decided to adjust my Bash prompt by appending the following to my ~/.bashrc file:

#...

function parse_git_branch {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo "("${ref#refs/heads/}")"
}

BLACK="[33[0;30m]"
BLUE="[33[0;34m]"
VIOLET="[33[1;35m]"
CYAN="[33[0;36m]"

PS1="n[$CYANu@h:$BLUEw$VIOLET $(parse_git_branch)$BLACK]n$ "

The prompt will now show the name of the branch I am working in when the current directory is part of a Git repository. The original code used yellow, red, and green to highlight parts of the prompt. That messed with my mind when I ran RSpec and saw yellow and red when I was expecting all green. Rather than get used to it, I changed the colors. I also added some newlines to perhaps keep the command line neater when deep in a directory tree.

Terminal screen shot

 

[Update 2010-07-23]

After running with the above settings for a while I decided I don’t care for the colors in the prompt. Don’t need the square brackets either. I do like seeing the current git branch. That simplifies things a bit.

#...

function parse_git_branch {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo "("${ref#refs/heads/}")"
}

PS1="nu@h:w  $(parse_git_branch)n$ "

 

[Update 2010-09-25]

Okay, maybe a little color…

#...

function parse_git_branch {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo "("${ref#refs/heads/}")"
}

VIOLET="[33[1;35m]"
NO_COLOR="[33[0;0m]"  

PS1="n$VIOLETu@h:w  $(parse_git_branch)$NO_COLORn$ "

See. I told you it was "tweaking."

Git Resources

I have been learning to use Git. The following is a list of resources I found to be useful, interesting, or that I want to explore further as I get into Git:

Website: Git – Fast Version Control System – The home of Git. When you think source code management it’s only natural to picture a monster eating trees.

Book: Book – Pro Git – by Scott Chacon – Concise coverage of using Git. You can purchase the book or read the whole book online.

Book: Safari Books Online: Version Control with Git, 1st Edition

Tool: msysgit – Run Git on Windows from a specialized BASH prompt.

[Update 2010-07-03: Changed the order of the list so the resources I have used the most are above this note.]

Website: GitHub – Secure Git hosting and collaborative development

Video: Webcast: Git in One Hour – Scott Chacon shows a lot of what he covers in his book in this screencast.

Video: James Gregory on GitJames Gregory does a screencast on Git as well.

Website: git ready – learn git one commit at a time

Article: An introduction to git-svn for Subversion/SVK users and deserters

Article: scie.nti.st – Hosting Git repositories, The Easy (and Secure) Way – Gitosis.

Article: Deploying A Web Application with Git and FTP – Rob Conery shows one way he uses Git.

Article: Git For Windows Developers – Git Series – Part 1 – Jason Meridth – Los Techies – Describes using msysgit.

Article: Branch-Per-Feature – How I Manage Subversion With Git Branches – Los Techies

Article: Git's guts: Branches, HEAD, and fast-forwards – James Gregory's Blog – Los Techies

Article: Martin Fowler – Version Control Tools – Not about Git specifically.

Article: ReinH – A Git Workflow for Agile Teams

Article: Jer on Rails – My Git Workflow

Article: JustinFrench.com – Git Aliases Rock

Article: GitHub – Guides – Put your git branch name in your shell prompt

Article: A Note About Git Commit Messages | tpope.net

Article/Tool: Michael Bien's Weblog – NetBeans GIT support – I have not tried the NBGit plugin yet but I have been playing with NetBeans a bit.

Podcast: Hanselminutes Podcast 108 – Exploring Distributed Source Control with Git

Tool: git_remote_branch

Tool: tortoisegit – Maybe like TortoiseSVN. I have not tried it.

Tool: EGit – Git plugin for Eclipse. I have not tried it.