app_reg.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. """
  18. A tool to manage Hue applications. This does not stop/restart a
  19. running Hue instance.
  20. Usage:
  21. %(PROG_NAME)s [flags] --install <path_to_app> [<path_to_app> ...] [--relative-paths]
  22. To register and install new application(s).
  23. Add '--relative-paths' to the end of the args list to force the app manager to register the new application using its path relative to the hue root.
  24. %(PROG_NAME)s [flags] --remove <application_name>
  25. To unregister and remove an installed application.
  26. %(PROG_NAME)s [flags] --list
  27. To list all registered applications.
  28. %(PROG_NAME)s [flags] --sync
  29. Synchronize all registered applications with the Hue environment.
  30. Useful after a `make clean'.
  31. Optional flags:
  32. --debug Turns on debugging output
  33. """
  34. import getopt
  35. import logging
  36. import os
  37. import sys
  38. import subprocess
  39. import build
  40. import common
  41. import pth
  42. import registry
  43. PROG_NAME = sys.argv[0]
  44. LOG = logging.getLogger()
  45. LOG_LEVEL = logging.INFO
  46. LOG_FORMAT = "%(message)s"
  47. DO_INSTALL = 'do_install'
  48. DO_REMOVE = 'do_remove'
  49. DO_LIST = 'do_list'
  50. DO_SYNC = 'do_sync'
  51. def usage(msg=None):
  52. """Print the usage with an optional message. And exit."""
  53. global __doc__
  54. if msg is not None:
  55. print >>sys.stderr, msg
  56. print >>sys.stderr, __doc__ % dict(PROG_NAME=PROG_NAME)
  57. sys.exit(1)
  58. def get_app_info(app_loc):
  59. """
  60. get_app_info(app_loc) -> (app_name, version, description)
  61. Runs the app's setup.py to get the info. May raise ValueError and OSError.
  62. """
  63. if not os.path.isdir(app_loc):
  64. msg = "Not a directory: %s" % (app_loc,)
  65. LOG.error(msg)
  66. raise ValueError(msg)
  67. save_cwd = os.getcwd()
  68. os.chdir(app_loc)
  69. try:
  70. cmdv = [ common.ENV_PYTHON, 'setup.py',
  71. '--name', '--version', '--description',
  72. '--author' ]
  73. LOG.debug("Running '%s'" % (' '.join(cmdv),))
  74. popen = subprocess.Popen(cmdv, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  75. res = popen.wait()
  76. stdout, stderr = popen.communicate()
  77. # Cmd failure?
  78. if res != 0:
  79. LOG.error("Error getting application info from %s:\n%s" % (app_loc, stderr))
  80. raise OSError(stderr)
  81. LOG.debug("Command output:\n<<<\n%s\n>>>" % (stdout,))
  82. return stdout.split('\n')[:4]
  83. finally:
  84. os.chdir(save_cwd)
  85. def _do_install_one(reg, app_loc, relative_path):
  86. """Install one app, without saving. Returns True/False."""
  87. LOG.info("=== Installing app at %s" % (app_loc,))
  88. try:
  89. # Relative to cwd.
  90. app_loc = os.path.realpath(app_loc)
  91. app_name, version, desc, author = get_app_info(app_loc)
  92. except (ValueError, OSError), ex:
  93. LOG.error(ex)
  94. return False
  95. app = registry.HueApp(app_name, version, app_loc, desc, author)
  96. if relative_path:
  97. app.use_rel_path()
  98. else:
  99. app.use_abs_path()
  100. if reg.contains(app):
  101. LOG.warn("=== %s is already installed" % (app,))
  102. return True
  103. return reg.register(app) and build.make_app(app) and app.install_conf()
  104. def do_install(app_loc_list, relative_paths=False):
  105. """Install the apps. Returns True/False."""
  106. reg = registry.AppRegistry()
  107. for app_loc in app_loc_list:
  108. if not _do_install_one(reg, app_loc, relative_paths):
  109. return False
  110. reg.save()
  111. return do_sync(reg)
  112. def do_list():
  113. """List all apps. Returns True/False."""
  114. reg = registry.AppRegistry()
  115. apps = reg.get_all_apps()
  116. LOG.info("%-18s %-7s %-15s %s" % ('Name', 'Version', 'Author', 'Path'))
  117. LOG.info("%s %s %s %s" % ('-' * 18, '-' * 7, '-' * 15, '-' * 35))
  118. for app in sorted(apps):
  119. LOG.info("%-18s %-7s %-15s %s" % (app.name, app.version, app.author, app.path))
  120. return True
  121. def do_remove(app_name):
  122. """Uninstall the given app. Returns True/False."""
  123. # TODO(bc) Does not detect dependency. The app to be uninstalled could be a
  124. # pre-req for other apps, as defined in various setup.py files.
  125. LOG.info("=== Uninstalling %s" % (app_name,))
  126. reg = registry.AppRegistry()
  127. try:
  128. app = reg.unregister(app_name)
  129. except KeyError:
  130. LOG.error("%s is not installed" % (app_name,))
  131. return False
  132. app.uninstall_conf()
  133. reg.save()
  134. # Update the pth file
  135. try:
  136. pthfile = pth.PthFile()
  137. pthfile.remove(app)
  138. pthfile.save()
  139. return True
  140. except (OSError, SystemError), ex:
  141. LOG.error("Failed to update the .pth file. Please fix any problem and run "
  142. "`%s --sync'\n%s" % (PROG_NAME, ex))
  143. return False
  144. def do_sync(reg=None):
  145. """Sync apps with virtualenv. Returns True/False."""
  146. if not reg:
  147. reg = registry.AppRegistry()
  148. apps = reg.get_all_apps()
  149. try:
  150. pthfile = pth.PthFile()
  151. pthfile.sync(apps)
  152. pthfile.save()
  153. build.make_syncdb()
  154. return True
  155. except (OSError, SystemError), ex:
  156. LOG.error("Failed to update the .pth file. Please fix any problem and run "
  157. "`%s --sync'\n%s" % (PROG_NAME, ex))
  158. return False
  159. def main():
  160. action = None
  161. app = None
  162. # Option parsing
  163. try:
  164. opts, tail = getopt.getopt(sys.argv[1:],
  165. 'ir:lds',
  166. ('install', 'remove=', 'list', 'debug', 'sync'))
  167. except getopt.GetoptError, ex:
  168. usage(str(ex))
  169. def verify_action(current, new_val):
  170. if current is not None:
  171. usage()
  172. return new_val
  173. for opt, arg in opts:
  174. if opt in ('-i', '--install'):
  175. action = verify_action(action, DO_INSTALL)
  176. elif opt in ('-r', '--remove'):
  177. action = verify_action(action, DO_REMOVE)
  178. app = arg
  179. elif opt in ('-l', '--list'):
  180. action = verify_action(action, DO_LIST)
  181. elif opt in ('-s', '--sync'):
  182. action = verify_action(action, DO_SYNC)
  183. elif opt in ('-d', '--debug'):
  184. global LOG_LEVEL
  185. LOG_LEVEL = logging.DEBUG
  186. if action == DO_INSTALL:
  187. # ['..', '--relative-paths', 'a', 'b'] => True
  188. # ['..', 'a', 'b'] -> False
  189. relative_paths = reduce(lambda accum, x: accum or x, map(lambda x: x in ['--relative-paths'], tail))
  190. app_loc_list = filter(lambda x: x not in ['--relative-paths'], tail)
  191. elif len(tail) != 0:
  192. usage("Unknown trailing arguments: %s" % ' '.join(tail))
  193. if action is None:
  194. usage()
  195. # Setup logging
  196. logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT)
  197. # Dispatch
  198. if action == DO_INSTALL:
  199. ok = do_install(app_loc_list, relative_paths)
  200. elif action == DO_REMOVE:
  201. ok = do_remove(app)
  202. elif action == DO_LIST:
  203. ok = do_list()
  204. elif action == DO_SYNC:
  205. ok = do_sync()
  206. if ok:
  207. return 0
  208. return 2
  209. if __name__ == '__main__':
  210. sys.exit(main())