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.
 
 
 
 

76 lines
2.7 KiB

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