From faab7937ce1333dc3381b2599f2dfaaf948b03e1 Mon Sep 17 00:00:00 2001 From: Grigori Fursin Date: Fri, 13 Sep 2019 11:33:34 +0200 Subject: [PATCH] added CK kernel functions save_state(), restore_state, reinit to let multiple CK instances co-exist --- CHANGES.txt | 2 ++ ck/kernel.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 26ef5b6a82..b9edd1dde4 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -18,6 +18,8 @@ "index_modules":[list of module UIDs and aliases] * removed AUTHORS.txt as non-standard (leaving CONTRIBUTIONS.txt) * added possibility to skip SSL certificate check for remote repositories + * added CK kernel functions save_state(), restore_state, reinit + to let multiple CK instances co-exist * V1.10.3 * fixed major bug with kernel version checking diff --git a/ck/kernel.py b/ck/kernel.py index 3a0787c2e0..af57270b86 100755 --- a/ck/kernel.py +++ b/ck/kernel.py @@ -348,6 +348,90 @@ type_long=None # In Python 3 -> int, in Python 2 -> long string_io=None # StringIO, which is imported differently in Python 2 and 3 +############################################################################## +# Save CK state +# +# TARGET: end users + +def save_state(): + """ + Input: None + + Output: dict with state + """ + + import copy + import os + + r={} + + r['cfg']=copy.deepcopy(cfg) + r['paths_repos']=copy.deepcopy(paths_repos) + + r['cache_repo_init']=cache_repo_init + r['paths_repos_all']=copy.deepcopy(paths_repos_all) + r['cache_repo_uoa']=copy.deepcopy(cache_repo_uoa) + r['cache_repo_info']=copy.deepcopy(cache_repo_info) + + r['os.environ']=copy.deepcopy(os.environ) + + return r + +############################################################################## +# Restore CK state +# +# TARGET: end users + +def restore_state(r): + """ + Input: dict with state + + Output: output from "init" function + """ + + global initialized, cfg, paths_repos, cache_repo_init, paths_repos_all, cache_repo_uoa, cache_repo_info + + import copy + import os + + cfg=r['cfg'] + paths_repos=r['paths_repos'] + + cache_repo_init=r['cache_repo_init'] + paths_repos_all=r['paths_repos_all'] + cache_repo_uoa=r['cache_repo_uoa'] + cache_repo_info=r['cache_repo_info'] + + os.environ=r['os.environ'] + + initialized=False + + return init({}) + +############################################################################## +# Reinitialize CK +# +# TARGET: end users + +def reinit(): + """ + Input: None + + Output: output from "init" function + """ + + global initialized, paths_repos, cache_repo_init, paths_repos_all, cache_repo_uoa, cache_repo_info + + initialized=False + paths_repos=[] + + cache_repo_init=False + paths_repos_all=[] + cache_repo_uoa={} + cache_repo_info={} + + return init({}) + ############################################################################## # Universal print of unicode string in utf8 that supports Python 2.x and 3.x # @@ -5529,12 +5613,13 @@ def short_help(i): h+='\nPython version used by CK: '+r['version'].replace('\n','\n ')+'\n' - h+='\nPath to the default repo: '+work['dir_default_repo_path']+'\n' - h+= 'Path to CK repositories: '+work['dir_repos']+'\n' + h+='\nPath to the default repo: '+work['dir_default_repo']+'\n' + h+= 'Path to the local repo: '+work['dir_local_repo']+'\n' + h+= 'Path to CK repositories: '+work['dir_repos']+'\n' - h+='\n'+cfg['help_web'].replace('\n','').strip().replace(' ','')+'\n' + h+='\n'+cfg['help_web'].replace('\n','').strip()+'\n' #.replace(' ','')+'\n' - h+='CK Google group: https://bit.ly/ck-google-group\n' + h+='CK Google group: https://bit.ly/ck-google-group\n' h+='CK Slack channel: https://bit.ly/ck-slack' if o=='con':