Skip to content

Commit 4e15bf8

Browse files
authored
Merge pull request #923 from kaliappan01/tar-py
tar_py added
2 parents 77fede6 + 8491008 commit 4e15bf8

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

tar_py/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# tar_py Name
2+
3+
**The script will be able create, list and extract tar ball files using python**
4+
5+
## Setup instructions
6+
7+
Explain how to setup and run your package/script in user's system
8+
9+
## Usage
10+
Create tar ball
11+
`python main.py -op create -tf tar_file_name_.tar -f folder_name/`
12+
13+
List tar ball contents
14+
`python main.py -op list -tf tar_file_name_.tar`
15+
16+
Extract tar ball
17+
`python main.py -op extract -tf tar_file_name_.tar -f folder_name/`
18+
19+
Note: add compression extension wherever necessary
20+
## Output
21+
22+
Display images/gifs/videos of output/result of your script so that users can visualize it
23+
24+
## Author(s)
25+
26+
[Kaliappan Yadav](https://github.com/kaliappan01)
27+
28+
## Disclaimers, if any
29+
30+
Use this section to mention if any particular disclaimer is required

tar_py/main.py

Whitespace-only changes.

tar_py/makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
VENV ?= .venv
2+
REQUIREMENTS_FILE ?= requirements.txt
3+
4+
init:
5+
python3 -m venv $(VENV)
6+
$(VENV)/bin/python -m pip install --upgrade pip
7+
if [ -f $(REQUIREMENTS_FILE) ]; \
8+
then $(VENV)/bin/python -m pip install -r $(REQUIREMENTS_FILE); \
9+
fi

tar_py/requirements.txt

Whitespace-only changes.

tar_py/tar_py.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import tarfile
2+
import os
3+
4+
5+
def list_tar(archive, verbosity):
6+
"""List a TAR archive with the tarfile Python module."""
7+
try:
8+
with tarfile.open(archive) as tfile:
9+
tfile.list(verbose=verbosity > 1)
10+
except Exception as err:
11+
msg = "error listing %s: %s" % (archive, err)
12+
print(msg)
13+
return None
14+
15+
16+
test_tar = list_tar
17+
18+
19+
def extract_tar(archive, outdir):
20+
"""Extract a TAR archive with the tarfile Python module."""
21+
try:
22+
with tarfile.open(archive) as tfile:
23+
tfile.extractall(path=outdir)
24+
except Exception as err:
25+
msg = "error extracting %s: %s" % (archive, err)
26+
print(msg)
27+
return None
28+
29+
30+
def create_tar(archive, folder_name, compression=None):
31+
"""Create a TAR archive with the tarfile Python module."""
32+
mode = "w:"
33+
if compression is not None:
34+
mode = get_tar_mode(compression)
35+
try:
36+
with tarfile.open(archive, mode) as tfile:
37+
for filename in os.listdir(folder_name):
38+
tfile.add(folder_name + filename, arcname=filename)
39+
except Exception as err:
40+
msg = "error creating %s: %s" % (archive, err)
41+
print(msg)
42+
return None
43+
44+
45+
def get_tar_mode(compression):
46+
"""Determine tarfile open mode according to the given compression."""
47+
if compression == 'gzip':
48+
return 'w:gz'
49+
if compression == 'bzip2':
50+
return 'w:bz2'
51+
if compression == 'lzma':
52+
return 'w:xz'
53+
if compression:
54+
msg = 'pytarfile does not support %s for tar compression'
55+
print(msg)
56+
# no compression
57+
return 'w'

0 commit comments

Comments
 (0)