File: svn_commit_segmentation.groovy

package info (click to toggle)
omegat 3.6.0.10%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 63,728 kB
  • sloc: xml: 114,044; java: 74,758; sh: 174; javascript: 108; makefile: 22
file content (398 lines) | stat: -rw-r--r-- 13,930 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/** :name=SVN - Commit Segmentation File :description=Commit segmentation file change to SVN remote repository
 *
 *  Commit segmentation file change to SVN remote repository
 *
 * Supported basic operations are shown in the next list.
 *
 * 1. svn add for added (unversioned) segmentation.conf.
 * 2. svn delete for deleting (missing) segmentation.conf.
 * 3. modified segmentation.conf.
 *
 * copy, move, merge or any other svn commands are not available.
 *
 * For project managers, you can move this script to
 * <your-scripts-folder>/project_changed/ sub-folder
 * for event driven automatically execution.
 *
 * @author  Yu Tang
 * @date    2014-09-12
 * @version 0.1
 */

// User defined settings

// If show ask prompt before commit for manually execution.
// This setting is ignored when automatically execution by project load event.
boolean askBeforeCommit = true // or false


import groovy.transform.Synchronized

import org.omegat.core.Core
import org.omegat.core.data.IProject
import org.omegat.core.segmentation.SRX
import org.omegat.core.team.SVNRemoteRepository
import org.omegat.gui.scripting.ScriptLogger
import org.omegat.util.Log
import org.tmatesoft.svn.core.SVNCommitInfo
import org.tmatesoft.svn.core.wc.SVNStatusType
import org.tmatesoft.svn.core.wc2.SvnCommit
import org.tmatesoft.svn.core.wc2.SvnGetStatus
import org.tmatesoft.svn.core.wc2.SvnOperationFactory
import org.tmatesoft.svn.core.wc2.SvnStatus
import org.tmatesoft.svn.core.wc2.SvnTarget
import org.tmatesoft.svn.core.wc2.SvnScheduleForAddition
import org.tmatesoft.svn.core.wc2.SvnScheduleForRemoval

import static javax.swing.JOptionPane.*
import static org.omegat.core.events.IProjectEventListener.PROJECT_CHANGE_TYPE.LOAD
import static org.omegat.util.FileUtil.computeRelativePath
import static org.omegat.util.Preferences.TEAM_AUTHOR
import static org.omegat.util.Preferences.getPreferenceDefault
import static org.tmatesoft.svn.core.wc.SVNStatusType.*

String THIS_SCRIPT_NAME = 'svn_commit_segmentation.groovy'
boolean isCalledByProjectEvent = binding.variables.containsKey('eventType')
String trigger
ILogger logger

if (isCalledByProjectEvent) {
    if (eventType == LOAD && SVNRemoteRepository.isSVNDirectory(new File(project.projectProperties.projectRoot))) {
        // Execute by Project Load event
        trigger = 'Project LOAD event'
        logger = new BasicLogger(console: console, thisScriptName: THIS_SCRIPT_NAME)
    } else {
        // No need executing
        return
    }
} else {
    // Execute by user
    trigger = 'user'
    logger = new InteractiveLogger(console: console, thisScriptName: THIS_SCRIPT_NAME)
}

def committer = new SegmentationCommitter(isCalledByProjectEvent || !askBeforeCommit, project, logger)
if (committer.committable()) {
    console.println "$THIS_SCRIPT_NAME is executed by $trigger (${(new Date()).timeString})"
    Thread.start {
        SegmentationCommitter.ExecutionStatus ret = committer.execute()
    }
    SegmentationCommitter.ExecutionStatus.COMMIT_EXECUTED_AND_WAIT
} else {
    logger.abortUpload SegmentationCommitter.ExecutionStatus.NO_COMMIT_ITEMS_FOUND.toString()
    return
}

/**
 * Commit segmentation.conf if modified
 */
class SegmentationCommitter {

// enum for execution status
    enum ExecutionStatus {
        NOW_EXECUTING('Now executing...'),
        COMMIT_EXECUTED('Your commit is executed.'),
        COMMIT_EXECUTED_AND_WAIT('Your commit is executed. Wait a little while and see detailed information.'),
        COMMIT_NOT_EXECUTED('Your commit is not executed.'),
        EMPTY_PROJECT('A project is not loaded yet.'),
        SVN_REPOSITORY_NOT_AVAILABLE('Writable SVN repository is not available.'),
        SEG_CONF_NOT_AVAILABLE('Segmentation config file is not available.'),
        NO_COMMIT_ITEMS_FOUND('No commit items found.'),
        UNEXPECTED_SVN_STATUS('Unexpected SVN status.'),
        COMMIT_CANCELED('SVN Commit operation has been canceled by user.'),
        COMMIT_FAILED('SVN Commit operation has been failed.');

