from abc import ABCMeta, abstractmethod
class Repository(object):
__metaclass__ = ABCMeta
def __init__(self, location, repo_name=None):
self.location = location
self.repo_name = repo_name
@abstractmethoddef retrieve_file_blob(self, sha):
pass @abstractmethoddef file_exists(self, sha):
passrepository.py
| git_repository.py | |
|---|---|
| 1 | from repository import Repository |
| 2 | from subprocess import Popen |
| 3 | import subprocess |
| 4 | |
| 5 | |
| 6 | class GitRepository(Repository): |
| 7 | |
| 8 | def retrieve_file_blob(self, sha): |
| 9 | """ |
| 10 | Retrieves the Blob for the specified file sha |
| 11 | |
| 12 | :param sha: The Git sha of the file. |
| 13 | :return: The blob given by the Git file sha. |
| 14 | |
| 15 | >>> repo = GitRepository('/home/j/src/git-sandbox') |
| 16 | >>> repo.retrieve_file_blob('ce013625030ba8dba906f756967f9e9ca394464a') |
| 17 | 'hello' |
| 18 | """ |
| 19 | # git show <sha> outputs the file, alternatively so can |
| 20 | # git cat-file -p <sha> |
| 21 | p = Popen(['git', 'show', sha], |
| 22 | stdout=subprocess.PIPE, cwd=self.location) |
| 23 | |
| 24 | return p.stdout.read()[:-1] |
| 25 | |
| 26 | def file_exists(self, sha): |
| 27 | """ |
| 28 | Checks whether the file exists at the specified file sha. |
| 29 | |
| 30 | :param sha: The Git sha of the file. |
| 31 | :return: Whether the file exists. |
| 32 | |
| 33 | >>> repo = GitRepository('/home/j/src/git-sandbox') |
| 34 | >>> repo.file_exists('ce013625030ba8dba906f756967f9e9ca394464a') |
| 35 | True |
| 36 | |
| 37 | >>> repo = GitRepository('/home/j/src/git-sandbox') |
| 38 | >>> repo.file_exists('ce013625030ba8dba906f756967f9e9ca3944642') |
| 39 | False |
| 40 | """ |
| 41 | p = Popen(['git', 'show', sha], cwd=self.location) |
| 42 | |
| 43 | p.communicate() |
| 44 | |
| 45 | return p.returncode == 0 |
| 46 | |
| 47 | if __name__ == "__main__": |
| 48 | import doctest |
| 49 | doctest.testmod() |