actions.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. import base64
  3. import hashlib
  4. import os
  5. import logging
  6. from pelican.utils import strftime
  7. from .utils import string_to_bool
  8. from .utils import datetime_from_timestamp
  9. from .registration import content_git_object_init
  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. @content_git_object_init.connect
  66. def git_permalink(content, git_content):
  67. '''
  68. Add git based permalink id to content metadata
  69. '''
  70. if not content.settings['GIT_GENERATE_PERMALINK']:
  71. return
  72. if not string_to_bool(content.metadata.get('git_permalink', 'yes')):
  73. # Disable for this content
  74. return
  75. if not git_content.is_committed():
  76. return
  77. permalink_hash = hashlib.sha1()
  78. permalink_hash.update(str(git_content.get_oldest_commit()))
  79. permalink_hash.update(str(git_content.get_oldest_filename()))
  80. git_permalink_id = base64.urlsafe_b64encode(permalink_hash.digest())
  81. permalink_id_metadata_key = content.settings['PERMALINK_ID_METADATA_KEY']
  82. if permalink_id_metadata_key in content.metadata:
  83. content.metadata[permalink_id_metadata_key] = (
  84. ','.join((
  85. content.metadata[permalink_id_metadata_key], git_permalink_id)))
  86. else:
  87. content.metadata[permalink_id_metadata_key] = git_permalink_id