From 7aa4b2d40ab176ceee1800fd6fe115fca6d5c93e Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Fri, 19 Aug 2016 13:37:57 +0200 Subject: [PATCH 1/5] Support comments in hostfile, skip empty lines. --- cdist/config.py | 11 +++++++++-- docs/changelog | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cdist/config.py b/cdist/config.py index 31b41781..8d83a072 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -99,8 +99,15 @@ class Config(object): import fileinput try: for host in fileinput.input(files=(source)): - # remove leading and trailing whitespace - yield host.strip() + # remove comment if present + comment_index = host.find('#') + if comment_index >= 0: + host = host[:comment_index] + # remove leading and trailing whitespaces + host = host.strip() + # skip empty lines + if host: + yield host except (IOError, OSError) as e: raise cdist.Error("Error reading hosts from \'{}\'".format( source)) diff --git a/docs/changelog b/docs/changelog index db86de45..12510041 100644 --- a/docs/changelog +++ b/docs/changelog @@ -1,6 +1,7 @@ Changelog --------- next: + * Core: Improve hostfile: support comments, skip empty lines (Darko Poljak) * Documentation: Add Parallelization chapter (Darko Poljak) * Core: Add -b, --enable-beta option for enabling beta functionalities (Darko Poljak) * Core: Add -j, --jobs option for parallel execution and add parallel support for global explorers (currently in beta) (Darko Poljak) From 72505e0f5f75878ae5127dc18ac14e292c877a9a Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Fri, 19 Aug 2016 14:33:41 +0200 Subject: [PATCH 2/5] Add hostfile format to cdist man page. --- docs/src/man1/cdist.rst | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/src/man1/cdist.rst b/docs/src/man1/cdist.rst index 47fe195c..eeba97f0 100644 --- a/docs/src/man1/cdist.rst +++ b/docs/src/man1/cdist.rst @@ -82,7 +82,7 @@ Configure one or more hosts. Read additional hosts to operate on from specified file or from stdin if '-' (each host on separate line). If no host or host file is specified then, by default, - read hosts from stdin. + read hosts from stdin. For the file format see below. .. option:: -i MANIFEST, --initial-manifest MANIFEST @@ -117,6 +117,15 @@ Configure one or more hosts. Command to use for remote execution (should behave like ssh) + +HOSTFILE FORMAT +~~~~~~~~~~~~~~~ +HOSTFILE contains hosts per line. +All characters after and including '#' until the end of line is a comment +and is stripped away. +Empty lines and comment lines (line that starts with '#') are skipped. + + SHELL ----- This command allows you to spawn a shell that enables access From 7f1e41f76904ee1d53646396e9f8e4253ca0b9be Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Fri, 19 Aug 2016 21:56:24 +0200 Subject: [PATCH 3/5] Move hostfile line processing to new method. --- cdist/config.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/cdist/config.py b/cdist/config.py index 8d83a072..2f28a22f 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -89,6 +89,25 @@ class Config(object): self.local.create_files_dirs() self.remote.create_files_dirs() + @staticmethod + def hostfile_process_line(line): + """Return host from read line or None if no host present.""" + if not line: + return None + # remove comment if present + comment_index = line.find('#') + if comment_index >= 0: + host = line[:comment_index] + else: + host = line + # remove leading and trailing whitespaces + host = host.strip() + # skip empty lines + if host: + return host + else: + return None + @staticmethod def hosts(source): """Yield hosts from source. @@ -99,13 +118,7 @@ class Config(object): import fileinput try: for host in fileinput.input(files=(source)): - # remove comment if present - comment_index = host.find('#') - if comment_index >= 0: - host = host[:comment_index] - # remove leading and trailing whitespaces - host = host.strip() - # skip empty lines + host = Config.hostfile_process_line(host) if host: yield host except (IOError, OSError) as e: From d9d739cd44ec005b3cf02bda54c44faf727f59d4 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sat, 20 Aug 2016 07:49:52 +0200 Subject: [PATCH 4/5] Update HOSTFILE FORMAT description. --- docs/src/man1/cdist.rst | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/src/man1/cdist.rst b/docs/src/man1/cdist.rst index eeba97f0..baeb0025 100644 --- a/docs/src/man1/cdist.rst +++ b/docs/src/man1/cdist.rst @@ -120,10 +120,15 @@ Configure one or more hosts. HOSTFILE FORMAT ~~~~~~~~~~~~~~~ -HOSTFILE contains hosts per line. -All characters after and including '#' until the end of line is a comment -and is stripped away. -Empty lines and comment lines (line that starts with '#') are skipped. +HOSTFILE contains hosts per line. +All characters after and including '#' until the end of line is a comment. +In a line, all leading and trailing whitespace characters are ignored. +Empty lines are ignored/skipped. + +Hostfile line is processed like the following. First, all comments are +removed. Then all leading and trailing whitespace characters are stripped. +If such a line results in empty line it is ignored/skipped. Otherwise, +host string is used. SHELL From b5262c850ed6f918a1ed9154ff9a4ce915b30999 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sun, 21 Aug 2016 21:48:21 +0200 Subject: [PATCH 5/5] Exit cleanly in case of non UTF-8 file. --- cdist/config.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cdist/config.py b/cdist/config.py index 2f28a22f..e20f1a7c 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -121,9 +121,10 @@ class Config(object): host = Config.hostfile_process_line(host) if host: yield host - except (IOError, OSError) as e: - raise cdist.Error("Error reading hosts from \'{}\'".format( - source)) + except (IOError, OSError, UnicodeError) as e: + raise cdist.Error( + "Error reading hosts from file \'{}\': {}".format( + source, e)) else: if source: for host in source: