actions.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. import base64
  3. import hashlib
  4. import logging
  5. import os
  6. from pelican.utils import strftime
  7. from .registration import content_git_object_init
  8. from .utils import datetime_from_timestamp
  9. from .utils import string_to_bool
  10. logger = logging.getLogger(__name__)
  11. @content_git_object_init.connect
  12. def filetime_from_git(content, git_content):
  13. '''
  14. Update modification and creation times from git
  15. '''
  16. if not content.settings['GIT_FILETIME_FROM_GIT']:
  17. # Disabled for everything
  18. return
  19. if not string_to_bool(content.metadata.get('gittime', 'yes')):
  20. # Disable for this content
  21. return
  22. path = content.source_path
  23. fs_creation_time = datetime_from_timestamp(os.stat(path).st_ctime, content)
  24. fs_modified_time = datetime_from_timestamp(os.stat(path).st_mtime, content)
  25. # 1. file is not managed by git
  26. # date: fs time
  27. # 2. file is staged, but has no commits
  28. # date: fs time
  29. # 3. file is managed, and clean
  30. # date: first commit time, update: last commit time or None
  31. # 4. file is managed, but dirty
  32. # date: first commit time, update: fs time
  33. if git_content.is_managed_by_git():
  34. if git_content.is_committed():
  35. content.date = git_content.get_oldest_commit_date()
  36. if git_content.is_modified():
  37. content.modified = fs_modified_time
  38. else:
  39. content.modified = git_content.get_newest_commit_date()
  40. else:
  41. # File isn't committed
  42. content.date = fs_creation_time
  43. else:
  44. # file is not managed by git
  45. content.date = fs_creation_time
  46. # Clean up content attributes
  47. if not hasattr(content, 'modified'):
  48. content.modified = content.date
  49. if hasattr(content, 'date'):
  50. content.locale_date = strftime(content.date, content.date_format)
  51. if hasattr(content, 'modified'):
  52. content.locale_modified = strftime(
  53. content.modified, content.date_format)
  54. @content_git_object_init.connect
  55. def git_sha_metadata(content, git_content):
  56. '''
  57. Add sha metadata to content
  58. '''
  59. if not content.settings['GIT_SHA_METADATA']:
  60. return
  61. if not git_content.is_committed():
  62. return
  63. content.metadata['gitsha_newest'] = str(git_content.get_newest_commit())
  64. content.metadata['gitsha_oldest'] = str(git_content.get_oldest_commit())
  65. def update_hash_from_str(hsh, str_input):
  66. """
  67. Convert a str to object supporting buffer API and update a hash with it.
  68. """
  69. byte_input = str(str_input).encode("UTF-8")
  70. hsh.update(byte_input)
  71. @content_git_object_init.connect
  72. def git_permalink(content, git_content):
  73. '''
  74. Add git based permalink id to content metadata
  75. '''
  76. if not content.settings['GIT_GENERATE_PERMALINK']:
  77. return
  78. if not string_to_bool(content.metadata.get('git_permalink', 'yes')):
  79. # Disable for this content
  80. return
  81. if not git_content.is_committed():
  82. return
  83. permalink_hash = hashlib.sha1()
  84. update_hash_from_str(permalink_hash, git_content.get_oldest_commit())
  85. update_hash_from_str(permalink_hash, git_content.get_oldest_filename())
  86. git_permalink_id_raw = base64.urlsafe_b64encode(permalink_hash.digest())
  87. git_permalink_id = git_permalink_id_raw.decode("UTF-8")
  88. permalink_id_metadata_key = content.settings['PERMALINK_ID_METADATA_KEY']
  89. if permalink_id_metadata_key in content.metadata:
  90. content.metadata[permalink_id_metadata_key] = (
  91. ','.join((
  92. content.metadata[permalink_id_metadata_key], git_permalink_id))
  93. )
  94. else:
  95. content.metadata[permalink_id_metadata_key] = git_permalink_id