app_reg.py 7.0 KB

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