Automate Your Project Initialization Using Python & Shell Script

Hello There My Gorgeous Friends On The Internet.

So every time I start a new project, I repeat the following steps:(most of you would, too)

  1. Create a directory for the project.
  2. Create and initialize a git repository for that directory.
  3. Push this directory onto Github.
  4. Open this project directory in VS Code Editor.

And repeating all these steps for every project is tedious. So as a beginner in python scripting, I thought of automating this whole process using a python script and then executing this script in the command line using shell script.

So what this script does is it asks for a name for your desired project directory and makes a directory inside a directory named ‘Projects’.(P.S- you can create your directory at your desired location. I just like my projects to be in a single directory. I know, nerd problems!)And then it initializes a newgit repositoryfor this directory and pushes onto your Github profile.

“Talk is cheap, show me the code.”

-Linus Torvalds

So here it is.

import sys
import os
from github import Github

path = "/path/to/your/desired/projects/directory"

def create():
    folder_name = str(sys.argv[1])
    os.makedirs(path+folder_name)
    username = #your github username
    password = #your github password/ssh key.
    user = Github(username,password).get_user()
    repo = user.create_repo(folder_name)
    print(f"New Repository '{folder_name}' created successfully.")

if __name__ == "__main__":
    create()

Just change the path in the above script to your desired path and you add your Github username and password into username and password variables, respectively.(using an environment variable to hide your password from the script is advised.)

You’re still with me, right? Cool, So now we’ll create a shell script which will add a command ‘create’ and then takes the project directory name as acommand-line argument.

#!/bin/bash
#shell script to automate project initialization.
function create(){
    cd  
    python ./automation1.py $1
    cd path/to/your/projects/directory/$1
    touch Readme.md
    git init
    git add .
    git commit -m "first commit"
    git remote add origin https://github.com/<your_git_username>/$1.git
    git push -u origin master
    code . #optional - opens up new directory in vs code editor
}

All we are doing is executing the python script that we wrote above using a command ‘create’ and then executing some bash commands in the background using this shell script.

Run the below command in terminal to use this script globally.

<path/to/this/file>/project_automation.sh

And now runcreate project_namein your terminal and enjoy your automation.🤘

Thanks For Scrolling, Hope You liked it. #HappyCoding