app_reg.py 7.7 KB

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