new --include and --exclude arguments for whitelisting and blacklisting from logging absolute file paths

This commit is contained in:
Alan Hamlett 2015-02-20 12:41:45 -08:00
parent 10331704e8
commit ef6342f16f
2 changed files with 49 additions and 14 deletions

View File

@ -29,11 +29,13 @@ format. An example config file looks like::
debug = false debug = false
api_key = your-api-key api_key = your-api-key
hidefilenames = false hidefilenames = false
ignore = exclude =
^COMMIT_EDITMSG$ ^COMMIT_EDITMSG$
^TAG_EDITMSG$ ^TAG_EDITMSG$
^/var/ ^/var/
^/etc/ ^/etc/
include =
.*
offline = true offline = true
proxy = https://user:pass@localhost:8080 proxy = https://user:pass@localhost:8080

View File

@ -168,9 +168,13 @@ def parseArguments(argv):
parser.add_argument('--hidefilenames', dest='hidefilenames', parser.add_argument('--hidefilenames', dest='hidefilenames',
action='store_true', action='store_true',
help='obfuscate file names; will not send file names to api') help='obfuscate file names; will not send file names to api')
parser.add_argument('--ignore', dest='ignore', action='append', parser.add_argument('--exclude', dest='exclude', action='append',
help='filename patterns to ignore; POSIX regex syntax; can be used'+ help='filename patterns to exclude from logging; POSIX regex '+
' more than once') 'syntax; can be used more than once')
parser.add_argument('--include', dest='include', action='append',
help='filename patterns to log; when used in combination with '+
'--exclude, files matching include will still be logged; '+
'POSIX regex syntax; can be used more than once')
parser.add_argument('--logfile', dest='logfile', parser.add_argument('--logfile', dest='logfile',
help='defaults to ~/.wakatime.log') help='defaults to ~/.wakatime.log')
parser.add_argument('--config', dest='config', parser.add_argument('--config', dest='config',
@ -202,13 +206,29 @@ def parseArguments(argv):
args.key = default_key args.key = default_key
else: else:
parser.error('Missing api key') parser.error('Missing api key')
if not args.ignore: if not args.exclude:
args.ignore = [] args.exclude = []
if configs.has_option('settings', 'ignore'): if configs.has_option('settings', 'ignore'):
try: try:
for pattern in configs.get('settings', 'ignore').split("\n"): for pattern in configs.get('settings', 'ignore').split("\n"):
if pattern.strip() != '': if pattern.strip() != '':
args.ignore.append(pattern) args.exclude.append(pattern)
except TypeError:
pass
if configs.has_option('settings', 'exclude'):
try:
for pattern in configs.get('settings', 'exclude').split("\n"):
if pattern.strip() != '':
args.exclude.append(pattern)
except TypeError:
pass
if not args.include:
args.include = []
if configs.has_option('settings', 'include'):
try:
for pattern in configs.get('settings', 'include').split("\n"):
if pattern.strip() != '':
args.include.append(pattern)
except TypeError: except TypeError:
pass pass
if args.offline and configs.has_option('settings', 'offline'): if args.offline and configs.has_option('settings', 'offline'):
@ -227,16 +247,29 @@ def parseArguments(argv):
return args, configs return args, configs
def should_ignore(fileName, patterns): def should_exclude(fileName, include, exclude):
if fileName is not None and fileName.strip() != '': if fileName is not None and fileName.strip() != '':
try: try:
for pattern in patterns: for pattern in include:
try:
compiled = re.compile(pattern, re.IGNORECASE)
if compiled.search(fileName):
return False
except re.error as ex:
log.warning(u('Regex error ({msg}) for include pattern: {pattern}').format(
msg=u(ex),
pattern=u(pattern),
))
except TypeError:
pass
try:
for pattern in exclude:
try: try:
compiled = re.compile(pattern, re.IGNORECASE) compiled = re.compile(pattern, re.IGNORECASE)
if compiled.search(fileName): if compiled.search(fileName):
return pattern return pattern
except re.error as ex: except re.error as ex:
log.warning(u('Regex error ({msg}) for ignore pattern: {pattern}').format( log.warning(u('Regex error ({msg}) for exclude pattern: {pattern}').format(
msg=u(ex), msg=u(ex),
pattern=u(pattern), pattern=u(pattern),
)) ))
@ -379,10 +412,10 @@ def main(argv=None):
setup_logging(args, __version__) setup_logging(args, __version__)
ignore = should_ignore(args.targetFile, args.ignore) exclude = should_exclude(args.targetFile, args.include, args.exclude)
if ignore is not False: if exclude is not False:
log.debug(u('File ignored because matches pattern: {pattern}').format( log.debug(u('File not logged because matches exclude pattern: {pattern}').format(
pattern=u(ignore), pattern=u(exclude),
)) ))
return 0 return 0