profile.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. import os
  4. from Naked.toolshed.system import file_exists, dir_exists, stderr, exit_success
  5. class Profiler:
  6. def __init__(self, dir_levels = 6):
  7. self.number_of_dir_levels = dir_levels # number of directory levels to bottom to top search
  8. def run(self):
  9. lib_found = False
  10. for i in range(self.number_of_dir_levels):
  11. if not self._is_lib_at_this_level():
  12. os.chdir(os.pardir)
  13. else:
  14. lib_found = True
  15. break
  16. if lib_found:
  17. os.chdir('lib') # chdir to the lib directory if it is found
  18. if file_exists('profiler.py'): # confirm that profiler.py exists
  19. os.system('python profiler.py') # run the profiler.py file
  20. exit_success()
  21. else:
  22. stderr("Unable to locate a profiler.py file in your lib directory.", 1)
  23. else:
  24. stderr("Unable to locate your profiler.py file. Please navigate to your project directory.", 1)
  25. def _is_lib_at_this_level(self):
  26. if dir_exists('lib'):
  27. return True
  28. else:
  29. return False
  30. def help():
  31. from Naked.toolshed.system import exit_success
  32. help_string = """
  33. Naked profile Command Help
  34. ==========================
  35. The profile command runs cProfile and pstats on the code that you enter in test code block of your PROJECT/lib/profiler.py file.
  36. USAGE
  37. naked profile
  38. SECONDARY COMMANDS
  39. none
  40. OPTIONS
  41. none
  42. This command searches bottom to top (from the working directory) through up to 6 directory levels to identify the lib/profiler.py path."""
  43. print(help_string)
  44. exit_success()