From b954e6e462fe4075c6efc5b8d2100f0c6a483da0 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 10 Jun 2018 14:16:05 +0200 Subject: [PATCH] Add timestamp meta data Signed-off-by: Knut Ahlers --- Makefile | 2 +- checksum.py | 26 +++++++++++++++++++++----- filters.txt | 3 ++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index d3e009b..de48f8d 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ default: auto-hook-pre-commit: - python3 checksum.py + git status --porcelain | grep -q '^.M filters.txt' && python3 checksum.py || true git status --porcelain | grep -q '^M filters.txt' && git add filters.txt || true diff --git a/checksum.py b/checksum.py index 9832847..c744857 100644 --- a/checksum.py +++ b/checksum.py @@ -3,18 +3,31 @@ import re import codecs import hashlib import base64 +import datetime -checksumRegexp = re.compile( - r'^\s*!\s*checksum[\s\-:]+([\w\+\/=]+).*\n', re.I | re.M) +import pytz + +checksumRegexp = re.compile(r'^! Checksum: ([\w\+\/=]+).*\n', re.M) +timeUpdRegexp = re.compile(r'^! TimeUpdated: (.*)\n', re.M) def addChecksum(data): checksum = calculateChecksum(data) - data = re.sub(checksumRegexp, '', data) - data = re.sub(r'(\r?\n)', r'\1! Checksum: %s\1' % checksum, data, 1) + data = re.sub(checksumRegexp, + r'! Checksum: {}\n'.format(checksum), + data, + 1) return data +def addDate(data): + d = datetime.datetime.utcnow().replace(microsecond=0) + d_with_timezone = d.replace(tzinfo=pytz.UTC) + now = d_with_timezone.isoformat() + + return re.sub(timeUpdRegexp, r'! TimeUpdated: {}\n'.format(now), data) + + def calculateChecksum(data): data = normalize(data) @@ -31,6 +44,9 @@ def normalize(data): if __name__ == '__main__': - data = addChecksum(open('filters.txt').read()) + data = open('filters.txt').read() + data = addDate(data) + data = addChecksum(data) + with open('filters.txt', 'w') as f: f.write(data) diff --git a/filters.txt b/filters.txt index 429be62..4d3a119 100644 --- a/filters.txt +++ b/filters.txt @@ -1,7 +1,8 @@ ! Title: Personal Clean-Web -! Checksum: E7mtj8wb20eOhhG24F1CGQ +! Checksum: O1mUaYsu7jkO/1NK70SXig ! Description: List of filter rules for uBlock Origin to enhance the browsing privacy and replace Ghostery ! Expires: 1 hour +! TimeUpdated: 2018-06-10T12:15:36+00:00 ! Homepage: https://github.com/Luzifer/browser-privacy ! License: https://unlicense.org/