utils.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # utils.py -- Test utilities for Dulwich.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) any later version of
  8. # the License.
  9. #
  10. # This program 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. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Utility functions common to Dulwich tests."""
  20. import os
  21. import shutil
  22. import tempfile
  23. from dulwich.repo import Repo
  24. def open_repo(name):
  25. """Open a copy of a repo in a temporary directory.
  26. Use this function for accessing repos in dulwich/tests/data/repos to avoid
  27. accidentally or intentionally modifying those repos in place. Use
  28. tear_down_repo to delete any temp files created.
  29. :param name: The name of the repository, relative to
  30. dulwich/tests/data/repos
  31. :returns: An initialized Repo object that lives in a temporary directory.
  32. """
  33. temp_dir = tempfile.mkdtemp()
  34. repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos', name)
  35. temp_repo_dir = os.path.join(temp_dir, name)
  36. shutil.copytree(repo_dir, temp_repo_dir, symlinks=True)
  37. return Repo(temp_repo_dir)
  38. def tear_down_repo(repo):
  39. """Tear down a test repository."""
  40. temp_dir = os.path.dirname(repo.path.rstrip(os.sep))
  41. shutil.rmtree(temp_dir)