1 # Copyright (c) 2012 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.
8 from docs_server_utils
import StringIdentity
9 from file_system
import FileSystem
, FileNotFoundError
, StatInfo
10 from future
import Gettable
, Future
12 def _ConvertToFilepath(path
):
13 return path
.replace('/', os
.sep
)
15 def _ConvertFromFilepath(path
):
16 return path
.replace(os
.sep
, '/')
18 def _ReadFile(filename
):
20 with
open(filename
, 'rb') as f
:
23 raise FileNotFoundError('Read failed for %s: %s' % (filename
, e
))
25 def _ListDir(dir_name
):
28 files
= os
.listdir(dir_name
)
30 raise FileNotFoundError('os.listdir failed for %s: %s' % (dir_name
, e
))
32 posix_path
= _ConvertFromFilepath(os_path
)
33 if os_path
.startswith('.'):
35 if os
.path
.isdir(os
.path
.join(dir_name
, os_path
)):
36 all_files
.append(posix_path
+ '/')
38 all_files
.append(posix_path
)
41 def _CreateStatInfo(path
):
43 path_mtime
= os
.stat(path
).st_mtime
44 if os
.path
.isdir(path
):
45 child_versions
= dict((_ConvertFromFilepath(filename
),
46 os
.stat(os
.path
.join(path
, filename
)).st_mtime
)
47 for filename
in os
.listdir(path
))
48 # This file system stat mimics subversion, where the stat of directories
49 # is max(file stats). That means we need to recursively check the whole
50 # file system tree :\ so approximate that by just checking this dir.
51 version
= max([path_mtime
] + child_versions
.values())
55 return StatInfo(version
, child_versions
)
57 raise FileNotFoundError('os.stat failed for %s: %s' % (path
, e
))
59 class LocalFileSystem(FileSystem
):
60 '''FileSystem implementation which fetches resources from the local
63 def __init__(self
, base_path
):
64 self
._base
_path
= _ConvertToFilepath(base_path
)
68 return LocalFileSystem(
69 os
.path
.join(sys
.path
[0], '..', '..', '..', '..', '..', root
))
71 def Read(self
, paths
):
75 full_path
= os
.path
.join(self
._base
_path
,
76 _ConvertToFilepath(path
).lstrip(os
.sep
))
77 if path
== '' or path
.endswith('/'):
78 result
[path
] = _ListDir(full_path
)
80 result
[path
] = _ReadFile(full_path
)
82 return Future(delegate
=Gettable(resolve
))
85 return Future(value
=())
88 full_path
= os
.path
.join(self
._base
_path
,
89 _ConvertToFilepath(path
).lstrip(os
.sep
))
90 return _CreateStatInfo(full_path
)
92 def GetIdentity(self
):
93 return '@'.join((self
.__class
__.__name
__, StringIdentity(self
._base
_path
)))
96 return 'LocalFileSystem(%s)' % self
._base
_path