kapott icon

workon.py

kapott | PRO | 05/31/19 09:01:53 AM UTC | 0 ⭐ | 783 👁️ | Never ⏰ | []
Python |

2.19 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/env python3
 
###
#
# workon <projectname>
# automate switching project + tracking time(ish)
#
###
 
import datetime
import os
import subprocess
import click
 
WO_OPTIONS = {
    "shell": "/bin/bash",
    "project_dir" : "/home/kapott/projects",
    "time_file" : "/home/kapott/projects/time.csv"
}
 
@click.command()
@click.argument('projectname')
def workon(projectname):
    project_path = WO_OPTIONS["project_dir"] + "/" + projectname
    add_time_entry(projectname)
    if os.path.exists(project_path):
        set_project(project_path)
    else:
        create_project(project_path)
 
def set_project(project_path):
    print(f"[■] Setting project dir to {project_path}")
    os.chdir(project_path)
 
    # See if we have a .workon file in there
    try:
        fh = open('.workon', 'r')
        print("[■] Excuting .workon file")
        for line in fh:
            os.system(line)
    except FileNotFoundError:
        print("[■] No .workon found")
 
    os.environ["WORKON"] = ''.join(project_path.split("/")[-1:])
    os.system(WO_OPTIONS["shell"])
 
def create_project(project_path):
    print("[■] Could not find project, creating one")
    os.mkdir(project_path)
    os.chdir(project_path)
    subprocess.call(["git", "init", "."])
 
    with open("README.md", "w") as r:
        r.write("Initial readme file.\n")
 
    with open(".gitignore", "w") as r:
        r.write("*.swp\t# Ignore vim temp files\n.workon\t# Ignore .workon files from Tom")
 
    with open(".workon", "w") as r:
        r.write('printf "[■] This is your .workon file at work:\\n\\n"\n')
    subprocess.call(["git", "add", "-A"])
    subprocess.call(["git", "commit", "-am", "'Initial commit.'"])
    set_project(project_path)
 
def add_time_entry(projectname):
    # "date","time","project name","start|stop"
    now = datetime.datetime.now()
    with open(WO_OPTIONS["time_file"], "a+") as f:
        datum = now.strftime("%Y%m%d")
        tijd = now.strftime("%H:%M")
        print(f"[■] Adding time entry for {projectname} @ {tijd}")
        entry = f'"{datum}","{tijd}","{projectname}","start"\n'
        f.write(entry)
 
if __name__ == '__main__':
    workon()

Comments