Handle migration of both files and dirs

This commit is contained in:
Dan Buch 2016-05-30 10:21:13 -04:00
parent eadd3918f1
commit b47e152395
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC

View File

@ -36,7 +36,7 @@ def main(sysargs=sys.argv[:]):
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description=_DESCRIPTION, description=_DESCRIPTION,
formatter_class=argparse.ArgumentDefaultsHelpFormatter) formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('basedir', nargs='?', metavar='BASEDIR', parser.add_argument('path', nargs='+',
type=os.path.abspath, default=os.getcwd()) type=os.path.abspath, default=os.getcwd())
parser.add_argument('-w', '--write', help='write changes back to file', parser.add_argument('-w', '--write', help='write changes back to file',
action='store_true', default=False) action='store_true', default=False)
@ -60,7 +60,7 @@ def main(sysargs=sys.argv[:]):
format='%(message)s' format='%(message)s'
) )
for filepath in _find_candidate_files(args.basedir): for filepath in _find_candidate_files(args.path):
updated_source = _update_filepath(filepath) updated_source = _update_filepath(filepath)
if args.write: if args.write:
logging.info('Updating %s', filepath) logging.info('Updating %s', filepath)
@ -74,21 +74,26 @@ def main(sysargs=sys.argv[:]):
return 0 return 0
def _find_candidate_files(basedir): def _find_candidate_files(paths):
for curdir, dirs, files in os.walk(basedir): for path in paths:
for i, dirname in enumerate(dirs[:]): if not os.path.isdir(path):
if dirname.startswith('.'): yield path
dirs.pop(i) continue
for filename in files: for curdir, dirs, files in os.walk(path):
if not filename.endswith('.go'): for i, dirname in enumerate(dirs[:]):
continue if dirname.startswith('.'):
dirs.pop(i)
filepath = os.path.join(curdir, filename) for filename in files:
if not os.access(filepath, os.R_OK | os.W_OK): if not filename.endswith('.go'):
continue continue
yield filepath filepath = os.path.join(curdir, filename)
if not os.access(filepath, os.R_OK | os.W_OK):
continue
yield filepath
def _update_filepath(filepath): def _update_filepath(filepath):