scm: change ._get_content() to .raw_bytes attribute to file nodes to

allow getting raw bytes instead of text content of a file and added
.md5 and .is_binary attributes to file nodes
This commit is contained in:
Daniel Dourvaris 2016-07-26 20:47:23 +03:00
parent 4482553490
commit 1a842e15d2
3 changed files with 32 additions and 32 deletions

View file

@ -868,7 +868,7 @@ class BaseCommit(object):
for f in files:
f_path = os.path.join(prefix, f.path)
file_info.append(
(f_path, f.mode, f.is_link(), f._get_content()))
(f_path, f.mode, f.is_link(), f.raw_bytes))
if write_metadata:
metadata = [

View file

@ -28,6 +28,7 @@ import stat
from zope.cachedescriptors.property import Lazy as LazyProperty
from rhodecode.lib.utils import safe_unicode, safe_str
from rhodecode.lib.utils2 import md5
from rhodecode.lib.vcs import path as vcspath
from rhodecode.lib.vcs.backends.base import EmptyCommit, FILEMODE_DEFAULT
from rhodecode.lib.vcs.conf.mtypes import get_mimetypes_db
@ -318,22 +319,35 @@ class FileNode(Node):
mode = self._mode
return mode
def _get_content(self):
@LazyProperty
def raw_bytes(self):
"""
Returns lazily the raw bytes of the FileNode.
"""
if self.commit:
content = self.commit.get_file_content(self.path)
if self._content is None:
self._content = self.commit.get_file_content(self.path)
content = self._content
else:
content = self._content
return content
@property
@LazyProperty
def md5(self):
"""
Returns md5 of the file node.
"""
return md5(self.raw_bytes)
@LazyProperty
def content(self):
"""
Returns lazily content of the FileNode. If possible, would try to
decode content from UTF-8.
"""
content = self._get_content()
content = self.raw_bytes
if bool(content and '\0' in content):
if self.is_binary:
return content
return safe_unicode(content)
@ -467,12 +481,12 @@ class FileNode(Node):
else:
return NodeState.NOT_CHANGED
@property
@LazyProperty
def is_binary(self):
"""
Returns True if file has binary content.
"""
_bin = '\0' in self._get_content()
_bin = self.raw_bytes and '\0' in self.raw_bytes
return _bin
@LazyProperty
@ -502,7 +516,7 @@ class FileNode(Node):
all_lines, empty_lines = 0, 0
if not self.is_binary:
content = self._get_content()
content = self.content
if count_empty:
all_lines = 0
empty_lines = 0
@ -717,22 +731,10 @@ class LargeFileNode(FileNode):
we override check since the LargeFileNode path is system absolute
"""
def _get_content(self):
def raw_bytes(self):
if self.commit:
with open(self.path, 'rb') as f:
content = f.read()
else:
content = self._content
return content
@property
def content(self):
"""
Returns lazily content of the `FileNode`. If possible, would try to
decode content from UTF-8.
"""
content = self._get_content()
if bool(content and '\0' in content):
return content
return safe_unicode(content)
return content

View file

@ -486,7 +486,7 @@ class ScmModel(BaseModel):
:param repo_name: name of repository
:param commit_id: commit id for which to list nodes
:param root_path: root path to list
:param flat: return as a list, if False returns a dict with decription
:param flat: return as a list, if False returns a dict with description
"""
_files = list()
@ -499,31 +499,29 @@ class ScmModel(BaseModel):
for f in files:
_content = None
_data = f.unicode_path
if not flat:
_data = {
"name": f.unicode_path,
"type": "file",
}
if extended_info:
_content = safe_str(f.content)
_data.update({
"md5": md5(_content),
"md5": f.md5,
"binary": f.is_binary,
"size": f.size,
"extension": f.extension,
"mimetype": f.mimetype,
"lines": f.lines()[0]
})
if content:
full_content = None
if not f.is_binary:
# in case we loaded the _content already
# re-use it, or load from f[ile]
full_content = _content or safe_str(f.content)
full_content = safe_str(f.content)
_data.update({
"content": full_content
"content": full_content,
})
_files.append(_data)
for d in dirs:
@ -1098,4 +1096,4 @@ def _check_rhodecode_hook(hook_path):
def _read_hook(hook_path):
with open(hook_path, 'rb') as f:
content = f.read()
return content
return content