-
Notifications
You must be signed in to change notification settings - Fork 598
/
CmdInfo.groovy
354 lines (304 loc) · 11.1 KB
/
CmdInfo.groovy
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
/*
* Copyright 2013-2024, Seqera Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.cli
import java.lang.management.ManagementFactory
import java.nio.file.spi.FileSystemProvider
import com.beust.jcommander.Parameter
import com.beust.jcommander.Parameters
import com.sun.management.OperatingSystemMXBean
import groovy.json.JsonOutput
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import nextflow.BuildInfo
import nextflow.exception.AbortOperationException
import nextflow.plugin.DefaultPlugins
import nextflow.plugin.Plugins
import nextflow.scm.AssetManager
import nextflow.util.MemoryUnit
import nextflow.util.Threads
import org.yaml.snakeyaml.Yaml
/**
* CLI sub-command INFO
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@Slf4j
@CompileStatic
@Parameters(commandDescription = "Print project and system runtime information")
class CmdInfo extends CmdBase {
static final public NAME = 'info'
private PrintStream out = System.out
@Parameter(description = 'project name')
List<String> args
@Parameter(names='-d',description = 'Show detailed information', arity = 0)
boolean detailed
@Parameter(names='-dd', hidden = true, arity = 0)
boolean moreDetailed
@Parameter(names='-o', description = 'Output format, either: text (default), json, yaml')
String format
@Parameter(names=['-u','-check-updates'], description = 'Check for remote updates')
boolean checkForUpdates
@Override
final String getName() { NAME }
@Override
void run() {
int level = moreDetailed ? 2 : ( detailed ? 1 : 0 )
if( !args ) {
println getInfo(level)
return
}
Plugins.init()
final manager = new AssetManager(args[0])
if( !manager.isLocal() )
throw new AbortOperationException("Unknown project `${args[0]}`")
if( !format || format == 'text' ) {
printText(manager,level)
return
}
def map = createMap(manager)
if( format == 'json' ) {
printJson(map)
}
else if( format == 'yaml' ) {
printYaml(map)
}
else
throw new AbortOperationException("Unknown output format: $format");
}
protected printText(AssetManager manager, int level) {
final manifest = manager.getManifest()
out.println " project name: ${manager.project}"
out.println " repository : ${manager.repositoryUrl}"
out.println " local path : ${manager.localPath}"
out.println " main script : ${manager.mainScriptName}"
if( manager.homePage && manager.homePage != manager.repositoryUrl )
out.println " home page : ${manager.homePage}"
if( manifest.description )
out.println " description : ${manifest.description}"
if( manifest.author )
out.println " author : ${manifest.author}"
def revs = manager.getRevisions(level)
if( revs.size() == 1 )
out.println " revision : ${revs[0]}"
else {
out.println " revisions : "
revs.each { out.println " $it" }
}
if( !checkForUpdates )
return
def updates = manager.getUpdates(level)
if( updates ) {
if( updates.size() == 1 && revs.size() == 1 )
out.println " updates : ${updates[0]}"
else {
out.println " updates : "
updates.each { out.println " $it" }
}
}
out.flush()
}
protected Map createMap(AssetManager manager) {
def result = [:]
result.projectName = manager.project
result.repository = manager.repositoryUrl
result.localPath = manager.localPath?.toString()
result.manifest = manager.manifest.toMap()
result.revisions = manager.getBranchesAndTags(checkForUpdates)
return result
}
protected printJson(Map map) {
out.println JsonOutput.prettyPrint(JsonOutput.toJson(map))
out.flush()
}
protected printYaml(Map map) {
out.println new Yaml().dump(map).toString()
out.flush()
}
final static private BLANK = ' '
final static private NEWLINE = '\n'
static String status(boolean detailed = false) {
getInfo( detailed ? 1 : 0, true )
}
static private String totMem(OperatingSystemMXBean os) {
try {
new MemoryUnit(os.totalPhysicalMemorySize).toString()
}
catch (Throwable t) {
log.debug "Unable to fetch totalPhysicalMemorySize - ${t.message ?: t}"
return '-'
}
}
static private String freeMem(OperatingSystemMXBean os) {
try {
return new MemoryUnit(os.freePhysicalMemorySize).toString()
}
catch (Throwable t) {
log.debug "Unable to fetch freePhysicalMemorySize - ${t.message ?: t}"
return '-'
}
}
static private String totSwap(OperatingSystemMXBean os) {
try {
new MemoryUnit(os.totalSwapSpaceSize).toString()
}
catch (Throwable t) {
log.debug "Unable to fetch totalSwapSpaceSize - ${t.message ?: t}"
return '-'
}
}
static private String freeSwap(OperatingSystemMXBean os) {
try {
return new MemoryUnit(os.freeSwapSpaceSize).toString()
}
catch (Throwable t) {
log.debug "Unable to fetch freeSwapSpaceSize - ${t.message ?: t}"
return '-'
}
}
/**
* @return A string containing some system runtime information
*/
static String getInfo(int level, boolean printProc=false) {
def props = System.getProperties()
def result = new StringBuilder()
result << BLANK << "Version: ${BuildInfo.version} build ${BuildInfo.buildNum}" << NEWLINE
result << BLANK << "Created: ${BuildInfo.timestampUTC} ${BuildInfo.timestampDelta}" << NEWLINE
result << BLANK << "System: ${props['os.name']} ${props['os.version']}" << NEWLINE
result << BLANK << "Runtime: Groovy ${GroovySystem.getVersion()} on ${System.getProperty('java.vm.name')} ${props['java.runtime.version']}" << NEWLINE
result << BLANK << "Encoding: ${System.getProperty('file.encoding')} (${System.getProperty('sun.jnu.encoding')})" << NEWLINE
if( printProc ) {
final os = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()
result << BLANK << "Process: ${ManagementFactory.getRuntimeMXBean().getName()} " << getLocalAddress() << NEWLINE
result << BLANK << "CPUs: ${os.availableProcessors} - Mem: ${totMem(os)} (${freeMem(os)}) - Swap: ${totSwap(os)} (${freeSwap(os)})"
if( Threads.useVirtual() )
result << " - Virtual threads ON"
}
if( level == 0 )
return result.toString()
List<String> capsule = []
List<String> args = []
ManagementFactory
.getRuntimeMXBean()
.getInputArguments()
.each { String it ->
if( it.startsWith('-Dcapsule.'))
capsule << it.substring(2)
else
args << it
}
// file system
result << BLANK << "File systems: "
result << FileSystemProvider.installedProviders().collect { it.scheme }.join(', ')
result << NEWLINE
// plugins
result << BLANK << "Core plugins: "
result << DefaultPlugins.INSTANCE.toSortedString(', ')
result << NEWLINE
// JVM options
result << BLANK << "JVM opts:" << NEWLINE
for( String entry : args ) {
int p = entry.indexOf('=')
if( p!=-1 ) {
def key = entry.substring(0,p)
def value = entry.substring(p+1)
dump(key,value,2, result)
}
else {
dump(entry,null,2, result)
}
}
// Capsule options
dump("Capsule", capsule, 1, result)
// Env
result << BLANK << "Environment:" << NEWLINE
def entries = System.getenv().keySet().sort()
for( def key : entries ) {
if( key.startsWith('NXF_') || level>1 ) {
dump( key, System.getenv().get(key), 2, result )
}
}
// java properties
if( level>1 ) {
result << BLANK << "Properties:" << NEWLINE
entries = System.getProperties().keySet().sort()
for( String key : entries ) {
if( key == 'java.class.path' ) continue
dump( key, System.getProperties().get(key), 2, result )
}
}
// Class path
dump("Class-path" , System.getProperty('java.class.path'), 1, result)
// final string
return result.toString()
}
static private void dump(String key, def value, int indent, StringBuilder result) {
if( value instanceof String && value.count(':') && (key=~/.*PATH$/ || key=='PERL5LIB' || key.contains('.path') || key.contains('-path') || key.contains('.dir')) ) {
value = value.split(":") as Collection
}
else if( value?.class?.isArray() ) {
value = value as Collection
}
if ( value instanceof Collection ) {
blanks(indent, result)
result << key << (indent==1 ? ':' : '=')
for( String item : value ) {
result << NEWLINE
blanks(indent+1, result)
result << item
}
}
else if( value ) {
blanks(indent, result)
result << key << (indent==1 ? ':' : '=')
if( value == '\n' ) {
result << '\\n'
}
else if( value == '\r' ) {
result << '\\r'
}
else if( value == '\n\r' ) {
result << '\\n\\r'
}
else if( value == '\r\n' ) {
result << '\\r\\n'
}
else {
result << value
}
}
else if( key ) {
blanks(indent, result)
result << key
}
result << NEWLINE
}
static private blanks( int n, StringBuilder result ) {
for( int i=0; i<n; i++ ) {
result << BLANK
}
}
/**
* @return A string holding the local host name and address used for logging
*/
static private getLocalAddress() {
try {
return "[${InetAddress.getLocalHost().getHostAddress()}]"
}
catch(Exception e) {
return null
}
}
}