Skip to content

Instantly share code, notes, and snippets.

@pdonorio
Last active November 6, 2015 09:22
Show Gist options
  • Save pdonorio/6e4852bfa9cb6f75c0a7 to your computer and use it in GitHub Desktop.
Save pdonorio/6e4852bfa9cb6f75c0a7 to your computer and use it in GitHub Desktop.
Github repository automatic creation for a python project

A way to easily start the next python project with one command line command

Based on the following python libraries:

  • argparse for command line options
  • pyscaffold for first packaging setup
  • mkdocs instead of sphinx for documentation in markdown
  • requests for connecting to github api
  • plumbum to integrate command line executables inside python

Note: i might create a docker image to use it, someday

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
grepoinit: GitHub Repository INIT;
to start your next python3 project.
Note: it requires libraries
$ pip3 install --upgrade plumbum mkdocs pyscaffold requests
"""
#########################
import os
import argparse
import getpass
import shutil
import requests
# import sys
from plumbum import local
from plumbum.cmd import \
git, putup, mkdocs, \
mv, rm # , ls
description = "Automatic python3 git project creation"
#########################
# Command line options
arg = argparse.ArgumentParser(description='Template for Github python3 repo')
arg.add_argument('repo', type=str, metavar='REPOSITORY_NAME',
help='Github repository name to create')
arg.add_argument('user', type=str, metavar='GITHUB_USERNAME',
help='Github existing account')
arg.add_argument("--force", action="store_true",
help='force removal of existing dir with the same name')
#########################
args = arg.parse_args()
# print("Arguments", args) # Debug
#########################
# Existing directory
if os.path.exists(args.repo):
if args.force:
shutil.rmtree(args.repo)
else:
print("Project '%s' already exists..." % args.repo)
print("Add --force to remove it")
exit(1)
#########################
# Inital scaffold
putup[args.repo]()
# Change dir
path = os.path.join(os.getcwd(), args.repo)
local.cwd.chdir(path)
# Remove useless
rm['-r', 'README.rst', 'docs']()
#########################
# Init new git repo
git["init"]()
var = 'tmp'
# Make the docs
mkdocs['new', var]()
mv[var+'/docs', var+'/mkdocs.yml', '.']()
rm['-r', var]()
# print(ls['-a']())
#########################
# Requirements?
with open('requirements.txt', 'w') as f:
f.write('Flask')
#########################
with open('README.md', 'w') as f:
f.write('#' + args.repo + '\n' + description)
git['add', '*', '.*']()
out = git['commit', '-m', '"Initial commit"']()
print(out)
#########################
gapi_epoint = 'https://api.github.com/user/repos'
params = {"name": args.repo, "description": description,
"homepage": "https://github.com", "private": False,
"has_issues": True, "has_wiki": True, "has_downloads": True}
print("Trying to access as '" + args.user + "' github account\n")
password = getpass.getpass()
try:
r = requests.post(gapi_epoint, auth=(args.user, password), json=params)
except requests.exceptions.ConnectionError as e:
print("Internet connection not available")
exit(1)
out = r.json()
if 'message' in out:
# if out['message'] == 'Bad credentials':
print(out['message'])
exit(1)
##############################
if 'clone_url' in out:
git['remote', 'add', 'origin', out['clone_url']]()
out = git['remote', '-v']()
print(out)
print("Success. Ready to push:")
out = git['push', 'origin', 'master']()
print(out)
else:
print("Something went bad with API calling. Cannot find repo url.")
#########################
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment