This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See http://git.codelutin.com/tutti.git commit c5e667d194ae4b7e54e4951d703eb329d912279b Author: Tony CHEMIT <chemit@codelutin.com> Date: Sat Jan 3 13:39:42 2015 +0100 ajout du module de mise à jour --- tutti-ui-swing-updater/LICENSE.txt | 0 tutti-ui-swing-updater/README.txt | 0 tutti-ui-swing-updater/pom.xml | 70 +++++ .../fr/ifremer/tutti/ui/swing/updater/Updater.java | 293 +++++++++++++++++++++ .../src/main/resources/update_runtime.bat | 31 +++ .../src/main/resources/update_runtime.sh | 36 +++ 6 files changed, 430 insertions(+) diff --git a/tutti-ui-swing-updater/LICENSE.txt b/tutti-ui-swing-updater/LICENSE.txt new file mode 100644 index 0000000..e69de29 diff --git a/tutti-ui-swing-updater/README.txt b/tutti-ui-swing-updater/README.txt new file mode 100644 index 0000000..e69de29 diff --git a/tutti-ui-swing-updater/pom.xml b/tutti-ui-swing-updater/pom.xml new file mode 100644 index 0000000..4d503b7 --- /dev/null +++ b/tutti-ui-swing-updater/pom.xml @@ -0,0 +1,70 @@ +<!-- + #%L + SIH Allegro ObsDeb :: UI Launcher + %% + Copyright (C) 2013 - 2014 Ifremer + %% + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 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 Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses />. + #L% +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>fr.ifremer</groupId> + <artifactId>tutti</artifactId> + <version>3.12-SNAPSHOT</version> + </parent> + + <groupId>fr.ifremer.tutti</groupId> + <artifactId>tutti-ui-swing-updater</artifactId> + + <name>Tutti :: UI Updater</name> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + + <!-- generate license bundled files --> + <license.generateBundle>true</license.generateBundle> + <license.bundleThirdPartyPath>META-INF/tutti-THIRD-PARTY.txt</license.bundleThirdPartyPath> + <license.bundleLicensePath>META-INF/tutti-LICENSE.txt</license.bundleLicensePath> + + </properties> + + <dependencies> + <!-- please, no dependency --> + </dependencies> + + <build> + + <plugins> + + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <configuration> + <archive> + <manifest> + <addClasspath>true</addClasspath> + <mainClass>fr.ifremer.tutti.ui.swing.updater.Updater</mainClass> + </manifest> + </archive> + </configuration> + </plugin> + + </plugins> + + </build> + +</project> + diff --git a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java new file mode 100644 index 0000000..54b05b4 --- /dev/null +++ b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java @@ -0,0 +1,293 @@ +package fr.ifremer.tutti.ui.swing.updater; + +import javax.swing.JOptionPane; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.PathMatcher; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.BasicFileAttributes; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +/** + * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro> + */ +public class Updater { + + public static final String APPLICATION_UPDATER_TITLE = "Allegro Campagne UI Updater"; + + public static final String NEW_DIR = "NEW"; + + public static final String OLD_DIR = "OLD"; + + public static final String JRE_DIR = "jre"; + + public static final String LAUNCHER_DIR = "launcher"; + + public static final String APPLICATION_DIR = "tutti"; + + public static final String I18N_DIR = "i18n"; + + public static final String HELP_DIR = "help"; + + public static final String REPORT_DIR = "report"; + + public static final String ICHTYOMETER_DIR = "report"; + + public static final String UPDATE_RUNTIME_CMD = "update_runtime"; + + public static final String BATCH_WINDOWS_EXT = ".bat"; + + public static final String BATCH_UNIX_EXT = ".sh"; + + public static final String VERSION_FILE = "version.appup"; + + public static final int NORMAL_EXIT_CODE = 0; + + public static final int ERROR_EXIT_CODE = 1; + + public static final int RUNTIME_UPDATE_EXIT_CODE = 90; + + private final Path baseDir; + + private final String backupDate; + + private final boolean windowsOS; + + public static void main(String... args) { + + // Instantiate the launcher + Updater updater = new Updater(); + int exitCode = updater.execute(); + + // exit + System.exit(exitCode); + + } + + public Updater() { + + // Get the current directory where application has been launched + baseDir = Paths.get(System.getProperty("user.dir")); + + // Compute the date to create backup directories + Date now = new Date(); + DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); + backupDate = df.format(now); + + windowsOS = System.getProperty("os.name").startsWith("Windows"); + + } + + public int execute() { + + int exitCode; + + try { + + // Check runtime update (jre or launcher) + boolean runtimeUpdate = checkRuntimeUpdate(); + + if (runtimeUpdate) { + + exitCode = RUNTIME_UPDATE_EXIT_CODE; + + } else { + + // Launch update + launchUpdate(); + + exitCode = NORMAL_EXIT_CODE; + + } + + } catch (Exception ex) { + + ex.printStackTrace(); + exitCode = ERROR_EXIT_CODE; + + } + + return exitCode; + + } + + protected boolean checkRuntimeUpdate() throws Exception { + + String scriptFilename = UPDATE_RUNTIME_CMD + (windowsOS ? BATCH_WINDOWS_EXT : BATCH_UNIX_EXT); + + Path runtimeUpdater = baseDir.resolve(scriptFilename); + + Files.deleteIfExists(runtimeUpdater); + + boolean mustUpdateRuntime = false; + + if (Files.isDirectory(baseDir.resolve(NEW_DIR).resolve(JRE_DIR)) + || Files.isDirectory(baseDir.resolve(NEW_DIR).resolve(LAUNCHER_DIR))) { + + // A new jre or/and a new launcher is available, so generate the script + URL resource = getClass().getResource("/" + scriptFilename); + + try (InputStream stream = resource.openStream()) { + + Files.copy(stream, runtimeUpdater, StandardCopyOption.REPLACE_EXISTING); + + } + + String message = String.format("Runtime updates available.\nYou must execute '%s' manually to apply new runtime.", runtimeUpdater.getFileName()); + System.out.println(message); + JOptionPane.showMessageDialog(null, message); + mustUpdateRuntime = true; + + } + + return mustUpdateRuntime; + + } + + protected void launchUpdate() throws IOException { + + // Compute the date to create backup directories + Date now = new Date(); + + System.out.println("updater started at " + now.toString()); + + updateModule(baseDir, APPLICATION_DIR); + updateModule(baseDir, I18N_DIR); + updateModule(baseDir, HELP_DIR); + updateModule(baseDir, REPORT_DIR); + updateModule(baseDir, ICHTYOMETER_DIR); + + // Cleaning process + cleanObsoleteFiles(); + cleanPath(baseDir.resolve(NEW_DIR)); + + System.out.println("updater ended at " + new Date().toString()); + + } + + private void updateModule(Path basePath, String moduleName) throws IOException { + + // Update a single module. moduleName corresponds to the name of the directory inside the basePath + Path modulePath = basePath.resolve(moduleName); + Path moduleNewPath = basePath.resolve(NEW_DIR).resolve(moduleName); + + if (Files.isDirectory(moduleNewPath)) { + String newVersion = getVersion(moduleNewPath); + + // Backup existing module + if (Files.isDirectory(modulePath)) { + String oldVersion = getVersion(modulePath); + Path moduleOldPath = basePath.resolve(OLD_DIR).resolve(String.format("%s-%s-%s", moduleName, oldVersion, backupDate)); + System.out.println(String.format("backup %s %s to %s", moduleName, oldVersion, moduleOldPath.toString())); + if (!Files.isDirectory(basePath.resolve(OLD_DIR))) { + Files.createDirectory(basePath.resolve(OLD_DIR)); + } + Files.move(modulePath, moduleOldPath); + } + + // Installing new module + System.out.println(String.format("install %s %s", moduleName, newVersion)); + Files.move(moduleNewPath, modulePath, StandardCopyOption.REPLACE_EXISTING); + } + } + + private String getVersion(Path path) throws IOException { + + // Return the version of a module from version.appup file + Path versionFile = path.resolve(VERSION_FILE); + List<String> lines = Files.readAllLines(versionFile, StandardCharsets.UTF_8); + if (lines == null || lines.isEmpty()) { + throw new IOException(versionFile.toString() + " is empty"); + } + return lines.get(0); + } + + private void cleanPath(Path path) throws IOException { + if (Files.isDirectory(path)) { + Files.walkFileTree(path, new RecursiveDeleteFileVisitor()); + } + } + + private void cleanPath(Path path, String glob) throws IOException { + if (Files.isDirectory(path)) { + Files.walkFileTree(path, new RecursiveDeleteFileVisitor(glob)); + } + } + + private void cleanObsoleteFiles() throws IOException { + + if (windowsOS) { + + // Delete obsolete batch files + Files.deleteIfExists(baseDir.resolve("tutti.bat")); + Files.deleteIfExists(baseDir.resolve(APPLICATION_DIR).resolve("launch.bat")); + + // Delete non Windows files + cleanPath(baseDir, "*" + BATCH_UNIX_EXT); + } else { + + // Delete obsolete script files + Files.deleteIfExists(baseDir.resolve(APPLICATION_DIR).resolve("launch.sh")); + + // Delete Windows files + cleanPath(baseDir, "*" + BATCH_WINDOWS_EXT); + cleanPath(baseDir, "*.exe"); + } + + // Delete embedded files + Files.deleteIfExists(baseDir.resolve(APPLICATION_DIR).resolve("tutti.exe")); + Files.deleteIfExists(baseDir.resolve(APPLICATION_DIR).resolve("tutti.sh")); + cleanPath(baseDir.resolve(APPLICATION_DIR).resolve("launcher")); + + } + + + private class RecursiveDeleteFileVisitor extends SimpleFileVisitor<Path> { + + PathMatcher matcher; + + public RecursiveDeleteFileVisitor() { + // no matcher == delete all + } + + public RecursiveDeleteFileVisitor(String glob) { + super(); + + // Create a matcher according the glob parameter + matcher = baseDir.getFileSystem().getPathMatcher("glob:" + glob); + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + + // If the file name matches the glob or if no matcher, delete the file + if (matcher == null || matcher.matches(file.getFileName())) { + Files.deleteIfExists(file); + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + + // if no matcher, delete the directory + if (matcher == null) { + Files.deleteIfExists(dir); + } + + return FileVisitResult.CONTINUE; + } + + } + +} diff --git a/tutti-ui-swing-updater/src/main/resources/update_runtime.bat b/tutti-ui-swing-updater/src/main/resources/update_runtime.bat new file mode 100644 index 0000000..e398c2b --- /dev/null +++ b/tutti-ui-swing-updater/src/main/resources/update_runtime.bat @@ -0,0 +1,31 @@ +@echo off +verify on +cd /d %~dp0% +set t=%time::=% +set BACKUP_DATE=%date:~6,4%%date:~3,2%%date:~0,2%%t:~0,6% +if not exist \"NEW\\jre\" goto launcher + set /p oldVersion=<jre\\version.appup + set /p newVersion=<NEW\\jre\\version.appup + set backupdir=OLD\\jre-%oldVersion%-%BACKUP_DATE% + echo Update jre version %oldVersion% to %newVersion% old jre keep in \"%backupdir%\" + if not exist \"OLD\" mkdir OLD + move /Y jre \"%backupdir%\" + move /Y NEW\\jre jre + +:launcher +if not exist \"NEW\\launcher\" goto end + set /p oldVersion=<launcher\\version.appup + set /p newVersion=<NEW\\launcher\\version.appup + set backupdir=OLD\\launcher-%oldVersion%-%BACKUP_DATE% + echo Update launcher version %oldVersion% to %newVersion% old launcher keep in \"%backupdir%\" + if not exist \"OLD\" mkdir OLD + move /Y tutti.exe launcher\\tutti.exe + move /Y launcher \"%backupdir%\" + move /Y NEW\\launcher launcher + del /F /Q launcher\\*.sh + move /Y launcher\\tutti.exe tutti.exe + +:end +if exist NEW\\jre rmdir /S /Q NEW\\jre +if exist NEW\\launcher rmdir /S /Q NEW\\launcher +start tutti.exe \ No newline at end of file diff --git a/tutti-ui-swing-updater/src/main/resources/update_runtime.sh b/tutti-ui-swing-updater/src/main/resources/update_runtime.sh new file mode 100644 index 0000000..a5f39e4 --- /dev/null +++ b/tutti-ui-swing-updater/src/main/resources/update_runtime.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +BACKUP_DATE=$(date +%Y%m%d%H%M%S) +if [ -d NEW/jre ]; then + oldVersion=`cat jre/version.appup` + newVersion=`cat NEW/jre/version.appup` + echo \"Update jre version $oldVersion to $newVersion\" + mkdir -p OLD + backupdir=OLD/jre-$oldVersion-$BACKUP_DATE + echo \"Backup jre old version to $backupdir\" + mv jre $backupdir + mv NEW/jre . +fi + +chmod +x jre/bin/java +if [ ! -h java ]; then + ln -sr jre/bin/java java +fi + +if [ -d NEW/launcher ]; then + oldVersion=`cat launcher/version.appup` + newVersion=`cat NEW/launcher/version.appup` + echo \"Update launcher version $oldVersion to $newVersion\" + mkdir -p OLD + backupdir=OLD/launcher-$oldVersion-$BACKUP_DATE + echo \"Backup launcher old version to $backupdir\" + mv tutti.sh launcher/ + mv launcher $backupdir + mv NEW/launcher . + rm launcher/*.exe + rm launcher/*.bat + mv launcher/tutti.sh . + chmod +x tutti.sh +fi + +./tutti.sh \ No newline at end of file -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.