Permalink
Browse files

launcher: make the main launcher cleaner

The launch script has been moved to the 'sonata' package, as the rest of the
code, an put into a dedicated function, that is launch now by a very simple
script at the top level. It will allow to 'import sonata' from the source
repository and having the right behavior ('sonata' will by the package not the
top level script).

Additionnaly, Sonata now depends on setuptools/distribute to generate
automatically a good startup script. Both the commited one and the one created
by setuptools can be used.
  • Loading branch information...
1 parent c4c7e78 commit 550aa73f4d9923e221cce75d1b6fe0e259c3b90a @multani committed May 25, 2011
Showing with 183 additions and 176 deletions.
  1. +6 −0 run-sonata
  2. +7 −10 setup.py
  3. +0 −166 sonata.py
  4. +170 −0 sonata/launcher.py
View
@@ -0,0 +1,6 @@
+#!/usr/bin/env python
+
+import sonata.launcher
+
+if __name__ == '__main__':
+ sonata.launcher.run()
View
@@ -2,7 +2,7 @@
import os, glob, shutil
-from distutils.core import setup, Extension
+from setuptools import setup, Extension
from sonata.version import version
@@ -46,9 +46,6 @@ def rmgeneric(path, __func__):
print "generating", mofile
os.system("msgfmt %s -o %s" % (pofile, mofile))
-# Copy script "sonata" file to sonata dir:
-shutil.copyfile("sonata.py", "sonata/sonata")
-
versionfile = open("sonata/genversion.py","wt")
versionfile.write("""
# generated by setup.py
@@ -78,7 +75,6 @@ def rmgeneric(path, __func__):
extra_compile_args=capture("pkg-config --cflags gtk+-2.0 pygtk-2.0").split(),
extra_link_args=capture("pkg-config --libs gtk+-2.0 pygtk-2.0").split()
),],
- scripts = ['sonata/sonata'],
data_files=[('share/sonata', ['README', 'CHANGELOG', 'TODO', 'TRANSLATORS']),
('share/applications', ['sonata.desktop']),
('share/pixmaps', glob.glob('sonata/pixmaps/*')),
@@ -107,7 +103,12 @@ def rmgeneric(path, __func__):
('share/locale/sl/LC_MESSAGES', ['mo/sl/sonata.mo']),
('share/locale/zh_TW/LC_MESSAGES', ['mo/zh_TW/sonata.mo']),
('share/locale/uk/LC_MESSAGES', ['mo/uk/sonata.mo'])],
- )
+ entry_points={
+ 'console_scripts': [
+ 'sonata=sonata.launcher:run',
+ ]
+ },
+)
# Cleanup (remove /build, /mo, and *.pyc files:
print "Cleaning up..."
@@ -129,10 +130,6 @@ def rmgeneric(path, __func__):
except:
pass
try:
- os.remove("sonata/sonata")
-except:
- pass
-try:
os.remove("sonata/genversion.py")
os.remove("sonata/genversion.pyc")
except:
View
166 sonata.py
@@ -1,166 +0,0 @@
-#!/usr/bin/env python
-"""Sonata is a simple GTK+ client for the Music Player Daemon.
-"""
-
-__author__ = "Scott Horowitz"
-__email__ = "stonecrest@gmail.com"
-__license__ = """
-Sonata, an elegant GTK+ client for the Music Player Daemon
-Copyright 2006-2008 Scott Horowitz <stonecrest@gmail.com>
-
-This file is part of Sonata.
-
-Sonata is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3 of the License, or
-(at your option) any later version.
-
-Sonata is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-"""
-
-import sys, platform, locale, gettext, os
-
-# XXX insert the correct sonata package dir in sys.path
-
-try:
- import sonata
-except ImportError:
- sys.stderr.write("Python failed to find the sonata modules.\n")
- sys.stderr.write("\nSearched in the following directories:\n" +
- "\n".join(sys.path) + "\n")
- sys.stderr.write("\nPerhaps Sonata is improperly installed?\n")
- sys.exit(1)
-
-try:
- from sonata.version import version
-except ImportError:
- sys.stderr.write("Python failed to find the sonata modules.\n")
- sys.stderr.write("\nAn old or incomplete installation was " +
- "found in the following directory:\n" +
- os.path.dirname(sonata.__file__) + "\n")
- sys.stderr.write("\nPerhaps you want to delete it?\n")
- sys.exit(1)
-
-# XXX check that version.VERSION is what this script was installed for
-
-
-## Apply global fixes:
-
-# the following line is to fix python-zsi 2.0 and thus lyrics in ubuntu:
-# https://bugs.launchpad.net/ubuntu/+source/zsi/+bug/208855
-sys.path.append('/usr/lib/python2.5/site-packages/oldxml')
-
-# hint for gnome.init to set the process name to 'sonata'
-if platform.system() == 'Linux':
- sys.argv[0] = 'sonata'
-
-# apply as well:
- try:
- import ctypes
- libc = ctypes.CDLL('libc.so.6')
- PR_SET_NAME = 15
- libc.prctl(PR_SET_NAME, sys.argv[0], 0, 0, 0)
- except Exception: # if it fails, it fails
- pass
-
-
-## Apply locale and translation:
-
-from sonata import misc
-misc.setlocale()
-
-# let gettext install _ as a built-in for all modules to see
-# XXX what's the correct way to find the localization?
-try:
- gettext.install('sonata', os.path.join(sonata.__file__.split('/lib')[0], 'share', 'locale'), unicode=1)
-except:
- print "Warning: trying to use an old translation"
- gettext.install('sonata', '/usr/share/locale', unicode=1)
-gettext.textdomain('sonata')
-
-
-## Check initial dependencies:
-
-# Test python version:
-if sys.version_info < (2,5):
- sys.stderr.write("Sonata requires Python 2.5 or newer. Aborting...\n")
- sys.exit(1)
-
-try:
- import mpd
-except:
- sys.stderr.write("Sonata requires python-mpd. Aborting...\n")
- sys.exit(1)
-
-
-## Initialize the plugin system:
-
-from sonata.pluginsystem import pluginsystem
-pluginsystem.find_plugins()
-pluginsystem.notify_of('enablables',
- lambda plugin, cb: cb(True),
- lambda plugin, cb: cb(False))
-
-
-## Load the command line interface:
-
-from sonata import cli
-args = cli.Args()
-args.parse(sys.argv)
-
-## Deal with GTK:
-
-if not args.skip_gui:
- # importing gtk does sys.setdefaultencoding("utf-8"), sets locale etc.
- import gtk
- if gtk.pygtk_version < (2, 12, 0):
- sys.stderr.write("Sonata requires PyGTK 2.12.0 or newer. Aborting...\n")
- sys.exit(1)
- # fix locale
- misc.setlocale()
-else:
- class FakeModule(object):
- pass
- # make sure the ui modules aren't imported
- for m in 'gtk', 'pango', 'sonata.ui', 'sonata.breadcrumbs':
- if m in sys.modules:
- print "Warning: module %s imported in CLI mode" % m
- else:
- sys.modules[m] = FakeModule()
- # like gtk, set utf-8 encoding of str objects
- reload(sys) # hack access to setdefaultencoding
- sys.setdefaultencoding("utf-8")
-
-
-## Global init:
-
-from socket import setdefaulttimeout as socketsettimeout
-socketsettimeout(5)
-
-if not args.skip_gui:
- gtk.gdk.threads_init()
-
- # we don't use gtk.LinkButton, but gtk.AboutDialog does;
- # in gtk 2.16.0 without this, the about uri opens doubly:
- gtk.link_button_set_uri_hook(lambda *args:None)
-
-## CLI actions:
-
-args.execute_cmds()
-
-
-## Load the main application:
-
-from sonata import main
-
-app = main.Base(args)
-try:
- app.main()
-except KeyboardInterrupt:
- pass
View
@@ -0,0 +1,170 @@
+#!/usr/bin/env python
+"""Sonata is a simple GTK+ client for the Music Player Daemon.
+"""
+
+__author__ = "Scott Horowitz"
+__email__ = "stonecrest@gmail.com"
+__license__ = """
+Sonata, an elegant GTK+ client for the Music Player Daemon
+Copyright 2006-2008 Scott Horowitz <stonecrest@gmail.com>
+
+This file is part of Sonata.
+
+Sonata is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+Sonata is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+
+import sys, platform, locale, gettext, os
+
+
+def run():
+ """Main entry point of Sonata"""
+
+ # XXX insert the correct sonata package dir in sys.path
+
+ try:
+ import sonata
+ except ImportError:
+ sys.stderr.write("Python failed to find the sonata modules.\n")
+ sys.stderr.write("\nSearched in the following directories:\n" +
+ "\n".join(sys.path) + "\n")
+ sys.stderr.write("\nPerhaps Sonata is improperly installed?\n")
+ sys.exit(1)
+
+ try:
+ from sonata.version import version
+ except ImportError:
+ sys.stderr.write("Python failed to find the sonata modules.\n")
+ sys.stderr.write("\nAn old or incomplete installation was " +
+ "found in the following directory:\n" +
+ os.path.dirname(sonata.__file__) + "\n")
+ sys.stderr.write("\nPerhaps you want to delete it?\n")
+ sys.exit(1)
+
+ # XXX check that version.VERSION is what this script was installed for
+
+
+ ## Apply global fixes:
+
+ # the following line is to fix python-zsi 2.0 and thus lyrics in ubuntu:
+ # https://bugs.launchpad.net/ubuntu/+source/zsi/+bug/208855
+ sys.path.append('/usr/lib/python2.5/site-packages/oldxml')
+
+ # hint for gnome.init to set the process name to 'sonata'
+ if platform.system() == 'Linux':
+ sys.argv[0] = 'sonata'
+
+ # apply as well:
+ try:
+ import ctypes
+ libc = ctypes.CDLL('libc.so.6')
+ PR_SET_NAME = 15
+ libc.prctl(PR_SET_NAME, sys.argv[0], 0, 0, 0)
+ except Exception: # if it fails, it fails
+ pass
+
+
+ ## Apply locale and translation:
+
+ from sonata import misc
+ misc.setlocale()
+
+ # let gettext install _ as a built-in for all modules to see
+ # XXX what's the correct way to find the localization?
+ try:
+ gettext.install('sonata', os.path.join(sonata.__file__.split('/lib')[0], 'share', 'locale'), unicode=1)
+ except:
+ print "Warning: trying to use an old translation"
+ gettext.install('sonata', '/usr/share/locale', unicode=1)
+ gettext.textdomain('sonata')
+
+
+ ## Check initial dependencies:
+
+ # Test python version:
+ if sys.version_info < (2,5):
+ sys.stderr.write("Sonata requires Python 2.5 or newer. Aborting...\n")
+ sys.exit(1)
+
+ try:
+ import mpd
+ except:
+ sys.stderr.write("Sonata requires python-mpd. Aborting...\n")
+ sys.exit(1)
+
+
+ ## Initialize the plugin system:
+
+ from sonata.pluginsystem import pluginsystem
+ pluginsystem.find_plugins()
+ pluginsystem.notify_of('enablables',
+ lambda plugin, cb: cb(True),
+ lambda plugin, cb: cb(False))
+
+
+ ## Load the command line interface:
+
+ from sonata import cli
+ args = cli.Args()
+ args.parse(sys.argv)
+
+ ## Deal with GTK:
+
+ if not args.skip_gui:
+ # importing gtk does sys.setdefaultencoding("utf-8"), sets locale etc.
+ import gtk
+ if gtk.pygtk_version < (2, 12, 0):
+ sys.stderr.write("Sonata requires PyGTK 2.12.0 or newer. Aborting...\n")
+ sys.exit(1)
+ # fix locale
+ misc.setlocale()
+ else:
+ class FakeModule(object):
+ pass
+ # make sure the ui modules aren't imported
+ for m in 'gtk', 'pango', 'sonata.ui', 'sonata.breadcrumbs':
+ if m in sys.modules:
+ print "Warning: module %s imported in CLI mode" % m
+ else:
+ sys.modules[m] = FakeModule()
+ # like gtk, set utf-8 encoding of str objects
+ reload(sys) # hack access to setdefaultencoding
+ sys.setdefaultencoding("utf-8")
+
+
+ ## Global init:
+
+ from socket import setdefaulttimeout as socketsettimeout
+ socketsettimeout(5)
+
+ if not args.skip_gui:
+ gtk.gdk.threads_init()
+
+ # we don't use gtk.LinkButton, but gtk.AboutDialog does;
+ # in gtk 2.16.0 without this, the about uri opens doubly:
+ gtk.link_button_set_uri_hook(lambda *args:None)
+
+ ## CLI actions:
+
+ args.execute_cmds()
+
+
+ ## Load the main application:
+
+ from sonata import main
+
+ app = main.Base(args)
+ try:
+ app.main()
+ except KeyboardInterrupt:
+ pass

0 comments on commit 550aa73

Please sign in to comment.