Remove state tracking, use KAS_WORK_DIR env variable
Config.has_changed didn't work reliably so far, so the state cache was only allowing to associate a work directory with configuration file. However this was rather unintuitive, specifically when moving the work dir to a different home - nothing told the user about the persistent association. We therefore agreed to remove the state cache for now. It can be reintroduced at any time once a consistent usage model exists. To establish a relationship to a work directory inside a shell session, kas now evaluates the KAS_WORK_DIR environment variable. Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
		
				
					committed by
					
						
						Daniel Wagner
					
				
			
			
				
	
			
			
			
						parent
						
							fe39f9f370
						
					
				
				
					commit
					c65657cac6
				
			@@ -29,7 +29,6 @@ Dependencies & installation
 | 
			
		||||
This projects depends on
 | 
			
		||||
 | 
			
		||||
- Python 3
 | 
			
		||||
- configparser
 | 
			
		||||
- PyYAML
 | 
			
		||||
 | 
			
		||||
If you need Python 2 support consider sending patches. The most
 | 
			
		||||
@@ -67,6 +66,10 @@ manually, e.g.
 | 
			
		||||
$ kas shell /path/to/kas-project.yml -c 'bitbake dosfsutils-native'
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
kas will place downloads and build artifacts under the current directory when
 | 
			
		||||
being invoked. You can specify a different location via the environment variable
 | 
			
		||||
`KAS_WORK_DIR`.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Use Cases
 | 
			
		||||
---------
 | 
			
		||||
 
 | 
			
		||||
@@ -67,7 +67,6 @@ class Build:
 | 
			
		||||
        macro.add(ReposCheckout())
 | 
			
		||||
        macro.add(SetupEnviron())
 | 
			
		||||
 | 
			
		||||
        # TODO if cfg.has_changed() was not working properly
 | 
			
		||||
        macro.add(WriteConfig())
 | 
			
		||||
 | 
			
		||||
        # Build
 | 
			
		||||
 
 | 
			
		||||
@@ -26,10 +26,8 @@ import logging
 | 
			
		||||
import errno
 | 
			
		||||
import json
 | 
			
		||||
import platform
 | 
			
		||||
import hashlib
 | 
			
		||||
import yaml
 | 
			
		||||
from .repos import Repo
 | 
			
		||||
from .kasstate import KasState
 | 
			
		||||
from .libkas import run_cmd
 | 
			
		||||
 | 
			
		||||
__license__ = 'MIT'
 | 
			
		||||
@@ -38,41 +36,15 @@ __copyright__ = 'Copyright (c) Siemens AG, 2017'
 | 
			
		||||
 | 
			
		||||
class Config:
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        self.state = KasState()
 | 
			
		||||
 | 
			
		||||
    def _get_prop(self, item, default):
 | 
			
		||||
        return self.state.get_option(self.section, item, default)
 | 
			
		||||
 | 
			
		||||
    def _set_prop(self, item, value):
 | 
			
		||||
        self.state.set_option(self.section, item, value)
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def section(self):
 | 
			
		||||
        return os.path.realpath(self.filename)
 | 
			
		||||
        self.__kas_work_dir = os.environ.get('KAS_WORK_DIR', os.getcwd())
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def build_dir(self):
 | 
			
		||||
        return self._get_prop('build_dir', os.getcwd() + '/build')
 | 
			
		||||
 | 
			
		||||
    @build_dir.setter
 | 
			
		||||
    def build_dir(self, value):
 | 
			
		||||
        self._set_prop('build_dir', value)
 | 
			
		||||
        return os.path.join(self.__kas_work_dir, 'build')
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def kas_work_dir(self):
 | 
			
		||||
        return self._get_prop('kas_work_dir', os.getcwd())
 | 
			
		||||
 | 
			
		||||
    @kas_work_dir.setter
 | 
			
		||||
    def kas_work_dir(self, value):
 | 
			
		||||
        self._set_prop('kas_work_dir', value)
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def hash(self):
 | 
			
		||||
        return self._get_prop('config_hash', 0)
 | 
			
		||||
 | 
			
		||||
    @hash.setter
 | 
			
		||||
    def hash(self, value):
 | 
			
		||||
        self._set_prop('config_hash', value)
 | 
			
		||||
        return self.__kas_work_dir
 | 
			
		||||
 | 
			
		||||
    def setup_environ(self):
 | 
			
		||||
        (distro, version, id) = platform.dist()
 | 
			
		||||
@@ -91,9 +63,6 @@ class Config:
 | 
			
		||||
    def get_repo_ref_dir(self):
 | 
			
		||||
        return os.environ.get('KAS_REPO_REF_DIR', None)
 | 
			
		||||
 | 
			
		||||
    def has_changed(self):
 | 
			
		||||
        return self.config_hash != self.hash
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ConfigPython(Config):
 | 
			
		||||
    def __init__(self, filename, target):
 | 
			
		||||
