2020"""
2121Dump creates an archive of data from a RethinkDB cluster.
2222"""
23- import click
24-
2523import datetime
2624import os
2725import platform
@@ -165,25 +163,10 @@ def parse_options(argv, prog=None):
165163return options
166164
167165
168- @click .command
169- @click .option ("--directory" ,default = "" ,help = "The target directory of the dump" )
170- @click .option ("--fields" ,default = None ,help = "The fields to be dumped" )
171- @click .option ("--delimiter" ,default = None ,help = "The delimiter of the exported data" )
172- @click .option ("--format" ,default = "json" ,help = "The format of the export data" )
173- @click .option ("--quiet" ,default = True ,help = "Verbose dump" )
174- @click .opton ("--debug" ,default = False ,help = "Whether to debug or not" )
175- @click .option ("--out_file" ,help = "The output file name" )
176- def cmd_dump (directory ,fields ,delimiter ,format ,quiet ,debug ,out_file ):
177- """
178- Dump creates an archive of data from a RethinkDB cluster.
179- """
180- click .echo ("dump command" )
181- # argv = None
182- # prog = None
183-
184- # options = parse_options(argv or sys.argv[1:], prog=prog)
166+ def main (argv = None ,prog = None ):
167+ options = parse_options (argv or sys .argv [1 :],prog = prog )
185168try :
186- if not quiet :
169+ if not options . quiet :
187170# Print a warning about the capabilities of dump, so no one is confused (hopefully)
188171print (
189172"""\
@@ -198,60 +181,42 @@ def cmd_dump(directory, fields, delimiter, format, quiet, debug, out_file):
198181
199182# -- _export options - need to be kep in-sync with _export
200183
201- # options.directory = os.path.realpath(tempfile.mkdtemp(dir=options.temp_dir))
202- # options.fields = None
203- # options.delimiter = None
204- # options.format = "json"
184+ options .directory = os .path .realpath (tempfile .mkdtemp (dir = options .temp_dir ))
185+ options .fields = None
186+ options .delimiter = None
187+ options .format = "json"
205188
206189# -- export to a directory
207190
208- # if not options.quiet:
209- # print(" Exporting to temporary directory...")
210- if not quiet :
191+ if not options .quiet :
211192print (" Exporting to temporary directory..." )
212193
213194try :
214- # cmd_export.run(options)
215- cmd_export .run ({
216- "directory" :directory ,
217- "quiet" :quiet ,
218- "fields" :fields ,
219- "delimter" :delimiter ,
220- "format" :format
221- })
195+ _export .run (options )
222196except Exception as exc :
223- # default_logger.exception(exc)
197+ default_logger .exception (exc )
224198
225- # if options.debug:
226- if debug :
227- sys .stderr .write (f"\n { traceback .format_exc ()} \n " )
199+ if options .debug :
200+ sys .stderr .write ("\n %s\n " % traceback .format_exc ())
228201
229- raise Exception (f "Error: export failed,{ exc } " )
202+ raise Exception ("Error: export failed,%s" % exc )
230203
231204# -- zip directory
232205
233- # if not options.quiet:
234- if not quiet :
206+ if not options .quiet :
235207print (" Zipping export directory..." )
236208
237209try :
238- # if hasattr(options.out_file, "read"):
239- if hasattr (out_file ,"read" )
240- # archive = tarfile.open(fileobj=options.out_file, mode="w:gz")
241- archive = tarfile .open (fileobj = out_file ,mode = "w:gz" )
210+ if hasattr (options .out_file ,"read" ):
211+ archive = tarfile .open (fileobj = options .out_file ,mode = "w:gz" )
242212else :
243- # archive = tarfile.open(name=options.out_file, mode="w:gz")
244- archive = tarfile .open (name = out_file ,mode = "w:gz" )
245- # for curr, _, files in os.walk(os.path.realpath(options.directory)):
246- for curr ,_ ,files in os .walk (os .path .realpath (directory )):
213+ archive = tarfile .open (name = options .out_file ,mode = "w:gz" )
214+ for curr ,_ ,files in os .walk (os .path .realpath (options .directory )):
247215for data_file in files :
248- # full_path = os.path.join(options.directory, curr, data_file)
249- full_path = os .path .join (directory ,curr ,data_file )
216+ full_path = os .path .join (options .directory ,curr ,data_file )
250217archive_path = os .path .join (
251- # options.dump_name,
252- dump_name
253- # os.path.relpath(full_path, options.directory),
254- os .path .relpath (full_path ,directory ),
218+ options .dump_name ,
219+ os .path .relpath (full_path ,options .directory ),
255220 )
256221archive .add (full_path ,arcname = archive_path )
257222os .unlink (full_path )
@@ -261,32 +226,30 @@ def cmd_dump(directory, fields, delimiter, format, quiet, debug, out_file):
261226
262227# --
263228
264- # if not options.quiet:
265- if not quiet :
229+ if not options .quiet :
266230print (
267231"Done (%.2f seconds): %s"
268232% (
269233time .time ()- start_time ,
270- # options.out_file.name
271- out_file .name
272- # if hasattr(options.out_file, "name")
273- if hasattr (out_file ,"name" )
274- # else options.out_file,
275- else out_file ,
234+ options .out_file .name
235+ if hasattr (options .out_file ,"name" )
236+ else options .out_file ,
276237 )
277238 )
278239except KeyboardInterrupt :
279240time .sleep (0.2 )
280241raise RuntimeError ("Interrupted" )
281242finally :
282- # if os.path.exists(options.directory):
283- # shutil.rmtree(options.directory)
284- if os .path .exists (directory ):
285- shutil .rmtree (directory )
243+ if os .path .exists (options .directory ):
244+ shutil .rmtree (options .directory )
286245
287246except Exception as ex :
288- # if options.debug:
289- if debug :
247+ if options .debug :
290248traceback .print_exc ()
291249print (ex ,file = sys .stderr )
292250return 1
251+ return 0
252+
253+
254+ if __name__ == "__main__" :
255+ sys .exit (main ())