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.
 
 
 
 

75 lines
2.7 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('-n', '--nometa', action='store_true',
  16. help="Don't copy or check metadata")
  17. parser.add_argument("path", action="store", nargs="+",
  18. help='Wildcard paths to copy')
  19. args = parser.parse_args(argv)
  20. # Verify arguments
  21. if args.dest_url is None:
  22. parser.error("must provide both source and destination URL")
  23. client_src = nilmdb.client.Client(args.url)
  24. client_dest = nilmdb.client.Client(args.dest_url)
  25. if client_src.geturl() == client_dest.geturl():
  26. parser.error("source and destination URL must be different")
  27. print("Source URL:", client_src.geturl())
  28. print(" Dest URL:", client_dest.geturl())
  29. # Find matching streams
  30. matched = []
  31. for path in args.path:
  32. matched.extend([s for s in client_src.stream_list(extended = True)
  33. if fnmatch.fnmatch(s[0], path)
  34. and s not in matched])
  35. # Create destination streams if they don't exist
  36. for stream in matched:
  37. src = nilmtools.filter.StreamInfo(client_src.geturl(), stream)
  38. dest = nilmtools.filter.get_stream_info(client_dest, src.path)
  39. if not dest:
  40. print("Creating destination stream", src.path)
  41. client_dest.stream_create(src.path, src.layout)
  42. # Copy them all by running the "copy" tool as if it were
  43. # invoked from the command line.
  44. for stream in matched:
  45. new_argv = ["--url", client_src.geturl(),
  46. "--dest-url", client_dest.geturl() ]
  47. if args.start:
  48. new_argv.extend(["--start", "@" + repr(args.start)])
  49. if args.end:
  50. new_argv.extend(["--end", "@" + repr(args.end)])
  51. if args.dry_run:
  52. new_argv.extend(["--dry-run"])
  53. if args.nometa:
  54. new_argv.extend(["--nometa"])
  55. if args.force_metadata:
  56. new_argv.extend(["--force-metadata"])
  57. new_argv.extend([stream[0], stream[0]])
  58. try:
  59. nilmtools.copy_one.main(new_argv)
  60. except SystemExit as e:
  61. # Ignore SystemExit which could be raised on --dry-run
  62. if e.code != 0: # pragma: no cover (shouldn't happen)
  63. raise
  64. if __name__ == "__main__":
  65. main()