Skip to content

Commit 506730a

Browse files
committed
add commentary and replace tabs with spaces
1 parent 1d285a8 commit 506730a

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

bin/jsonpatch

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ parser = argparse.ArgumentParser(
1414
parser.add_argument('ORIGINAL', type=argparse.FileType('r'),
1515
help='Original file')
1616
parser.add_argument('PATCH', type=argparse.FileType('r'),
17-
nargs='?', default=sys.stdin,
17+
nargs='?', default=sys.stdin,
1818
help='Patch file (read from stdin if omitted)')
1919
parser.add_argument('--indent', type=int, default=None,
2020
help='Indent output by n spaces')
2121
parser.add_argument('-b', '--backup', action='store_true',
22-
help='Back up ORIGINAL if modifying in-place')
22+
help='Back up ORIGINAL if modifying in-place')
2323
parser.add_argument('-i', '--in-place', action='store_true',
24-
help='Modify ORIGINAL in-place instead of to stdout')
24+
help='Modify ORIGINAL in-place instead of to stdout')
2525
parser.add_argument('-v', '--version', action='version',
2626
version='%(prog)s ' + jsonpatch.__version__)
2727

@@ -42,31 +42,61 @@ def patch_files():
4242

4343
if args.in_place:
4444
dirname = os.path.abspath(os.path.dirname(args.ORIGINAL.name))
45+
4546
try:
47+
# Attempt to replace the file atomically. We do this by
48+
# creating a temporary file in the same directory as the
49+
# original file so we can atomically move the new file over
50+
# the original later.
51+
4652
fd, pathname = tempfile.mkstemp(dir=dirname)
4753
fp = os.fdopen(fd, 'w')
4854
atomic = True
55+
4956
except OSError:
57+
# We failed to create the temporary file for an atomic
58+
# replace, so fall back to non-atomic mode by backing up
59+
# the original (if desired) and writing a new file.
60+
5061
if args.backup:
5162
os.rename(args.ORIGINAL.name, args.ORIGINAL.name + '.orig')
5263
fp = open(args.ORIGINAL.name, 'w')
5364
atomic = False
5465

5566
else:
67+
# Since we're not replacing the original file in-place, write
68+
# the modified JSON to stdout instead.
69+
5670
fp = sys.stdout
5771

72+
# By this point we have some sort of file object we can write the
73+
# modified JSON to.
74+
5875
json.dump(result, fp, indent=args.indent)
5976
fp.write('\n')
6077

6178
if args.in_place:
79+
# Close the new file. If we aren't replacing atomically, this
80+
# is our last step, since everything else is already in place.
81+
6282
fp.close()
83+
6384
if atomic:
6485
try:
86+
# Complete the atomic replace by linking the original
87+
# to a backup (if desired), fixing up the permissions
88+
# on the temporary file, and moving it into place.
89+
6590
if args.backup:
6691
os.link(args.ORIGINAL.name, args.ORIGINAL.name + '.orig')
6792
os.chmod(pathname, os.stat(args.ORIGINAL.name).st_mode)
6893
os.rename(pathname, args.ORIGINAL.name)
94+
6995
except OSError:
96+
# In the event we could not actually do the atomic
97+
# replace, unlink the original to move it out of the
98+
# way and finally move the temporary file into place.
99+
70100
os.unlink(args.ORIGINAL.name)
71101
os.rename(pathname, args.ORIGINAL.name)
72102

0 commit comments

Comments
 (0)