Skip to content

Commit

Permalink
teach mkdir about --exists-okay
Browse files Browse the repository at this point in the history
adds the --exists-okay flag to the mkdir command, which allows you to
avoid errors if you are calling mkdir to ensure that a directory exists.
  • Loading branch information
larsks committed Nov 10, 2017
1 parent 450c434 commit dced95c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions ampy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ def get(remote_file, local_file):
local_file.write(contents)

@cli.command()
@click.option('--exists-okay', is_flag=True)
@click.argument('directory')
def mkdir(directory):
def mkdir(directory, exists_okay):
"""
Create a directory on the board.
Expand All @@ -119,7 +120,8 @@ def mkdir(directory):
"""
# Run the mkdir command.
board_files = files.Files(_board)
board_files.mkdir(directory)
board_files.mkdir(directory,
exists_okay=exists_okay)

@cli.command()
@click.argument('directory', default='/')
Expand Down
5 changes: 3 additions & 2 deletions ampy/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def ls(self, directory='/'):
# Parse the result list and return it.
return ast.literal_eval(out.decode('utf-8'))

def mkdir(self, directory):
def mkdir(self, directory, exists_okay=False):
"""Create the specified directory. Note this cannot create a recursive
hierarchy of directories, instead each one should be created separately.
"""
Expand All @@ -123,7 +123,8 @@ def mkdir(self, directory):
except PyboardError as ex:
# Check if this is an OSError #17, i.e. directory already exists.
if ex.args[2].decode('utf-8').find('OSError: [Errno 17] EEXIST') != -1:
raise DirectoryExistsError('Directory already exists: {0}'.format(directory))
if not exists_okay:
raise DirectoryExistsError('Directory already exists: {0}'.format(directory))
else:
raise ex
self._pyboard.exit_raw_repl()
Expand Down

0 comments on commit dced95c

Please sign in to comment.