Commit 71340d69 authored by Klint Youngmeyer's avatar Klint Youngmeyer
Browse files

Initial commit

parents
EMAC Micropython Helper Scripts
===============================
These scripts are useful while developing on the REPL or for repetetive tasks within a larger script.
cat.py
------
This script is mostly intended to be used from the REPL to verify the contents of files.
### Usage
Just pass the file name, as a string, to the `cat` function.
### Example
```
>>> from cat import cat
>>> cat('filename.txt')
This is what is in the file.
>>>
```
extract.py
----------
This script houses the `tar_extract` function, which is intented to be used with the [utarfile.py](https://github.com/micropython/micropython-lib/tree/master/utarfile) and [shutil.py](https://github.com/micropython/micropython-lib/tree/master/shutil) modules to make archive extraction easier. The `tar_extract` function will create a directory (if necessary) and extract the files to that (or the current) directory.
### Usage
```python
import os
import extract
os.chdir('/fc')
extract.tar_extract('/flash/tar_file.tar')
```
"""Contains the cat functuion"""
def cat(file_name):
"""Prints the contents of a file to the screen"""
with open(file_name, 'r') as file_descriptor:
print(file_descriptor.read())
"""This module houses helper functions for extracting archive files"""
import os
# shutil.py and utarfile.py can be obtained
# from https://github.com/micropython/micropython-lib
import shutil
import utarfile
# This function, and by extension the utarfile.py module, is only intended to
# be used on a tar archive, without any compression. The tar file should be
# saved to non-volatile memory, as a file. Use os.chdir(directory) to move to
# the desired destination for the extracted files, before extracting.
def tar_extract(file_name):
"""This function extracts an uncompressed tar file into the directory name
it was archived from"""
tar = utarfile.TarFile(file_name)
for i in tar:
print(i)
if i.type == utarfile.DIRTYPE:
if i.name != './':
os.mkdir(i.name.strip('/'))
else:
sub_file = tar.extractfile(i)
with open(i.name, "wb") as dest:
shutil.copyfileobj(sub_file, dest)
dest.close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment