diff options
author | Lianhao Lu <lianhao.lu@intel.com> | 2018-08-02 16:16:24 +0800 |
---|---|---|
committer | Lianhao Lu <lianhao.lu@intel.com> | 2018-08-02 16:42:51 +0800 |
commit | 1de1b090628422d1e25da1384a95a81e91cc6e97 (patch) | |
tree | cd1f3cd46480bf04300c6a006850c1b7b560b5ca /vnfsdk_pkgtools | |
parent | 40f7a0f4634b48145335b4e95f57fca0afe39c53 (diff) |
Support verbose logging
The previous verbose logging switch '-v' doesn't work as we expected.
Change the code to follow python conventions to make it work.
Issue-ID: VNFSDK-297
Change-Id: Ia0c41b5067d9c28acccabc4137797310052fce5e
Signed-off-by: Lianhao Lu <lianhao.lu@intel.com>
Diffstat (limited to 'vnfsdk_pkgtools')
-rw-r--r-- | vnfsdk_pkgtools/cli/__main__.py | 33 | ||||
-rw-r--r-- | vnfsdk_pkgtools/packager/csar.py | 48 |
2 files changed, 48 insertions, 33 deletions
diff --git a/vnfsdk_pkgtools/cli/__main__.py b/vnfsdk_pkgtools/cli/__main__.py index fee321c..23dbe02 100644 --- a/vnfsdk_pkgtools/cli/__main__.py +++ b/vnfsdk_pkgtools/cli/__main__.py @@ -28,19 +28,18 @@ def csar_create_func(namespace): csar.write(namespace.source, namespace.entry, namespace.destination, - logging, args=namespace) + def csar_open_func(namespace): csar.read(namespace.source, - namespace.destination, - logging) + namespace.destination) + def csar_validate_func(namespace): workdir = tempfile.mkdtemp() try: reader = None reader = csar.read(namespace.source, - workdir, - logging) + workdir) driver = validator.get_validator(namespace.parser) driver.validate(reader) @@ -53,15 +52,15 @@ def parse_args(args_list): CLI entry point """ parser = argparse.ArgumentParser(description='VNF SDK CSAR manipulation tool') - - subparsers = parser.add_subparsers(help='csar-create') - csar_create = subparsers.add_parser('csar-create') - csar_create.set_defaults(func=csar_create_func) - csar_create.add_argument('-v', '--verbose', + parser.add_argument('-v', '--verbose', dest='verbosity', action='count', default=0, help='Set verbosity level (can be passed multiple times)') + + subparsers = parser.add_subparsers(help='csar-create') + csar_create = subparsers.add_parser('csar-create') + csar_create.set_defaults(func=csar_create_func) csar_create.add_argument( 'source', help='Service template directory') @@ -112,8 +111,22 @@ def parse_args(args_list): return parser.parse_args(args_list) + +def init_logging(args): + verbosity = [logging.WARNING, logging.INFO, logging.DEBUG] + + logging.basicConfig() + logger = logging.getLogger('vnfsdk_pkgtools') + if args.verbosity >= len(verbosity): + verbose = verbosity[-1] + else: + verbose = verbosity[args.verbosity] + logger.setLevel(verbose) + + def main(): args = parse_args(sys.argv[1:]) + init_logging(args) args.func(args) diff --git a/vnfsdk_pkgtools/packager/csar.py b/vnfsdk_pkgtools/packager/csar.py index 31fba8b..162985f 100644 --- a/vnfsdk_pkgtools/packager/csar.py +++ b/vnfsdk_pkgtools/packager/csar.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging import os import pprint import tempfile @@ -23,6 +24,8 @@ from ruamel import yaml # @UnresolvedImport from vnfsdk_pkgtools.packager import manifest +LOG = logging.getLogger(__name__) + META_FILE = 'TOSCA-Metadata/TOSCA.meta' META_FILE_VERSION_KEY = 'TOSCA-Meta-File-Version' META_FILE_VERSION_VALUE = '1.0' @@ -58,7 +61,7 @@ def check_file_dir(root, entry, msg, check_for_non=False, check_dir=False): raise ValueError(error_msg.format(path)) -def write(source, entry, destination, logger, args): +def write(source, entry, destination, args): source = os.path.expanduser(source) destination = os.path.expanduser(destination) metadata = BASE_METADATA.copy() @@ -119,7 +122,7 @@ def write(source, entry, destination, logger, args): check_dir=True) metadata[META_ENTRY_LICENSES_DIR_KEY] = args.licenses - logger.debug('Compressing root directory to ZIP') + LOG.debug('Compressing root directory to ZIP') with zipfile.ZipFile(destination, 'w', zipfile.ZIP_DEFLATED) as f: for root, dirs, files in os.walk(source): for file in files: @@ -127,34 +130,33 @@ def write(source, entry, destination, logger, args): # 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)) + LOG.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)) + LOG.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) if len(os.listdir(dir_full_path)) == 0: dir_relative_path = os.path.relpath(dir_full_path, source) + os.sep - logger.debug('Writing to archive: {0}'.format(dir_relative_path)) + LOG.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') + LOG.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)) + LOG.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)) + LOG.debug('Writing new metadata file to {0}'.format(META_FILE)) f.writestr(META_FILE, yaml.dump(metadata, default_flow_style=False)) class _CSARReader(object): - def __init__(self, source, destination, logger): - self.logger = logger + def __init__(self, source, destination): if os.path.isdir(destination) and os.listdir(destination): raise ValueError('{0} already exists and is not empty. ' 'Please specify the location where the CSAR ' @@ -220,22 +222,22 @@ class _CSARReader(object): return self.metadata.get(META_ENTRY_LICENSES_DIR_KEY) def _extract(self): - self.logger.debug('Extracting CSAR contents') + LOG.debug('Extracting CSAR contents') if not os.path.exists(self.destination): os.mkdir(self.destination) with zipfile.ZipFile(self.source) as f: f.extractall(self.destination) - self.logger.debug('CSAR contents successfully extracted') + LOG.debug('CSAR contents successfully extracted') def _read_metadata(self): csar_metafile = os.path.join(self.destination, META_FILE) if not os.path.exists(csar_metafile): raise ValueError('Metadata file {0} is missing from the CSAR'.format(csar_metafile)) - self.logger.debug('CSAR metadata file: {0}'.format(csar_metafile)) - self.logger.debug('Attempting to parse CSAR metadata YAML') + LOG.debug('CSAR metadata file: {0}'.format(csar_metafile)) + LOG.debug('Attempting to parse CSAR metadata YAML') with open(csar_metafile) as f: self.metadata.update(yaml.load(f)) - self.logger.debug('CSAR metadata:\n{0}'.format(pprint.pformat(self.metadata))) + LOG.debug('CSAR metadata:\n{0}'.format(pprint.pformat(self.metadata))) def _validate(self): def validate_key(key, expected=None): @@ -249,11 +251,11 @@ class _CSARReader(object): validate_key(META_CSAR_VERSION_KEY, expected=META_CSAR_VERSION_VALUE) validate_key(META_CREATED_BY_KEY) validate_key(META_ENTRY_DEFINITIONS_KEY) - self.logger.debug('CSAR entry definitions: {0}'.format(self.entry_definitions)) - self.logger.debug('CSAR manifest file: {0}'.format(self.entry_manifest_file)) - self.logger.debug('CSAR change history file: {0}'.format(self.entry_history_file)) - self.logger.debug('CSAR tests directory: {0}'.format(self.entry_tests_dir)) - self.logger.debug('CSAR licenses directory: {0}'.format(self.entry_licenses_dir)) + LOG.debug('CSAR entry definitions: {0}'.format(self.entry_definitions)) + LOG.debug('CSAR manifest file: {0}'.format(self.entry_manifest_file)) + LOG.debug('CSAR change history file: {0}'.format(self.entry_history_file)) + LOG.debug('CSAR tests directory: {0}'.format(self.entry_tests_dir)) + LOG.debug('CSAR licenses directory: {0}'.format(self.entry_licenses_dir)) check_file_dir(self.destination, self.entry_definitions, @@ -297,12 +299,12 @@ class _CSARReader(object): if response.status_code != 200: raise ValueError('Server at {0} returned a {1} status code' .format(url, response.status_code)) - self.logger.info('Downloading {0} to {1}'.format(url, target)) + LOG.info('Downloading {0} to {1}'.format(url, target)) with open(target, 'wb') as f: for chunk in response.iter_content(chunk_size=8192): if chunk: f.write(chunk) -def read(source, destination, logger): - return _CSARReader(source=source, destination=destination, logger=logger) +def read(source, destination): + return _CSARReader(source=source, destination=destination) |