- Status:
- Discarded
- Change Summary:
-
No longer using Flask to develop this, see: https://reviews.reviewboard.org/r/6825/
[WIP] Git file retrieval wrapper
Review Request #6808 — Created Jan. 18, 2015 and discarded
This is a WIP for my project. The attached files is the sample of a basic prototype running on Flask.
main.py is the Flask web server with two functions:
- retrieve_file_blob: This is meant to return a response that contains the file blob given the Git sha. Calls GitRepository.retrieve_file_blob (see below) to obtain this information.
- file_exists: This is meant to return a response that checks whether the file exists given by the Git sha. Calls GitRepository.file_exists (see below) to obtain this information.respository.py: Contains a Repository class that represents a base SCM repository. Not much there currenty, aside from two abstract methods: retrieve_file_blob and file_exists. Meant for abstraction for future SCM support.
git_repository.py: Contains a class GitRepository which Extends Repository, provides implementation for the functions listed in Repository. Also has doctests to test functionality (doctests will only work for my local system so far).
Just local testing, see doctests in git_repository.py.
I have also ran the webserver and tried to access the URLs for getting the file and checking if the file exists to test the response.
from flask import Flask
from flask import Response
from git_repository import GitRepository
app = Flask(__name__)
app.debug=True
@app.route('/file/<sha>')
def retrieve_file_blob(sha):
repo = GitRepository('/home/j/src/git-sandbox')
return Response(repo.retrieve_file_blob(sha),
mimetype='application/octet-stream')
@app.route('/exists/<sha>')
def file_exists(sha):
repo = GitRepository('/home/j/src/git-sandbox')
return Response(repo.file_exists(sha),
mimetype='application/octet-stream')
if __name__ == '__main__':
app.run()
from repository import Repository
from subprocess import Popen
import subprocess
class GitRepository(Repository):
def retrieve_file_blob(self, sha):
"""
Retrieves the Blob for the specified file sha
:param sha: The Git sha of the file.
:return: The blob given by the Git file sha.
>>> repo = GitRepository('/home/j/src/git-sandbox')
>>> repo.retrieve_file_blob('ce013625030ba8dba906f756967f9e9ca394464a')
'hello'
"""
# git show <sha> outputs the file, alternatively so can
# git cat-file -p <sha>
p = Popen(['git', 'show', sha],
stdout=subprocess.PIPE, cwd=self.location)
return p.stdout.read()[:-1]
def file_exists(self, sha):
"""
Checks whether the file exists at the specified file sha.
:param sha: The Git sha of the file.
:return: Whether the file exists.
>>> repo = GitRepository('/home/j/src/git-sandbox')