21ab4e
From a0ffa6727b00a4000ed1340ff106bcf0e6bfc866 Mon Sep 17 00:00:00 2001
21ab4e
From: Milind Changire <mchangir@redhat.com>
21ab4e
Date: Mon, 12 Jun 2017 14:03:07 +0530
21ab4e
Subject: [PATCH 496/509] tools/glusterfind: add --field-separator option
21ab4e
21ab4e
Problem:
21ab4e
Default field separator is a space character.
21ab4e
This gets in the way if the file name itself has embedded spaces.
21ab4e
21ab4e
Solution:
21ab4e
Add --field-separator option to "pre" and "query" commands.
21ab4e
The field separator string will be used to separate strings in the
21ab4e
output lines that get written to the output file.
21ab4e
21ab4e
eg.
21ab4e
old output:
21ab4e
NEW file1.txt
21ab4e
RENAME file2 Copy.txt file3.txt
21ab4e
21ab4e
with --field-separator as "==="
21ab4e
new output:
21ab4e
NEW===file1.txt
21ab4e
RENAME===file2 Copy.txt===file3.txt
21ab4e
21ab4e
mainline:
21ab4e
> BUG: 1453151
21ab4e
> Reviewed-on: https://review.gluster.org/17481
21ab4e
> Smoke: Gluster Build System <jenkins@build.gluster.org>
21ab4e
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
21ab4e
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
21ab4e
> Reviewed-by: Aravinda VK <avishwan@redhat.com>
21ab4e
(cherry picked from commit 64588d4f6d69ce6e9b82335d12fa6c42794c38fa)
21ab4e
21ab4e
Change-Id: I71e878fed58ba1113d97044ac9f6404ee66227c7
21ab4e
BUG: 1450722
21ab4e
Signed-off-by: Milind Changire <mchangir@redhat.com>
21ab4e
Reviewed-on: https://code.engineering.redhat.com/gerrit/108767
21ab4e
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
21ab4e
---
21ab4e
 tools/glusterfind/src/brickfind.py |  5 ++++-
21ab4e
 tools/glusterfind/src/main.py      | 35 ++++++++++++++++++++++++++++-------
21ab4e
 tools/glusterfind/src/utils.py     |  5 +++--
21ab4e
 3 files changed, 35 insertions(+), 10 deletions(-)
21ab4e
21ab4e
diff --git a/tools/glusterfind/src/brickfind.py b/tools/glusterfind/src/brickfind.py
21ab4e
index efc840b..439c881 100644
21ab4e
--- a/tools/glusterfind/src/brickfind.py
21ab4e
+++ b/tools/glusterfind/src/brickfind.py
21ab4e
@@ -42,7 +42,8 @@ def brickfind_crawl(brick, args):
21ab4e
             path = path.strip()
21ab4e
             path = path[brick_path_len+1:]
21ab4e
             output_write(fout, path, args.output_prefix,
21ab4e
-                         encode=(not args.no_encode), tag=args.tag)
21ab4e
+                         encode=(not args.no_encode), tag=args.tag,
21ab4e
+                         field_separator=args.field_separator)
21ab4e
 
21ab4e
         ignore_dirs = [os.path.join(brick, dirname)
21ab4e
                        for dirname in
21ab4e
@@ -73,6 +74,8 @@ def _get_args():
21ab4e
                         action="store_true")
21ab4e
     parser.add_argument("--output-prefix", help="File prefix in output",
21ab4e
                         default=".")
21ab4e
+    parser.add_argument("--field-separator", help="Field separator",
21ab4e
+                        default=" ")
21ab4e
 
21ab4e
     return parser.parse_args()
21ab4e
 
21ab4e
diff --git a/tools/glusterfind/src/main.py b/tools/glusterfind/src/main.py
21ab4e
index d146354..8e31a07 100644
21ab4e
--- a/tools/glusterfind/src/main.py
21ab4e
+++ b/tools/glusterfind/src/main.py
21ab4e
@@ -132,6 +132,10 @@ def run_cmd_nodes(task, args, **kwargs):
21ab4e
             mkdirp(os.path.dirname(node_outfile),
21ab4e
                    exit_on_err=True, logger=logger)
21ab4e
 
21ab4e
+            FS = args.field_separator
21ab4e
+            if not is_host_local(host_uuid):
21ab4e
+                FS = "'" + FS + "'"
21ab4e
+
21ab4e
             cmd = [change_detector,
21ab4e
                    args.session,
21ab4e
                    args.volume,
21ab4e
@@ -143,7 +147,8 @@ def run_cmd_nodes(task, args, **kwargs):
21ab4e
                 (["--debug"] if args.debug else []) + \
21ab4e
                 (["--no-encode"] if args.no_encode else []) + \
21ab4e
                 (["--only-namespace-changes"] if args.only_namespace_changes
21ab4e
-                 else [])
21ab4e
+                 else []) + \
21ab4e
+                (["--field-separator", FS] if args.full else [])
21ab4e
 
21ab4e
             opts["node_outfile"] = node_outfile
21ab4e
             opts["copy_outfile"] = True
21ab4e
@@ -162,6 +167,10 @@ def run_cmd_nodes(task, args, **kwargs):
21ab4e
             mkdirp(os.path.dirname(node_outfile),
21ab4e
                    exit_on_err=True, logger=logger)
21ab4e
 
