Add opportunity to import package from directory

Change-Id: I23cdc5c6bfbf46fc0ca1149caeaf0bc43b2f7829
Closes-bug: #1620984
This commit is contained in:
Omar Shykhkerimov 2016-09-20 14:20:22 +03:00
parent 3ff7576c4d
commit 5b677b2abc
5 changed files with 44 additions and 2 deletions

View File

@ -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)

View File

@ -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'

View File

@ -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."""

View File

@ -671,7 +671,8 @@ def _handle_package_exists(mc, data, package, exists_action):
@utils.arg('filename', metavar='<FILE>',
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='<CATEGORY>', 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"

View File

@ -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.