OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 |
| 6 from itertools import izip |
| 7 import local_git_util |
| 8 import posixpath |
| 9 |
| 10 from docs_server_utils import StringIdentity |
| 11 from environment import IsTest |
| 12 from file_system import FileNotFoundError, FileSystem, StatInfo |
| 13 from future import Future |
| 14 from local_file_system import LocalFileSystem |
| 15 from path_util import AssertIsValid, IsDirectory, ToDirectory |
| 16 from third_party.json_schema_compiler.memoize import memoize |
| 17 |
| 18 |
| 19 class LocalGitFileSystem(FileSystem): |
| 20 '''Class to fetch filesystem data from this script's local git repository. |
| 21 ''' |
| 22 @classmethod |
| 23 def Create(cls, branch='master', commit=None): |
| 24 if IsTest(): |
| 25 return LocalFileSystem.Create('') |
| 26 return LocalGitFileSystem(branch, commit) |
| 27 |
| 28 def __init__(self, branch, pinned_commit): |
| 29 self._branch = branch |
| 30 self._pinned_commit = pinned_commit |
| 31 if self._pinned_commit: |
| 32 commit = self._pinned_commit |
| 33 else: |
| 34 if branch != 'master': |
| 35 commit = 'branch-heads/%s' % branch |
| 36 else: |
| 37 commit = 'origin/master' |
| 38 try: |
| 39 self._commit = local_git_util.ParseRevision(commit) |
| 40 except ImportError: |
| 41 # We ignore ImportErrors here. It means we're running in AppEngine, so |
| 42 # this doesn't need to work anyway. |
| 43 pass |
| 44 |
| 45 def Read(self, paths, skip_not_found=False): |
| 46 |
| 47 def get_entry_name(entry): |
| 48 if entry['type'] == 'tree': |
| 49 return entry['name'] + '/' |
| 50 return entry['name'] |
| 51 |
| 52 def read_path(path): |
| 53 try: |
| 54 if IsDirectory(path): |
| 55 return [get_entry_name(e) |
| 56 for e in local_git_util.ListDir(path, self._commit)] |
| 57 else: |
| 58 return local_git_util.ReadFile(path, self._commit) |
| 59 except FileNotFoundError as e: |
| 60 if skip_not_found: |
| 61 return None |
| 62 raise e |
| 63 |
| 64 results = dict((path, read_path(path)) for path in paths) |
| 65 return Future(value=dict((k, v) for k, v in results.iteritems() |
| 66 if v is not None)) |
| 67 |
| 68 def Refresh(self): |
| 69 return Future(value=()) |
| 70 |
| 71 def GetCommitID(self): |
| 72 '''Returns a future that resolves to the commit ID for this file system's |
| 73 revision. |
| 74 ''' |
| 75 return Future(value=self._commit) |
| 76 |
| 77 def GetPreviousCommitID(self): |
| 78 '''Returns a future that resolves to the parent commit ID of this file |
| 79 system's revision. |
| 80 ''' |
| 81 return Future(value=local_git_util.GetParentRevision(self._commit)) |
| 82 |
| 83 def StatAsync(self, path): |
| 84 |
| 85 def get_child_versions(path): |
| 86 return dict((e['name'], e['id']) |
| 87 for e in local_git_util.ListDir(path, self._commit)) |
| 88 |
| 89 def get_file_version(dir, filename): |
| 90 try: |
| 91 return next(e['id'] for e in local_git_util.ListDir(dir, self._commit) |
| 92 if e['name'] == filename) |
| 93 except StopIteration: |
| 94 raise FileNotFoundError('%s not found in revision %s' % |
| 95 (path, self._commit)) |
| 96 |
| 97 dir, filename = posixpath.split(path) |
| 98 if path == '': |
| 99 version = local_git_util.GetRootTree(self._commit) |
| 100 child_versions = get_child_versions('') |
| 101 elif IsDirectory(path): |
| 102 parent_dir, stat_dir = posixpath.split(dir) |
| 103 version = get_file_version(parent_dir, stat_dir) |
| 104 child_versions = get_child_versions(dir) |
| 105 else: |
| 106 version = get_file_version(dir, filename) |
| 107 child_versions = None |
| 108 |
| 109 #print 'Accessing local git for stat on %s (%s)' % (path, version) |
| 110 return Future(value=StatInfo(version, child_versions)) |
| 111 |
| 112 def GetIdentity(self): |
| 113 if self._branch == 'master': |
| 114 # A master FS always carries the same identity even if pinned to a commit. |
| 115 str_id = 'master' |
| 116 elif self._pinned_commit is not None: |
| 117 str_id = self._pinned_commit |
| 118 else: |
| 119 str_id = 'branch-heads/%s' % self._branch |
| 120 return '@'.join((self.__class__.__name__, str_id)) |
| 121 |
| 122 def GetVersion(self): |
| 123 return self._pinned_commit |
OLD | NEW |