Skip to content

Commit 22105bd

Browse files
committed
Initial commit
0 parents  commit 22105bd

13 files changed

+2544
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mkp/_version.py export-subst

.gitignore

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
5+
# C extensions
6+
*.so
7+
8+
# Distribution / packaging
9+
.Python
10+
env/
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
*.egg-info/
23+
.installed.cfg
24+
*.egg
25+
26+
# PyInstaller
27+
# Usually these files are written by a python script from a template
28+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
29+
*.manifest
30+
*.spec
31+
32+
# Installer logs
33+
pip-log.txt
34+
pip-delete-this-directory.txt
35+
36+
# Unit test / coverage reports
37+
htmlcov/
38+
.tox/
39+
.coverage
40+
.coverage.*
41+
.cache
42+
nosetests.xml
43+
coverage.xml
44+
*,cover
45+
46+
# Translations
47+
*.mo
48+
*.pot
49+
50+
# Django stuff:
51+
*.log
52+
53+
# Sphinx documentation
54+
docs/_build/
55+
56+
# PyBuilder
57+
target/
58+
59+
# pyenv
60+
.python-version
61+
62+
# Vim
63+
*.swp

MANIFEST.in

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include README.md
2+
include versioneer.py
3+
include mkp/_version.py

README.md

Whitespace-only changes.

dev-requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pytest
2+
pytest-cov
3+
pytest-flakes
4+
pytest-pep8
5+
wheel

mkp/__init__.py

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import ast
2+
import io
3+
import os
4+
import os.path
5+
import pprint
6+
import tarfile
7+
8+
from ._version import get_versions
9+
__version__ = get_versions()['version']
10+
del get_versions
11+
12+
13+
_DIRECTORIES = [
14+
'agents', 'checkman', 'checks', 'doc', 'inventory', 'notifications',
15+
'pnp-templates', 'web',
16+
]
17+
18+
19+
_VERSION_PACKAGED = 'python-mkp'
20+
21+
22+
def load_bytes(data):
23+
bytes_io = io.BytesIO(data)
24+
return Package(bytes_io)
25+
26+
27+
def find_files(path):
28+
result = {}
29+
for directory in _DIRECTORIES:
30+
result[directory] = _find_files_in_directory(os.path.join(path, directory))
31+
32+
return result
33+
34+
35+
def _find_files_in_directory(path):
36+
result = []
37+
for root, dirs, files in os.walk(path):
38+
for dirname in dirs:
39+
if dirname.startswith('.'):
40+
dirs.remove(dirname)
41+
for filename in files:
42+
if filename.startswith('.'):
43+
continue
44+
elif filename.endswith('~'):
45+
continue
46+
abspath = os.path.join(root, filename)
47+
relpath = os.path.relpath(abspath, start=path)
48+
result.append(relpath)
49+
return result
50+
51+
52+
def pack_to_bytes(info, path):
53+
_patch_info(info)
54+
bytes_io = io.BytesIO()
55+
with tarfile.open(fileobj=bytes_io, mode='w:gz') as archive:
56+
info_data = pprint.pformat(info).encode()
57+
tarinfo, fileobj = _create_tarinfo_and_buffer(info_data, 'info')
58+
archive.addfile(tarinfo, fileobj=fileobj)
59+
60+
for directory in _DIRECTORIES:
61+
files = info['files'].get(directory, [])
62+
if not files:
63+
continue
64+
65+
directory_archive = _create_directory_archive(os.path.join(path, directory), files)
66+
tarinfo, fileobj = _create_tarinfo_and_buffer(directory_archive, directory + '.tar')
67+
archive.addfile(tarinfo, fileobj)
68+
69+
return bytes_io.getvalue()
70+
71+
72+
def _patch_info(info):
73+
info['version.packaged'] = _VERSION_PACKAGED
74+
75+
76+
def _create_directory_archive(path, files):
77+
bytes_io = io.BytesIO()
78+
with tarfile.open(fileobj=bytes_io, mode='w') as archive:
79+
for filename in files:
80+
archive.add(os.path.join(path, filename), arcname=filename)
81+
82+
return bytes_io.getvalue()
83+
84+
85+
def _create_tarinfo_and_buffer(data, filename):
86+
tarinfo = tarfile.TarInfo(filename)
87+
tarinfo.size = len(data)
88+
bytes_io = io.BytesIO(data)
89+
return tarinfo, bytes_io
90+
91+
92+
class Package(object):
93+
94+
def __init__(self, fileobj):
95+
self.archive = tarfile.open(fileobj=fileobj)
96+
self._info = self._load_info()
97+
98+
def _load_info(self):
99+
info_file = self.archive.extractfile('info')
100+
return ast.literal_eval(info_file.read().decode())
101+
102+
@property
103+
def info(self):
104+
return self._info
105+
106+
def extract_files(self, path):
107+
for directory in _DIRECTORIES:
108+
self._extract_files_in_directory(path, directory)
109+
110+
def _extract_files_in_directory(self, path, directory):
111+
files = self.info['files'].get(directory, [])
112+
113+
if not files:
114+
return
115+
116+
target_path = os.path.join(path, directory)
117+
os.makedirs(target_path)
118+
dir_archive_file = self.archive.extractfile(directory + '.tar')
119+
120+
with tarfile.open(fileobj=dir_archive_file) as archive:
121+
members = [member for member in archive.getmembers() if member.name in files]
122+
archive.extractall(path=target_path, members=members)

0 commit comments

Comments
 (0)