Skip to content

Commit

Permalink
[tools] pyconfig 增加 silent 模式,不显示窗口但可以更新 .config 和 rtconfig.h
Browse files Browse the repository at this point in the history
Signed-off-by: MurphyZhao <[email protected]>
  • Loading branch information
murphyzhao committed Dec 26, 2018
1 parent 2befae8 commit 2d19eaf
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 10 deletions.
16 changes: 14 additions & 2 deletions tools/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def GenCconfigFile(env, BuildOptions):
f = open('cconfig.h', 'r')
if f:
contents = f.read()
f.close();
f.close()

prep = PatchedPreProcessor()
prep.process_contents(contents)
Expand Down Expand Up @@ -349,8 +349,20 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
action = 'store_true',
default = False,
help = 'make menuconfig for RT-Thread BSP')
if GetOption('pyconfig'):
AddOption('--pyconfig-silent',
dest = 'pyconfig_silent',
action = 'store_true',
default = False,
help = 'Don`t show pyconfig window')

if GetOption('pyconfig_silent'):
from menuconfig import pyconfig_silent

pyconfig_silent(Rtt_Root)
exit(0)
elif GetOption('pyconfig'):
from menuconfig import pyconfig

pyconfig(Rtt_Root)
exit(0)

Expand Down
28 changes: 28 additions & 0 deletions tools/menuconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,31 @@ def pyconfig(RTT_ROOT):
if mtime != mtime2:
mk_rtconfig(fn)


# pyconfig_silent for windows and linux
def pyconfig_silent(RTT_ROOT):
import pymenuconfig
print("In pyconfig silent mode. Don`t display menuconfig window.")

touch_env()
env_dir = get_env_dir()

os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')

fn = '.config'

if os.path.isfile(fn):
mtime = os.path.getmtime(fn)
else:
mtime = -1

pymenuconfig.main(['--kconfig', 'Kconfig', '--config', '.config', '--silent', 'True'])

if os.path.isfile(fn):
mtime2 = os.path.getmtime(fn)
else:
mtime2 = -1

# make rtconfig.h
if mtime != mtime2:
mk_rtconfig(fn)
49 changes: 41 additions & 8 deletions tools/pymenuconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,11 @@ class MenuConfig(object):
('Save as', ACTION_SAVE_AS),
)

def __init__(self, kconfig):
def __init__(self, kconfig, __silent=None):
self.kconfig = kconfig
self.__silent = __silent
if self.__silent is True:
return

# Instantiate Tk widgets
self.root = tk.Tk()
Expand Down Expand Up @@ -728,6 +731,8 @@ def handle_keypress(self, ev):
def _close_window(self):
if self.prevent_losing_changes():
print('Exiting..')
if self.__silent is True:
return
self.root.destroy()

def _action_exit(self):
Expand Down Expand Up @@ -949,6 +954,8 @@ def update_status(self):
- current config path
- status string (see set_status_string())
"""
if self.__silent is True:
return
self.tk_status.set('{} [{}] {}'.format(
'<UNSAVED>' if self.unsaved_changes else '',
self.config_path if self.config_path else '',
Expand Down Expand Up @@ -1017,6 +1024,10 @@ def prevent_losing_changes(self):
self.mark_as_changed()
if not self.unsaved_changes:
return True

if self.__silent:
saved = self.save_config()
return saved
res = messagebox.askyesnocancel(
parent=self.root,
title='Unsaved changes',
Expand Down Expand Up @@ -1056,11 +1067,13 @@ def open_config(self, path=None):
self.kconfig.load_config(path)
except IOError as e:
self.set_status_string('Failed to load: \'{}\''.format(path))
self.refresh_display()
if not self.__silent:
self.refresh_display()
print('Failed to load config \'{}\': {}'.format(path, e))
return False
self.set_status_string('Opened config')
self.refresh_display()
if not self.__silent:
self.refresh_display()
return True

def save_config(self, force_file_dialog=False):
Expand Down Expand Up @@ -1154,19 +1167,39 @@ def main(argv=None):
type=str,
help='path to .config file to load'
)
if "--silent" in argv:
parser.add_argument(
'--silent',
dest = '_silent_',
type=str,
help='silent mode, not show window'
)
args = parser.parse_args(argv)
kconfig_path = args.kconfig
config_path = args.config
# Verify that Kconfig file exists
if not os.path.isfile(kconfig_path):
raise RuntimeError('\'{}\': no such file'.format(kconfig_path))

# Parse Kconfig files
kconf = kconfiglib.Kconfig(filename=kconfig_path)
mc = MenuConfig(kconf)
# If config file was specified, load it
if config_path:
mc.open_config(config_path)
tk.mainloop()

if "--silent" not in argv:
print("In normal mode. Will show menuconfig window.")
mc = MenuConfig(kconf)
# If config file was specified, load it
if config_path:
mc.open_config(config_path)

print("Enter mainloop. Waiting...")
tk.mainloop()
else:
print("In silent mode. Don`t show menuconfig window.")
mc = MenuConfig(kconf, True)
# If config file was specified, load it
if config_path:
mc.open_config(config_path)
mc._close_window()


if __name__ == '__main__':
Expand Down

0 comments on commit 2d19eaf

Please sign in to comment.