##// END OF EJS Templates
scm: change ._get_content() to .raw_bytes attribute to file nodes to...
dan -
r501:274fedc6 default
parent child Browse files
Show More
@@ -868,7 +868,7 b' class BaseCommit(object):'
868 for f in files:
868 for f in files:
869 f_path = os.path.join(prefix, f.path)
869 f_path = os.path.join(prefix, f.path)
870 file_info.append(
870 file_info.append(
871 (f_path, f.mode, f.is_link(), f._get_content()))
871 (f_path, f.mode, f.is_link(), f.raw_bytes))
872
872
873 if write_metadata:
873 if write_metadata:
874 metadata = [
874 metadata = [
@@ -28,6 +28,7 b' import stat'
28 from zope.cachedescriptors.property import Lazy as LazyProperty
28 from zope.cachedescriptors.property import Lazy as LazyProperty
29
29
30 from rhodecode.lib.utils import safe_unicode, safe_str
30 from rhodecode.lib.utils import safe_unicode, safe_str
31 from rhodecode.lib.utils2 import md5
31 from rhodecode.lib.vcs import path as vcspath
32 from rhodecode.lib.vcs import path as vcspath
32 from rhodecode.lib.vcs.backends.base import EmptyCommit, FILEMODE_DEFAULT
33 from rhodecode.lib.vcs.backends.base import EmptyCommit, FILEMODE_DEFAULT
33 from rhodecode.lib.vcs.conf.mtypes import get_mimetypes_db
34 from rhodecode.lib.vcs.conf.mtypes import get_mimetypes_db
@@ -318,22 +319,35 b' class FileNode(Node):'
318 mode = self._mode
319 mode = self._mode
319 return mode
320 return mode
320
321
321 def _get_content(self):
322 @LazyProperty
323 def raw_bytes(self):
324 """
325 Returns lazily the raw bytes of the FileNode.
326 """
322 if self.commit:
327 if self.commit:
323 content = self.commit.get_file_content(self.path)
328 if self._content is None:
329 self._content = self.commit.get_file_content(self.path)
330 content = self._content
324 else:
331 else:
325 content = self._content
332 content = self._content
326 return content
333 return content
327
334
328 @property
335 @LazyProperty
336 def md5(self):
337 """
338 Returns md5 of the file node.
339 """
340 return md5(self.raw_bytes)
341
342 @LazyProperty
329 def content(self):
343 def content(self):
330 """
344 """
331 Returns lazily content of the FileNode. If possible, would try to
345 Returns lazily content of the FileNode. If possible, would try to
332 decode content from UTF-8.
346 decode content from UTF-8.
333 """
347 """
334 content = self._get_content()
348 content = self.raw_bytes
335
349
336 if bool(content and '\0' in content):
350 if self.is_binary:
337 return content
351 return content
338 return safe_unicode(content)
352 return safe_unicode(content)
339
353
@@ -467,12 +481,12 b' class FileNode(Node):'
467 else:
481 else:
468 return NodeState.NOT_CHANGED
482 return NodeState.NOT_CHANGED
469
483
470 @property
484 @LazyProperty
471 def is_binary(self):
485 def is_binary(self):
472 """
486 """
473 Returns True if file has binary content.
487 Returns True if file has binary content.
474 """
488 """
475 _bin = '\0' in self._get_content()
489 _bin = self.raw_bytes and '\0' in self.raw_bytes
476 return _bin
490 return _bin
477
491
478 @LazyProperty
492 @LazyProperty
@@ -502,7 +516,7 b' class FileNode(Node):'
502 all_lines, empty_lines = 0, 0
516 all_lines, empty_lines = 0, 0
503
517
504 if not self.is_binary:
518 if not self.is_binary:
505 content = self._get_content()
519 content = self.content
506 if count_empty:
520 if count_empty:
507 all_lines = 0
521 all_lines = 0
508 empty_lines = 0
522 empty_lines = 0
@@ -717,22 +731,10 b' class LargeFileNode(FileNode):'
717 we override check since the LargeFileNode path is system absolute
731 we override check since the LargeFileNode path is system absolute
718 """
732 """
719
733
720 def _get_content(self):
734 def raw_bytes(self):
721 if self.commit:
735 if self.commit:
722 with open(self.path, 'rb') as f:
736 with open(self.path, 'rb') as f:
723 content = f.read()
737 content = f.read()
724 else:
738 else:
725 content = self._content
739 content = self._content
726 return content
740 return content No newline at end of file
727
728 @property
729 def content(self):
730 """
731 Returns lazily content of the `FileNode`. If possible, would try to
732 decode content from UTF-8.
733 """
734 content = self._get_content()
735
736 if bool(content and '\0' in content):
737 return content
738 return safe_unicode(content)
@@ -486,7 +486,7 b' class ScmModel(BaseModel):'
486 :param repo_name: name of repository
486 :param repo_name: name of repository
487 :param commit_id: commit id for which to list nodes
487 :param commit_id: commit id for which to list nodes
488 :param root_path: root path to list
488 :param root_path: root path to list
489 :param flat: return as a list, if False returns a dict with decription
489 :param flat: return as a list, if False returns a dict with description
490
490
491 """
491 """
492 _files = list()
492 _files = list()
@@ -499,31 +499,29 b' class ScmModel(BaseModel):'
499 for f in files:
499 for f in files:
500 _content = None
500 _content = None
501 _data = f.unicode_path
501 _data = f.unicode_path
502
502 if not flat:
503 if not flat:
503 _data = {
504 _data = {
504 "name": f.unicode_path,
505 "name": f.unicode_path,
505 "type": "file",
506 "type": "file",
506 }
507 }
507 if extended_info:
508 if extended_info:
508 _content = safe_str(f.content)
509 _data.update({
509 _data.update({
510 "md5": md5(_content),
510 "md5": f.md5,
511 "binary": f.is_binary,
511 "binary": f.is_binary,
512 "size": f.size,
512 "size": f.size,
513 "extension": f.extension,
513 "extension": f.extension,
514
515 "mimetype": f.mimetype,
514 "mimetype": f.mimetype,
516 "lines": f.lines()[0]
515 "lines": f.lines()[0]
517 })
516 })
517
518 if content:
518 if content:
519 full_content = None
519 full_content = None
520 if not f.is_binary:
520 if not f.is_binary:
521 # in case we loaded the _content already
521 full_content = safe_str(f.content)
522 # re-use it, or load from f[ile]
523 full_content = _content or safe_str(f.content)
524
522
525 _data.update({
523 _data.update({
526 "content": full_content
524 "content": full_content,
527 })
525 })
528 _files.append(_data)
526 _files.append(_data)
529 for d in dirs:
527 for d in dirs:
@@ -1098,4 +1096,4 b' def _check_rhodecode_hook(hook_path):'
1098 def _read_hook(hook_path):
1096 def _read_hook(hook_path):
1099 with open(hook_path, 'rb') as f:
1097 with open(hook_path, 'rb') as f:
1100 content = f.read()
1098 content = f.read()
1101 return content
1099 return content No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now