CSAR_OUTPUT_FILE = 'output.csar'
Args = collections.namedtuple('Args',
- ['source', 'entry', 'manifest', 'history', 'tests', 'licenses'])
+ ['source', 'entry', 'manifest', 'history', 'tests', 'licenses', 'digest'])
ARGS_MANIFEST = {
'source': CSAR_RESOURCE_DIR,
'history': 'ChangeLog.txt',
'tests': 'Tests',
'licenses': 'Licenses',
+ 'digest': None
}
+ARGS_MANIFEST_DIGEST = {
+ 'source': CSAR_RESOURCE_DIR,
+ 'entry': CSAR_ENTRY_FILE,
+ 'manifest': 'test_entry.mf',
+ 'history': 'ChangeLog.txt',
+ 'tests': 'Tests',
+ 'licenses': 'Licenses',
+ 'digest': 'sha256'
+ }
+
+
ARGS_NO_MANIFEST = {
'source': CSAR_RESOURCE_DIR,
'entry': CSAR_ENTRY_FILE,
'history': None,
'tests': None,
'licenses': None,
+ 'digest': None,
}
csar.write(args.source, args.entry, csar_target_dir + '/' + CSAR_OUTPUT_FILE, logging, args)
csar.read(csar_target_dir + '/' + CSAR_OUTPUT_FILE, csar_extract_dir, logging)
assert filecmp.cmp(args.source + '/' + args.entry, csar_extract_dir + '/' + args.entry)
- if(args.manifest):
+ if(args.manifest and not args.digest):
assert filecmp.cmp(args.source + '/' + args.manifest,
csar_extract_dir + '/' + args.manifest)
if(args.history):
if not os.path.exists(license_path):
os.makedirs(license_path)
csar_write_test(Args(**ARGS_MANIFEST))
+
+
+def test_CSARWrite_manifest_digest():
+ # Because git can not store emptry directory, we need to create manually here
+ license_path = ARGS_MANIFEST['source'] + '/' + ARGS_MANIFEST['licenses']
+ if not os.path.exists(license_path):
+ os.makedirs(license_path)
+ csar_write_test(Args(**ARGS_MANIFEST_DIGEST))
metadata:
vnf_product_name: test
vnf_provider_id: test
-vnf_pacakage_version: 1.0
-vnf_release_date_time: 2017.09.15T15:00+8:00
+vnf_package_version: 1.0
+vnf_release_data_time: 2017-09-15T15:00:03+08:00
csar_create.add_argument(
'--licenses',
help='Directory containing license information, relative to service template directory')
+ csar_create.add_argument(
+ '--digest',
+ choices=['SHA256', 'SHA512'],
+ help='If present, means to check the file deigest in manifest; compute the digest using the specified hash algorithm of all files in the csar package to be put into the manifest file')
csar_open = subparsers.add_parser('csar-open')
import requests
from ruamel import yaml # @UnresolvedImport
+from vnfsdk_pkgtools.packager import manifest
META_FILE = 'TOSCA-Metadata/TOSCA.meta'
META_FILE_VERSION_KEY = 'TOSCA-Meta-File-Version'
msg='Please specify a valid manifest file.',
check_dir=False)
metadata[META_ENTRY_MANIFEST_FILE_KEY] = args.manifest
+ manifest_file = manifest.Manifest(source, args.manifest)
+ manifest_file_full_path = os.path.join(source, args.manifest)
+ else:
+ manifest_file = None
+ manifest_file_full_path = None
+
if(args.history):
check_file_dir(root=source,
for root, dirs, files in os.walk(source):
for file in files:
file_full_path = os.path.join(root, file)
- file_relative_path = os.path.relpath(file_full_path, source)
- logger.debug('Writing to archive: {0}'.format(file_relative_path))
- f.write(file_full_path, file_relative_path)
+ # skip manifest file here in case we need to generate digest
+ if file_full_path!=manifest_file_full_path:
+ file_relative_path = os.path.relpath(file_full_path, source)
+ logger.debug('Writing to archive: {0}'.format(file_relative_path))
+ f.write(file_full_path, file_relative_path)
+ if manifest_file and args.digest:
+ logger.debug('Update file digest: {0}'.format(file_relative_path))
+ manifest_file.add_file(file_relative_path, args.digest)
# add empty dir
for dir in dirs:
dir_full_path = os.path.join(root, dir)
logger.debug('Writing to archive: {0}'.format(dir_relative_path))
f.write(dir_full_path + os.sep, dir_relative_path)
+ if manifest_file:
+ if args.digest:
+ logger.debug('Update manifest file to temporary file')
+ manifest_file_full_path = manifest_file.update_to_file(True)
+ logger.debug('Writing to archive: {0}'.format(args.manifest))
+ f.write(manifest_file_full_path, args.manifest)
+
logger.debug('Writing new metadata file to {0}'.format(META_FILE))
f.writestr(META_FILE, yaml.dump(metadata, default_flow_style=False))
self.source = os.path.expanduser(source)
self.destination = os.path.expanduser(destination)
self.metadata = {}
+ self.manifest = None
try:
if not os.path.exists(self.source):
raise ValueError('{0} does not exists. Please specify a valid CSAR path.'
'The manifest file {0} referenced by the metadata '
'file does not exist.'.format(self.entry_manifest_file),
check_dir=False)
+ self.manifest = manifest.Manifest(self.destination,
+ self.entry_manifest_file)
+
if(self.entry_history_file):
check_file_dir(self.destination,
from collections import namedtuple
import os
+import tempfile
import udatetime
ret += "Hash: %s\n" % digest[1]
return ret
- def update_to_file(self):
+ def update_to_file(self, temporary=False):
content = self.return_as_string()
- with open(os.path.join(self.root, self.path), 'w') as fp:
+ if temporary:
+ abs_path = tempfile.mktemp()
+ else:
+ abs_path = os.path.abspath(os.path.join(self.root, self.path))
+
+ with open(abs_path, 'w') as fp:
fp.write(content)
+ return abs_path