From d3b508c3a12db2296542da37bdf40768424cc337 Mon Sep 17 00:00:00 2001 From: cuu Date: Fri, 26 Apr 2019 14:01:36 +0800 Subject: [PATCH] fix fds leak and remove SkinMap --- sys.py/UI/fonts.py | 2 +- sys.py/UI/foot_bar.py | 6 ++++-- sys.py/UI/icon_pool.py | 4 ++-- sys.py/UI/main_screen.py | 10 ++++----- sys.py/UI/skin_manager.py | 25 ++++++++++++++++++++-- sys.py/UI/title_bar.py | 5 +++-- sys.py/UI/untitled_icon.py | 4 ++-- sys.py/UI/util_funcs.py | 21 ------------------- sys.py/config.py | 2 +- sys.py/run.py | 43 +++++++++++++++++++++++++++++++++++++- 10 files changed, 83 insertions(+), 39 deletions(-) diff --git a/sys.py/UI/fonts.py b/sys.py/UI/fonts.py index 13903e1..20c4926 100644 --- a/sys.py/UI/fonts.py +++ b/sys.py/UI/fonts.py @@ -13,7 +13,7 @@ if not pygame.font.get_init(): fonts = {} if not fonts: - skinpath = "../skin/"+config.SKIN+"/truetype" + skinpath = config.SKIN+"/truetype" fonts_path = {} fonts_path["varela"] = "%s/VarelaRound-Regular.ttf" % skinpath diff --git a/sys.py/UI/foot_bar.py b/sys.py/UI/foot_bar.py index 2e80e5e..d908e64 100644 --- a/sys.py/UI/foot_bar.py +++ b/sys.py/UI/foot_bar.py @@ -6,7 +6,7 @@ import os ##local import from constants import Width,Height,ICON_TYPES,ALIGN -from util_funcs import FileExists,midRect,SkinMap +from util_funcs import FileExists,midRect from icon_item import IconItem from fonts import fonts from multi_icon_item import MultiIconItem @@ -14,7 +14,9 @@ from icon_pool import MyIconPool from libs.roundrects import aa_round_rect from lang_manager import MyLangManager from widget import Widget -icon_base_path = SkinMap("gameshell/footbar_icons/") +from skin_manager import MySkinManager + +icon_base_path = MySkinManager.GiveIcon("gameshell/footbar_icons/") class FootBarIcon(MultiIconItem): diff --git a/sys.py/UI/icon_pool.py b/sys.py/UI/icon_pool.py index dc04b1c..256a6af 100644 --- a/sys.py/UI/icon_pool.py +++ b/sys.py/UI/icon_pool.py @@ -6,12 +6,12 @@ from sys import exit import os import sys -from util_funcs import SkinMap +from skin_manager import MySkinManager ##pool only store surfaces class IconPool(object): - _GameShellIconPath = SkinMap("gameshell/icons/") + _GameShellIconPath = MySkinManager.GiveIcon("gameshell/icons/") _Icons = {} _Sizes = {} def __init__(self): diff --git a/sys.py/UI/main_screen.py b/sys.py/UI/main_screen.py index ffb96bc..50c6973 100644 --- a/sys.py/UI/main_screen.py +++ b/sys.py/UI/main_screen.py @@ -20,7 +20,7 @@ from page import Page,PageStack from title_bar import TitleBar from foot_bar import FootBar from constants import Width,Height,bg_color -from util_funcs import midRect,FileExists,ReplaceSuffix,ReadTheFileContent,CmdClean,MakeExecutable,SkinMap +from util_funcs import midRect,FileExists,ReplaceSuffix,ReadTheFileContent,CmdClean,MakeExecutable from keys_def import CurKeys from label import Label from untitled_icon import UntitledIcon @@ -469,8 +469,8 @@ class MainScreen(Widget): iconitem.AddLabel(MyLangManager.Tr(i2),self._IconFont) if FileExists( _dir+"/"+i+"/"+i2+".png"): ### 20_Prog/Prog.png , cut 20_ iconitem._ImageName = _dir+"/"+i+"/"+i2+".png" - elif FileExists( SkinMap(_dir+"/"+i2+".png") ): - iconitem._ImageName = SkinMap(_dir+"/"+i2+".png") + elif FileExists( MySkinManager.GiveIcon(_dir+"/"+i2+".png") ): + iconitem._ImageName = MySkinManager.GiveIcon(_dir+"/"+i2+".png") else: untitled = UntitledIcon() untitled.Init() @@ -577,8 +577,8 @@ class MainScreen(Widget): iconitem._CmdPath = os.path.realpath(_dir+"/"+i) MakeExecutable(iconitem._CmdPath) iconitem._MyType = ICON_TYPES["EXE"] - if FileExists( SkinMap( _dir+"/"+ReplaceSuffix(i2,"png"))): - iconitem._ImageName = SkinMap(_dir+"/"+ReplaceSuffix(i2,"png")) + if FileExists( MySkinManager.GiveIcon( _dir+"/"+ReplaceSuffix(i2,"png"))): + iconitem._ImageName = MySkinManager.GiveIcon(_dir+"/"+ReplaceSuffix(i2,"png")) else: untitled = UntitledIcon() untitled.Init() diff --git a/sys.py/UI/skin_manager.py b/sys.py/UI/skin_manager.py index 1deee6a..9cb2fc9 100644 --- a/sys.py/UI/skin_manager.py +++ b/sys.py/UI/skin_manager.py @@ -4,6 +4,8 @@ import pygame import config import ConfigParser +from util_funcs import FileExists + class CaseConfigParser(ConfigParser.SafeConfigParser): def optionxform(self, optionstr): return optionstr @@ -21,7 +23,8 @@ class SkinManager(object): _Colors = {} _Config = None - + DefaultSkin = "../skin/default" + def __init__(self): self.Init() @@ -52,7 +55,7 @@ class SkinManager(object): self._Config = CaseConfigParser() - fname = "../skin/"+config.SKIN+"/config.cfg" + fname = config.SKIN+"/config.ini" try: self._Config.read(fname) @@ -78,6 +81,24 @@ class SkinManager(object): else: return pygame.Color(255,0,0) + def GiveIcon(self,orig_file_or_dir): + #doing a wrapper for items under /home/cpi/apps/Menu/*, to be like Menu/GameShell/* + if orig_file_or_dir.startswith("/home/cpi/apps/Menu"): + orig_file_or_dir = orig_file_or_dir.replace("/home/cpi/apps/Menu/","../Menu/GameShell/") + + if orig_file_or_dir.startswith(".."): + ret = orig_file_or_dir.replace("..",config.SKIN) + if FileExists(ret) == False: + ret = orig_file_or_dir.replace("..",self.DefaultSkin) + else: + ret = config.SKIN+"/sys.py/"+orig_file_or_dir + if FileExists(ret) == False: + ret = self.DefaultSkin+"/sys.py/"+orig_file_or_dir + + if FileExists( ret ): + return ret + else: ## if not existed both in default or custom skin ,return where it is + return orig_file_or_dir ##global MySkinManager Handler MySkinManager = None diff --git a/sys.py/UI/title_bar.py b/sys.py/UI/title_bar.py index 18a0514..28f88ff 100644 --- a/sys.py/UI/title_bar.py +++ b/sys.py/UI/title_bar.py @@ -18,7 +18,8 @@ from icon_item import IconItem from multi_icon_item import MultiIconItem from icon_pool import MyIconPool from lang_manager import MyLangManager -from util_funcs import midRect,SwapAndShow,SkinMap +from util_funcs import midRect,SwapAndShow +from skin_manager import MySkinManager from widget import Widget from config import Battery @@ -26,7 +27,7 @@ from libs.roundrects import aa_round_rect from libs.DBUS import is_wifi_connected_now,wifi_strength -icon_base_path = SkinMap("gameshell/titlebar_icons/") +icon_base_path = MySkinManager.GiveIcon("gameshell/titlebar_icons/") class TitleBar(Widget): _Width = Width _Height = 25 diff --git a/sys.py/UI/untitled_icon.py b/sys.py/UI/untitled_icon.py index c7f2b70..7fae615 100644 --- a/sys.py/UI/untitled_icon.py +++ b/sys.py/UI/untitled_icon.py @@ -11,12 +11,12 @@ from datetime import datetime import base64 from beeprint import pp -from util_funcs import midRect, SkinMap +from util_funcs import midRect from fonts import fonts from skin_manager import MySkinManager -BlankPng = SkinMap("gameshell/blank.png") # 80x80 +BlankPng = MySkinManager.GiveIcon("gameshell/blank.png") # 80x80 ## use blank circle as bg, Two alpha As Icon Label #Upper and Lower diff --git a/sys.py/UI/util_funcs.py b/sys.py/UI/util_funcs.py index 68262bf..3ca4f10 100644 --- a/sys.py/UI/util_funcs.py +++ b/sys.py/UI/util_funcs.py @@ -13,27 +13,6 @@ import string from Xlib import X,display import config -def SkinMap(orig_file_or_dir): - DefaultSkin = "default" - - #doing a wrapper for items under /home/cpi/apps/Menu/*, to be like Menu/GameShell/* - if orig_file_or_dir.startswith("/home/cpi/apps/Menu"): - orig_file_or_dir = orig_file_or_dir.replace("/home/cpi/apps/Menu/","../Menu/GameShell/") - - if orig_file_or_dir.startswith(".."): - ret = orig_file_or_dir.replace("..","../skin/"+config.SKIN) - if FileExists(ret) == False: - ret = orig_file_or_dir.replace("..","../skin/"+DefaultSkin) - else: - ret = "../skin/"+config.SKIN+"/sys.py/"+orig_file_or_dir - if FileExists(ret) == False: - ret = "../skin/"+DefaultSkin+"/sys.py/"+orig_file_or_dir - - if FileExists( ret ): - return ret - else: ## if not existed both in default or custom skin ,return where it is - return orig_file_or_dir - def get_git_revision_hash(): return subprocess.check_output(['git', 'rev-parse', 'HEAD']) diff --git a/sys.py/config.py b/sys.py/config.py index e5184a3..4225c11 100644 --- a/sys.py/config.py +++ b/sys.py/config.py @@ -14,7 +14,7 @@ UPDATE_URL="https://raw.githubusercontent.com/clockworkpi/CPI/master/launcher_ve VERSION="stable 1.24" -SKIN="default" +SKIN="../skin/default" ## three timer values in seconds: dim screen, close screen,PowerOff ## zero means no action diff --git a/sys.py/run.py b/sys.py/run.py index f2143a8..d53adaa 100644 --- a/sys.py/run.py +++ b/sys.py/run.py @@ -4,6 +4,9 @@ import dbus import dbus.service import sys import commands +import logging +import errno + from wicd import misc ##misc.to_bool ##misc.misc.noneToString @@ -288,7 +291,36 @@ def RecordKeyDns(thekey,main_screen): return True return False + + + +def release_self_fds(): + fds_flags= ["pipe","socket",".ttf"] + """List process currently open FDs and their target """ + if sys.platform != 'linux2': + raise NotImplementedError('Unsupported platform: %s' % sys.platform) + + ret = {} + base = '/proc/self/fd' + for num in os.listdir(base): + path = None + try: + path = os.readlink(os.path.join(base, num)) + except OSError as err: + # Last FD is always the "listdir" one (which may be closed) + if err.errno != errno.ENOENT: + raise + ret[int(num)] = path + for key in ret: + if ret[key] != None and isinstance(ret[key], str): + for i in fds_flags: + if i in ret[key]: + os.close(key) + break + return ret + + def event_process(event,main_screen): global sound_patch global everytime_keydown @@ -316,6 +348,7 @@ def event_process(event,main_screen): exec_app_cmd += event.message exec_app_cmd += "; sync & cd "+GetExePath()+"; exec python "+myscriptname print(exec_app_cmd) + release_self_fds() os.execlp("/bin/sh","/bin/sh","-c", exec_app_cmd) os.chdir( GetExePath()) os.exelp("python","python"," "+myscriptname) @@ -334,6 +367,7 @@ def event_process(event,main_screen): exec_app_cmd += event.message exec_app_cmd += "; sync & cd "+GetExePath()+"; exec python "+myscriptname print(exec_app_cmd) + release_self_fds() os.execlp("/bin/sh","/bin/sh","-c", exec_app_cmd) os.chdir( GetExePath()) os.exelp("python","python"," "+myscriptname) @@ -541,7 +575,14 @@ def PreparationInAdv(): if "arm" not in platform.machine(): return - + + if FileExists("%s/.gameshell_skin" % os.path.expanduser('~')) == True: + with open("%s/.gameshell_skin" % os.path.expanduser('~'),"r") as f: + gameshell_skin = f.read() + + gameshell_skin = gameshell_skin.strip() + config.SKIN= gameshell_skin + if FileExists(".powerlevel") == False: os.system("touch .powerlevel")