axnew 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. import optparse
  5. import distutils.dir_util
  6. def usage():
  7. print('Ax toolkit project creation tool\n')
  8. print('Usage:\n\tax [-n <project_name>] <path_to_project>')
  9. print('\n<path_to_project> must be an existing path')
  10. def rename_main_file(dstdir, fname):
  11. fn = fname
  12. if len(fn) < 3:
  13. fn += '.go'
  14. else:
  15. e = fn[len(fn)-3:]
  16. if e != '.go':
  17. fn += '.go'
  18. fromname = os.path.join(dstdir, 'main.go')
  19. toname = os.path.join(dstdir, fn)
  20. try:
  21. os.rename(fromname, toname)
  22. except:
  23. print('Error while renaming file: ' + sys.exc_info()[0])
  24. sys.exit(6)
  25. def main():
  26. p = optparse.OptionParser()
  27. p.add_option('-n', '--name', dest='project_name')
  28. (opts, args) = p.parse_args()
  29. if len(args) < 1:
  30. usage()
  31. sys.exit(2)
  32. dstdir = args[0]
  33. try:
  34. dstdir = os.path.realpath(dstdir)
  35. except:
  36. print('Error: invalid path "%s"' % dstdir)
  37. sys.exit(3)
  38. if os.path.isdir(dstdir) and os.listdir(dstdir) != []:
  39. print('Error: directory "%s" is not empty' % dstdir)
  40. sys.exit(4)
  41. srcdir = os.path.split(__file__)[0]
  42. try:
  43. distutils.dir_util.copy_tree(os.path.join(srcdir, 'skeleton'), dstdir)
  44. except:
  45. print('Error while copying the project files: ', sys.exc_info()[0])
  46. sys.exit(5)
  47. if opts.project_name != None:
  48. rename_main_file(dstdir, opts.project_name)
  49. print('Ax project has been created in "%s" directory' % dstdir)
  50. print('Execute\n\t$ go build\nin this directory to build the project')
  51. print('Then type\n\t$ ./%s\nto start' %
  52. os.path.split(os.path.normpath(dstdir))[1])
  53. sys.exit(0)
  54. if __name__ != '__main__':
  55. usage()
  56. sys.exit(1)
  57. main()