Posted By

alejandrombernardis on 10/14/11


Tagged

python


Versions (?)

Py | Handler


 / Published in: Python
 

URL: http://www.alejandrob.com.ar/

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (c) 2011 The Octopus Apps Inc.
  5. # Licensed under the Apache License, Version 2.0 (the "License")
  6. #
  7. # Author: Alejandro M. Bernardis
  8. # Email: alejandro.m.bernardis at gmail.com
  9. # Created: Apr 5, 2011 12:27:14 AM
  10. #
  11.  
  12. import os
  13. import re
  14. import sys
  15. from servertools import get_version
  16. from servertools.common import DB, REPO, USER, services_list
  17.  
  18. # Variables
  19. # ------------------------------------------------------------------------------
  20. path_root = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
  21. path_lib = os.path.join(path_root, 'lib')
  22. path_package = os.path.join(path_root, 'servertools')
  23. path_extras = [path_root, path_lib]
  24.  
  25. # Methods
  26. # ------------------------------------------------------------------------------
  27. usage_string = """Usage:
  28. option: %prog [-h|-s|-v] [--help|--services|--version]
  29. or: %prog <service> ... args
  30. or: %prog <service> [-h|--help]"""
  31.  
  32. version_string = """The Octopus Apps - - Server Tools
  33. version %s"""
  34.  
  35. error_string = """Error:
  36. The action "%s" does not available
  37. """
  38.  
  39. def usage():
  40. print usage_string.replace('%prog', os.path.basename(__file__))
  41. sys.exit(1)
  42.  
  43. def usage_version():
  44. print version_string % get_version()
  45. sys.exit(1)
  46.  
  47. def usage_service():
  48. print """Services:
  49. Database:
  50. - dbcreate create a new database
  51. - dbremove permanently remove a database
  52. - dbadd associated with a user repository
  53. - dbdel a database detaches a repository
  54. - dblist displays a list of all databases
  55.  
  56. Repository:
  57. - repocreate create a new repository
  58. - reporemove permanently remove a repository
  59. - repoadd enabled to access the repository
  60. - repodel disable access to the repository
  61. - repolist displays a list of all repositories
  62.  
  63. User:
  64. - usercreate create a new user
  65. - userremove permanently remove a user
  66. - useradd associated with a user repository
  67. - userdel a user detaches a repository
  68. - userlist displays a list of all users
  69. """
  70.  
  71. # Methods
  72. # ------------------------------------------------------------------------------
  73. def fix_sys_paths(paths=[]):
  74. """Fixed system paths"""
  75. sys.path = paths + sys.path
  76.  
  77. def run(fname, gargs):
  78. """Execute program."""
  79. fix_sys_paths(path_extras)
  80. execfile(fname, gargs)
  81.  
  82. def validate(args, pdir=path_package):
  83. """Validate the action to run."""
  84. largs = len(args)
  85.  
  86. if largs == 2:
  87. argument = args[1].lower()
  88.  
  89. if argument in ['-v', '--version']:
  90. usage_version()
  91.  
  92. elif argument in ['-s', '--services']:
  93. usage_service()
  94.  
  95. elif largs >= 2:
  96. action = args[1].lower()
  97. pattern = r'^(%s|%s|%s)' % (DB, REPO, USER)
  98.  
  99. if action in services_list:
  100. fname = os.path.join(pdir, os.path.basename(
  101. '%s.py' % re.search(pattern, action).group()))
  102.  
  103. if os.path.exists(fname):
  104. run(fname, globals())
  105. return
  106.  
  107. if action not in ['-h', '--help']:
  108. print error_string % action
  109.  
  110. usage()
  111.  
  112. # Main
  113. # ------------------------------------------------------------------------------
  114. if __name__ == '__main__':
  115. validate(sys.argv)

Report this snippet  

You need to login to post a comment.