src/cmdlib.py: Closer in line to cmdlib.sh
Add load_json
Add fatal as is in cmdlib.sh
Update info as is in cmdlib.sh
Add docstrings
# Python version of cmdlib.sh
+"""
+Houses helper code for python based coreos-assembler commands.
+"""
import hashlib
import json
@@ -6,15 +9,34 @@ import os
import subprocess
import sys
import tempfile
+
from datetime import datetime
def run_verbose(args, **kwargs):
+ """
+ Prints out the command being executed before executing a subprocess call.
+
+ :param args: All non-keyword arguments
+ :type args: list
+ :param kwargs: All keyword arguments
+ :type kwargs: dict
+ :raises: CalledProcessError
+ """
print("+ {}".format(subprocess.list2cmdline(args)))
subprocess.check_call(args, **kwargs)
def write_json(path, data):
+ """
+ Shortcut for writing a structure as json to the file system.
+
+ :param path: The full path to the file to write
+ :type: path: str
+ :param data: structure to write out as json
+ :type data: dict or list
+ :raises: ValueError, OSError
+ """
dn = os.path.dirname(path)
f = tempfile.NamedTemporaryFile(mode='w', dir=dn, delete=False)
json.dump(data, f, indent=4)
@@ -22,20 +44,65 @@ def write_json(path, data):
os.rename(f.name, path)
-def sha256sum_file(filename):
+def load_json(path):
+ """
+ Shortcut for loading json from a file path.
+
+ :param path: The full path to the file
+ :type: path: str
+ :returns: loaded json
+ :rtype: dict
+ :raises: IOError, ValueError
+ """
+ with open(path) as f:
+ return json.load(f)
+
+def sha256sum_file(path):
+ """
+ Calculates the sha256 sum from a path.
+
+ :param path: The full path to the file
+ :type: path: str
+ :returns: The calculated sha256 sum
+ :type: str
+ """
h = hashlib.sha256()
- with open(filename, 'rb', buffering=0) as f:
+ with open(path, 'rb', buffering=0) as f:
for b in iter(lambda: f.read(128 * 1024), b''):
h.update(b)
return h.hexdigest()
def fatal(msg):
- print('error: {}'.format(msg), file=sys.stderr)
+ """
+ Prints fatal error messages and exits execution.
+
+ :param msg: The message to show to output
+ :type msg: str
+ :raises: SystemExit
+ """
+ print('fatal: {}'.format(msg), file=sys.stderr)
raise SystemExit(1)
+def info(msg):
+ """
+ Prints info messages.
+
+ :param msg: The message to show to output
+ :type msg: str
+ """
+ print('info: {}'.format(msg), file=sys.stderr)
+
def rfc3339_time(t=None):
+ """
+ Produces a rfc3339 compliant time string.
+
+ :param t: The full path to the file
+ :type: t: datetime.datetime
+ :returns: a rfc3339 compliant time string
+ :rtype: str
+ """
if t is None:
t = datetime.utcnow()
else:
@@ -46,6 +113,12 @@ def rfc3339_time(t=None):
def rm_allow_noent(path):
+ """
+ Removes a file but doesn't error if the file does not exist.
+
+ :param path: The full path to the file
+ :type: path: str
+ """
try:
os.unlink(path)
except FileNotFoundError:
