Skip to content

Commit

Permalink
Merge pull request #16 from vslavik/diff
Browse files Browse the repository at this point in the history
Add --diff-only option to only see changes to the generated files.
  • Loading branch information
vslavik committed Dec 20, 2013
2 parents b1ef132 + 05f6e5b commit 24da810
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/bkl/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
# Set to true to prevent any output from being written
dry_run = False

# Set to true to show diff with the existing file instead of updating it
diff_only = False

# Set to true to force writing of output files, even if they exist and would be
# unchanged. In other words, always touch output files. This is useful for
# makefiles that support automatic regeneration.
Expand Down Expand Up @@ -124,6 +127,15 @@ def commit(self):
status = "."
logger.info("%s\t%s", status, rel_fn)
return
if diff_only:
import sys
from difflib import unified_diff
for line in unified_diff(old.splitlines(True) if old is not None else [],
self.text.splitlines(True),
os.path.normpath(os.path.join("old", self.filename)),
os.path.normpath(os.path.join("new", self.filename))):
sys.stdout.write(line)
return
else:
old = None

Expand Down
8 changes: 8 additions & 0 deletions src/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def get_version(self):
"", "--dry-run",
action="store_true", dest="dry_run", default=False,
help="don't write any files, just pretend to do it")
parser.add_option(
"", "--diff-only",
action="store_true", dest="diff_only", default=False,
help="only output diffs instead of modiyfing the files, implies --dry-run")
parser.add_option(
"", "--force",
action="store_true", dest="force", default=False,
Expand Down Expand Up @@ -129,6 +133,9 @@ def get_version(self):
log_level = logging.WARNING
logger.setLevel(log_level)

if options.diff_only and options.force:
sys.stderr.write("--diff-only and --force option can't be used together\n")
sys.exit(3)

# note: we intentionally import bakefile this late so that the logging
# module is already initialized
Expand All @@ -140,6 +147,7 @@ def get_version(self):
try:
start_time = time()
bkl.io.dry_run = options.dry_run
bkl.io.diff_only = options.diff_only
bkl.io.force_output = options.force
if options.dump:
intr = bkl.dumper.DumpingInterpreter()
Expand Down

0 comments on commit 24da810

Please sign in to comment.