#!/usr/bin/env python3 import git import os import shutil import stat import tempfile repo = git.Repo(os.path.dirname(git.__file__), search_parent_directories=True) gitpython_hash = repo.head.object.hexsha # http://stackoverflow.com/a/21263493/228539 def del_rw(action, name, exc): os.chmod(name, stat.S_IWRITE) if os.path.isdir(name): os.rmdir(name) else: os.remove(name) url = 'https://github.com/gitpython-developers/GitPython' try: with tempfile.TemporaryDirectory() as checkout_dir: dir = os.path.join(checkout_dir, 'no_tree') repo = git.Repo.clone_from(url, dir) # http://bugs.python.org/issue26660 shutil.rmtree(dir, onerror=del_rw) except PermissionError as e: raise PermissionError('Failed without tree (GitPython {})'.format(gitpython_hash)) from e try: with tempfile.TemporaryDirectory() as checkout_dir: dir = os.path.join(checkout_dir, 'tree') repo = git.Repo.clone_from(url, dir) tree = repo.tree('master') # http://bugs.python.org/issue26660 shutil.rmtree(dir, onerror=del_rw) except PermissionError as e: raise PermissionError('Failed with tree (GitPython {})'.format(gitpython_hash)) from e print('Succeeded both with and without tree (GitPython {})'.format(gitpython_hash))