        private final String message
        private ExecutionStatus(String message) {this.message = message}
        @Override
        String toString() {this.message}
    }

// return value constants (for v3.1.0 or later)
    //final def RET_VAL_COMMIT_NOT_EXECUTED = "Your commit is not executed."
    //final def RET_VAL_COMMIT_EXECUTED = "Your commit is executed. Wait a little while and see detailed information."

// fields
    private final boolean askBeforeCommit
    private final IProject project
    private final File file
    private final ILogger logger
    private final Closure<Integer> askUserForCommit

// Constructors
    SegmentationCommitter(boolean forceCommit, IProject project, ILogger logger) {
        this.askBeforeCommit = askBeforeCommit
        this.project = project
        this.file = getTargetFile(project)
        this.logger = logger
        this.askUserForCommit = getAskUserForCommit(forceCommit)
    }

    boolean committable() {
        SVNStatusType status = file.getSvnNodeStatus()
        switch (status) {
            case STATUS_UNVERSIONED:
            case STATUS_MISSING:
            case STATUS_MODIFIED:
            case STATUS_ADDED:   // just in case
            case STATUS_DELETED: // just in case
                // OK
                return true
            case STATUS_NORMAL:
                // exit if no need commit
                return false
            default:
                // illegal state
                logger.log "Unexpected SVN status '$status.'"
                return false
        }
    }

    @Synchronized
    ExecutionStatus execute() {
        ExecutionStatus executionStatus = initialExecutionStatus
        if (executionStatus != ExecutionStatus.NOW_EXECUTING) {
            logger.log executionStatus
            return executionStatus
        }

        SVNStatusType status = file.getSvnNodeStatus()
        def prepare = { /* do nothing for default */ }
        switch (status) {
            case STATUS_UNVERSIONED:
                prepare = {
                    logger.log 'file.svnAdd()'
                    file.svnAdd()
                }
                break
            case STATUS_MISSING:
                prepare = {
                    logger.log 'file.svnDelete()'
                    file.svnDelete()
                }
                break
            case STATUS_MODIFIED:
            case STATUS_ADDED:   // just in case
            case STATUS_DELETED: // just in case
                // OK
                break
            case STATUS_NORMAL:
                // exit if no need commit
                executionStatus = ExecutionStatus.NO_COMMIT_ITEMS_FOUND
                logger.log executionStatus
                return executionStatus
            default:
                // illegal state
                executionStatus = ExecutionStatus.UNEXPECTED_SVN_STATUS
                logger.log "Unexpected SVN status '$status.'"
                return executionStatus
        }

// ask user for commit
        Core.autoSave.disable()
        File rootDir = new File(project.projectProperties.projectRoot)
        def commitStatusString = getCommitStatusString(status, file, rootDir)
        int ret = askUserForCommit(commitStatusString)
        if (ret == CANCEL_OPTION) {
            executionStatus = ExecutionStatus.COMMIT_CANCELED
            logger.log executionStatus
            Core.autoSave.enable()
            return executionStatus
        }

// prepared for commit
        prepare()

// commit
        logger.startUpload()
        try {
            SVNCommitInfo info = file.commit(getCommitMessage(status))
            if (info.errorMessage) {
                throw new IOException(info.errorMessage.message)
            }
            executionStatus = ExecutionStatus.COMMIT_EXECUTED
            logger.finishUpload info, commitStatusString
        } catch (ex) {
            executionStatus = ExecutionStatus.COMMIT_FAILED
            logger.finishUpload ex.message
        } finally {
            Core.autoSave.enable()
        }

// exit
        executionStatus
    }

