kapott

workon.py

May 31st, 2019
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. ###
  4. #
  5. # workon <projectname>
  6. # automate switching project + tracking time(ish)
  7. #
  8. ###
  9.  
  10. import datetime
  11. import os
  12. import subprocess
  13. import click
  14.  
  15. WO_OPTIONS = {
  16.     "shell": "/bin/bash",
  17.     "project_dir" : "/home/kapott/projects",
  18.     "time_file" : "/home/kapott/projects/time.csv"
  19. }
  20.  
  21. @click.command()
  22. @click.argument('projectname')
  23. def workon(projectname):
  24.     project_path = WO_OPTIONS["project_dir"] + "/" + projectname
  25.     add_time_entry(projectname)
  26.     if os.path.exists(project_path):
  27.         set_project(project_path)
  28.     else:
  29.         create_project(project_path)
  30.  
  31. def set_project(project_path):
  32.     print(f"[■] Setting project dir to {project_path}")
  33.     os.chdir(project_path)
  34.  
  35.     # See if we have a .workon file in there
  36.     try:
  37.         fh = open('.workon', 'r')
  38.         print("[■] Excuting .workon file")
  39.         for line in fh:
  40.             os.system(line)
  41.     except FileNotFoundError:
  42.         print("[■] No .workon found")
  43.  
  44.     os.environ["WORKON"] = ''.join(project_path.split("/")[-1:])
  45.     os.system(WO_OPTIONS["shell"])
  46.  
  47. def create_project(project_path):
  48.     print("[■] Could not find project, creating one")
  49.     os.mkdir(project_path)
  50.     os.chdir(project_path)
  51.     subprocess.call(["git", "init", "."])
  52.  
  53.     with open("README.md", "w") as r:
  54.         r.write("Initial readme file.\n")
  55.  
  56.     with open(".gitignore", "w") as r:
  57.         r.write("*.swp\t# Ignore vim temp files\n.workon\t# Ignore .workon files from Tom")
  58.  
  59.     with open(".workon", "w") as r:
  60.         r.write('printf "[■] This is your .workon file at work:\\n\\n"\n')
  61.     subprocess.call(["git", "add", "-A"])
  62.     subprocess.call(["git", "commit", "-am", "'Initial commit.'"])
  63.     set_project(project_path)
  64.  
  65. def add_time_entry(projectname):
  66.     # "date","time","project name","start|stop"
  67.     now = datetime.datetime.now()
  68.     with open(WO_OPTIONS["time_file"], "a+") as f:
  69.         datum = now.strftime("%Y%m%d")
  70.         tijd = now.strftime("%H:%M")
  71.         print(f"[■] Adding time entry for {projectname} @ {tijd}")
  72.         entry = f'"{datum}","{tijd}","{projectname}","start"\n'
  73.         f.write(entry)
  74.  
  75. if __name__ == '__main__':
  76.     workon()
Advertisement
Add Comment
Please, Sign In to add comment