diff --git a/CouchPotato.py b/CouchPotato.py old mode 100755 new mode 100644 index 45f44cf..a07f235 --- a/CouchPotato.py +++ b/CouchPotato.py @@ -1,72 +1,167 @@ -#!/usr/bin/env python -from os.path import dirname -from signal import signal, SIGTERM +from threading import Thread +from wx.lib.softwareupdate import SoftwareUpdate import os -import subprocess import sys -import time +import webbrowser +import wx -# Root path -base_path = dirname(os.path.abspath(__file__)) +# Include proper dirs +if hasattr(sys, 'frozen'): + import libs + base_path = os.path.dirname(os.path.dirname(os.path.abspath(libs.__file__))) + print base_path +else: + base_path = os.path.dirname(os.path.abspath(__file__)) -# Insert local directories into path -sys.path.insert(0, os.path.join(base_path, 'libs')) +lib_dir = os.path.join(base_path, 'libs') -from couchpotato.core.logger import CPLog -log = CPLog(__name__) +sys.path.insert(0, base_path) +sys.path.insert(0, lib_dir) # Get options via arg from couchpotato.runner import getOptions -from couchpotato.core.helpers.variable import getDataDir -options = getOptions(base_path, sys.argv[1:]) -data_dir = getDataDir() - -def start(): - try: - args = [sys.executable] + [os.path.join(base_path, __file__)] + sys.argv[1:] - new_environ = os.environ.copy() - new_environ['cp_main'] = 'true' - - if os.name == 'nt': - for key, value in new_environ.iteritems(): - if isinstance(value, unicode): - new_environ[key] = value.encode('iso-8859-1') - - subprocess.call(args, env = new_environ) - return os.path.isfile(os.path.join(data_dir, 'restart')) - except KeyboardInterrupt, e: - pass - except Exception, e: - log.critical(e) - return 0 - from couchpotato.runner import runCouchPotato -def main(): - if os.environ.get('cp_main', 'false') == 'true': + + +class TaskBarIcon(wx.TaskBarIcon): + + TBMENU_OPEN = wx.NewId() + TBMENU_SETTINGS = wx.NewId() + TBMENU_ABOUT = wx.ID_ABOUT + TBMENU_EXIT = wx.ID_EXIT + + def __init__(self, frame): + wx.TaskBarIcon.__init__(self) + self.frame = frame + + icon = wx.Icon('icon.ico', wx.BITMAP_TYPE_ANY) + self.SetIcon(icon) + + self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.onTaskBarActivate) + + self.Bind(wx.EVT_MENU, self.onOpen, id = self.TBMENU_OPEN) + self.Bind(wx.EVT_MENU, self.onSettings, id = self.TBMENU_SETTINGS) + self.Bind(wx.EVT_MENU, self.onAbout, id = self.TBMENU_ABOUT) + self.Bind(wx.EVT_MENU, self.onTaskBarClose, id = self.TBMENU_EXIT) + + + def CreatePopupMenu(self): + menu = wx.Menu() + menu.Append(self.TBMENU_OPEN, "Open") + menu.Append(self.TBMENU_SETTINGS, "Settings") + menu.Append(self.TBMENU_ABOUT, "About") + menu.Append(self.TBMENU_EXIT, "Close") + return menu + + def onOpen(self, event): + url = self.frame.parent.getSetting('base_url') + webbrowser.open(url) + + def onSettings(self, event): + url = self.frame.parent.getSetting('base_url') + '/settings/' + webbrowser.open(url) + + def onAbout(self, event): + print 'onAbout' + + def onTaskBarActivate(self, evt): + if not self.frame.IsShown(): + self.frame.Show(True) + self.frame.Raise() + + def onTaskBarClose(self, evt): + wx.CallAfter(self.frame.Close) + + def makeIcon(self, img): + if "wxMSW" in wx.PlatformInfo: + img = img.Scale(16, 16) + elif "wxGTK" in wx.PlatformInfo: + img = img.Scale(22, 22) + + icon = wx.IconFromBitmap(img.CopyFromBitmap()) + return icon + + +class MainFrame(wx.Frame): + + def __init__(self, parent): + wx.Frame.__init__(self, None) + + self.parent = parent + self.tbicon = TaskBarIcon(self) + + +class WorkerThread(Thread): + + def __init__(self, desktop): + Thread.__init__(self) + self._desktop = desktop + + self.start() + + def run(self): + + args = ['--nogit', '--console_log']#, '--quiet'] + options = getOptions(base_path, args) + try: - runCouchPotato(options, base_path, sys.argv[1:]) + runCouchPotato(options, base_path, args, desktop = self._desktop) + except KeyboardInterrupt, e: + raise except Exception, e: - log.critical(e) - else: - while 1: - restart = start() - if not restart: - break + raise + finally: + pass - from couchpotato.core.event import fireEvent - fireEvent('app.crappy_shutdown', single = True) - time.sleep(1) - sys.exit() +class CouchPotatoApp(wx.App, SoftwareUpdate): -if __name__ == '__main__': + settings = {} + events = {} + restart = False + + def OnInit(self): - signal(SIGTERM, lambda signum, stack_frame: sys.exit(1)) + # Updater + base_url = 'http://couchpotatoapp.com/updates/' + self.InitUpdates(base_url, base_url + 'changelog.txt', + icon = wx.Icon('icon.ico')) - if options.daemon and options.pid_file and not os.environ.get('cp_main'): - from daemon import Daemon - daemon = Daemon(options.pid_file) - daemon.daemonize() + self.frame = MainFrame(self) + self.frame.Bind(wx.EVT_CLOSE, self.onClose) + + # CouchPotato thread + self.worker = WorkerThread(self) + + return True + + def setSettings(self, settings = {}): + self.settings = settings + + def getSetting(self, name): + return self.settings.get(name) + + def addEvents(self, events = {}): + for name in events.iterkeys(): + self.events[name] = events[name] + + def onClose(self, event): + onClose = self.events.get('onClose') + if self.events.get('onClose'): + onClose(event) + else: + self.afterShutdown() + + def afterShutdown(self, restart = False): + self.frame.Destroy() + self.restart = restart + + +if __name__ == '__main__': + app = CouchPotatoApp(redirect = False) + app.MainLoop() - main() + #path = os.path.join(sys.path[0].decode(sys.getfilesystemencoding()), sys.argv[0]) + #if app.restart: + # wx.Process.Open(sys.executable + ' ' + path) diff --git a/icon.icns b/icon.icns new file mode 100644 index 0000000..04cbc86 Binary files /dev/null and b/icon.icns differ diff --git a/icon.ico b/icon.ico new file mode 100644 index 0000000..f93f9fb Binary files /dev/null and b/icon.ico differ diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..c505c6b --- /dev/null +++ b/setup.py @@ -0,0 +1,86 @@ +from esky import bdist_esky +from setuptools import setup +import sys +import version +import os + + +# Include proper dirs +base_path = os.path.dirname(os.path.abspath(__file__)) +lib_dir = os.path.join(base_path, 'libs') + +sys.path.insert(0, base_path) +sys.path.insert(0, lib_dir) + + + +# Windows +if sys.platform == "win32": + import py2exe + + FREEZER = 'py2exe' + FREEZER_OPTIONS = dict( + compressed = 0, + optimize = 0, + bundle_files = 3, + dll_excludes = [ + 'MSVCP90.dll', + 'mswsock.dll', + 'powrprof.dll', + 'USP10.dll', + ], + packages = ['couchpotato', 'libs'], + includes = [ + 'telnetlib', + 'xml.etree.ElementTree', + 'xml.etree.cElementTree', + 'xml.dom', + 'xml.dom.minidom', + ], + ) + exeICON = 'icon.ico' + + +# OSX +elif sys.platform == "darwin": + import py2app + + FREEZER = 'py2app' + FREEZER_OPTIONS = dict( + argv_emulation = False, + iconfile = 'icon.icns', + plist = dict( + LSUIElement = True, + ), + packages = ['couchpotato', 'libs'], + includes = [ + 'telnetlib', + 'xml.etree.ElementTree', + 'xml.etree.cElementTree', + 'xml.dom', + 'xml.dom.minidom', + ], + ) + exeICON = None + +# Common +NAME = "CouchPotato" +APP = [bdist_esky.Executable("CouchPotato.py", gui_only = True, icon = exeICON,)] +DATA_FILES = ['icon.ico'] +ESKY_OPTIONS = dict( + freezer_module = FREEZER, + freezer_options = FREEZER_OPTIONS, + bundle_msvcrt = True, +) + + +# Build the app and the esky bundle +setup( + name = NAME, + scripts = APP, + version = version.VERSION, + data_files = DATA_FILES, + options = dict(bdist_esky = ESKY_OPTIONS), +) + + diff --git a/version.py b/version.py new file mode 100644 index 0000000..f424186 --- /dev/null +++ b/version.py @@ -0,0 +1 @@ +VERSION = '0.5'