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