Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
python-mkp
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Container registry
Operate
Terraform modules
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
checkmk
python-mkp
Commits
86398b35
Commit
86398b35
authored
9 years ago
by
Thomas Reifenberger
Browse files
Options
Downloads
Patches
Plain Diff
Add functions to load and pack to files
parent
4e22b293
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
mkp/__init__.py
+22
-4
22 additions, 4 deletions
mkp/__init__.py
test/integration/test_mkp.py
+34
-0
34 additions, 0 deletions
test/integration/test_mkp.py
with
56 additions
and
4 deletions
mkp/__init__.py
+
22
−
4
View file @
86398b35
...
...
@@ -19,6 +19,11 @@ _DIRECTORIES = [
_VERSION_PACKAGED
=
'
python-mkp
'
def
load_file
(
path
):
file_io
=
open
(
path
,
'
rb
'
)
return
Package
(
file_io
)
def
load_bytes
(
data
):
bytes_io
=
io
.
BytesIO
(
data
)
return
Package
(
bytes_io
)
...
...
@@ -49,11 +54,16 @@ def _find_files_in_directory(path):
return
result
def
pack_to_file
(
info
,
path
,
outfile
):
with
open
(
outfile
,
'
wb
'
)
as
f
:
f
.
write
(
pack_to_bytes
(
info
,
path
))
def
pack_to_bytes
(
info
,
path
):
_patch_info
(
info
)
bytes_io
=
io
.
BytesIO
()
with
tarfile
.
open
(
fileobj
=
bytes_io
,
mode
=
'
w:gz
'
)
as
archive
:
info_data
=
pprint
.
pformat
(
info
).
encode
(
)
info_data
=
encode_info
(
info
)
tarinfo
,
fileobj
=
_create_tarinfo_and_buffer
(
info_data
,
'
info
'
)
archive
.
addfile
(
tarinfo
,
fileobj
=
fileobj
)
...
...
@@ -89,15 +99,23 @@ def _create_tarinfo_and_buffer(data, filename):
return
tarinfo
,
bytes_io
def
encode_info
(
info
):
return
pprint
.
pformat
(
info
).
encode
()
def
decode_info
(
info_bytes
):
return
ast
.
literal_eval
(
info_bytes
.
decode
())
class
Package
(
object
):
def
__init__
(
self
,
fileobj
):
self
.
archive
=
tarfile
.
open
(
fileobj
=
fileobj
)
self
.
_info
=
self
.
_
load
_info
()
self
.
_info
=
self
.
_
get
_info
()
def
_
load
_info
(
self
):
def
_
get
_info
(
self
):
info_file
=
self
.
archive
.
extractfile
(
'
info
'
)
return
ast
.
literal_eval
(
info_file
.
read
()
.
decode
()
)
return
decode_info
(
info_file
.
read
())
@property
def
info
(
self
):
...
...
This diff is collapsed.
Click to expand it.
test/integration/test_mkp.py
+
34
−
0
View file @
86398b35
...
...
@@ -17,6 +17,15 @@ def test_load_bytes(original_mkp_file):
assert
package
.
info
[
'
title
'
]
==
'
Title of test
'
def
test_load_file
(
original_mkp_file
,
tmpdir
):
tmpdir
.
join
(
'
test.mkp
'
).
write_binary
(
original_mkp_file
)
package
=
mkp
.
load_file
(
str
(
tmpdir
.
join
(
'
test.mkp
'
)))
assert
type
(
package
)
==
mkp
.
Package
assert
package
.
info
[
'
title
'
]
==
'
Title of test
'
def
test_extract_files
(
original_mkp_file
,
tmpdir
):
package
=
mkp
.
load_bytes
(
original_mkp_file
)
...
...
@@ -51,6 +60,31 @@ def test_pack_to_bytes(tmpdir):
assert
agent_file
.
read
()
==
b
'
hello
'
def
test_pack_to_file
(
tmpdir
):
info
=
{
'
files
'
:
{
'
agents
'
:
[
'
special/agent_test
'
]},
'
title
'
:
'
Test package
'
,
}
tmpdir
.
join
(
'
agents
'
,
'
special
'
,
'
agent_test
'
).
write_binary
(
b
'
hello
'
,
ensure
=
True
)
outfile
=
tmpdir
.
join
(
'
test.mkp
'
)
mkp
.
pack_to_file
(
info
,
str
(
tmpdir
),
str
(
outfile
))
archive
=
tarfile
.
open
(
str
(
outfile
))
info_file
=
archive
.
extractfile
(
'
info
'
).
read
()
extracted_info
=
ast
.
literal_eval
(
info_file
.
decode
())
assert
extracted_info
[
'
files
'
]
==
info
[
'
files
'
]
assert
extracted_info
[
'
title
'
]
==
info
[
'
title
'
]
assert
extracted_info
[
'
version.packaged
'
]
==
'
python-mkp
'
agents_archive_file
=
archive
.
extractfile
(
'
agents.tar
'
)
agents_archive
=
tarfile
.
open
(
fileobj
=
agents_archive_file
,
mode
=
'
r:
'
)
agent_file
=
agents_archive
.
extractfile
(
'
special/agent_test
'
)
assert
agent_file
.
read
()
==
b
'
hello
'
def
test_find_files_searches_all_directories
(
tmpdir
):
for
directory
in
DIRECTORIES
:
tmpdir
.
join
(
directory
,
'
test
'
).
write_binary
(
b
'
Foo
'
,
ensure
=
True
)
...
...
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