1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 Loading mechanism for service templates.
21 from urlparse import urlparse
25 from .exceptions import AriaCliError
26 from ..utils import archive as archive_utils
29 def get(source, service_template_filename):
31 Get a source and return a path to the main service template file
33 The behavior based on then source argument content is:
35 * local ``.yaml`` file: return the file
36 * local archive (``.csar``, ``.zip``, ``.tar``, ``.tar.gz``, and ``.tar.bz2``): extract it
37 locally and return path service template file
38 * URL: download and get service template from downloaded archive
39 * GitHub repo: download and get service template from downloaded archive
41 :param source: path/URL/GitHub repo to archive/service-template file
42 :type source: basestring
43 :param service_template_filename: path to service template if source is a non-CSAR archive
44 with CSAR archives, this is read from the metadata file)
45 :type service_template_filename: basestring
46 :return: path to main service template file
49 if urlparse(source).scheme:
50 downloaded_file = utils.download_file(source)
51 return _get_service_template_file_from_archive(
52 downloaded_file, service_template_filename)
53 elif os.path.isfile(source):
54 if _is_archive(source):
55 return _get_service_template_file_from_archive(source, service_template_filename)
57 # Maybe check if yaml.
58 return os.path.abspath(source)
59 elif len(source.split('/')) == 2:
60 url = _map_to_github_url(source)
61 downloaded_file = utils.download_file(url)
62 return _get_service_template_file_from_archive(
63 downloaded_file, service_template_filename)
66 'You must provide either a path to a local file, a remote URL '
67 'or a GitHub `organization/repository[:tag/branch]`')
70 def _get_service_template_file_from_archive(archive, service_template_filename):
72 Extract archive to temporary location and get path to service template file.
74 :param archive: path to archive file
75 :type archive: basestring
76 :param service_template_filename: path to service template file relative to archive
77 :type service_template_filename: basestring
78 :return: absolute path to service template file
82 if csar.is_csar_archive(archive):
83 service_template_file = _extract_csar_archive(archive)
85 extract_directory = archive_utils.extract_archive(archive)
86 print extract_directory
87 service_template_dir = os.path.join(
89 os.listdir(extract_directory)[0],
91 print service_template_dir
92 service_template_file = os.path.join(service_template_dir, service_template_filename)
93 print service_template_file
94 print service_template_filename
96 if not os.path.isfile(service_template_file):
98 'Could not find `{0}`. Please provide the name of the main '
99 'service template file by using the `-n/--service-template-filename` flag'
100 .format(service_template_filename))
101 return service_template_file
104 def _map_to_github_url(source):
106 Returns a path to a downloaded GitHub archive.
108 :param source: GitHub repo: ``org/repo[:tag/branch]``
109 :type source: basestring
110 :return: URL to the archive file for the given repo in GitHub
114 source_parts = source.split(':', 1)
115 repo = source_parts[0]
116 tag = source_parts[1] if len(source_parts) == 2 else 'master'
117 url = 'https://github.com/{0}/archive/{1}.tar.gz'.format(repo, tag)
121 def _is_archive(source):
122 return archive_utils.is_archive(source) or csar.is_csar_archive(source)
125 def _extract_csar_archive(archive):
126 reader = csar.read(source=archive)
127 main_service_template_file_name = os.path.basename(reader.entry_definitions)
128 return os.path.join(reader.destination,
129 main_service_template_file_name)