Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. * Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  4. *
  5. * Mumble-Django is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This package is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. """
  15. import os, subprocess, signal
  16. from os.path import join, exists
  17. from shutil import copyfile
  18. from django.conf import settings
  19. def get_available_versions():
  20. """ Return murmur versions installed inside the LAB_DIR. """
  21. dirs = os.listdir( settings.TEST_MURMUR_LAB_DIR );
  22. dirs.sort();
  23. return dirs;
  24. def run_callback( version, callback, *args, **kwargs ):
  25. """ Initialize the database and run murmur, then call the callback.
  26. After the callback has returned, kill murmur.
  27. The callback will be passed the Popen object that wraps murmur,
  28. and any arguments that were passed to run_callback.
  29. If the callback raises an exception, murmur will still be properly
  30. shutdown and the exception will be reraised.
  31. The callback can either return an arbitrary value, or a tuple.
  32. If it returns a tuple, it must be of the form:
  33. ( <any> intended_return_value, <bool> call_update_dbase )
  34. That means: If the second value evaluates to True, update_dbase
  35. will be called; the first value will be returned by run_callback.
  36. If the callback returns anything other than a tuple, that value
  37. will be returned directly.
  38. So, If run_callback should return a tuple, you will need to return
  39. the tuple form mentioned above in the callback, and put your tuple
  40. into the first parameter.
  41. """
  42. murmur_root = join( settings.TEST_MURMUR_LAB_DIR, version );
  43. if not exists( murmur_root ):
  44. raise EnvironmentError( "This version could not be found: '%s' does not exist!" % murmur_root );
  45. init_dbase( version );
  46. process = run_murmur( version );
  47. try:
  48. result = callback( process, *args, **kwargs );
  49. except Exception, err:
  50. raise err;
  51. else:
  52. if type(result) == tuple:
  53. if result[1]:
  54. update_dbase( version );
  55. return result[0];
  56. else:
  57. return result;
  58. finally:
  59. kill_murmur( process );
  60. def init_dbase( version ):
  61. """ Initialize Murmur's database by copying the one from FILES_DIR. """
  62. dbasefile = join( settings.TEST_MURMUR_FILES_DIR, "murmur-%s.db3" % version );
  63. if not exists( dbasefile ):
  64. raise EnvironmentError( "This version could not be found: '%s' does not exist!" % dbasefile );
  65. murmurfile = join( settings.TEST_MURMUR_LAB_DIR, version, "murmur.sqlite" );
  66. copyfile( dbasefile, murmurfile );
  67. def update_dbase( version ):
  68. """ Copy Murmur's database to FILES_DIR (the inverse of init_dbase). """
  69. murmurfile = join( settings.TEST_MURMUR_LAB_DIR, version, "murmur.sqlite" );
  70. if not exists( murmurfile ):
  71. raise EnvironmentError( "Murmur's database could not be found: '%s' does not exist!" % murmurfile );
  72. dbasefile = join( settings.TEST_MURMUR_FILES_DIR, "murmur-%s.db3" % version );
  73. copyfile( dbasefile, target );
  74. def run_murmur( version ):
  75. """ Run the given Murmur version as a subprocess.
  76. Either returns a Popen object or raises an EnvironmentError.
  77. """
  78. murmur_root = join( settings.TEST_MURMUR_LAB_DIR, version );
  79. if not exists( murmur_root ):
  80. raise EnvironmentError( "This version could not be found: '%s' does not exist!" % murmur_root );
  81. binary_candidates = ( 'murmur.64', 'murmur.x86', 'murmurd' );
  82. files = os.listdir( murmur_root );
  83. for binname in binary_candidates:
  84. if exists( join( murmur_root, binname ) ):
  85. process = subprocess.Popen(
  86. ( join( murmur_root, binname ), '-fg' ),
  87. stdin=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
  88. cwd=murmur_root
  89. );
  90. return process;
  91. raise EnvironmentError( "Murmur binary not found. (Tried %s)" % unicode(binary_candidates) );
  92. def kill_murmur( process ):
  93. """ Send a sigterm to the given process. """
  94. return os.kill( process.pid, signal.SIGTERM );