Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move all user messaging to the command-line tool.
  • Loading branch information
Ton van den Heuvel committed Dec 22, 2011
1 parent 2eb3b53 commit 05760aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
12 changes: 10 additions & 2 deletions scripts/shelve.py
Expand Up @@ -30,7 +30,12 @@
elif args.patch_name is not None:
shelf = Shelf(os.getcwd())
if args.apply_patch:
shelf.apply_patch(args.patch_name)
if shelf.apply_patch(args.patch_name):
print("Applying patch '%s' succeeded, shelved patch has been removed." % patch_name)
else:
# The patch did not apply cleanly, inform the user that the
# patch will not be removed.
print("Patch '%s' did not apply successfully, shelved patch will not be removed." % patch_name)
else:
# Check if patch already exists, if it does, issue a warning and
# give the user an option to overwrite the patch.
Expand All @@ -43,7 +48,10 @@
while not args.patch_name:
args.patch_name = input("Please provide a different patch name: ")

shelf.create_patch(args.patch_name)
if shelf.create_patch(args.patch_name):
print("Done shelving changes for patch '%s'." % patch_name)
else:
print("No changes in repository, patch '%s' not created." % patch_name)
else:
parser.print_help()
except ShelfException as e:
Expand Down
20 changes: 9 additions & 11 deletions shelf/shelf.py
Expand Up @@ -60,7 +60,8 @@ def get_patch(cls, patch_name):
def apply_patch(self, patch_name):
"""Applies the patch *patch_name* on to the current working directory in
case the patch exists. In case applying the patch was successfull, the
patch is automatically removed from the shelf.
patch is automatically removed from the shelf. Returns ``True`` in case
applying the patch was successfull, otherwise ``False`` is returned.
:raises: :py:exc:`~shelf.exception.ShelfException` in case *patch_name* does not exist.
"""
Expand All @@ -82,20 +83,18 @@ def apply_patch(self, patch_name):

if patch_return_code == 0:
# Applying the patch succeeded, remove shelved patch.
print("Applying patch '%s' succeeded, removing shelved patch." % patch_name)
os.unlink(patch_path)
else:
# The patch did not apply cleanly, inform the user that the
# patch will not be removed.
print("Patch '%s' did not apply successfully, shelved patch will not be removed." % patch_name)

return patch_return_code == 0
else:
raise ShelfException("patch '%s' does not exist" % patch_name)

def create_patch(self, patch_name):
"""Creates a patch based on the changes in the current repository. In
case the specified patch *patch_name* already exists, ask the user to
overwrite the patch. In case creating the patch was successfull, all
changes in the current repository are reverted.
changes in the current repository are reverted. Returns ``True`` in case
a patch was created, and ``False`` otherwise.
:raises: :py:exc:`~shelf.exception.ShelfException` in case *patch_name* already exists.
"""
Expand All @@ -106,7 +105,7 @@ def create_patch(self, patch_name):

# Determine the contents for the new patch.
patch = self.repository.diff()
if patch:
if patch != '':
# Create the patch.
patch_file = open(patch_path, 'wb')
patch_file.write(patch.encode('utf-8'))
Expand All @@ -125,6 +124,5 @@ def create_patch(self, patch_name):
if status == FileStatus.Added:
os.unlink(os.path.join(self.repository.root_path, file_name))

print("Done shelving changes for patch '%s'." % patch_name)
else:
print("No changes in repository, patch '%s' not created." % patch_name)
# Return whether a non-empty patch was created.
return patch != ''

0 comments on commit 05760aa

Please sign in to comment.