Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import subprocess
  3. import sys
  4. import os.path
  5. import shutil
  6.  
  7. def menu(*options, preamble=None, prompt="Select option: "):
  8. NUMBERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9. if len(options) > len(NUMBERS):
  10. raise ValueError("Too many options!")
  11. while True:
  12. if preamble is not None:
  13. print(preamble)
  14. for i, opt in enumerate(options):
  15. print(" {}. {}".format(NUMBERS[i], opt))
  16. n = input(prompt).upper()
  17. try:
  18. i = NUMBERS.index(n)
  19. return options[i]
  20. except (ValueError, IndexError):
  21. print("Please select a valid option")
  22.  
  23. def git_init(dirname, *, bare=False):
  24. if not os.path.exists(dirname):
  25. os.mkdir(dirname)
  26. subprocess.check_call(['git', 'init', ('--bare' if bare else '')], cwd=dirname)
  27.  
  28. def git_init_from_other(newdir, olddir):
  29. shutil.copytree(olddir, newdir)
  30.  
  31. def templates():
  32. return os.listdir('templates')
  33.  
  34. if __name__ == '__main__':
  35. if len(sys.argv) > 2:
  36. sys.exit("Too many args!")
  37. elif len(sys.argv) == 2:
  38. target = sys.argv[1]
  39. else:
  40. target = input("New repo name? ")
  41.  
  42. if os.path.exists(target):
  43. sys.exit("Repository already exists")
  44.  
  45. tpls = templates()
  46.  
  47. tpl = menu(None, *tpls, preamble="Select a template:")
  48.  
  49. if tpl is None:
  50. git_init(target, bare=True)
  51. else:
  52. git_init_from_other(target, os.path.join('templates', tpl))
  53.  
  54. print("Remote URL: storage:code/{}".format(target))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement