OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2013 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 from caching_file_system import CachingFileSystem | |
6 from local_file_system import LocalFileSystem | |
7 from offline_file_system import OfflineFileSystem | |
8 from subversion_file_system import SubversionFileSystem | |
9 from third_party.json_schema_compiler.memoize import memoize | |
10 | |
not at google - send to devlin
2013/10/08 15:48:39
no amount of --similarity=? will convince git cl t
Jeffrey Yasskin
2013/10/08 17:46:05
Half the time I try to increase the --similarity a
not at google - send to devlin
2013/10/08 18:27:37
hm ok I'll give it a go.
| |
11 | |
12 class HostFileSystemProvider(object): | |
Jeffrey Yasskin
2013/10/08 17:46:05
What's the difference between a "Creator" and a "P
not at google - send to devlin
2013/10/08 18:27:37
the memoization is an implementation detail, thoug
| |
13 '''Provides cached instances of host file systems for trunk or any branch. | |
14 ''' | |
15 def __init__(self, | |
16 object_store_creator, | |
17 max_trunk_revision=None, | |
18 default_trunk_instance=None, | |
19 offline=False, | |
20 constructor_for_test=None): | |
21 ''' | |
22 |object_store_creator| | |
23 Provides caches for file systems that need one. | |
24 |max_trunk_revision| | |
25 If not None, the maximum revision that a 'trunk' file system will be | |
26 created at. If None, 'trunk' file systems will use HEAD. | |
27 |default_trunk_instance| | |
28 If not None, 'trunk' file systems provided by this class without a | |
29 specific revision will return |default_trunk_instance| instead. | |
30 |offline| | |
31 If True all provided file systems will be wrapped in an OfflineFileSystem. | |
32 |constructor_for_test| | |
33 Provides a custom constructor rather than creating SubversionFileSystems. | |
34 ''' | |
35 self._object_store_creator = object_store_creator | |
36 self._max_trunk_revision = max_trunk_revision | |
37 self._default_trunk_instance = default_trunk_instance | |
38 self._offline = offline | |
39 self._constructor_for_test = constructor_for_test | |
Jeffrey Yasskin
2013/10/08 17:46:05
Don't do it in this change (to keep the diff small
not at google - send to devlin
2013/10/08 18:27:37
SGTM.
| |
40 | |
41 @memoize | |
Jeffrey Yasskin
2013/10/08 17:46:05
Memoization is dangerous in long-lived servers whe
not at google - send to devlin
2013/10/08 18:27:37
that's not the level that the memoization happens
Jeffrey Yasskin
2013/10/08 20:49:39
'k, sounds good.
| |
42 def GetTrunk(self, revision=None): | |
Jeffrey Yasskin
2013/10/08 17:46:05
Please describe the meaning of 'revision' on trunk
not at google - send to devlin
2013/10/08 18:27:37
Done.
| |
43 if revision is None: | |
44 if self._default_trunk_instance is not None: | |
45 return self._default_trunk_instance | |
46 return self._Create('trunk', revision=self._max_trunk_revision) | |
47 if self._max_trunk_revision is not None: | |
48 revision = min(revision, self._max_trunk_revision) | |
49 return self._Create('trunk', revision=revision) | |
50 | |
51 @memoize | |
Jeffrey Yasskin
2013/10/08 17:46:05
Branches also change over time, but maybe we don't
not at google - send to devlin
2013/10/08 18:27:37
correct, branches change, and it would be seriousl
Jeffrey Yasskin
2013/10/08 20:49:39
Given my misunderstanding about this memoization t
not at google - send to devlin
2013/10/08 21:29:36
Strictly speaking yes. We should be making sure th
| |
52 def GetBranch(self, branch): | |
53 '''Creates either SVN file systems or specialized file systems from the | |
Jeffrey Yasskin
2013/10/08 17:46:05
This seems like it still belongs on _Create. You s
not at google - send to devlin
2013/10/08 18:27:37
Done.
| |
54 constructor passed into this instance. Wraps the resulting file system in | |
55 an Offline file system if the offline flag is set, and finally wraps it in a | |
56 Caching file system. | |
57 ''' | |
58 assert isinstance(branch, basestring), 'Branch %s must be a string' % branch | |
59 assert branch != 'trunk', 'Cannot specify branch=\'trunk\', use GetTrunk()' | |
60 return self._Create(branch) | |
61 | |
62 def _Create(self, branch, revision=None): | |
63 if self._constructor_for_test is not None: | |
64 file_system = self._constructor_for_test(branch=branch, revision=revision) | |
65 else: | |
66 file_system = SubversionFileSystem.Create(branch=branch, | |
67 revision=revision) | |
68 if self._offline: | |
69 file_system = OfflineFileSystem(file_system) | |
70 return CachingFileSystem(file_system, self._object_store_creator) | |
71 | |
72 @staticmethod | |
Jeffrey Yasskin
2013/10/08 17:46:05
If these had been @classmethod, you wouldn't have
not at google - send to devlin
2013/10/08 18:27:37
that sounds like a nice pattern in general, thanks
| |
73 def ForLocal(object_store_creator): | |
74 '''Used in creating a server instance on localhost. | |
75 ''' | |
76 return HostFileSystemProvider( | |
77 object_store_creator, | |
78 constructor_for_test=lambda **_: LocalFileSystem.Create()) | |
Jeffrey Yasskin
2013/10/08 17:46:05
'constructor_for_test' seems like the wrong name i
not at google - send to devlin
2013/10/08 18:27:37
Local only makes sense when testing (or when runni
| |
79 | |
80 @staticmethod | |
81 def ForTest(file_system, object_store_creator): | |
82 '''Used in creating a test server instance. The HostFileSystemProvider | |
83 returned here will always return |file_system| when its Create() method is | |
84 called. | |
85 ''' | |
86 return HostFileSystemProvider( | |
87 object_store_creator, | |
88 constructor_for_test=lambda **_: file_system) | |
OLD | NEW |