Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All files actions (remove and get) #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ ENV/

# Rope project settings
.ropeproject

.vscode/
2 changes: 2 additions & 0 deletions ampy/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import cli
cli.main()
82 changes: 70 additions & 12 deletions ampy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,17 @@ def cli(port, baud, delay):


@cli.command()
@click.argument("remote_file")
@click.option(
'--all',
is_flag=True,
required=False,
default=False,
help = '''Provides posibility to get all files from micropython device.
Also receives one argument which is destination path and is optional'''
)
@click.argument("remote_file", default = "all")
@click.argument("local_file", type=click.File("wb"), required=False)
def get(remote_file, local_file):
def get(all, remote_file, local_file):
"""
Retrieve a file from the board.

Expand All @@ -120,15 +128,47 @@ def get(remote_file, local_file):
Or to get main.py and save it as main.py locally run:

ampy --port /board/serial/port get main.py main.py

Also to get all files to the current folder:

ampy --port /board/serial/port get --all

Or to the mentioned folder:

ampy --port /board/serial/port get --all ./destination/
"""
# Get the file contents.
board_files = files.Files(_board)
contents = board_files.get(remote_file)
# Print the file out if no local file was provided, otherwise save it.
if local_file is None:
print(contents.decode("utf-8"))
filelist = board_files.ls(long_format=False)

# When --all flag is set - the next argument is remote_file
# but will be used as a path to save files
if all:
if remote_file.endswith('/'):
destination_folder = remote_file
else:
destination_folder = remote_file + '/'
if not os.path.exists(destination_folder):
print('''Specified path doesn\'t exist. Files will be
downloaded to current location''')
destination_folder = ''
else:
local_file.write(contents)
destination_folder = ''

if all or remote_file == 'all': # download all files
for file in filelist:
contents = board_files.get(file)
file = file.replace('/', destination_folder)
with open(file, 'wb') as thefile:
thefile.write(contents)
else: # download specified
contents = board_files.get(remote_file)
# Print the file out if no local file was provided, otherwise save it.
if local_file is None:
print(contents.decode("utf-8"))
else:
local_file.write(contents)



@cli.command()
Expand Down Expand Up @@ -265,8 +305,15 @@ def put(local, remote):


@cli.command()
@click.argument("remote_file")
def rm(remote_file):
@click.option(
'--all',
is_flag=True,
required=False,
default=False,
help = 'Provides posibility to remove all files from micropython device.'
)
@click.argument("remote_file", default = "all")
def rm(all, remote_file):
"""Remove a file from the board.

Remove the specified file from the board's filesystem. Must specify one
Expand All @@ -277,11 +324,20 @@ def rm(remote_file):
For example to delete main.py from the root of a board run:

ampy --port /board/serial/port rm main.py

To delete all files and folders from the board:

ampy --port /board/serial/port rm --all
"""
# Delete the provided file/directory on the board.
board_files = files.Files(_board)
board_files.rm(remote_file)
filelist = board_files.ls(long_format=False)

if all or remote_file == 'all': # remove all files
for file in filelist:
board_files.rm(file)
else: # remove specified
board_files.rm(remote_file)

@cli.command()
@click.option(
Expand Down Expand Up @@ -414,8 +470,7 @@ def reset():
# serial when restarted via microcontroller.reset()
pass


if __name__ == "__main__":
def main():
try:
cli()
finally:
Expand All @@ -428,3 +483,6 @@ def reset():
# and shouldn't cause a new error or problem if the connection can't
# be closed.
pass

if __name__ == "__main__":
main()
17 changes: 10 additions & 7 deletions ampy/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def ls(self, directory="/", long_format=True, recursive=False):
directory = "/" + directory

command = """\
try:
try:
import os
except ImportError:
import uos as os\n"""
Expand All @@ -118,10 +118,10 @@ def _listdir(dir_or_file):
try:
# if its a directory, then it should provide some children.
children = os.listdir(dir_or_file)
except OSError:
except OSError:
# probably a file. run stat() to confirm.
os.stat(dir_or_file)
result.add(dir_or_file)
result.add(dir_or_file)
else:
# probably a directory, add to result if empty.
if children:
Expand All @@ -132,17 +132,17 @@ def _listdir(dir_or_file):
next = dir_or_file + child
else:
next = dir_or_file + '/' + child

_listdir(next)
else:
result.add(dir_or_file)
result.add(dir_or_file)

_listdir(directory)
return sorted(result)\n"""
else:
command += """\
def listdir(directory):
if directory == '/':
if directory == '/':
return sorted([directory + f for f in os.listdir(directory)])
else:
return sorted([directory + '/' + f for f in os.listdir(directory)])\n"""
Expand All @@ -152,7 +152,7 @@ def listdir(directory):
command += """
r = []
for f in listdir('{0}'):
size = os.stat(f)[6]
size = os.stat(f)[6]
r.append('{{0}} - {{1}} bytes'.format(f, size))
print(r)
""".format(
Expand Down Expand Up @@ -247,6 +247,9 @@ def rm(self, filename):
# Check for OSError #13, the directory isn't empty.
if message.find("OSError: [Errno 13] EACCES") != -1:
raise RuntimeError("Directory is not empty: {0}".format(filename))
# Check for OSError #21, the object is a directory.
if message.find("OSError: [Errno 21] EISDIR") != -1:
self.rmdir(filename)
else:
raise ex
self._pyboard.exit_raw_repl()
Expand Down