"Fossies" - the Fresh Open Source Software Archive

Member "glusterfs-8.2/tools/glusterfind/src/brickfind.py" (16 Sep 2020, 4267 Bytes) of package /linux/misc/glusterfs-8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "brickfind.py" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/python3
    2 # -*- coding: utf-8 -*-
    3 
    4 # Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com/>
    5 # This file is part of GlusterFS.
    6 #
    7 # This file is licensed to you under your choice of the GNU Lesser
    8 # General Public License, version 3 or any later version (LGPLv3 or
    9 # later), or the GNU General Public License, version 2 (GPLv2), in all
   10 # cases as published by the Free Software Foundation.
   11 
   12 import os
   13 import sys
   14 import logging
   15 from argparse import ArgumentParser, RawDescriptionHelpFormatter
   16 try:
   17     import urllib.parse as urllib
   18 except ImportError:
   19     import urllib
   20 import time
   21 
   22 from utils import mkdirp, setup_logger, create_file, output_write, find
   23 import conf
   24 
   25 
   26 PROG_DESCRIPTION = """
   27 Changelog Crawler
   28 """
   29 
   30 logger = logging.getLogger()
   31 
   32 
   33 def brickfind_crawl(brick, args):
   34     if brick.endswith("/"):
   35         brick = brick[0:len(brick)-1]
   36 
   37     working_dir = os.path.dirname(args.outfile)
   38     mkdirp(working_dir, exit_on_err=True, logger=logger)
   39     create_file(args.outfile, exit_on_err=True, logger=logger)
   40 
   41     with open(args.outfile, "a+") as fout:
   42         brick_path_len = len(brick)
   43 
   44         def output_callback(path, filter_result, is_dir):
   45             path = path.strip()
   46             path = path[brick_path_len+1:]
   47 
   48             if args.type == "both":
   49                 output_write(fout, path, args.output_prefix,
   50                              encode=(not args.no_encode), tag=args.tag,
   51                              field_separator=args.field_separator)
   52             else:
   53                 if (is_dir and args.type == "d") or (
   54                     (not is_dir) and args.type == "f"):
   55                     output_write(fout, path, args.output_prefix,
   56                     encode=(not args.no_encode), tag=args.tag,
   57                     field_separator=args.field_separator)
   58 
   59         ignore_dirs = [os.path.join(brick, dirname)
   60                        for dirname in
   61                        conf.get_opt("brick_ignore_dirs").split(",")]
   62 
   63         find(brick, callback_func=output_callback,
   64              ignore_dirs=ignore_dirs)
   65 
   66         fout.flush()
   67         os.fsync(fout.fileno())
   68 
   69 
   70 def _get_args():
   71     parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter,
   72                             description=PROG_DESCRIPTION)
   73 
   74     parser.add_argument("session", help="Session Name")
   75     parser.add_argument("volume", help="Volume Name")
   76     parser.add_argument("node", help="Node Name")
   77     parser.add_argument("brick", help="Brick Name")
   78     parser.add_argument("outfile", help="Output File")
   79     parser.add_argument("tag", help="Tag to prefix file name with")
   80     parser.add_argument("--only-query", help="Only query, No session update",
   81                         action="store_true")
   82     parser.add_argument("--debug", help="Debug", action="store_true")
   83     parser.add_argument("--no-encode",
   84                         help="Do not encode path in outfile",
   85                         action="store_true")
   86     parser.add_argument("--output-prefix", help="File prefix in output",
   87                         default=".")
   88     parser.add_argument('--type', help="type: f, f-files only"
   89                         " d, d-directories only, by default = both",
   90                         default='both')
   91     parser.add_argument("--field-separator", help="Field separator",
   92                         default=" ")
   93 
   94     return parser.parse_args()
   95 
   96 
   97 if __name__ == "__main__":
   98     args = _get_args()
   99     session_dir = os.path.join(conf.get_opt("session_dir"), args.session)
  100     status_file = os.path.join(session_dir, args.volume,
  101                      "%s.status" % urllib.quote_plus(args.brick))
  102     status_file_pre = status_file + ".pre"
  103     mkdirp(os.path.join(session_dir, args.volume), exit_on_err=True,
  104            logger=logger)
  105     mkdirp(os.path.join(conf.get_opt("log_dir"), args.session, args.volume),
  106            exit_on_err=True)
  107     log_file = os.path.join(conf.get_opt("log_dir"),
  108                             args.session,
  109                             args.volume,
  110                             "brickfind.log")
  111     setup_logger(logger, log_file, args.debug)
  112 
  113     time_to_update = int(time.time())
  114     brickfind_crawl(args.brick, args)
  115     if not args.only_query:
  116         with open(status_file_pre, "w") as f:
  117             f.write(str(time_to_update))
  118     sys.exit(0)