PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/silverlining/mgr-scripts/run-command.py

https://bitbucket.org/ianb/silverlining/
Python | 88 lines | 73 code | 9 blank | 6 comment | 17 complexity | 0b37dc1b977b6c89cbf3a53f37248c1e MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/env python
  2. import sys
  3. sys.path.insert(0, '/usr/local/share/silverlining/lib')
  4. import os
  5. from silversupport import appdata
  6. from silversupport.appconfig import AppConfig
  7. def main():
  8. args = sys.argv[1:]
  9. hostname, path = appdata.normalize_location(args[0])
  10. tmp_dir = args[1]
  11. command = args[2]
  12. rest = args[3:]
  13. instance_name = appdata.instance_for_location(hostname, path)
  14. if not instance_name:
  15. print 'No instance found attached to %s%s' % (hostname, path)
  16. return 1
  17. app_config = AppConfig.from_instance_name(instance_name)
  18. if tmp_dir and tmp_dir != 'NONE':
  19. rest = [
  20. r.replace('$TMP', tmp_dir)
  21. for r in rest]
  22. path = find_command_path(app_config.app_dir, command)
  23. check_command_python(path)
  24. os.environ['SILVER_VERSION'] = 'silverlining/0.0'
  25. app_config.activate_services()
  26. app_config.activate_path()
  27. # Buffering can happen because this isn't obviously hooked up to a
  28. # terminal (even though it is indirectly):
  29. sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
  30. sys.argv = [path] + rest
  31. os.chdir(app_config.app_dir)
  32. ns = {'__file__': path, '__name__': '__main__'}
  33. if os.path.basename(path) == 'ipython':
  34. ## ipython-specific hack
  35. if not os.access(os.environ['HOME'], os.W_OK):
  36. os.environ['HOME'] = '/tmp'
  37. os.chdir(app_config.app_dir)
  38. if os.path.basename(path) in ('python', 'python2.6'):
  39. from code import InteractiveConsole
  40. console = InteractiveConsole()
  41. console.interact()
  42. else:
  43. execfile(path, ns)
  44. def find_command_path(app_dir, command_name):
  45. if command_name.startswith(os.path.sep):
  46. # Absolute path
  47. return command_name
  48. places = [os.path.join(app_dir, 'bin'),
  49. app_dir,
  50. '/bin',
  51. '/usr/bin']
  52. for place in places:
  53. place = os.path.join(place, command_name)
  54. if os.path.exists(place):
  55. return place
  56. print >> sys.stderr, (
  57. "%s not found (looked in %s)"
  58. % (command_name, ', '.join(places)))
  59. sys.exit(1000)
  60. def check_command_python(path):
  61. if path.endswith('.py'):
  62. # Good enough
  63. return
  64. if path.endswith('python') or path.endswith('python2.6'):
  65. return
  66. fp = open(path)
  67. first = fp.readline()
  68. fp.close()
  69. if not first.startswith('#!'):
  70. print >> sys.stderr, (
  71. "Not a #! script: %s" % path)
  72. sys.exit(1001)
  73. if not 'python' in first:
  74. print >> sys.stderr, (
  75. "#! line in script is not for Python (%s): %s"
  76. % (first.strip(), path))
  77. sys.exit(1001)
  78. if __name__ == '__main__':
  79. sys.exit(main())