Source code for libka.ka_data_utils

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# pylint: disable=line-too-long
# kate: space-indent on; indent-width 4; replace-tabs on; indent-mode python; remove-trailing-space modified;
# vim: expandtab ts=4
# pylint: enable=line-too-long

############################################################################
#   Copyright © 2016-2018 José Manuel Santamaría Lema <panfaust@gmail.com> #
#                                                                          #
#   This program is free software; you can redistribute it and/or modify   #
#   it under the terms of the GNU General Public License as published by   #
#   the Free Software Foundation; either version 2 of the License, or      #
#   (at your option) any later version.                                    #
############################################################################

"""
This module provides a few functions to operate with the data provided by
the `ka-metadata` repository.
"""

import os
import sys
import json

from jsmin import jsmin

from libka.ka_print import ka_print_error
from libka.ka_print import ka_print_plain
from libka.ka_configuration import getConfigParser

#FIXME: Remove deprecated functions once they are not used somewhere else.
#       Also remove the getConfigParser usage once we have a snake case substitute.

[docs] def getDataFilesPath(): # pylint: disable=invalid-name """DEPRECATED: use `get_data_files_path` instead""" return get_data_files_path()
[docs] def get_data_files_path(): """Returns the current data files path.""" ka_config = getConfigParser() data_files_path = ka_config['global']['ka-metatadata-path'] data_files_path = os.path.expanduser(data_files_path) if not os.path.exists(data_files_path): ka_print_error('ERROR: metadata repository not found at %s' % data_files_path) ka_print_plain('You must clone the Kubuntu Automation metadata repository in %s' % data_files_path) ka_metatadata_git_https = 'https://git.launchpad.net/~kubuntu-packagers/ka/+git/ka-metadata' ka_metatadata_git_ssh = 'git+ssh://git.launchpad.net/~kubuntu-packagers/ka/+git/ka-metadata' ka_print_plain('You can do that with git clone %s\nOR git clone %s' % (ka_metatadata_git_https, ka_metatadata_git_ssh)) sys.exit(1) return data_files_path
[docs] def readPackagesFile(file_name, catch_exceptions=True): # pylint: disable=invalid-name """DEPRECATED: use `read_packages_file` instead.""" return read_packages_file(file_name, catch_exceptions)
[docs] def read_packages_file(file_name, catch_exceptions=True): """Return a list of packages, stripping comments starting with `#`""" #Find out the data files path data_files_path = get_data_files_path() #Load the data file file_path = os.path.join(data_files_path, file_name) try: #Read file lines packages_file = open(file_path, "r") packages = packages_file.readlines() packages_file.close() #Filter out comments packages = map(lambda line: line.split('#')[0].strip("\r\n\t "), packages) packages = filter(lambda line: line and not line.startswith("#"), packages) #Remove comments to the left of the package name except Exception as exception: # pylint: disable=broad-except if catch_exceptions: ka_print_error("Error loading packages file %s" % file_path) print(exception) sys.exit(1) else: return [] #Return result return list(packages)
[docs] def readJsonDataFile(file_name, catch_exceptions=True): # pylint: disable=invalid-name """DEPRECATED: use `read_json_data_file` instead.""" return read_json_data_file(file_name, catch_exceptions)
[docs] def read_json_data_file(file_name, catch_exceptions=True): """ Returns a JSON map with the file contents, stripping comments. Comments may start with '//' """ #Find out the data files path data_files_path = get_data_files_path() #Load the data file file_path = data_files_path + '/' + file_name try: json_file = open(file_path, 'r') json_string = jsmin(json_file.read()) json_file.close() result = json.loads(json_string) except Exception as exception: # pylint: disable=broad-except if catch_exceptions: ka_print_error("Error loading JSON file %s" % file_path) print(exception) sys.exit(1) else: return "" return result
[docs] def read_bd_relations_file(file_name, nodoc=False, catch_exceptions=True): """ Returns a map with the file contents, stipping comments. Comments may start with '//' If `nodoc` is `True`, the build dependencies marked with `<!nodoc>` will be ignored. If `nodoc` is `False`, the build dependencies marked with `<!nodoc>` will be taken into account, removing the final `"<!nodoc>"` string, for instance if a source package has an entry in the file like this: .. code-block:: text "qt3d-opensource-src": [ "qtbase-opensource-src", "qtdeclarative-opensource-src", "qtbase-opensource-src<!nodoc>", "qtdeclarative-opensource-src<!nodoc>", "qttools-opensource-src<!nodoc>" ] it will be processed like this: .. code-block:: text "qt3d-opensource-src": [ "qtbase-opensource-src", "qtdeclarative-opensource-src", "qttools-opensource-src" ] """ #Read the contents of the JSON file bd_relations_map_orig = read_json_data_file(file_name, catch_exceptions) #Handle the <!nodoc> build depends result_map = {} if nodoc: #Filter out the <!nodoc> depends for source_package in bd_relations_map_orig: source_depends_list_orig = bd_relations_map_orig[source_package] source_depends_list_new = [] for source_depends in source_depends_list_orig: if not source_depends.endswith("<!nodoc>"): source_depends_list_new.append(source_depends) result_map[source_package] = source_depends_list_new else: #Remove the final <!nodoc> of some depends for source_package in bd_relations_map_orig: source_depends_list_orig = bd_relations_map_orig[source_package] source_depends_list_new = [] for source_depends in source_depends_list_orig: real_source_depends = source_depends.split("<!nodoc>")[0] if real_source_depends not in source_depends_list_new: source_depends_list_new.append(real_source_depends) result_map[source_package] = source_depends_list_new #Return result return result_map