You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 lines
2.5 KiB

  1. #!/usr/bin/python
  2. # Copy streams between NilmDB servers with wildcards
  3. import nilmtools.filter
  4. import nilmtools.copy_one
  5. import nilmdb.client
  6. import argparse
  7. import fnmatch
  8. def main(argv = None):
  9. f = nilmtools.filter.Filter()
  10. # Reuse filter's parser, since it handles most options we need.
  11. parser = f.setup_parser(description = """\
  12. Copy all streams matching the given wildcard from one host to another.
  13. Example: %(prog)s -u http://host1/nilmdb -U http://host2/nilmdb /sharon/*
  14. """, skip_paths = True)
  15. parser.add_argument("path", action="store", nargs="+",
  16. help='Wildcard paths to copy')
  17. args = parser.parse_args(argv)
  18. # Verify arguments
  19. if args.dest_url is None:
  20. parser.error("must provide both source and destination URL")
  21. client_src = nilmdb.client.Client(args.url)
  22. client_dest = nilmdb.client.Client(args.dest_url)
  23. if client_src.geturl() == client_dest.geturl():
  24. parser.error("source and destination URL must be different")
  25. print "Source URL:", client_src.geturl()
  26. print " Dest URL:", client_dest.geturl()
  27. # Find matching streams
  28. matched = []
  29. for path in args.path:
  30. matched.extend([s for s in client_src.stream_list(extended = True)
  31. if fnmatch.fnmatch(s[0], path)
  32. and s not in matched])
  33. # Create destination streams if they don't exist
  34. for stream in matched:
  35. src = nilmtools.filter.StreamInfo(client_src.geturl(), stream)
  36. dest = nilmtools.filter.get_stream_info(client_dest, src.path)
  37. if not dest:
  38. print "Creating destination stream", src.path
  39. client_dest.stream_create(src.path, src.layout)
  40. # Copy them all by running the "copy" tool as if it were
  41. # invoked from the command line.
  42. for stream in matched:
  43. new_argv = ["--url", client_src.geturl(),
  44. "--dest-url", client_dest.geturl() ]
  45. if args.start:
  46. new_argv.extend(["--start", "@" + repr(args.start)])
  47. if args.end:
  48. new_argv.extend(["--end", "@" + repr(args.end)])
  49. if args.dry_run:
  50. new_argv.extend(["--dry-run"])
  51. if args.force_metadata:
  52. new_argv.extend(["--force-metadata"])
  53. new_argv.extend([stream[0], stream[0]])
  54. try:
  55. nilmtools.copy_one.main(new_argv)
  56. except SystemExit as e:
  57. # Ignore SystemExit which could be raised on --dry-run
  58. if e.code != 0:
  59. raise
  60. if __name__ == "__main__":
  61. main()