Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
hash_backup
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anders Blomdell
hash_backup
Commits
821d3622
Commit
821d3622
authored
4 years ago
by
Anders Blomdell
Browse files
Options
Downloads
Patches
Plain Diff
Add tar_stream wraps tarfile with memory optimizations
parent
6e592645
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
tar_stream.py
+47
-0
47 additions, 0 deletions
tar_stream.py
test/test_tar_stream.py
+56
-0
56 additions, 0 deletions
test/test_tar_stream.py
with
103 additions
and
0 deletions
tar_stream.py
0 → 100755
+
47
−
0
View file @
821d3622
#!/usr/bin/python3
import
tarfile
import
os
import
threading
class
TarWriter
:
def
__init__
(
self
,
fileobj
):
self
.
tarfile
=
tarfile
.
open
(
fileobj
=
fileobj
,
mode
=
'
w|
'
,
format
=
tarfile
.
PAX_FORMAT
)
def
add
(
self
,
path
):
self
.
tarfile
.
add
(
path
,
recursive
=
False
)
# Keep memory consumption down
self
.
tarfile
.
members
=
[]
self
.
tarfile
.
inodes
=
{}
pass
def
close
(
self
):
self
.
tarfile
.
close
()
self
.
tarfile
=
None
class
TarReader
:
def
__init__
(
self
,
fileobj
):
self
.
fileobj
=
fileobj
self
.
tarfile
=
None
pass
def
__iter__
(
self
):
if
self
.
tarfile
==
None
:
self
.
tarfile
=
tarfile
.
open
(
fileobj
=
self
.
fileobj
,
mode
=
'
r|
'
)
pass
for
e
in
self
.
tarfile
:
yield
e
# Keep memory consumption down
self
.
tarfile
.
members
=
[]
pass
pass
def
close
(
self
):
self
.
tarfile
.
close
()
self
.
tarfile
=
None
This diff is collapsed.
Click to expand it.
test/test_tar_stream.py
0 → 100755
+
56
−
0
View file @
821d3622
#!/usr/bin/python3
from
tar_stream
import
TarReader
,
TarWriter
import
time
import
os
import
threading
def
walk_files
(
root
):
for
p
,
d
,
f
in
os
.
walk
(
root
):
for
n
in
sorted
(
f
):
yield
os
.
path
.
join
(
p
,
n
)
pass
for
n
in
sorted
(
d
):
yield
os
.
path
.
join
(
p
,
n
)
pass
pass
pass
def
write_tar
(
t
,
files
):
for
p
in
files
:
t
.
add
(
p
)
pass
pass
def
reader
(
tar_reader
):
print
(
"
READER
"
,
tar_reader
)
for
e
in
tar_reader
:
print
(
'
R
'
,
e
.
size
,
e
.
name
)
if
e
.
isfile
():
a
=
tar_reader
.
tarfile
.
extractfile
(
e
).
read
()
b
=
open
(
e
.
name
,
'
rb
'
).
read
()
if
a
!=
b
:
raise
Exception
(
'
DIFFERS...
'
,
a
,
b
)
pass
elif
e
.
issym
():
print
(
e
.
linkname
)
print
(
e
.
linkpath
)
pass
pass
pass
if
__name__
==
'
__main__
'
:
(
r
,
w
)
=
os
.
pipe
()
tw
=
TarWriter
(
os
.
fdopen
(
w
,
'
wb
'
))
tr
=
TarReader
(
os
.
fdopen
(
r
,
'
rb
'
))
t
=
threading
.
Thread
(
target
=
reader
,
args
=
(
tr
,))
t
.
daemon
=
True
t
.
start
()
write_tar
(
tw
,
walk_files
(
'
.
'
))
tw
.
close
()
t
.
join
()
tr
.
close
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment