Commit 3a2c2a64 authored by ale's avatar ale

implement --include, and make --exclude actually do something

parent d58c66dc
......@@ -69,6 +69,11 @@ def submit(results, submit_url):
def main():
logging.basicConfig(level=logging.INFO)
parser = optparse.OptionParser()
parser.add_option(
'--include', dest='includes', metavar='PATTERN',
action='append', default=[],
help='Only include files matching a pattern (the paths will be relative '
'to the backup root)')
parser.add_option(
'--exclude', dest='excludes', metavar='PATTERN',
action='append', default=[],
......@@ -82,7 +87,7 @@ def main():
if not args:
parser.error('Must specify a PATH')
files = incremental_scan.scan_dirs(args)
files = incremental_scan.scan_dirs(args, opts.includes, opts.excludes)
scan_results = clamav.clamscan(files)
if scan_results:
log.info('found new viruses:\n%s', json.dumps(scan_results, indent=4))
......
......@@ -19,18 +19,19 @@ def match_any(path, patterns):
return False
def scan_mirror_metadata(root_dir, mm_path, excludes, changed):
def scan_mirror_metadata(root_dir, mm_path, includes, excludes, changed):
"""Extract filenames from a mirror_metadata diff file."""
with gzip.GzipFile(mm_path, 'r') as fd:
for line in fd:
if line.startswith('File '):
path = line.rstrip('\n')[5:]
if match_any(path, excludes):
if ((excludes and match_any(path, excludes)) or
(includes and not match_any(path, includes))):
continue
changed.add(os.path.join(root_dir, path))
def scan_dir(root_dir, excludes, changed):
def scan_dir(root_dir, includes, excludes, changed):
"""Analyze rdiff-backup metadata to look for changed files.
The scan is incremental: the timestamp of the most recent backup
......@@ -51,23 +52,28 @@ def scan_dir(root_dir, excludes, changed):
if stamp < last_scanned_at:
continue
scan_mirror_metadata(
root_dir, os.path.join(data_dir, filename), excludes, changed)
root_dir, os.path.join(data_dir, filename), includes, excludes, changed)
if stamp:
with open(stamp_file, 'w') as fd:
fd.write('%s\n' % stamp)
def scan_dirs(dirs, excludes=[]):
def scan_dirs(dirs, includes=[], excludes=[]):
"""Analyze multiple rdiff-backup dirs."""
changed = set()
for root_dir in dirs:
scan_dir(root_dir, excludes, changed)
scan_dir(root_dir, includes, excludes, changed)
return changed
def main():
parser = optparse.OptionParser(usage='%prog <PATH>...')
parser.add_option(
'--include', dest='includes', metavar='PATTERN',
action='append', default=[],
help='Only include files matching a pattern (the paths will be relative '
'to the backup root)')
parser.add_option(
'--exclude', dest='excludes', metavar='PATTERN',
action='append', default=[],
......@@ -77,7 +83,7 @@ def main():
if not args:
parser.error('Must specify a PATH')
for path in scan_dirs(args, opts.excludes):
for path in scan_dirs(args, opts.includes, opts.excludes):
print path
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment