From 5b677b2abc2d89bcba6da9b26861885fb34be377 Mon Sep 17 00:00:00 2001 From: Omar Shykhkerimov Date: Tue, 20 Sep 2016 14:20:22 +0300 Subject: [PATCH] Add opportunity to import package from directory Change-Id: I23cdc5c6bfbf46fc0ca1149caeaf0bc43b2f7829 Closes-bug: #1620984 --- muranoclient/common/utils.py | 10 ++++++++++ .../unit/fixture_data/empty-app/manifest.yaml | 18 ++++++++++++++++++ muranoclient/tests/unit/test_utils.py | 8 ++++++++ muranoclient/v1/shell.py | 5 +++-- ...ackage-from-directory-8f2b5e393004ef97.yaml | 5 +++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 muranoclient/tests/unit/fixture_data/empty-app/manifest.yaml create mode 100644 releasenotes/notes/import-package-from-directory-8f2b5e393004ef97.yaml diff --git a/muranoclient/common/utils.py b/muranoclient/common/utils.py index dd9c1855..5034befa 100644 --- a/muranoclient/common/utils.py +++ b/muranoclient/common/utils.py @@ -227,6 +227,16 @@ class File(object): else: if os.path.isfile(self.name): return open(self.name, mode) + if os.path.isdir(self.name): + tmp = tempfile.NamedTemporaryFile() + archive = zipfile.ZipFile(tmp, 'w', zipfile.ZIP_DEFLATED) + for root, dirs, files in os.walk(self.name): + for _file in files: + destination = os.path.relpath( + os.path.join(root, _file), os.path.join(self.name)) + archive.write(os.path.join(root, _file), destination) + tmp.flush() + return open(tmp.name, mode) url = urllib.parse.urlparse(self.name) if url.scheme in ('http', 'https'): resp = requests.get(self.name, stream=True) diff --git a/muranoclient/tests/unit/fixture_data/empty-app/manifest.yaml b/muranoclient/tests/unit/fixture_data/empty-app/manifest.yaml new file mode 100644 index 00000000..cefc672d --- /dev/null +++ b/muranoclient/tests/unit/fixture_data/empty-app/manifest.yaml @@ -0,0 +1,18 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +Format: 1.3 +Type: Application +FullName: empty +Name: empty +Description: empty description +Author: 'Mirantis, Inc' diff --git a/muranoclient/tests/unit/test_utils.py b/muranoclient/tests/unit/test_utils.py index 5ce2e57c..9ccfc901 100644 --- a/muranoclient/tests/unit/test_utils.py +++ b/muranoclient/tests/unit/test_utils.py @@ -115,6 +115,14 @@ class PackageTest(testtools.TestCase): path=path, ).manifest['FullName']) + def test_package_from_directory(self): + path = os.path.join(os.path.dirname(os.path.realpath(__file__)), + "fixture_data/empty-app") + pkg = utils.Package(utils.File(path)) + self.assertEqual('empty', pkg.manifest['FullName']) + pkg = utils.Package.from_location('', path=path) + self.assertEqual('empty', pkg.manifest['FullName']) + @requests_mock.mock() def test_from_location_url(self, m): """Test that url overrides name specification.""" diff --git a/muranoclient/v1/shell.py b/muranoclient/v1/shell.py index f3a8919e..487af3dd 100644 --- a/muranoclient/v1/shell.py +++ b/muranoclient/v1/shell.py @@ -671,7 +671,8 @@ def _handle_package_exists(mc, data, package, exists_action): @utils.arg('filename', metavar='', nargs='+', - help='URL of the murano zip package, FQPN, or path to zip package.') + help='URL of the murano zip package, FQPN, path to zip package' + ' or path to directory with package.') @utils.arg('-c', '--categories', metavar='', nargs='*', help='Category list to attach.') @utils.arg('--is-public', action='store_true', default=False, @@ -708,7 +709,7 @@ def do_package_import(mc, args): total_reqs = collections.OrderedDict() main_packages_names = [] for filename in args.filename: - if os.path.isfile(filename): + if os.path.isfile(filename) or os.path.isdir(filename): _file = filename else: print("Package file '{0}' does not exist, attempting to download" diff --git a/releasenotes/notes/import-package-from-directory-8f2b5e393004ef97.yaml b/releasenotes/notes/import-package-from-directory-8f2b5e393004ef97.yaml new file mode 100644 index 00000000..7e37f3c4 --- /dev/null +++ b/releasenotes/notes/import-package-from-directory-8f2b5e393004ef97.yaml @@ -0,0 +1,5 @@ +--- +features: + - Ability to load package from directory was added. If specified + directory contains all the needed files then package will be + imported as usual. \ No newline at end of file