-
Notifications
You must be signed in to change notification settings - Fork 15
/
xmipp
executable file
·422 lines (355 loc) · 17 KB
/
xmipp
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env python3
# ***************************************************************************
# * Authors: Alberto García ([email protected])
# * Martín Salinas ([email protected])
# *
# *
# * 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; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# * 02111-1307 USA
# *
# * All comments concerning this program package may be sent to the
# * e-mail address '[email protected]'
# ***************************************************************************/
# General imports
import argparse, sys, os, multiprocessing
from typing import Dict
# Installer imports
from installer.constants import (MODE_ALL, MODE_COMPILE_AND_INSTALL, MODE_CONFIG, MODE_CLEAN_BIN,
MODE_CLEAN_ALL, MODE_VERSION, MODE_GET_MODELS, MODE_TEST, MODE_GIT, MODE_ADD_MODEL, MODE_CONFIG_BUILD,
COMMON_USAGE_HELP_MESSAGE, DEFAULT_MODELS_DIR, CONFIG_FILE, CMAKE_INSTALL_PREFIX,
CMAKE_CONFIGURE_ERROR, CMAKE_COMPILE_ERROR, CMAKE_INSTALL_ERROR, PARAM_LOGIN, PARAM_SHORT,
PARAM_JOBS, PARAM_BRANCH, PARAM_GIT_COMMAND, PARAM_TEST_PRO, PARAM_TEST_PRO, PARAM_TEST_FUNC, PARAM_MODEL_PATH,
PARAM_MODELS_DIRECTORY, PARAM_KEEP_OUTPUT, PARAM_SHOW_TESTS, PARAM_TEST_NAME, PARAM_UPDATE,
PARAM_OVERWRITE, BUILD_PATH, INSTALL_PATH, BUILD_TYPE, SOURCES_PATH, XMIPP_SOURCES, XMIPP,
LOG_FILE, CMAKE_ERROR, MODE_GET_SOURCES, VERSION_FILE, PARAMS, LONG_VERSION, SHORT_VERSION, SEND_INSTALLATION_STATISTICS)
from installer.utils import runStreamingJob, runJob
from installer.parser import ModeHelpFormatter, GeneralHelpFormatter, ErrorHandlerArgumentParser, getParamNames
from installer.config import readConfig, writeConfig
from installer.cmake import getCMake, getCMakeVarsStr
from installer.main import getSources, exitXmipp, handleRetCode, getSectionMessage, getSuccessMessage, getVersionMessage
from installer.logger import logger, yellow, red, blue
from installer.api import sendApiPOST
from installer.test import runTests
####################### EXECUTION MODES #######################
def __getProjectRootDir() -> str:
"""
### Returns the root directory of Xmipp.
#### Returns:
- (str): Absolute path to Xmipp's root directory.
"""
return os.path.dirname(os.path.abspath(__file__))
####################### EXECUTION MODES #######################
def modeAddModel(args: argparse.Namespace):
"""
### Checks the params for execution mode "addModel" and then runs it.
#### Params:
- args (Namespace): Command line arguments parsed by argparse library.
"""
pass
def modeCleanBin():
"""
### Removes all compiled binaries.
#### Params:
- args (Namespace): Command line arguments parsed by argparse library.
"""
runJob(f"find {SOURCES_PATH}/* -name \"*.so\" -exec rm -rf {{}} \;", showCommand=True)
runJob(f"find {SOURCES_PATH}/* -name \"*.os\" -exec rm -rf {{}} \;", showCommand=True)
runJob(f"find {SOURCES_PATH}/* -name \"*.o\" -exec rm -rf {{}} \;", showCommand=True)
runJob("find . -iname \"*.pyc\" -delete", showCommand=True)
runJob(f"rm -rf {CONFIG_FILE} {BUILD_PATH}", showCommand=True)
runJob("find . -iname \"*.dblite\" -delete", showCommand=True)
runJob(f"find {os.path.join(SOURCES_PATH, XMIPP, 'applications', 'programs')} --type d -empty", showCommand=True)
def modeCleanAll():
"""
### Removes all compiled binaries and cloned sources.
#### Params:
- args (Namespace): Command line arguments parsed by argparse library.
"""
# Print warning text and await input
confirmationText = 'YeS'
warningStr = "WARNING: This will DELETE ALL content from src and build.\n"
warningStr += "\tNotice that if you have unpushed changes, they will be deleted.\n"
warningStr += f"\nIf you are sure you want to do this, type '{confirmationText}' (case sensitive):"
logger(yellow(warningStr), forceConsoleOutput=True)
try:
userInput = input()
except KeyboardInterrupt:
userInput = ''
logger("", forceConsoleOutput=True)
if userInput == confirmationText:
# Get xmipp sources
xmippSources = [os.path.join(SOURCES_PATH, source) for source in XMIPP_SOURCES]
# Get installation path
configDict = readConfig(CONFIG_FILE) if os.path.exists(CONFIG_FILE) else {}
installDir = configDict.get(CMAKE_INSTALL_PREFIX, INSTALL_PATH)
runJob(f"rm -rf {BUILD_PATH} {installDir} {' '.join(xmippSources)} {CONFIG_FILE}", showCommand=True)
else:
logger(red("Operation cancelled."), forceConsoleOutput=True)
def modeCompileAndInstall(args: argparse.Namespace, configDict: Dict={}):
"""
### Checks the params for execution mode "compileAndInstall" and then runs it.
#### Params:
- args (Namespace): Command line arguments parsed by argparse library.
- configDict (dict): Optional. If provided, it will use it's variables. If not, it will read from config file.
"""
# If config variable dictionary is not received this mode is the first being executed
if not configDict:
logger.startLogFile(LOG_FILE)
configDict = readConfig(CONFIG_FILE)
if not getCMake(configDict):
apiPost = True if configDict.get(SEND_INSTALLATION_STATISTICS) == 'ON' else False
handleRetCode(CMAKE_ERROR, predefinedErrorCode=CMAKE_ERROR, sendAPI=apiPost)
# Compile with CMake
cmakeExecutable = getCMake(configDict)
logger(getSectionMessage("Compiling with CMake"), forceConsoleOutput=True)
retCode = runStreamingJob(f"{cmakeExecutable} --build {BUILD_PATH} --config {BUILD_TYPE} -j {args.jobs}",
showOutput=True, substitute=True)
apiPost = True if configDict.get(
SEND_INSTALLATION_STATISTICS) == 'ON' else False
handleRetCode(retCode, predefinedErrorCode=CMAKE_COMPILE_ERROR, sendAPI=apiPost)
# Install with CMake
logger(getSectionMessage("Installing with CMake"), forceConsoleOutput=True)
retCode = runStreamingJob(f"{cmakeExecutable} --install {BUILD_PATH} --config {BUILD_TYPE}",
showOutput=True, substitute=True)
handleRetCode(retCode, predefinedErrorCode=CMAKE_INSTALL_ERROR, sendAPI=apiPost)
def modeConfigBuild(configDict: Dict={}, sendAPIPost: bool=False):
"""
### Configures the project using CMake.
#### Params:
- args (Namespace): Command line arguments parsed by argparse library.
- configDict (dict): Optional. If provided, it will use it's variables. If not, it will read from config file.
"""
# If config variable dictionary is not received this mode is the first being executed
if not configDict:
logger.startLogFile(LOG_FILE)
configDict = readConfig(CONFIG_FILE) if os.path.exists(CONFIG_FILE) else modeConfig()
# Check if CMake exists
cmakeExecutable = getCMake(configDict)
if not getCMake(configDict):
handleRetCode(CMAKE_ERROR, predefinedErrorCode=CMAKE_ERROR, sendAPI=sendAPIPost)
logger(getSectionMessage("Configuring with CMake"), forceConsoleOutput=True)
configureCmd = f"{cmakeExecutable} -S . -B {BUILD_PATH} -D CMAKE_BUILD_TYPE={BUILD_TYPE}"
configureCmd += f" {getCMakeVarsStr(configDict)}"
retCode = runStreamingJob(configureCmd, showOutput=True, substitute=True)
handleRetCode(retCode, predefinedErrorCode=CMAKE_CONFIGURE_ERROR, sendAPI=sendAPIPost)
def modeConfig(overwrite: bool=False) -> Dict:
"""
### Generates a template config file.
#### Params:
- overwrite (bool): If True, file is created from scratch with default values.
#### Returns:
- (dict): Dictionary containig all config variables.
"""
configDict = {}
if not overwrite and os.path.exists(CONFIG_FILE):
configDict = readConfig(CONFIG_FILE)
writeConfig(CONFIG_FILE, configDict=configDict)
return readConfig(CONFIG_FILE)
def modeGetModels(args: argparse.Namespace):
"""
### Checks the params for execution mode "getModels" and then runs it.
#### Params:
- args (Namespace): Command line arguments parsed by argparse library.
"""
pass
def modeGetSources(args: argparse.Namespace):
"""
### Downloads all Xmipp's sources.
#### Params:
- args (Namespace): Command line arguments parsed by argparse library.
"""
# Clone sources from specified branch
getSources(branch=args.branch)
def modeGit(args: argparse.Namespace):
"""
### Executes the given git command into all xmipp source repositories.
#### Params:
- args (Namespace): Command line arguments parsed by argparse library.
"""
cmd = f"git {' '.join(args.command)}"
logger(f"Running command '{cmd}' for all xmipp sources...", forceConsoleOutput=True)
XMIPP_SOURCES.insert(0, XMIPP)
for source in XMIPP_SOURCES:
logger("", forceConsoleOutput=True)
# Check if source exists to perform command, else skip
sourcePath = os.path.abspath(os.path.join(SOURCES_PATH, source))
if not os.path.exists(sourcePath):
logger(yellow(f"WARNING: Source {source} does not exist in path {sourcePath}. Skipping."), forceConsoleOutput=True)
continue
logger(blue(f"Running command for {source} in path {sourcePath}..."), forceConsoleOutput=True)
runJob(cmd, cwd=sourcePath, showOutput=True, showError=True)
def modeTest(args: argparse.Namespace):
"""
### Checks the params for execution mode "test" and then runs it.
#### Params:
- parser (ErrorHandlerArgumentParser): Parser object used to parse the arguments.
- args (Namespace): Command line arguments parsed by argparse library.
"""
if args.show == True:
logger("Showing test--------------------------------------", forceConsoleOutput=True)
runTests(PARAMS[PARAM_SHOW_TESTS][LONG_VERSION])
elif args.allPrograms:
logger("Running all tests--------------------------------------", forceConsoleOutput=True)
runTests(PARAMS[PARAM_TEST_PRO][LONG_VERSION])
elif args.allFuncs:
runTests(PARAMS[PARAM_TEST_FUNC][LONG_VERSION])
elif args.testName:
logger("Running test {}-------------------------------".format(args.testName), forceConsoleOutput=True)
runTests(args.testName)
def modeVersion(args: argparse.Namespace):
"""
### Checks the params for execution mode "version" and then runs it.
#### Params:
- args (Namespace): Command line arguments parsed by argparse library.
"""
logger(getVersionMessage(short=args.short), forceConsoleOutput=True)
def modeAll(args: argparse.Namespace):
"""
### Runs get sources & modes config, configBuild, and compileAndInstall
#### Params:
- args (Namespace): Command line arguments parsed by argparse library.
"""
# Initiate logger
logger.startLogFile(LOG_FILE)
# Read and/or write variables from config file
configDict = modeConfig()
# Clone sources from specified branch
modeGetSources(args)
# Configure with CMake
modeConfigBuild(configDict=configDict, sendAPIPost=True)
# Compile and install with CMake
modeCompileAndInstall(args, configDict=configDict)
def runSelectedMode(parser: ErrorHandlerArgumentParser, args: argparse.Namespace):
"""
### This function runs the selected execution mode.
#### Params:
- parser (ErrorHandlerArgumentParser): Parser object used to parse the arguments.
- args (Namespace): Command line arguments parsed by argparse library.
"""
sendAPI = False
if args.mode == MODE_ADD_MODEL:
modeAddModel(args)
elif args.mode == MODE_ALL:
sendAPI = True
modeAll(args)
elif args.mode == MODE_CLEAN_ALL:
modeCleanAll()
elif args.mode == MODE_CLEAN_BIN:
modeCleanBin()
elif args.mode == MODE_COMPILE_AND_INSTALL:
modeCompileAndInstall(args)
sendAPI = True
elif args.mode == MODE_CONFIG_BUILD:
sendAPI = True
modeConfigBuild()
elif args.mode == MODE_CONFIG:
modeConfig(overwrite=args.overwrite)
elif args.mode == MODE_GET_MODELS:
modeGetModels(args)
elif args.mode == MODE_GET_SOURCES:
modeGetSources(args)
elif args.mode == MODE_GIT:
modeGit(args)
elif args.mode == MODE_TEST:
modeTest(args)
elif args.mode == MODE_VERSION:
modeVersion(args)
else:
# If method was none of the above, exit with error
logger(red(f"Mode \"{args.mode}\" not recognized. {COMMON_USAGE_HELP_MESSAGE}"), forceConsoleOutput=True)
exitXmipp(retCode=1)
# Send API message
apiPost = True if readConfig(CONFIG_FILE).get(
SEND_INSTALLATION_STATISTICS) == 'ON' else False
if sendAPI and apiPost and os.path.exists(VERSION_FILE):
sendApiPOST()
# Print success message for specific modes
if args.mode == MODE_ALL or args.mode == MODE_COMPILE_AND_INSTALL:
logger(getSuccessMessage(), forceConsoleOutput=True)
exitXmipp()
####################### MAIN EXECUTION THREAD #######################
if __name__ == "__main__":
""" Calls main function when executed. """
# Defining default jobs: 120% current thread count (not all jobs take 100% of CPU time continuously)
JOBS = multiprocessing.cpu_count() + int(multiprocessing.cpu_count() * 0.2)
# Creating parser to parse the command-line arguments
parser = ErrorHandlerArgumentParser(formatter_class=GeneralHelpFormatter, prog="xmipp")
# Adding subparsers to have other variables deppending on the value of the mode
subparsers = parser.add_subparsers(dest="mode")
# Arguments for mode 'addModel'
addModelSubparser = subparsers.add_parser(MODE_ADD_MODEL, formatter_class=ModeHelpFormatter)
addModelSubparser.add_argument(*getParamNames(PARAM_LOGIN))
addModelSubparser.add_argument(*getParamNames(PARAM_MODEL_PATH))
addModelSubparser.add_argument(*getParamNames(PARAM_UPDATE), action='store_true')
# Arguments for mode 'all'
allSubparser = subparsers.add_parser(MODE_ALL, formatter_class=ModeHelpFormatter)
allSubparser.add_argument(*getParamNames(PARAM_JOBS), type=int, default=JOBS)
allSubparser.add_argument(*getParamNames(PARAM_BRANCH))
allSubparser.add_argument(*getParamNames(PARAM_KEEP_OUTPUT), action='store_true')
# Arguments for mode 'cleanAll'
cleanAllSubparser = subparsers.add_parser(MODE_CLEAN_ALL, formatter_class=ModeHelpFormatter)
# Arguments for mode 'cleanBin'
cleanBinSubparser = subparsers.add_parser(MODE_CLEAN_BIN, formatter_class=ModeHelpFormatter)
# Arguments for mode 'compileAndInstall'
compileAndInstallSubparser = subparsers.add_parser(MODE_COMPILE_AND_INSTALL, formatter_class=ModeHelpFormatter)
compileAndInstallSubparser.add_argument(*getParamNames(PARAM_JOBS), type=int, default=JOBS)
compileAndInstallSubparser.add_argument(*getParamNames(PARAM_BRANCH))
compileAndInstallSubparser.add_argument(*getParamNames(PARAM_KEEP_OUTPUT), action='store_true')
# Arguments for mode 'configBuild'
buildConfigSubparser = subparsers.add_parser(MODE_CONFIG_BUILD, formatter_class=ModeHelpFormatter)
buildConfigSubparser.add_argument(*getParamNames(PARAM_KEEP_OUTPUT), action='store_true')
# Arguments for mode 'config'
configSubparser = subparsers.add_parser(MODE_CONFIG, formatter_class=ModeHelpFormatter)
configSubparser.add_argument(*getParamNames(PARAM_OVERWRITE), action='store_true')
# Arguments for mode 'getModels'
getModelsSubparser = subparsers.add_parser(MODE_GET_MODELS, formatter_class=ModeHelpFormatter)
getModelsSubparser.add_argument(*getParamNames(PARAM_MODELS_DIRECTORY), default=os.path.join(__getProjectRootDir(), DEFAULT_MODELS_DIR))
# Arguments for mode 'getSources'
getSourcesSubparser = subparsers.add_parser(MODE_GET_SOURCES, formatter_class=ModeHelpFormatter)
getSourcesSubparser.add_argument(*getParamNames(PARAM_BRANCH))
getSourcesSubparser.add_argument(*getParamNames(PARAM_KEEP_OUTPUT), action='store_true')
# Arguments for mode 'git'
gitSubparser = subparsers.add_parser(MODE_GIT, formatter_class=ModeHelpFormatter)
gitSubparser.add_argument(*getParamNames(PARAM_GIT_COMMAND), nargs='+')
# Arguments for mode 'test'
testSubparser = subparsers.add_parser(MODE_TEST, formatter_class=ModeHelpFormatter)
testSubparser.add_argument(*getParamNames(PARAM_TEST_NAME), nargs='?', default=None)
testSubparser.add_argument(*getParamNames(PARAM_TEST_PRO), action='store_true')
testSubparser.add_argument(*getParamNames(PARAM_TEST_FUNC), action='store_true')
testSubparser.add_argument(*getParamNames(PARAM_SHOW_TESTS), action='store_true')
# Arguments for mode 'version'
versionSubparser = subparsers.add_parser(MODE_VERSION, formatter_class=ModeHelpFormatter)
versionSubparser.add_argument(*getParamNames(PARAM_SHORT), action='store_true')
# Applying default mode value if needed
if len(sys.argv) == 1 or (
len(sys.argv) > 1 and
sys.argv[1].startswith('-') and
'-h' not in sys.argv and
'--help' not in sys.argv):
sys.argv.insert(1, MODE_ALL)
# Parse arguments
args = parser.parse_args()
# Error control for number of jobs
if hasattr(args, 'jobs') and args.jobs < 1:
parser.error(f"Wrong job number \"{args.jobs}\". Number of jobs has to be 1 or greater.")
# Error control for branch
if hasattr(args, "branch") and args.branch is not None and len(args.branch.split(' ')) > 1:
parser.error(f"Incorrect branch name \"{args.branch}\". Branch names can only be one word long.")
if hasattr(args, "keep_output") and args.keep_output:
logger.setAllowSubstitution(False)
# Running always under this own directory.
os.chdir(__getProjectRootDir())
# Running installer in selected mode
runSelectedMode(parser, args)