Changeset 5877 in josm for trunk


Ignore:
Timestamp:
2013-04-18T00:42:36+02:00 (11 years ago)
Author:
Don-vip
Message:

see #8616 - Attempt to determine OS detailed version on some non LSB-compliant Linux systems

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r5874 r5877  
    88import java.io.BufferedReader;
    99import java.io.File;
     10import java.io.FileReader;
    1011import java.io.IOException;
    1112import java.io.InputStreamReader;
     
    9192        if ("Linux".equalsIgnoreCase(osName)) {
    9293            try {
     94                // Try lsb_release (only available on LSB-compliant Linux systems, see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod )
    9395                Process p = Runtime.getRuntime().exec("lsb_release -ds");
    9496                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
     
    103105                }
    104106            } catch (IOException e) {
    105                 e.printStackTrace();
     107                // Non LSB-compliant Linux system. List of common fallback release files: http://linuxmafia.com/faq/Admin/release-files.html
     108                for (LinuxReleaseInfo info : new LinuxReleaseInfo[]{
     109                        new LinuxReleaseInfo("/etc/lsb-release", "DISTRIB_DESCRIPTION", "DISTRIB_ID", "DISTRIB_RELEASE"),
     110                        new LinuxReleaseInfo("/etc/os-release", "PRETTY_NAME", "NAME", "VERSION"),
     111                        new LinuxReleaseInfo("/etc/arch-release"),
     112                        new LinuxReleaseInfo("/etc/debian_version", "Debian GNU/Linux "),
     113                        new LinuxReleaseInfo("/etc/fedora-release"),
     114                        new LinuxReleaseInfo("/etc/gentoo-release"),
     115                        new LinuxReleaseInfo("/etc/redhat-release")
     116                }) {
     117                    String description = info.extractDescription();
     118                    if (description != null && !description.isEmpty()) {
     119                        return "Linux " + description;
     120                    }
     121                }
    106122            }
    107123        }
    108124        return osName;
    109125    }
     126   
     127    protected static class LinuxReleaseInfo {
     128        private final String path;
     129        private final String descriptionField;
     130        private final String idField;
     131        private final String releaseField;
     132        private final boolean plainText;
     133        private final String prefix;
     134       
     135        public LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) {
     136            this(path, descriptionField, idField, releaseField, false, null);
     137        }
     138
     139        public LinuxReleaseInfo(String path) {
     140            this(path, null, null, null, true, null);
     141        }
     142
     143        public LinuxReleaseInfo(String path, String prefix) {
     144            this(path, null, null, null, true, prefix);
     145        }
     146       
     147        private LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField, boolean plainText, String prefix) {
     148            this.path = path;
     149            this.descriptionField = descriptionField;
     150            this.idField = idField;
     151            this.releaseField = releaseField;
     152            this.plainText = plainText;
     153            this.prefix = prefix;
     154        }
     155
     156        @Override public String toString() {
     157            return "ReleaseInfo [path=" + path + ", descriptionField=" + descriptionField +
     158                    ", idField=" + idField + ", releaseField=" + releaseField + "]";
     159        }
     160       
     161        /**
     162         * Extracts OS detailed information from a Linux release file (/etc/xxx-release)
     163         * @return The OS detailed information, or {@code null}
     164         */
     165        public String extractDescription() {
     166            String result = null;
     167            if (path != null) {
     168                File file = new File(path);
     169                if (file.exists()) {
     170                    BufferedReader reader = null;
     171                    try {
     172                        reader = new BufferedReader(new FileReader(file));
     173                        String id = null;
     174                        String release = null;
     175                        String line;
     176                        while (result == null && (line = reader.readLine()) != null) {
     177                            if (line.contains("=")) {
     178                                String[] tokens = line.split("=");
     179                                if (tokens.length >= 2) {
     180                                    // Description, if available, contains exactly what we need
     181                                    if (descriptionField != null && descriptionField.equalsIgnoreCase(tokens[0])) {
     182                                        result = Utils.strip(tokens[1]);
     183                                    } else if (idField != null && idField.equalsIgnoreCase(tokens[0])) {
     184                                        id = Utils.strip(tokens[1]);
     185                                    } else if (releaseField != null && releaseField.equalsIgnoreCase(tokens[0])) {
     186                                        release = Utils.strip(tokens[1]);
     187                                    }
     188                                }
     189                            } else if (plainText && !line.isEmpty()) {
     190                                // Files composed of a single line
     191                                result = Utils.strip(line);
     192                            }
     193                        }
     194                        // If no description has been found, try to rebuild it with "id" + "release" (i.e. "name" + "version")
     195                        if (result == null && id != null && release != null) {
     196                            result = id + " " + release;
     197                        }
     198                    } catch (IOException e) {
     199                        // Ignore
     200                    } finally {
     201                        Utils.close(reader);
     202                    }
     203                }
     204            }
     205            // Append prefix if any
     206            if (result != null && !result.isEmpty() && prefix != null && !prefix.isEmpty()) {
     207                result = prefix + result;
     208            }
     209            return result;
     210        }
     211    }
    110212}
Note: See TracChangeset for help on using the changeset viewer.