process_rename_test.py.in 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (C) 2010 CZ NIC
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. """Tests to see if every python process renames itself."""
  16. import unittest
  17. import os
  18. import os.path
  19. import isc.util.process
  20. import re
  21. class TestRename(unittest.TestCase):
  22. """Test scanning all python scripts if they rename themself."""
  23. def __scan(self, directory, script, fun):
  24. # Scan one script if it contains call to the renaming function
  25. filename = os.path.join(directory, script)
  26. data = ''.join(open(filename).readlines())
  27. prettyname = 'src' + filename[filename.rfind('../') + 2:]
  28. self.assertTrue(fun.search(data),
  29. "Didn't find a call to isc.util.process.rename in " + prettyname)
  30. def test_calls(self):
  31. """
  32. Test if every script renames itself.
  33. Scan all Makefile and look for scripts.
  34. Then scan them by looking at the source text
  35. (without actually running them)
  36. """
  37. # Regexp to find all the *_SCRIPTS = something lines,
  38. # including line continuations (backslash and newline)
  39. lines = re.compile(r'^\w+_SCRIPTS\s*=\s*((.|\\\n)*)$',
  40. re.MULTILINE)
  41. # Script name regular expression
  42. scripts = re.compile(r'((\w|[-.0-9])+)')
  43. # Line with the call
  44. fun = re.compile(r'^\s*isc\.util\.process\.rename\s*\(.*\)\s*(|#.*)$',
  45. re.MULTILINE)
  46. # Find all Makefile and extract names of scripts
  47. for (d, _, fs) in os.walk('@top_builddir@'):
  48. if 'Makefile' in fs:
  49. makefile = ''.join(open(os.path.join(d,
  50. "Makefile")).readlines())
  51. for (var, _) in lines.findall(makefile):
  52. for (script, _) in scripts.findall(var):
  53. self.__scan(d, script, fun)
  54. if __name__ == "__main__":
  55. unittest.main()