From d309b58b5a00d5fb826a317feed0005b6b6768a6 Mon Sep 17 00:00:00 2001 From: Omair Majid Date: Apr 08 2014 00:21:24 +0000 Subject: Split generated data from code Also use the generated paths instead of hardcoded ones. --- diff --git a/ChangeLog b/ChangeLog index 25b1b9b..c3cbede 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,25 @@ +2014-04-10 Omair Majid + + * Makefile.am: + (python_PYTHON): Add switch_java_globals.py and switch_java_boot.py. + (EXTRA_DIST): Add switch_java_globals.py.in. Remove + switch_java_functions.py.in. + (check-local): Include srcdir and current dir in PYTHONPATH. + * configure.ac: Add switch_java_globals.py and system-switch-java to + AC_CONFIG_FILES + * po/POTFILES.in: Add switch_java_functions.py. Remove + switch_java_functions.py.in. + * po/system-switch-java.pot: Regenerate. + * system-switch-java: Rename to ... + * system-swtich-java.in: New file. + (main): Move to ... + * swtich_java_boot.py: New file. + * switch_java_functions.py.in: Rename to ... + * switch_java_functions.py: New file. Import constants from + switch_java_globals. + * switch_java_globals.py.in: New file. + * switch_java_gui.py: Use constants from switch_java_globals. + 2014-04-10 Jiri Vanek * switch_java_tui.py: (main) text made wider to 60, diff --git a/Makefile.am b/Makefile.am index a14f5e5..131ea75 100644 --- a/Makefile.am +++ b/Makefile.am @@ -8,13 +8,14 @@ desktopdir = $(datadir)/applications nodist_desktop_DATA = $(PACKAGE).desktop pythondir = $(datadir)/$(PACKAGE) -python_PYTHON = switch_java_functions.py switch_java_gui.py switch_java_tui.py +python_PYTHON = switch_java_functions.py switch_java_gui.py switch_java_tui.py \ + switch_java_globals.py switch_java_boot.py dist_python_DATA = $(PACKAGE).glade $(PACKAGE).desktop: $(PACKAGE).desktop.in @INTLTOOL_MERGE@ --desktop-style $(srcdir)/po $< $@ -EXTRA_DIST = switch_java_functions.py.in $(PACKAGE).desktop.in COPYING.icon +EXTRA_DIST = switch_java_globals.py.in $(PACKAGE).desktop.in COPYING.icon DISTCLEANFILES = \ $(PACKAGE).desktop @@ -24,4 +25,4 @@ install-exec-local: ln -sf consolehelper $(DESTDIR)$(bindir)/$(PACKAGE) check-local: - $(PYTHON) -m unittest discover -s $(srcdir)/tests + PYTHONPATH=.:$(srcdir) $(PYTHON) -m unittest discover -s $(srcdir)/tests diff --git a/configure.ac b/configure.ac index edafcc4..e03be26 100644 --- a/configure.ac +++ b/configure.ac @@ -13,7 +13,8 @@ AM_PATH_PYTHON AC_CONFIG_FILES([ Makefile po/Makefile.in - switch_java_functions.py + switch_java_globals.py + system-switch-java ]) AC_OUTPUT diff --git a/po/POTFILES.in b/po/POTFILES.in index 78affc4..c927965 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,2 +1,2 @@ -switch_java_functions.py.in +switch_java_functions.py system-switch-java.desktop.in diff --git a/po/system-switch-java.pot b/po/system-switch-java.pot index e51988d..6f0e818 100644 --- a/po/system-switch-java.pot +++ b/po/system-switch-java.pot @@ -8,23 +8,24 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2008-07-03 18:39-0400\n" +"POT-Creation-Date: 2014-04-10 15:01-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" +"Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" -#: ../switch_java_functions.py.in:34 +#: ../switch_java_functions.py:32 msgid "display text interface even if graphics display is available" msgstr "" -#: ../switch_java_functions.py.in:37 +#: ../switch_java_functions.py:35 msgid "An attempt to parse /var/lib/alternatives/java failed." msgstr "" -#: ../switch_java_functions.py.in:40 +#: ../switch_java_functions.py:38 msgid "" "No supported Java packages were found. A supported Java package is one that " "installs a file of the form:\n" @@ -37,34 +38,34 @@ msgid "" "/usr/lib/jvm/jre-1.4.2-gcj/bin/java" msgstr "" -#: ../switch_java_functions.py.in:51 +#: ../switch_java_functions.py:49 msgid "Select the default Java toolset for the system." msgstr "" -#: ../switch_java_functions.py.in:52 +#: ../switch_java_functions.py:50 msgid "Java Toolset Configuration" msgstr "" -#: ../switch_java_functions.py.in:53 +#: ../switch_java_functions.py:51 msgid "Installed Java Toolsets" msgstr "" -#: ../switch_java_functions.py.in:54 +#: ../switch_java_functions.py:52 msgid "The default Java toolset can only be configured by the root user." msgstr "" -#: ../switch_java_functions.py.in:57 +#: ../switch_java_functions.py:55 msgid "OK" msgstr "" -#: ../switch_java_functions.py.in:58 +#: ../switch_java_functions.py:56 msgid "Close" msgstr "" #: ../system-switch-java.desktop.in.h:1 -msgid "Change the default Java toolset" +msgid "Java Toolset" msgstr "" #: ../system-switch-java.desktop.in.h:2 -msgid "Java Toolset" +msgid "Change the default Java toolset" msgstr "" diff --git a/switch_java_boot.py b/switch_java_boot.py new file mode 100644 index 0000000..80b0bd7 --- /dev/null +++ b/switch_java_boot.py @@ -0,0 +1,80 @@ +# switch_java_boot - the Java toolset switcher frontend +# Copyright (C) 2007 Red Hat, Inc. + +# This program 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 2 of the License, or +# (at your option) any later version. + +# This program 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; see the file COPYING. If not, write to the +# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301 USA. + +import os +import os.path +import sys +from optparse import OptionParser + +from switch_java_functions import * +from switch_java_globals import VERSION + +def main(): + parser = OptionParser(version=VERSION) + parser.add_option('-t', '--text', action='store_true', + dest='force_text', default=False, help=TEXT_MESSAGE) + options, args = parser.parse_args() + show_gui = False + if not options.force_text: + try: + if os.environ['DISPLAY'] != '': + show_gui = True + except KeyError: + pass + if os.getuid() != 0: + show_dialog(show_gui, ROOT_MESSAGE) + sys.exit(1) + java_identifiers = [] + best_identifier = '' + # Get list of identifiers for installed Java environments. + try: + java_identifiers, best_identifier = get_java_identifiers() + except JavaParseError: + show_dialog(show_gui, PARSE_ERROR_MESSAGE) + sys.exit(1) + except JavaOpenError: + pass + if len(java_identifiers) == 0: + show_dialog(show_gui, NO_JAVA_MESSAGE) + sys.exit(1) + # Get default Java alternative. Default to best alternative if no + # recognized default is currently set. That is, if + # /etc/alternatives/java does not point to a recognized Java + # alternative, point it to the best alternative. This will + # override a deliberate custom setting of /etc/alternatives/java + # but that's probably the right thing to do for an easy-to-use GUI + # tool. In almost all cases /etc/alternatives/java pointing to + # something unrecognized will represent an error condition that + # should be fixed, rather than a custom setting. + default_java_command = get_default_java_command() + if default_java_command not in JAVA.values(): + default_java_command = JAVA[best_identifier] + default_java = ALTERNATIVES[default_java_command] + pretty_names = get_pretty_names(java_identifiers) + if show_gui: + from switch_java_gui import mainDialog + else: + from switch_java_tui import mainDialog + mainDialog().main(java_identifiers, default_java, pretty_names) + +def show_dialog(show_gui, message): + if show_gui: + from switch_java_gui import mainDialog + else: + from switch_java_tui import mainDialog + mainDialog().show_dialog(message) diff --git a/switch_java_functions.py b/switch_java_functions.py new file mode 100644 index 0000000..355bd3f --- /dev/null +++ b/switch_java_functions.py @@ -0,0 +1,321 @@ +# switch_java_functions - functions for switching Java alternatives +# Copyright (C) 2007 Red Hat, Inc. + +# This program 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 2 of the License, or +# (at your option) any later version. + +# This program 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; see the file COPYING. If not, write to the +# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301 USA. + +import gettext +import os +import os.path +import re + +from switch_java_globals import PROGNAME, PACKAGE_NAME, VERSION, LOCALE_DIR +COPYRIGHT = 'Copyright (C) 2007 Red Hat, Inc.' + +# Internationalization. +gettext.bindtextdomain(PROGNAME, LOCALE_DIR) +gettext.textdomain(PROGNAME) +_ = gettext.gettext + +TEXT_MESSAGE = _('''\ +display text interface even if graphics display is available\ +''') +PARSE_ERROR_MESSAGE = _('''\ +An attempt to parse /var/lib/alternatives/java failed.\ +''') +NO_JAVA_MESSAGE = _('''\ +No supported Java packages were found. A supported Java package is\ + one that installs a file of the form: + +/usr/lib/jvm/jre-$version-$vendor/bin/java + +For example, java-1.4.2-gcj-compat is a supported package because it\ + installs: + +/usr/lib/jvm/jre-1.4.2-gcj/bin/java\ +''') +INSTRUCTION_MESSAGE = _('Select the default Java toolset for the system.') +TITLE_MESSAGE = _('Java Toolset Configuration') +SELECTION_MESSAGE = _('Installed Java Toolsets') +ROOT_MESSAGE = _('''\ +The default Java toolset can only be configured by the root user.\ +''') +OK_MESSAGE = _('OK') +CLOSE_MESSAGE = _('Close') +JAVA_PATH = '/etc/alternatives/java' +ALTERNATIVES = {} +JAVA = {} +JRE = {} +JCE = {} +JAVAC = {} +SDK = {} +PLUGIN = {} +JAVADOCDIR = {} + +class JavaOpenError(Exception): + pass +class JavaParseError(Exception): + pass + +def switch_java(java): + vendor, version, arch = get_java_split(java) + # There are problems with the jre_ibm, jre_1.4.2, java_sdk, + # java_sdk_1.4.2 and libjavaplugin.so alternatives in the JPackage + # java-1.4.2-ibm and java-1.5.0-ibm packages, but not in the RHEL + # ones. We suppress error output from the alternatives commands + # so that JPackage users won't be alarmed. The only consequence + # for them is that the seldom-used /usr/lib/jvm/jre-ibm, + # /usr/lib/jvm/jre-1.4.2, /usr/lib/jvm/java-1.4.2 and + # /usr/lib/jvm/java-ibm symlinks will not be updated. In the case + # of the plugin, JPackage and Red Hat plugin packages are + # incompatible anyway, so failing to set an alternative will not + # cause additional problems. + suppress = '' + if vendor == 'ibm': + suppress = ' >/dev/null 2>&1' + os.system('/usr/sbin/alternatives --set java ' + JAVA[java]) + os.system('/usr/sbin/alternatives --set jre_' + vendor + + ' ' + JRE[java] + suppress) + os.system('/usr/sbin/alternatives --set jre_' + version + + ' ' + JRE[java] + suppress) + if JCE[java] != None: + os.system('/usr/sbin/alternatives --set jce_' + version + + '_' + vendor + '_local_policy' + arch + ' ' + JCE[java]) + if JAVAC[java] != None: + os.system('/usr/sbin/alternatives --set javac ' + JAVAC[java]) + os.system('/usr/sbin/alternatives --set java_sdk_' + vendor + + ' ' + SDK[java] + suppress) + os.system('/usr/sbin/alternatives --set java_sdk_' + version + + ' ' + SDK[java] + suppress) + if PLUGIN[java] != None: + os.system('/usr/sbin/alternatives --set libjavaplugin.so' + arch + + ' ' + PLUGIN[java]) + if JAVADOCDIR[java] != None: + os.system('/usr/sbin/alternatives --set javadocdir ' + + JAVADOCDIR[java]) + +def get_java_identifiers(): + java_identifiers = [] + # indicates whether each java_identifier is a jdk or jre + jdks = [] + best_identifier = None + alternatives, best = get_alternatives('java') + jre_expression = re.compile('/usr/lib/jvm/jre-([^/]+)/bin/java') + jdk_expression = re.compile('/usr/lib/jvm/java-([^/]+)/jre/bin/java') + best_indentifier_index = -1 + for i in range(len(alternatives)): + alternative = alternatives[i] + jre_search = jre_expression.search(alternative) + jdk_search = jdk_expression.search(alternative) + java = None + if not jre_search == None: + java = jre_search.group(1) + jdks.append(False) + elif not jdk_search == None: + java = jdk_search.group(1) + jdks.append(True) + else: + continue + java_identifiers.append(java) + if i == best: + best_identifier_index = len(java_identifiers) - 1 + identifiers_and_jdks = zip(java_identifiers, jdks) + if len(identifiers_and_jdks) > 0: + best_identifier = identifiers_and_jdks[best_identifier_index][0] + identifiers_and_jdks.sort(cmp, lambda i : get_sorting_name(i[0])) + initialize_alternatives_dictionaries(identifiers_and_jdks) + return java_identifiers, best_identifier + +def get_plugin_alternatives(plugin_alternatives, arch): + try: + alternatives, best = get_alternatives('libjavaplugin.so' + arch) + plugin_expression = re.compile('/usr/lib/jvm/jre-([^/]*)/') + for alternative in alternatives: + java_search = plugin_expression.search(alternative) + if java_search == None: + # Skip unrecognized libjavaplugin.so alternative. + continue + java = java_search.group(1) + plugin_alternatives[java] = alternative + except JavaParseError: + # Ignore libjavaplugin.so parse errors. + pass + except JavaOpenError: + # No libjavaplugin.so alternatives were found. + pass + return plugin_alternatives + +def get_javadocdir_alternatives(): + javadocdir_alternatives = {} + try: + alternatives, best = get_alternatives('javadocdir') + javadocdir_expression = re.compile('/usr/share/javadoc/java-([^/]*)/') + for alternative in alternatives: + java_search = javadocdir_expression.search(alternative) + if java_search == None: + # Skip unrecognized javadocdir alternative. + continue + java = java_search.group(1) + javadocdir_alternatives[java] = alternative + except JavaParseError: + # Ignore javadocdir parse errors. + pass + except JavaOpenError: + # No javadocdir alternatives were found. + pass + return javadocdir_alternatives + +def get_alternatives(master): + alternatives = [] + highest_priority = -1 + best = -1 + slave_line_count = 0 + # Skip mode and master symlink lines. + first_slave_index = 2 + index = first_slave_index + try: + file = open('/var/lib/alternatives/' + master, 'r') + except: + raise JavaOpenError + try: + lines = file.readlines() + # index points to first slave line. + line = lines[index] + # Count number of slave lines to ignore. + while line != '\n': + index = index + 1 + line = lines[index] + # index points to blank line separating slaves from target. + slave_line_count = (index - first_slave_index) / 2 + index = index + 1 + # index points to target. + while index < len(lines): + line = lines[index] + # Accept trailing blank lines at the end of the file. + # Debian's update-alternatives requires this. + if line == '\n': + break + # Remove newline. + alternative = line[:-1] + # Exclude alternative targets read from + # /var/lib/alternatives/$master that do not exist in the + # filesystem. This inconsistent state can be the result + # of an rpm post script failing. + append = False + if os.path.exists(alternative): + append = True + alternatives.append(alternative) + index = index + 1 + # index points to priority. + line = lines[index] + if append: + priority = int(line[:-1]) + if priority > highest_priority: + highest_priority = priority + best = len(alternatives) - 1 + index = index + 1 + # index points to first slave. + index = index + slave_line_count + # index points to next target or end-of-file. + except: + raise JavaParseError + return alternatives, best + +def get_sorting_name(alternative): + vendor, version, arch = get_java_split(alternative) + return vendor + version + arch + +def initialize_alternatives_dictionaries(java_identifiers_and_jdks): + plugin_alternatives = get_plugin_alternatives({}, '') + javadocdir_alternatives = get_javadocdir_alternatives() + arch_found = False + for (java, is_jdk) in java_identifiers_and_jdks: + vendor, version, arch = get_java_split(java) + if is_jdk: + JAVA[java] = '/usr/lib/jvm/java-' + java + '/jre/bin/java' + else: + JAVA[java] = '/usr/lib/jvm/jre-' + java + '/bin/java' + # Command-to-alternative-name map to set default alternative. + ALTERNATIVES[JAVA[java]] = java + if is_jdk: + JRE[java] = '/usr/lib/jvm/java-' + java + '/jre' + else: + JRE[java] = '/usr/lib/jvm/jre-' + java + jce = '/usr/lib/jvm-private/java-' + java\ + + '/jce/vanilla/local_policy.jar' + if os.path.exists(jce): + JCE[java] = jce + else: + JCE[java] = None + javac = '/usr/lib/jvm/java-' + java + '/bin/javac' + if os.path.exists(javac): + JAVAC[java] = javac + SDK[java] = '/usr/lib/jvm/java-' + java + else: + JAVAC[java] = None + SDK[java] = None + if arch != '' and not arch_found: + plugin_alternatives = get_plugin_alternatives(plugin_alternatives, + arch) + arch_found = True + PLUGIN[java] = None + if java in plugin_alternatives: + PLUGIN[java] = plugin_alternatives[java] + JAVADOCDIR[java] = None + if java in javadocdir_alternatives: + JAVADOCDIR[java] = javadocdir_alternatives[java] + +def get_default_java_command(path=JAVA_PATH): + '''Return the default java command (default is '/etc/alternatives/java')''' + if os.path.exists(path) and os.path.islink(path): + return os.readlink(path) + else: + return None + +def get_pretty_names(alternative_names): + '''Return a dictionary that maps each item in the input list to the pretty string representation. + + See get_pretty_name for more information''' + pretty_names = {} + for java in alternative_names: + pretty_names[java] = get_pretty_name(java) + return pretty_names + +def get_pretty_name(java): + '''Return a pretty name of the form "formatted-name version optional-arch"''' + vendor, version, arch = get_java_split(java) + if vendor == 'sun' or vendor == 'blackdown': + pretty_name = vendor.capitalize() + ' ' + version + elif vendor == 'icedtea': + pretty_name = 'IcedTea' + ' ' + version + elif vendor == 'openjdk': + pretty_name = 'OpenJDK' + ' ' + version + else: + pretty_name = vendor.upper() + ' ' + version + if arch != '': + pretty_name = pretty_name + ' ' + '64-bit' + return pretty_name + +def get_java_split(java): + '''Return the tuple (version, vendor, arch) for a directory suffix (1.5.0-gcj.x86_64) + + arch is empty if the directory suffix does not contain arch.''' + vendor_arch = java.split('-')[1].split('.') + vendor = vendor_arch[0] + arch = '' + if len(vendor_arch) > 1: + arch = '.' + vendor_arch[1] + version = java.split('-')[0] + return vendor, version, arch diff --git a/switch_java_functions.py.in b/switch_java_functions.py.in deleted file mode 100755 index 6b3b05c..0000000 --- a/switch_java_functions.py.in +++ /dev/null @@ -1,322 +0,0 @@ -# switch_java_functions - functions for switching Java alternatives -# Copyright (C) 2007 Red Hat, Inc. - -# This program 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 2 of the License, or -# (at your option) any later version. - -# This program 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; see the file COPYING. If not, write to the -# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -# 02110-1301 USA. - -import gettext -import os -import os.path -import re - -PROGNAME = 'system-switch-java' -COPYRIGHT = 'Copyright (C) 2007 Red Hat, Inc.' -VERSION = '@VERSION@' - -# Internationalization. -gettext.bindtextdomain(PROGNAME, '/usr/share/locale') -gettext.textdomain(PROGNAME) -_ = gettext.gettext - -TEXT_MESSAGE = _('''\ -display text interface even if graphics display is available\ -''') -PARSE_ERROR_MESSAGE = _('''\ -An attempt to parse /var/lib/alternatives/java failed.\ -''') -NO_JAVA_MESSAGE = _('''\ -No supported Java packages were found. A supported Java package is\ - one that installs a file of the form: - -/usr/lib/jvm/jre-$version-$vendor/bin/java - -For example, java-1.4.2-gcj-compat is a supported package because it\ - installs: - -/usr/lib/jvm/jre-1.4.2-gcj/bin/java\ -''') -INSTRUCTION_MESSAGE = _('Select the default Java toolset for the system.') -TITLE_MESSAGE = _('Java Toolset Configuration') -SELECTION_MESSAGE = _('Installed Java Toolsets') -ROOT_MESSAGE = _('''\ -The default Java toolset can only be configured by the root user.\ -''') -OK_MESSAGE = _('OK') -CLOSE_MESSAGE = _('Close') -JAVA_PATH = '/etc/alternatives/java' -ALTERNATIVES = {} -JAVA = {} -JRE = {} -JCE = {} -JAVAC = {} -SDK = {} -PLUGIN = {} -JAVADOCDIR = {} - -class JavaOpenError(Exception): - pass -class JavaParseError(Exception): - pass - -def switch_java(java): - vendor, version, arch = get_java_split(java) - # There are problems with the jre_ibm, jre_1.4.2, java_sdk, - # java_sdk_1.4.2 and libjavaplugin.so alternatives in the JPackage - # java-1.4.2-ibm and java-1.5.0-ibm packages, but not in the RHEL - # ones. We suppress error output from the alternatives commands - # so that JPackage users won't be alarmed. The only consequence - # for them is that the seldom-used /usr/lib/jvm/jre-ibm, - # /usr/lib/jvm/jre-1.4.2, /usr/lib/jvm/java-1.4.2 and - # /usr/lib/jvm/java-ibm symlinks will not be updated. In the case - # of the plugin, JPackage and Red Hat plugin packages are - # incompatible anyway, so failing to set an alternative will not - # cause additional problems. - suppress = '' - if vendor == 'ibm': - suppress = ' >/dev/null 2>&1' - os.system('/usr/sbin/alternatives --set java ' + JAVA[java]) - os.system('/usr/sbin/alternatives --set jre_' + vendor - + ' ' + JRE[java] + suppress) - os.system('/usr/sbin/alternatives --set jre_' + version - + ' ' + JRE[java] + suppress) - if JCE[java] != None: - os.system('/usr/sbin/alternatives --set jce_' + version - + '_' + vendor + '_local_policy' + arch + ' ' + JCE[java]) - if JAVAC[java] != None: - os.system('/usr/sbin/alternatives --set javac ' + JAVAC[java]) - os.system('/usr/sbin/alternatives --set java_sdk_' + vendor - + ' ' + SDK[java] + suppress) - os.system('/usr/sbin/alternatives --set java_sdk_' + version - + ' ' + SDK[java] + suppress) - if PLUGIN[java] != None: - os.system('/usr/sbin/alternatives --set libjavaplugin.so' + arch - + ' ' + PLUGIN[java]) - if JAVADOCDIR[java] != None: - os.system('/usr/sbin/alternatives --set javadocdir ' - + JAVADOCDIR[java]) - -def get_java_identifiers(): - java_identifiers = [] - # indicates whether each java_identifier is a jdk or jre - jdks = [] - best_identifier = None - alternatives, best = get_alternatives('java') - jre_expression = re.compile('/usr/lib/jvm/jre-([^/]+)/bin/java') - jdk_expression = re.compile('/usr/lib/jvm/java-([^/]+)/jre/bin/java') - best_indentifier_index = -1 - for i in range(len(alternatives)): - alternative = alternatives[i] - jre_search = jre_expression.search(alternative) - jdk_search = jdk_expression.search(alternative) - java = None - if not jre_search == None: - java = jre_search.group(1) - jdks.append(False) - elif not jdk_search == None: - java = jdk_search.group(1) - jdks.append(True) - else: - continue - java_identifiers.append(java) - if i == best: - best_identifier_index = len(java_identifiers) - 1 - identifiers_and_jdks = zip(java_identifiers, jdks) - if len(identifiers_and_jdks) > 0: - best_identifier = identifiers_and_jdks[best_identifier_index][0] - identifiers_and_jdks.sort(cmp, lambda i : get_sorting_name(i[0])) - initialize_alternatives_dictionaries(identifiers_and_jdks) - return java_identifiers, best_identifier - -def get_plugin_alternatives(plugin_alternatives, arch): - try: - alternatives, best = get_alternatives('libjavaplugin.so' + arch) - plugin_expression = re.compile('/usr/lib/jvm/jre-([^/]*)/') - for alternative in alternatives: - java_search = plugin_expression.search(alternative) - if java_search == None: - # Skip unrecognized libjavaplugin.so alternative. - continue - java = java_search.group(1) - plugin_alternatives[java] = alternative - except JavaParseError: - # Ignore libjavaplugin.so parse errors. - pass - except JavaOpenError: - # No libjavaplugin.so alternatives were found. - pass - return plugin_alternatives - -def get_javadocdir_alternatives(): - javadocdir_alternatives = {} - try: - alternatives, best = get_alternatives('javadocdir') - javadocdir_expression = re.compile('/usr/share/javadoc/java-([^/]*)/') - for alternative in alternatives: - java_search = javadocdir_expression.search(alternative) - if java_search == None: - # Skip unrecognized javadocdir alternative. - continue - java = java_search.group(1) - javadocdir_alternatives[java] = alternative - except JavaParseError: - # Ignore javadocdir parse errors. - pass - except JavaOpenError: - # No javadocdir alternatives were found. - pass - return javadocdir_alternatives - -def get_alternatives(master): - alternatives = [] - highest_priority = -1 - best = -1 - slave_line_count = 0 - # Skip mode and master symlink lines. - first_slave_index = 2 - index = first_slave_index - try: - file = open('/var/lib/alternatives/' + master, 'r') - except: - raise JavaOpenError - try: - lines = file.readlines() - # index points to first slave line. - line = lines[index] - # Count number of slave lines to ignore. - while line != '\n': - index = index + 1 - line = lines[index] - # index points to blank line separating slaves from target. - slave_line_count = (index - first_slave_index) / 2 - index = index + 1 - # index points to target. - while index < len(lines): - line = lines[index] - # Accept trailing blank lines at the end of the file. - # Debian's update-alternatives requires this. - if line == '\n': - break - # Remove newline. - alternative = line[:-1] - # Exclude alternative targets read from - # /var/lib/alternatives/$master that do not exist in the - # filesystem. This inconsistent state can be the result - # of an rpm post script failing. - append = False - if os.path.exists(alternative): - append = True - alternatives.append(alternative) - index = index + 1 - # index points to priority. - line = lines[index] - if append: - priority = int(line[:-1]) - if priority > highest_priority: - highest_priority = priority - best = len(alternatives) - 1 - index = index + 1 - # index points to first slave. - index = index + slave_line_count - # index points to next target or end-of-file. - except: - raise JavaParseError - return alternatives, best - -def get_sorting_name(alternative): - vendor, version, arch = get_java_split(alternative) - return vendor + version + arch - -def initialize_alternatives_dictionaries(java_identifiers_and_jdks): - plugin_alternatives = get_plugin_alternatives({}, '') - javadocdir_alternatives = get_javadocdir_alternatives() - arch_found = False - for (java, is_jdk) in java_identifiers_and_jdks: - vendor, version, arch = get_java_split(java) - if is_jdk: - JAVA[java] = '/usr/lib/jvm/java-' + java + '/jre/bin/java' - else: - JAVA[java] = '/usr/lib/jvm/jre-' + java + '/bin/java' - # Command-to-alternative-name map to set default alternative. - ALTERNATIVES[JAVA[java]] = java - if is_jdk: - JRE[java] = '/usr/lib/jvm/java-' + java + '/jre' - else: - JRE[java] = '/usr/lib/jvm/jre-' + java - jce = '/usr/lib/jvm-private/java-' + java\ - + '/jce/vanilla/local_policy.jar' - if os.path.exists(jce): - JCE[java] = jce - else: - JCE[java] = None - javac = '/usr/lib/jvm/java-' + java + '/bin/javac' - if os.path.exists(javac): - JAVAC[java] = javac - SDK[java] = '/usr/lib/jvm/java-' + java - else: - JAVAC[java] = None - SDK[java] = None - if arch != '' and not arch_found: - plugin_alternatives = get_plugin_alternatives(plugin_alternatives, - arch) - arch_found = True - PLUGIN[java] = None - if java in plugin_alternatives: - PLUGIN[java] = plugin_alternatives[java] - JAVADOCDIR[java] = None - if java in javadocdir_alternatives: - JAVADOCDIR[java] = javadocdir_alternatives[java] - -def get_default_java_command(path=JAVA_PATH): - '''Return the default java command (default is '/etc/alternatives/java')''' - if os.path.exists(path) and os.path.islink(path): - return os.readlink(path) - else: - return None - -def get_pretty_names(alternative_names): - '''Return a dictionary that maps each item in the input list to the pretty string representation. - - See get_pretty_name for more information''' - pretty_names = {} - for java in alternative_names: - pretty_names[java] = get_pretty_name(java) - return pretty_names - -def get_pretty_name(java): - '''Return a pretty name of the form "formatted-name version optional-arch"''' - vendor, version, arch = get_java_split(java) - if vendor == 'sun' or vendor == 'blackdown': - pretty_name = vendor.capitalize() + ' ' + version - elif vendor == 'icedtea': - pretty_name = 'IcedTea' + ' ' + version - elif vendor == 'openjdk': - pretty_name = 'OpenJDK' + ' ' + version - else: - pretty_name = vendor.upper() + ' ' + version - if arch != '': - pretty_name = pretty_name + ' ' + '64-bit' - return pretty_name - -def get_java_split(java): - '''Return the tuple (version, vendor, arch) for a directory suffix (1.5.0-gcj.x86_64) - - arch is empty if the directory suffix does not contain arch.''' - vendor_arch = java.split('-')[1].split('.') - vendor = vendor_arch[0] - arch = '' - if len(vendor_arch) > 1: - arch = '.' + vendor_arch[1] - version = java.split('-')[0] - return vendor, version, arch diff --git a/switch_java_globals.py.in b/switch_java_globals.py.in new file mode 100644 index 0000000..d58d3df --- /dev/null +++ b/switch_java_globals.py.in @@ -0,0 +1,27 @@ +# switch_java_globals - global constants +# Copyright (C) 2014 Red Hat, Inc. + +# This program 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 2 of the License, or +# (at your option) any later version. + +# This program 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; see the file COPYING. If not, write to the +# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301 USA. + +PACKAGE_NAME='@PACKAGE_NAME@' +PROGNAME='@PACKAGE_NAME@' +VERSION='@PACKAGE_VERSION@' + +# TODO use @ bindir @ and friends rather than @ prefix @ +PREFIX='@prefix@' +BIN_DIR='@prefix@/bin' +DATA_ROOT_DIR='@prefix@/share' +LOCALE_DIR='@prefix@/locale' diff --git a/switch_java_gui.py b/switch_java_gui.py index 1d46fee..2081c6d 100644 --- a/switch_java_gui.py +++ b/switch_java_gui.py @@ -19,6 +19,7 @@ import gtk import gtk.glade from switch_java_functions import * +from switch_java_globals import DATA_ROOT_DIR, PACKAGE_NAME class mainDialog: def __init__(self): @@ -26,9 +27,9 @@ class mainDialog: def main(self, java_identifiers, default_java, pretty_names): # Initialize UI. - xml = gtk.glade.XML('/usr/share/system-switch-java/' - + 'system-switch-java.glade', - None, domain=PROGNAME) + xml = gtk.glade.XML(DATA_ROOT_DIR + '/' + PACKAGE_NAME + '/' + + PACKAGE_NAME + '.glade', + None, domain=PACKAGE_NAME) self.dialog = xml.get_widget('dialog') self.dialog.set_title(TITLE_MESSAGE) radio_vbox = xml.get_widget('radio-vbox') @@ -64,8 +65,8 @@ class mainDialog: self.cancel_button_clicked) self.dialog.connect('delete-event', self.dialog_delete_event) self.dialog.connect('hide', gtk.main_quit) - self.dialog.set_icon_from_file('/usr/share/pixmaps/' - + 'system-switch-java.png') + self.dialog.set_icon_from_file(DATA_ROOT_DIR + '/pixmaps/' + + PACKAGE_NAME + '.png') self.dialog.show() gtk.main() @@ -92,6 +93,6 @@ class mainDialog: gtk.BUTTONS_OK, message) dialog.set_title(TITLE_MESSAGE) - dialog.set_icon_from_file('/usr/share/pixmaps/system-switch-java.png') + dialog.set_icon_from_file(DATA_ROOT_DIR + '/pixmaps/' + PACKAGE_NAME + '.png') dialog.run() dialog.destroy() diff --git a/system-switch-java b/system-switch-java deleted file mode 100644 index 6e20194..0000000 --- a/system-switch-java +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env python -# system-switch-java - the Java toolset switcher frontend -# Copyright (C) 2007 Red Hat, Inc. - -# This program 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 2 of the License, or -# (at your option) any later version. - -# This program 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; see the file COPYING. If not, write to the -# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -# 02110-1301 USA. - -import os -import os.path -import signal -import sys -from optparse import OptionParser - -DIRECTORY = '/usr/share/system-switch-java/' -if not DIRECTORY in sys.path: - sys.path.append(DIRECTORY) -from switch_java_functions import * - -def main(): - parser = OptionParser(version=VERSION) - parser.add_option('-t', '--text', action='store_true', - dest='force_text', default=False, help=TEXT_MESSAGE) - options, args = parser.parse_args() - show_gui = False - if not options.force_text: - try: - if os.environ['DISPLAY'] != '': - show_gui = True - except KeyError: - pass - if os.getuid() != 0: - show_dialog(show_gui, ROOT_MESSAGE) - sys.exit(1) - java_identifiers = [] - best_identifier = '' - # Get list of identifiers for installed Java environments. - try: - java_identifiers, best_identifier = get_java_identifiers() - except JavaParseError: - show_dialog(show_gui, PARSE_ERROR_MESSAGE) - sys.exit(1) - except JavaOpenError: - pass - if len(java_identifiers) == 0: - show_dialog(show_gui, NO_JAVA_MESSAGE) - sys.exit(1) - # Get default Java alternative. Default to best alternative if no - # recognized default is currently set. That is, if - # /etc/alternatives/java does not point to a recognized Java - # alternative, point it to the best alternative. This will - # override a deliberate custom setting of /etc/alternatives/java - # but that's probably the right thing to do for an easy-to-use GUI - # tool. In almost all cases /etc/alternatives/java pointing to - # something unrecognized will represent an error condition that - # should be fixed, rather than a custom setting. - default_java_command = get_default_java_command() - if default_java_command not in JAVA.values(): - default_java_command = JAVA[best_identifier] - default_java = ALTERNATIVES[default_java_command] - pretty_names = get_pretty_names(java_identifiers) - if show_gui: - from switch_java_gui import mainDialog - else: - from switch_java_tui import mainDialog - mainDialog().main(java_identifiers, default_java, pretty_names) - -def show_dialog(show_gui, message): - if show_gui: - from switch_java_gui import mainDialog - else: - from switch_java_tui import mainDialog - mainDialog().show_dialog(message) - -if __name__ == '__main__': - # Make Ctrl-C work. - signal.signal(signal.SIGINT, signal.SIG_DFL) - main() - sys.exit(0) diff --git a/system-switch-java.in b/system-switch-java.in new file mode 100644 index 0000000..6923191 --- /dev/null +++ b/system-switch-java.in @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# system-switch-java - the Java toolset switcher frontend +# Copyright (C) 2007 Red Hat, Inc. + +# This program 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 2 of the License, or +# (at your option) any later version. + +# This program 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; see the file COPYING. If not, write to the +# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301 USA. + +import signal +import sys + +DIRECTORY = '@prefix@/share/@PACKAGE_NAME@/' +if not DIRECTORY in sys.path: + sys.path.append(DIRECTORY) +import switch_java_boot + +if __name__ == '__main__': + # Make Ctrl-C work. + signal.signal(signal.SIGINT, signal.SIG_DFL) + switch_java_boot.main() + sys.exit(0)