From d1682f56c45766955d463528f60ab5f56b585f27 Mon Sep 17 00:00:00 2001
From: Thomas Reifenberger <tom-mi@users.noreply.github.com>
Date: Thu, 19 Nov 2015 23:13:27 +0100
Subject: [PATCH] Add dist function

---
 mkp/__init__.py              | 18 +++++++++++++
 test/integration/test_mkp.py | 51 ++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)

diff --git a/mkp/__init__.py b/mkp/__init__.py
index e64003c..bec3233 100644
--- a/mkp/__init__.py
+++ b/mkp/__init__.py
@@ -19,6 +19,24 @@ _DIRECTORIES = [
 _VERSION_PACKAGED = 'python-mkp'
 
 
+_DIST_DIR = 'dist'
+
+
+def dist(info, path=None):
+    if not path:
+        import __main__ as main
+        path = os.path.dirname(os.path.realpath(main.__file__))
+
+    info['files'] = find_files(path)
+    dist_dir = os.path.join(path, _DIST_DIR)
+    filename = '{}-{}.mkp'.format(info['name'], info['version'])
+
+    if not os.path.exists(dist_dir):
+        os.makedirs(dist_dir)
+
+    pack_to_file(info, path, os.path.join(dist_dir, filename))
+
+
 def load_file(path):
     file_io = open(path, 'rb')
     return Package(file_io)
diff --git a/test/integration/test_mkp.py b/test/integration/test_mkp.py
index f61af32..c2e3506 100644
--- a/test/integration/test_mkp.py
+++ b/test/integration/test_mkp.py
@@ -128,3 +128,54 @@ def test_pack_and_unpack_covers_all_known_directories(tmpdir):
 
     for directory in DIRECTORIES:
         assert dest.join(directory, 'test').exists()
+
+
+def test_dist(tmpdir):
+    tmpdir.join('agents', 'special', 'agent_test').write_binary(b'hello', ensure=True)
+    tmpdir.join('checks', 'foo').write_binary(b'Check Me!', ensure=True)
+    info = {
+        'author': 'John Doe',
+        'name': 'foo',
+        'version': '42',
+    }
+
+    mkp.dist(info, str(tmpdir))
+
+    assert tmpdir.join('dist', 'foo-42.mkp').exists()
+    package = mkp.load_file(str(tmpdir.join('dist', 'foo-42.mkp')))
+    assert package.info['author'] == 'John Doe'
+    assert package.info['name'] == 'foo'
+    assert package.info['files']['agents'] == ['special/agent_test']
+    assert package.info['files']['checks'] == ['foo']
+    assert package.info['version'] == '42'
+    assert package.info['version.packaged'] == 'python-mkp'
+
+
+def test_dist_uses_script_path_when_no_path_is_given(tmpdir):
+
+    script = tmpdir.join('dist.py')
+    script.write_text(u'''#!/usr/bin/env python
+
+from mkp import dist
+
+
+dist({
+    'author': 'John Doe',
+    'name': 'foo',
+    'version': '42',
+})
+''', 'utf-8')
+    script.chmod(0700)
+    tmpdir.join('agents', 'special', 'agent_test').write_binary(b'hello', ensure=True)
+    tmpdir.join('checks', 'foo').write_binary(b'Check Me!', ensure=True)
+
+    script.sysexec()
+
+    assert tmpdir.join('dist', 'foo-42.mkp').exists()
+    package = mkp.load_file(str(tmpdir.join('dist', 'foo-42.mkp')))
+    assert package.info['author'] == 'John Doe'
+    assert package.info['name'] == 'foo'
+    assert package.info['files']['agents'] == ['special/agent_test']
+    assert package.info['files']['checks'] == ['foo']
+    assert package.info['version'] == '42'
+    assert package.info['version.packaged'] == 'python-mkp'
-- 
GitLab