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.

100 lines
2.8KB

  1. """Module containing index utilities"""
  2. from functools import wraps
  3. import os
  4. import struct
  5. import tempfile
  6. from git.compat import is_win
  7. import os.path as osp
  8. __all__ = ('TemporaryFileSwap', 'post_clear_cache', 'default_index', 'git_working_dir')
  9. #{ Aliases
  10. pack = struct.pack
  11. unpack = struct.unpack
  12. #} END aliases
  13. class TemporaryFileSwap(object):
  14. """Utility class moving a file to a temporary location within the same directory
  15. and moving it back on to where on object deletion."""
  16. __slots__ = ("file_path", "tmp_file_path")
  17. def __init__(self, file_path):
  18. self.file_path = file_path
  19. self.tmp_file_path = self.file_path + tempfile.mktemp('', '', '')
  20. # it may be that the source does not exist
  21. try:
  22. os.rename(self.file_path, self.tmp_file_path)
  23. except OSError:
  24. pass
  25. def __del__(self):
  26. if osp.isfile(self.tmp_file_path):
  27. if is_win and osp.exists(self.file_path):
  28. os.remove(self.file_path)
  29. os.rename(self.tmp_file_path, self.file_path)
  30. # END temp file exists
  31. #{ Decorators
  32. def post_clear_cache(func):
  33. """Decorator for functions that alter the index using the git command. This would
  34. invalidate our possibly existing entries dictionary which is why it must be
  35. deleted to allow it to be lazily reread later.
  36. :note:
  37. This decorator will not be required once all functions are implemented
  38. natively which in fact is possible, but probably not feasible performance wise.
  39. """
  40. @wraps(func)
  41. def post_clear_cache_if_not_raised(self, *args, **kwargs):
  42. rval = func(self, *args, **kwargs)
  43. self._delete_entries_cache()
  44. return rval
  45. # END wrapper method
  46. return post_clear_cache_if_not_raised
  47. def default_index(func):
  48. """Decorator assuring the wrapped method may only run if we are the default
  49. repository index. This is as we rely on git commands that operate
  50. on that index only. """
  51. @wraps(func)
  52. def check_default_index(self, *args, **kwargs):
  53. if self._file_path != self._index_path():
  54. raise AssertionError(
  55. "Cannot call %r on indices that do not represent the default git index" % func.__name__)
  56. return func(self, *args, **kwargs)
  57. # END wrapper method
  58. return check_default_index
  59. def git_working_dir(func):
  60. """Decorator which changes the current working dir to the one of the git
  61. repository in order to assure relative paths are handled correctly"""
  62. @wraps(func)
  63. def set_git_working_dir(self, *args, **kwargs):
  64. cur_wd = os.getcwd()
  65. os.chdir(self.repo.working_tree_dir)
  66. try:
  67. return func(self, *args, **kwargs)
  68. finally:
  69. os.chdir(cur_wd)
  70. # END handle working dir
  71. # END wrapper
  72. return set_git_working_dir
  73. #} END decorators