21ab4e
+            FS = args.field_separator
21ab4e
+            if not is_host_local(host_uuid):
21ab4e
+                FS = "'" + FS + "'"
21ab4e
+
21ab4e
             cmd = [change_detector,
21ab4e
                    args.session,
21ab4e
                    args.volume,
21ab4e
@@ -174,7 +183,8 @@ def run_cmd_nodes(task, args, **kwargs):
21ab4e
                 (["--debug"] if args.debug else []) + \
21ab4e
                 (["--no-encode"] if args.no_encode else []) + \
21ab4e
                 (["--only-namespace-changes"]
21ab4e
-                    if args.only_namespace_changes else [])
21ab4e
+                    if args.only_namespace_changes else []) + \
21ab4e
+                (["--field-separator", FS] if args.full else [])
21ab4e
 
21ab4e
             opts["node_outfile"] = node_outfile
21ab4e
             opts["copy_outfile"] = True
21ab4e
@@ -332,6 +342,8 @@ def _get_args():
21ab4e
                             help="Tag prefix for file names emitted during"
21ab4e
                             " a full find operation; default: \"NEW\"",
21ab4e
                             default="NEW")
21ab4e
+    parser_pre.add_argument("--field-separator", help="Field separator string",
21ab4e
+                            default=" ")
21ab4e
 
21ab4e
     # query <VOLUME> <OUTFILE> --since-time <SINCE_TIME>
21ab4e
     #       [--output-prefix <OUTPUT_PREFIX>] [--full]
21ab4e
@@ -357,6 +369,9 @@ def _get_args():
21ab4e
                               help="Tag prefix for file names emitted during"
21ab4e
                               " a full find operation; default: \"NEW\"",
21ab4e
                               default="NEW")
21ab4e
+    parser_query.add_argument("--field-separator",
21ab4e
+                              help="Field separator string",
21ab4e
+                              default=" ")
21ab4e
 
21ab4e
     # post <SESSION> <VOLUME>
21ab4e
     parser_post = subparsers.add_parser('post')
21ab4e
@@ -442,7 +457,7 @@ def enable_volume_options(args):
21ab4e
                 % args.volume)
21ab4e
 
21ab4e
 
21ab4e
-def write_output(outfile, outfilemerger):
21ab4e
+def write_output(outfile, outfilemerger, field_separator):
21ab4e
     with codecs.open(outfile, "a", encoding="utf-8") as f:
21ab4e
         for row in outfilemerger.get():
21ab4e
             # Multiple paths in case of Hardlinks
21ab4e
@@ -459,9 +474,15 @@ def write_output(outfile, outfilemerger):
21ab4e
                     continue
21ab4e
 
21ab4e
                 if row_2_rep and row_2_rep != "":
21ab4e
-                    f.write(u"{0} {1} {2}\n".format(row[0], p_rep, row_2_rep))
21ab4e
+                    f.write(u"{0}{1}{2}{3}{4}\n".format(row[0],
21ab4e
+                                                        field_separator,
21ab4e
+                                                        p_rep,
21ab4e
+                                                        field_separator,
21ab4e
+                                                        row_2_rep))
21ab4e
                 else:
21ab4e
-                    f.write(u"{0} {1}\n".format(row[0], p_rep))
21ab4e
+                    f.write(u"{0}{1}{2}\n".format(row[0],
21ab4e
+                                                  field_separator,
21ab4e
+                                                  p_rep))
21ab4e
 
21ab4e
 
21ab4e
 def mode_create(session_dir, args):
21ab4e
@@ -571,7 +592,7 @@ def mode_query(session_dir, args):
21ab4e
         # Read each Changelogs db and generate finaldb
21ab4e
         create_file(args.outfile, exit_on_err=True, logger=logger)
21ab4e
         outfilemerger = OutputMerger(args.outfile + ".db", node_outfiles)
21ab4e
-        write_output(args.outfile, outfilemerger)
21ab4e
+        write_output(args.outfile, outfilemerger, args.field_separator)
21ab4e
 
21ab4e
     try:
21ab4e
         os.remove(args.outfile + ".db")
21ab4e
@@ -630,7 +651,7 @@ def mode_pre(session_dir, args):
21ab4e
         # Read each Changelogs db and generate finaldb
21ab4e
         create_file(args.outfile, exit_on_err=True, logger=logger)
21ab4e
         outfilemerger = OutputMerger(args.outfile + ".db", node_outfiles)
21ab4e
-        write_output(args.outfile, outfilemerger)
21ab4e
+        write_output(args.outfile, outfilemerger, args.field_separator)
21ab4e
 
21ab4e
     try:
21ab4e
         os.remove(args.outfile + ".db")
21ab4e
diff --git a/tools/glusterfind/src/utils.py b/tools/glusterfind/src/utils.py
21ab4e
index 70737be..b08233e 100644
21ab4e
--- a/tools/glusterfind/src/utils.py
21ab4e
+++ b/tools/glusterfind/src/utils.py
21ab4e
@@ -75,7 +75,8 @@ def find(path, callback_func=lambda x: True, filter_func=lambda x: True,
21ab4e
                 callback_func(full_path, filter_result)
21ab4e
 
21ab4e
 
21ab4e
-def output_write(f, path, prefix=".", encode=False, tag=""):
21ab4e
+def output_write(f, path, prefix=".", encode=False, tag="",
21ab4e
+                 field_separator=" "):
21ab4e
     if path == "":
21ab4e
         return
21ab4e
 
21ab4e
@@ -86,7 +87,7 @@ def output_write(f, path, prefix=".", encode=False, tag=""):
21ab4e
         path = urllib.quote_plus(path)
21ab4e
 
21ab4e
     # set the field separator
21ab4e
-    FS = "" if tag == "" else " "
21ab4e
+    FS = "" if tag == "" else field_separator
21ab4e
 
21ab4e
     f.write("%s%s%s\n" % (tag.strip(), FS, path))
21ab4e
 
21ab4e
-- 
21ab4e
1.8.3.1
21ab4e