@@ -103,7 +72,6 @@ class ConfigPython(Config):
 | 
			
		||||
            with open(self.filename) as file:
 | 
			
		||||
                env = {}
 | 
			
		||||
                data = file.read()
 | 
			
		||||
                self.hash = hashlib.sha512(str(data).encode('utf-8')).hexdigest()
 | 
			
		||||
                exec(data, env)
 | 
			
		||||
                self._config = env
 | 
			
		||||
        except IOError:
 | 
			
		||||
@@ -291,8 +259,6 @@ class ConfigJson(ConfigStatic):
 | 
			
		||||
        self.filename = os.path.abspath(filename)
 | 
			
		||||
        try:
 | 
			
		||||
            with open(self.filename, 'r') as f:
 | 
			
		||||
                self.hash = hashlib.sha512(f.read().encode('utf-8')).hexdigest()
 | 
			
		||||
                f.seek(0)
 | 
			
		||||
                self._config = json.load(f)
 | 
			
		||||
        except json.decoder.JSONDecodeError  as msg:
 | 
			
		||||
            logging.error('Could not load JSON config: {}'.format(msg))
 | 
			
		||||
@@ -320,8 +286,6 @@ class ConfigYaml(ConfigStatic):
 | 
			
		||||
        self.filename = os.path.abspath(filename)
 | 
			
		||||
        try:
 | 
			
		||||
            with open(self.filename, 'r') as f:
 | 
			
		||||
                self.hash = hashlib.sha512(f.read().encode('utf-8')).hexdigest()
 | 
			
		||||
                f.seek(0)
 | 
			
		||||
                self._config = yaml.load(f)
 | 
			
		||||
        except yaml.loader.ParserError as msg:
 | 
			
		||||
            logging.error('Could not load YAML config: {}'.format(msg))
 | 
			
		||||
 
 | 
			
		||||
@@ -1,57 +0,0 @@
 | 
			
		||||
# kas - setup tool for bitbake based projects
 | 
			
		||||
#
 | 
			
		||||
# Copyright (c) Siemens AG, 2017
 | 
			
		||||
#
 | 
			
		||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
# of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
# in the Software without restriction, including without limitation the rights
 | 
			
		||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
# copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
# furnished to do so, subject to the following conditions:
 | 
			
		||||
#
 | 
			
		||||
# The above copyright notice and this permission notice shall be
 | 
			
		||||
# included in all copies or substantial portions of the Software.
 | 
			
		||||
#
 | 
			
		||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
			
		||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 | 
			
		||||
# SOFTWARE.
 | 
			
		||||
 | 
			
		||||
import codecs
 | 
			
		||||
import os
 | 
			
		||||
from configparser import SafeConfigParser
 | 
			
		||||
 | 
			
		||||
__license__ = 'MIT'
 | 
			
		||||
__copyright__ = 'Copyright (c) Siemens AG, 2017'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class KasState:
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        self.filename = os.path.join(os.environ.get('XDG_CONFIG_HOME',
 | 
			
		||||
                                                    os.path.expandvars('$HOME/.config')),
 | 
			
		||||
                                     os.path.join('kas', 'kasstate.ini'))
 | 
			
		||||
        self.parser = SafeConfigParser()
 | 
			
		||||
        try:
 | 
			
		||||
            with codecs.open(self.filename, 'r', encoding='utf-8') as f:
 | 
			
		||||
                self.parser.readfp(f)
 | 
			
		||||
        except:
 | 
			
		||||
            pass
 | 
			
		||||
 | 
			
		||||
    def __del__(self):
 | 
			
		||||
        if not os.path.exists(os.path.dirname(self.filename)):
 | 
			
		||||
            os.makedirs(os.path.dirname(self.filename))
 | 
			
		||||
        with codecs.open(self.filename, 'w', encoding='utf-8') as f:
 | 
			
		||||
            self.parser.write(f)
 | 
			
		||||
 | 
			
		||||
    def get_option(self, section, option, default):
 | 
			
		||||
        if not self.parser.has_option(section, option):
 | 
			
		||||
            self.set_option(section, option, default)
 | 
			
		||||
        return self.parser.get(section, option)
 | 
			
		||||
 | 
			
		||||
    def set_option(self, section, option, value):
 | 
			
		||||
        if not self.parser.has_section(section):
 | 
			
		||||
            self.parser.add_section(section)
 | 
			
		||||
        self.parser.set(section, option, value)
 | 
			
		||||
		Reference in New Issue
	
	Block a user