-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Kwaizer/logging
Implement logging for albs-sign-file
- Loading branch information
Showing
6 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import logging | ||
import logging.handlers | ||
|
||
class SysLog: | ||
|
||
def __init__(self, tag_name: str, level: int = logging.INFO): | ||
self._tag_name = tag_name | ||
self._logger = logging.getLogger() | ||
self._level = level | ||
self._logger.setLevel(self._level) | ||
formatter = logging.Formatter(self._tag_name + ': %(message)s') | ||
handler = logging.handlers.SysLogHandler(address='/dev/log') | ||
handler.setFormatter(formatter) | ||
self.logger.addHandler(handler) | ||
|
||
def sign_log( | ||
self, | ||
file_name: str, | ||
hash_before: str, | ||
hash_after: str, | ||
pgp_keyid: str, | ||
): | ||
self._logger.info( | ||
'Filename: %s. Hash before: %s. ' | ||
'Hash after: %s. Sign key ID: %s', | ||
file_name, | ||
hash_before, | ||
hash_after, | ||
pgp_keyid, | ||
) | ||
|
||
@property | ||
def logger(self) -> logging.Logger: | ||
return self._logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import hashlib | ||
|
||
|
||
def hash_file(file_path, hasher=None, buff_size=1048576): | ||
""" | ||
Returns checksum (hexadecimal digest) of the file. | ||
Parameters | ||
---------- | ||
file_path : str or file-like | ||
File to hash. It could be either a path or a file descriptor. | ||
hasher : hashlib.Hasher | ||
Any hash algorithm from hashlib. | ||
buff_size : int | ||
Number of bytes to read at once. | ||
Returns | ||
------- | ||
str | ||
Checksum (hexadecimal digest) of the file. | ||
""" | ||
if hasher is None: | ||
hasher = get_hasher() | ||
|
||
def feed_hasher(_fd): | ||
buff = _fd.read(buff_size) | ||
while len(buff): | ||
if not isinstance(buff, bytes): | ||
buff = buff.encode('utf') | ||
hasher.update(buff) | ||
buff = _fd.read(buff_size) | ||
|
||
if isinstance(file_path, str): | ||
with open(file_path, "rb") as fd: | ||
feed_hasher(fd) | ||
else: | ||
file_path.seek(0) | ||
feed_hasher(file_path) | ||
return hasher.hexdigest() | ||
|
||
|
||
def get_hasher(): | ||
""" | ||
Returns a corresponding hashlib hashing function for the specified checksum | ||
type. | ||
Returns | ||
------- | ||
_hashlib.HASH | ||
Hashlib hashing function. | ||
""" | ||
return hashlib.new('sha256') |