    File getTargetFile(IProject project) {
        if (!project.projectLoaded) {
            return null
        }

        File file = new File(project.projectProperties.projectInternal, SRX.CONF_SENTSEG)
        file.metaClass {
            svnAdd = { ->
                SvnOperationFactory svnOperationFactory = new SvnOperationFactory()
                try {
                    SvnScheduleForAddition op = svnOperationFactory.createScheduleForAddition()
                    op.setSingleTarget(SvnTarget.fromFile(delegate as File))
                    op.run()
                } finally {
                    svnOperationFactory.dispose()
                }
            }

            svnDelete = { ->
                SvnOperationFactory svnOperationFactory = new SvnOperationFactory()
                try {
                    SvnScheduleForRemoval op = svnOperationFactory.createScheduleForRemoval()
                    op.setSingleTarget(SvnTarget.fromFile(delegate as File))
                    op.run()
                } finally {
                    svnOperationFactory.dispose()
                }
            }

            commit = { String commitMessage ->
                SvnOperationFactory svnOperationFactory = new SvnOperationFactory()
                SVNCommitInfo ret = null
                try {
                    SvnCommit op = svnOperationFactory.createCommit()
                    op.setSingleTarget(SvnTarget.fromFile(delegate as File))
                    op.setCommitMessage(commitMessage)
                    ret = op.run()
                } finally {
                    svnOperationFactory.dispose()
                }
                ret
            }

            getSvnNodeStatus = { ->
                SvnOperationFactory svnOperationFactory = new SvnOperationFactory()
                SvnStatus stat = null
                try {
                    SvnGetStatus getStatus = svnOperationFactory.createGetStatus()
                    getStatus.setSingleTarget(SvnTarget.fromFile(delegate as File))
                    stat = getStatus.run()
                } catch (e) {
                    throw new RuntimeException(e.message)
                } finally {
                    svnOperationFactory.dispose()
                }

                if (stat == null) {
                    throw new RuntimeException('Segmentation config file is not available.')
                }
                stat.nodeStatus
            }
        }
        file
    }

// CommitMessage
    static String getCommitMessage(SVNStatusType status) {
        def author = getPreferenceDefault(TEAM_AUTHOR, System.getProperty('user.name'))
        def verb = status.toString().capitalize()
        "$verb segmentation file by $author"
    }

// validateState
    private ExecutionStatus getInitialExecutionStatus() {
        if (!project.projectLoaded) {
            return ExecutionStatus.EMPTY_PROJECT
        }
        if (!SVNRemoteRepository.isSVNDirectory(new File(project.projectProperties.projectRoot))) {
            return ExecutionStatus.SVN_REPOSITORY_NOT_AVAILABLE
        }
        ExecutionStatus.NOW_EXECUTING
    }

// ask user for commit
    static Closure<Integer> getAskUserForCommit(boolean forceOK) {
        if (forceOK) {
            { String commitStatusString ->
                OK_OPTION
            }
        } else {
            { String commitStatusString ->
                def title = 'Commit segmentation file'
                def msg = "Your commit includes this change:\n" + commitStatusString
                Core.mainWindow.showConfirmDialog(msg, title, OK_CANCEL_OPTION, PLAIN_MESSAGE)
            }
        }
    }

// commit status string
    static String getCommitStatusString(SVNStatusType status, File commitItem, File rootDir) {
        def path = computeRelativePath(rootDir, commitItem)
        def op = "Unk"
        switch (status) {
        // add
            case STATUS_UNVERSIONED:
            case STATUS_ADDED:
                op = "Add"
                break
        // delete
            case STATUS_MISSING:
            case STATUS_DELETED:
                op = "Del"
                break
        // modified
            case STATUS_MODIFIED:
                op = "Modified"
                break
        }
        "\n$op (file) $path"
    }
}

interface ILogger {
    void log(arg)
    void startUpload()
    void finishUpload(SVNCommitInfo info, String commitStatusString)
    void finishUpload(String exceptionMessage)
}

class BasicLogger implements ILogger {
    ScriptLogger console
    String thisScriptName

    void log(arg) {console.println(arg)}

    void startUpload() {
        Log.logInfoRB 'SVN_START', 'upload'
        Core.mainWindow.showStatusMessageRB 'TEAM_SYNCHRONIZE'
        this.log 'Now uploading ...'
    }

    void abortUpload(String message) {
        // in case no commit items found
    }

    void finishUpload(SVNCommitInfo info, String commitStatusString) {
        console.println "Commit completed.$commitStatusString"
        Log.logDebug SVNRemoteRepository.LOGGER, "SVN committed followed file into new revision {0}$commitStatusString", info.newRevision
        Log.logInfoRB "SVN_FINISH", "upload"
        Core.mainWindow.showStatusMessageRB null
    }

    void finishUpload(String exceptionMessage) {
        Log.logErrorRB "SVN_ERROR", "upload", exceptionMessage
        this.log "Commit failed: $exceptionMessage"
        Core.mainWindow.showStatusMessageRB "SVN_ERROR", "upload", exceptionMessage
    }
}

class InteractiveLogger extends BasicLogger {
    @Override
    void finishUpload(String exceptionMessage) {
        super.finishUpload exceptionMessage
        Core.mainWindow.showMessageDialog exceptionMessage
    }

    @Override
    void abortUpload(String message) {
        this.log message
    }
}