home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- from __future__ import with_statement
- from fileutil import cd
- from path import path
- from subprocess import Popen, PIPE
- from threading import RLock
- from util import default_timer
- import ctypes
- import logging
- import os
- import time
- import traceback
- import sys
- log = logging.getLogger('research')
- DEFAULT_IDLE_MINUTES = 5
- DEFAULT_REVIVE_INTERVAL = 60 * 60
-
- def REVIVE_INTERVAL():
- pref = pref
- import common
- return pref('research.revive_interval_seconds', default = DEFAULT_REVIVE_INTERVAL, type = int)
-
-
- def MAX_PERCENT():
- return 0.75
-
- DEFAULT_CPU_PERCENT = 0.75
- DEFAULT_BANDWIDTH_PERCENT = 0.75
- STARTED = True
- FAILED_TO_START = False
- ALREADY_RUNNING = 'ALREADY_RUNNING'
- KEY_WOW64_64KEY = 256
- DEBUG_PLURA = False
- KIBIBYTE = 2 ** 10
- MEBIBYTE = 2 ** 20
- GIBIBYTE = 2 ** 30
- PERMGEN_SPACE = 32 * MEBIBYTE
- MEMORY_LIMIT_BUFFER = 1.2
-
- def expected_memory(size):
- return (size + PERMGEN_SPACE) * MEMORY_LIMIT_BUFFER
-
-
- def find_java_home():
- k = None
- k2 = None
- if not sys.platform.startswith('win'):
- if 'JAVA_HOME' in os.environ:
- path = os.environ['JAVA_HOME']
- if os.path.exists(path):
- return path
- else:
- return '/usr'
- 'JAVA_HOME' in os.environ
-
- try:
- import _winreg
- except ImportError:
- return False
- else:
-
- try:
-
- try:
- k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\JavaSoft\\Java Runtime Environment\\\\', 0, _winreg.KEY_READ | KEY_WOW64_64KEY)
- except Exception:
- k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\JavaSoft\\Java Runtime Environment\\\\', 0, _winreg.KEY_READ)
-
- (ver, tp) = _winreg.QueryValueEx(k, 'CurrentVersion')
- except Exception:
- return False
-
- if tp != _winreg.REG_SZ:
- return False
-
- try:
- if float(ver[:3]) <= 1.5:
- return False
- except ValueError:
- tp != _winreg.REG_SZ
- tp != _winreg.REG_SZ
- return False
-
-
- try:
- k2 = _winreg.OpenKey(k, ver, 0, _winreg.KEY_READ)
- (java_home, tp) = _winreg.QueryValueEx(k2, 'JavaHome')
- except Exception:
- tp != _winreg.REG_SZ
- tp != _winreg.REG_SZ
- return False
-
- if tp != _winreg.REG_SZ:
- return False
- return java_home
- if k2 is not None:
- k2.Close()
-
-
- if k is not None:
- k.Close()
-
-
-
- def java_exe(java_home):
-
- def exe(filename):
- if sys.platform.startswith('win'):
- return filename + '.exe'
- return filename
-
- java = path(java_home) / 'bin' / exe('java')
- javaw = path(java_home) / 'bin' / exe('javaw')
- if javaw.isfile():
- return javaw
- return java
-
- IDLE_PRIORITY_CLASS = 64
-
- class Driver(object):
- profile = None
- instance = None
- lock = RLock()
- numstarts = 0
- numfounddead = 0
- dead_at = 0
- dead = False
-
- def __init__(self):
- pass
-
-
- def res_path(cls):
- pth = path(__file__).parent
- return pth / 'res'
-
- res_path = classmethod(res_path)
-
- def jar_path(cls):
- return cls.res_path() / 'process.jar'
-
- jar_path = classmethod(jar_path)
-
- def jar2_path(cls):
- return cls.res_path() / 'security.jar'
-
- jar2_path = classmethod(jar2_path)
-
- def policy_path(cls):
- return cls.res_path() / 'java.policy'
-
- policy_path = classmethod(policy_path)
-
- def java_path(cls):
- return java_exe(find_java_home())
-
- java_path = classmethod(java_path)
-
- def username(cls):
- uname = getattr(getattr(cls, 'profile', None), 'username', None)
- if uname:
- to_hex = to_hex
- string_xor = string_xor
- import util.primitives.strings
- uname = to_hex(string_xor(x = 'digsbygrid', y = uname.encode('utf-8'), adjustx = True), '')
- return [
- '-u',
- uname]
- return []
-
- username = classmethod(username)
-
- def chassis_desired_cpuUsage(cls):
-
- try:
- chassis_types = getattr(cls, 'chassis_types', None)
- if chassis_types is None:
- if sys.platform.startswith('win'):
- import comtypes.client as comtypes
- wmi = comtypes.client.CoGetObject('winmgmts:')
- encs = wmi.InstancesOf('Win32_SystemEnclosure')
- enc = list(encs)[0]
- cls.chassis_types = chassis_types = enc.Properties_(strName = 'ChassisTypes').Value
- log.info('chassis types (success): %r', cls.chassis_types)
- else:
- log.warning('hardcoding chassis type for this platform')
- cls.chassis_types = chassis_types = [
- 2]
-
- rpm = rpm
- import engine
- speeds = [ rpm.get(chassis, DEFAULT_CPU_PERCENT) for chassis in chassis_types ] + [
- MAX_PERCENT()]
- lowest = min(speeds)
- if lowest != sys.maxint:
- return lowest
- return min(DEFAULT_CPU_PERCENT, MAX_PERCENT())
- except Exception:
- traceback.print_exc()
- cls.chassis_types = [
- 2]
- log.info('chassis types (fail): %r', cls.chassis_types)
- return min(DEFAULT_CPU_PERCENT, MAX_PERCENT())
-
-
- chassis_desired_cpuUsage = classmethod(chassis_desired_cpuUsage)
-
- def cpuUsage(cls):
- default = cls.chassis_desired_cpuUsage()
-
- try:
- return get_cpu_percent(default) / 100
- except ImportError:
- pass
-
- return default
-
- cpuUsage = classmethod(cpuUsage)
-
- def cpuUsageStr(cls):
- return [
- '-p',
- str(int(cls.cpuUsage() * 1000))]
-
- cpuUsageStr = classmethod(cpuUsageStr)
-
- def numCpus(cls):
- return []
-
- numCpus = classmethod(numCpus)
-
- def bandwidthUsage(cls):
- default = DEFAULT_BANDWIDTH_PERCENT
-
- try:
- pref = pref
- import common
- percent = pref('research.bandwidth_percent', default, type = float)
- if percent >= 0 and percent <= 100:
- percent = percent / 100
-
- if percent >= 0:
- return percent
- except ImportError:
- pass
-
- return default
-
- bandwidthUsage = classmethod(bandwidthUsage)
-
- def bandwidthUsageStr(cls):
- return [
- '-b',
- str(int(cls.bandwidthUsage() * 1000))]
-
- bandwidthUsageStr = classmethod(bandwidthUsageStr)
-
- def memoryLimits(cls):
- if not sys.platform.startswith('win'):
- return [
- '-Xmx512m']
-
- try:
- import gui.native.sysinfo as gui
- s = gui.native.sysinfo.SystemInformation()
- ram = s._ram()
- except Exception:
- sys.platform.startswith('win')
- sys.platform.startswith('win')
- return [
- '-Xmx256m']
-
- AvailPhys = ram.get('ullAvailPhys')
- TotalPhys = ram.get('ullTotalPhys')
- if AvailPhys >= expected_memory(GIBIBYTE) and TotalPhys >= 2 * GIBIBYTE:
- return [
- '-Xmx1024m']
- if AvailPhys >= expected_memory(512 * MEBIBYTE) and TotalPhys >= GIBIBYTE:
- return [
- '-Xmx512m']
- if AvailPhys >= expected_memory(256 * MEBIBYTE):
- return [
- '-Xmx256m']
- if TotalPhys >= GIBIBYTE:
- return [
- '-Xmx256m']
- return [
- '-Xmx128m']
-
- memoryLimits = classmethod(memoryLimits)
-
- def commandline(cls):
- return [
- str(cls.java_path()),
- '-Djava.security.manager=com.pluraprocessing.node.security.PluraSecurityManager',
- '-Djava.security.policy=' + str(cls.policy_path()) + ''] + cls.memoryLimits() + [
- '-jar',
- str(cls.jar_path())] + cls.username() + cls.cpuUsageStr() + cls.bandwidthUsageStr()
-
- commandline = classmethod(commandline)
-
- def running(cls):
- cls.lock.__enter__()
-
- try:
- proc = getattr(cls, 'proc', None)
- if proc is not None:
- pass
- return proc.poll() is None
- finally:
- pass
-
-
- running = classmethod(running)
-
- def set_dead(cls):
- cls.dead = True
- cls.dead_at = default_timer()
-
- set_dead = classmethod(set_dead)
-
- def is_dead(cls):
- if cls.dead:
- revive_at = cls.dead_at + REVIVE_INTERVAL()
- now = default_timer()
- if now > revive_at or now < cls.dead_at:
- cls.dead = False
- cls.numfounddead = 0
- cls.dead_at = 0
- return False
- return True
- return False
-
- is_dead = classmethod(is_dead)
-
- def start(cls, profile):
- cls.lock.__enter__()
-
- try:
- if cls.numfounddead > 10:
- return FAILED_TO_START
- if cls.running():
- return ALREADY_RUNNING
- if getattr(cls, 'proc', None) is not None:
- if cls.numfounddead == 10:
- log.info("found process dead 10 times, won't try again for a while")
- cls.numfounddead += 1
- cls.set_dead()
- cls.stop()
- return FAILED_TO_START
- cls.numfounddead += 1
- cls.stop()
- return cls.start(profile)
- cls.profile = profile
- java_home = find_java_home()
- if not java_home:
- log.info('java home not found, will not try starting again for a while')
- cls.set_dead()
- return FAILED_TO_START
- java = java_exe(java_home)
- if not java.isfile():
- log.info('java not found, will not try starting again for a while')
- cls.set_dead()
- return FAILED_TO_START
- desired_cpu = cls.cpuUsage()
- if desired_cpu <= 0 or desired_cpu > 1:
- log.info('cpuUsage was not in valid range (was: %r), will not try starting again for a while', desired_cpu)
- cls.set_dead()
- return FAILED_TO_START
- import subprocess
- startupinfo = subprocess.STARTUPINFO()
- startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
- startupinfo.wShowWindow = subprocess.SW_HIDE
- cmdline = cls.commandline()
- log.info('running: %s %r', cmdline, cls.numstarts)
- pref = pref
- import common
- use_pipes = not pref('research.debug_output', type = bool, default = DEBUG_PLURA)
-
- try:
- cd(cls.res_path()).__enter__()
-
- try:
- pass
- finally:
- pass
-
- except Exception:
- startupinfo
- startupinfo
- desired_cpu > 1
- log.info('failed Popen')
- traceback.print_exc()
- cls.numfounddead += 1
- return FAILED_TO_START
- cls
-
-
- try:
- if use_pipes:
- proc.stderr.close()
- proc.stdout.close()
- except Exception:
- startupinfo
- startupinfo
- desired_cpu > 1
- log.error('error closing stderr or stdout')
- traceback.print_exc()
- cls.kill()
- return FAILED_TO_START
- java.isfile()
-
- log.debug('successfully started')
-
- try:
- import hooks
- hooks.notify('digsby.research.started')
- except Exception:
- startupinfo
- startupinfo
- desired_cpu > 1
- traceback.print_exc()
- except:
- java.isfile()
-
- cls.numstarts += 1
- return STARTED
- finally:
- pass
-
-
- start = classmethod(start)
-
- def kill(cls):
- cls.lock.__enter__()
-
- try:
- start = time.clock()
- proc = getattr(cls, 'proc', None)
- end = time.clock()
- return end - start
- finally:
- pass
-
-
- kill = classmethod(kill)
- stop = kill
- _start_time = 0
-
-
- def get_process_runtime_secs(handle):
- f = ctypes.wintypes.FILETIME()
- ctypes.windll.kernel32.GetSystemTimeAsFileTime(ctypes.byref(f))
- (c, e, k, u) = (ctypes.wintypes.FILETIME(), ctypes.wintypes.FILETIME(), ctypes.wintypes.FILETIME(), ctypes.wintypes.FILETIME())
- ctypes.windll.kernel32.GetProcessTimes(handle, ctypes.byref(c), ctypes.byref(e), ctypes.byref(k), ctypes.byref(u))
- seconds_run = ((f.dwHighDateTime << 32) + f.dwLowDateTime - ((c.dwHighDateTime << 32) + c.dwLowDateTime)) / (1 * 10000000)
- return seconds_run
-
-
- def test_conditions(useridlems):
- pref = pref
- import common
- idle_time = pref('research.idle_time_ms', default = pref('research.idle_time_min', default = DEFAULT_IDLE_MINUTES, type = int) * 60 * 1000, type = int)
- if useridlems < idle_time:
- if not pref('research.always_on', default = False):
- return False
-
- import wx
- if wx.GetPowerType() != wx.POWER_SOCKET and not pref('research.battery_override', False):
- return False
- return bool(get_research_pref())
-
-
- def get_research_pref():
- import common
- return common.profile.localprefs['research.enabled']
-
-
- def set_research_pref(val):
- import common
- common.profile.localprefs['research.enabled'] = val
-
-
- def get_cpu_percent(chassis_percent = None):
- if chassis_percent is None:
- chassis_percent = Driver.chassis_desired_cpuUsage()
-
- import common
-
- try:
- return int(common.profile.localprefs['research.cpu_percent'])
- except (KeyError, AttributeError):
- _e = None
- chassis_percent = float(chassis_percent)
- chassis_percent *= 100
-
- try:
- if chassis_percent < common.profile.defaultprefs.get('research.cpu_percent', chassis_percent):
- return min(chassis_percent, common.pref('research.cpu_percent', chassis_percent, type = float))
- return common.pref('research.cpu_percent', chassis_percent, type = float)
- except AttributeError:
- _e2 = None
- return common.pref('research.cpu_percent', chassis_percent, type = float)
-
-
- None<EXCEPTION MATCH>AttributeError
-
-
-
- def get_bandwidth_percent():
- import common
-
- try:
- return common.profile.localprefs['research.bandwidth_percent']
- except KeyError:
- return common.pref('research.bandwidth_percent', DEFAULT_BANDWIDTH_PERCENT, type = float)
-
-
-
- def control(useridlems):
- if Driver.is_dead():
- return 2000
- if not test_conditions(useridlems):
-
- try:
- Driver.stop()
- except Exception:
- Driver.is_dead()
- Driver.is_dead()
- traceback.print_exc()
- except:
- Driver.is_dead()
-
- if Driver._start_time != 0:
- log.debug('tried to run for %s', default_timer() - Driver._start_time)
-
- Driver._start_time = 0
- else:
-
- try:
- profile = profile
- import common
- ret = Driver.start(profile)
- except Exception:
- Driver.is_dead()
- Driver.is_dead()
- traceback.print_exc()
- except:
- Driver.is_dead()
-
- if ret == STARTED:
- Driver._start_time = default_timer()
- elif ret == FAILED_TO_START:
- Driver._start_time = 0
- elif ret == ALREADY_RUNNING:
- return 500
- return 500
- return test_conditions(useridlems)
-
-
- def check_cli_args(*a):
- import sys
- set_val = sys.opts.set_plura_option
- if set_val is None:
- return None
- log.info('setting intial research pref to %r', set_val)
- set_research_pref(set_val)
-
- inited = False
-
- def init(*a, **k):
- global inited
- if not inited:
- inited = True
- import atexit
- atexit.register(Driver.kill)
-
-
-