55import os
66import re
77import sys
8+ import time
89import argparse
910import logging
1011import subprocess
1112from logging .handlers import RotatingFileHandler
1213
13- try :
14- import simplejson as json
15- except ImportError :
16- import json
17-
1814# PY3?
1915is_py3 = sys .version_info >= (3 , 3 )
2016
3430RET_FAILED = 1
3531RET_INVALID_ARGS = 2
3632
33+ USE_SHELL = sys .platform .startswith ("win" )
34+
35+
3736def error_exit (msg , status = RET_FAILED ):
3837 """ error message display and exit."""
3938 LOG .error ('%s\n ' % msg )
4039 sys .exit (status )
4140
42- USE_SHELL = sys . platform . startswith ( "win" )
41+
4342def exec_cmd_with_stderr (command ,
4443 retry_times = 1 ,
4544 retry_interval_sec = 0 ,
@@ -69,10 +68,12 @@ def exec_cmd_with_stderr(command,
6968 except subprocess .CalledProcessError , er :
7069 ret = er .returncode
7170 retry_times -= 1
72- if retry_interval_sec > 0 : time .sleep (retry_interval_sec )
71+ if retry_interval_sec > 0 :
72+ time .sleep (retry_interval_sec )
7373
7474 return ret , output , errout
7575
76+
7677def setup_logging (logfile = DEFAULT_LOG , max_bytes = None , backup_count = None ):
7778 """Sets up logging and associated handlers."""
7879
@@ -119,7 +120,8 @@ def deal_with_file(filename, exts=''):
119120 # Skip files that end with certain extensions or characters
120121 if any (str (filename ).endswith (ext ) for ext in EXCLUDE_EXT ):
121122 LOG .info ("skip the file of %s" % filename )
122- elif str (filename ).startswith ('.' ) and not str (filename ).startswith ('./' ):
123+ elif str (filename ).startswith ('.' ) and \
124+ not str (filename ).startswith ('./' ):
123125 LOG .info ("skip the file of %s" % filename )
124126 else :
125127 cmd = cmd + " " + filename
@@ -146,7 +148,8 @@ def deal_with_dir(dirpath, exts=''):
146148 if not str (name ).strip ().startswith ("." ):
147149 f = os .path .join (dirpath , name )
148150
149- if os .path .isdir (f ) and os .access (f , os .W_OK ) and not os .path .islink (f ):
151+ if os .path .isdir (f ) and os .access (f , os .W_OK ) and \
152+ not os .path .islink (f ):
150153 # directory, recurse into it
151154 deal_with_dir (f , exts )
152155
@@ -159,10 +162,10 @@ def deal_with_dir(dirpath, exts=''):
159162 LOG .info ("skip the file of %s" % name )
160163
161164
162- INDENT = ' ' * 2
163165def format_help (help_info , choices = None ):
164166 """ format help infomation string. """
165167
168+ INDENT = ' ' * 2
166169 if isinstance (help_info , list ):
167170 help_str_list = help_info [:]
168171 else :
@@ -188,12 +191,11 @@ def parse_argument():
188191 Please see the readme for complete examples.
189192 """
190193
191- parser = argparse .ArgumentParser (description = 'Cmd line tool for rm spaces in the end of line.' ,\
194+ parser = argparse .ArgumentParser (description = 'Cmd line tool for rm spaces in the end of line.' ,
192195 epilog = epilog_example , formatter_class = argparse .RawDescriptionHelpFormatter )
193196
194197 parser .add_argument ('-v' , '--version' , action = 'version' ,
195- version = '%(prog)s 1.0'
196- )
198+ version = '%(prog)s 1.0' )
197199
198200 parser .add_argument ('--max-bytes' , action = 'store' , dest = 'max_bytes' ,
199201 type = int , default = 64 * 1024 * 1024 ,
@@ -222,14 +224,14 @@ def parse_argument():
222224 options = parser .parse_args ()
223225
224226 if (options .file is None and options .dir is None ) \
225- or (options .file and options .dir ):
227+ or (options .file and options .dir ):
226228 print ("Error: parameter -d or -f just for one.\n " )
227229 parser .print_usage ()
228230 sys .exit (RET_FAILED )
229231
230232 return parser , options
231233
232- ################# main route ######################
234+ # # ################ main route ######################
233235if __name__ == '__main__' :
234236
235237 parser , options = parse_argument ()
@@ -262,4 +264,3 @@ def parse_argument():
262264 except Exception as e :
263265 print (sys .exc_info ())
264266 sys .exit (RET_FAILED )
265-
0 commit comments