app_reg.py 7.8 KB

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