Browse Source

Add copy_wildcard script

tags/nilmtools-1.0
Jim Paris 9 years ago
parent
commit
f2bebea5d0
3 changed files with 72 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. +1
    -0
      setup.py
  3. +68
    -0
      src/copy_wildcard.py

+ 3
- 0
.gitignore View File

@@ -1,3 +1,6 @@
oldprep
newprep
*.dat
build/
*.pyc
dist/


+ 1
- 0
setup.py View File

@@ -75,6 +75,7 @@ setup(name='nilmtools',
'nilm-decimate-auto = nilmtools.decimate_auto:main',
'nilm-insert = nilmtools.insert:main',
'nilm-copy = nilmtools.copy_one:main',
'nilm-copy-wildcard = nilmtools.copy_wildcard:main',
'nilm-sinefit = nilmtools.sinefit:main',
],
},


+ 68
- 0
src/copy_wildcard.py View File

@@ -0,0 +1,68 @@
#!/usr/bin/python

# Copy streams between NilmDB servers with wildcards

import nilmtools.filter
import nilmtools.copy_one
import nilmdb.client
import argparse
import fnmatch

def main(argv = None):
f = nilmtools.filter.Filter()
# Reuse filter's parser, since it handles most options we need.
parser = f.setup_parser(description = """\
Copy all streams matching the given wildcard from one host to another.

Example: %(prog)s -u http://host1/nilmdb -U http://host2/nilmdb /sharon/*
""", skip_paths = True)
parser.add_argument("path", action="store", nargs="+",
help='Wildcard paths to copy')
args = parser.parse_args(argv)

# Verify arguments
if args.dest_url is None:
parser.error("must provide both source and destination URL")
client_src = nilmdb.client.Client(args.url)
client_dest = nilmdb.client.Client(args.dest_url)
if client_src.geturl() == client_dest.geturl():
parser.error("source and destination URL must be different")
print "Source URL:", client_src.geturl()
print " Dest URL:", client_dest.geturl()

# Find matching streams
matched = []
for path in args.path:
matched.extend([s for s in client_src.stream_list(extended = True)
if fnmatch.fnmatch(s[0], path)
and s not in matched])

# Create destination streams if they don't exist
for stream in matched:
src = nilmtools.filter.StreamInfo(client_src.geturl(), stream)
dest = nilmtools.filter.get_stream_info(client_dest, src.path)
if not dest:
print "Creating destination stream", src.path
client_dest.stream_create(src.path, src.layout)

# Copy them all by running the "copy" tool as if it were
# invoked from the command line.
for stream in matched:
copy_argv = ["--url", client_src.geturl(),
"--dest-url", client_dest.geturl() ]
if args.start:
copy_argv.extend(["--start", "@" + repr(args.start)])
if args.end:
copy_argv.extend(["--end", "@" + repr(args.end)])
if args.dry_run:
copy_argv.extend(["--dry-run"])
copy_argv.extend([stream[0], stream[0]])
try:
nilmtools.copy_one.main(copy_argv)
except SystemExit as e:
# Ignore SystemExit which could be raised on --dry-run
if e.code != 0:
raise

if __name__ == "__main__":
main()

Loading…
Cancel
Save