Automate a New Project Creation

Although it is not such a demanding task I looked into automating the process of creating a new project since automating stuff is always nice.
Instead of creating some git repo and upload it to github with a README file manually, we are going to do it in one command, and it’s not even hard to implement.

The process includes:

  • Creating the repository on github
  • Creating a local matching repository
  • Linking between them and uploading a README file.

Here, I will be explaining the main snippets of the code but you can check out the entire code on my github.

First of all, install the following package with

pip install PyGithub

Next, our import statements will be

from github import Github
import subprocess
import os

Then we will have to connect to github’s API using a token.
You can find here a simple guide to getting this token.

After you got a token we will create our repository using

g = Github(token)
user = g.get_user()
repo = user.create_repo(NAME_OF_THE_REPO)

So far we have accomplished the first step of the process.

On step two, we will simply create an empty new folder with


We could also create a file in this location using

with open('/'.join([PROJECT_PATH, '']), 'w') as file_obj:
file_obj.write(': '.join(['New project created', PROJECT_NAME]))

Now, we will call a bash script that will execute the last step['sh', './', PROJECT_PATH,   user.login, PROJECT_NAME])

The ‘./’ contents are

cd $1
git init
git add -A
git commit -m "initial commit"
git branch -M master
git remote add origin$2/$3.git
git push -u origin master

We are almost done.

Technically, you can be done by now, but I am about to make a slight addition so that our script will function more like a command-line application meaning that we will be able to dynamically pass arguments to it.

In order to do so, I am gonna use the package docopt.
Just pip install docopt in case you don’t have it already installed.

Now in the top of your python script add the following doc:

"""Automate Project Creation
Usage: <project_name> <token> <base_dir> (--readme | --load_readme=<path>)
    -h --help                  Shows this screen.
    -r --readme                Create a README file.
    -lr --load_readme=<path>   Path to an existing README file to include in the project  [default: None].

Then at the very bottom of the script add

if __name__ == '__main__':
    args = docopt(__doc__)

    if args['--readme'] and args['--load_readme'] != 'None' or \
         not args['--readme'] and args['--load_readme'] == 'None':
        raise CreateOrLoadReadme([args['--readme'], args['--load_readme']], 'You can either load or create new readme.')

    create_project(args['<project_name>'], args['<token>'], args['<base_dir>'],
                    args['--readme'], args['--load_readme'])

With these changes we are now able from the terminal to execute for example python TestProject <yourToken> /Projects --readme

and our new project with the name TestProject will be initialized with a readme file.

If you are interested, you can fork my repository from here and use it whenever you need to save 30 seconds of creating and linking a github repository.

One response to “Automate a New Project Creation”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: