def makeTempDir(): import os, random, string, sys from lib.errorMessage import errorMessage from lib.printMessage import printMessage from lib.readMRtrixConfSetting import readMRtrixConfSetting global args, tempDir if args.cont: printMessage('Skipping temporary directory creation due to use of -continue option') return if tempDir: errorMessage('Script error: Cannot use multiple temporary directories') if args.tempdir: dir_path = os.path.abspath(args.tempdir) else: dir_path = readMRtrixConfSetting('TmpFileDir') if not dir_path: if os.name == 'posix': dir_path = '/tmp' else: dir_path = '.' prefix = readMRtrixConfSetting('TmpFilePrefix') if not prefix: prefix = os.path.basename(sys.argv[0]) + '-tmp-' tempDir = dir_path while os.path.isdir(tempDir): random_string = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6)) tempDir = os.path.join(dir_path, prefix + random_string) + os.sep os.makedirs(tempDir) printMessage('Generated temporary directory: ' + tempDir) with open(os.path.join(tempDir, 'cwd.txt'), 'w') as outfile: outfile.write(workingDir + '\n') with open(os.path.join(tempDir, 'command.txt'), 'w') as outfile: outfile.write(' '.join(sys.argv) + '\n')
def initialise(): import argparse, os, random, string, sys from lib.printMessage import printMessage from lib.readMRtrixConfSetting import readMRtrixConfSetting global args, cleanup, lastFile, mrtrixNThreads, mrtrixQuiet, tempDir, verbosity, workingDir global colourClear, colourConsole, colourError, colourPrint, colourWarn workingDir = os.getcwd() args = parser.parse_args() if args.nocleanup: cleanup = False if args.nthreads: mrtrixNThreads = "-nthreads " + args.nthreads if args.quiet: verbosity = 0 mrtrixQuiet = "-quiet" if args.verbose: verbosity = 2 mrtrixQuiet = "" if args.cont: tempDir = os.path.abspath(args.cont[0]) lastFile = args.cont[1] else: if args.tempdir: dir_path = os.path.abspath(args.tempdir) else: dir_path = readMRtrixConfSetting("TmpFileDir") if not dir_path: if os.name == "posix": dir_path = "/tmp" else: dir_path = "." prefix = readMRtrixConfSetting("TmpFilePrefix") if not prefix: prefix = os.path.basename(sys.argv[0]) + "-tmp-" tempDir = dir_path while os.path.isdir(tempDir): random_string = "".join(random.choice(string.ascii_uppercase + string.digits) for x in range(6)) tempDir = os.path.join(dir_path, prefix + random_string) + os.sep os.makedirs(tempDir) printMessage("Generated temporary directory: " + tempDir) with open(os.path.join(tempDir, "cwd.txt"), "w") as outfile: outfile.write(workingDir + "\n") with open(os.path.join(tempDir, "command.txt"), "w") as outfile: outfile.write(" ".join(sys.argv) + "\n") use_colour = readMRtrixConfSetting("TerminalColor") if use_colour: use_colour = use_colour.lower() in ("yes", "true", "1") else: use_colour = not sys.platform.startswith("win") if use_colour: colourClear = "\033[0m" colourConsole = "\033[03;34m" colourError = "\033[01;31m" colourPrint = "\033[03;32m" colourWarn = "\033[00;31m"
def initialise(n): import os, random, string, sys from lib.printMessage import printMessage from lib.readMRtrixConfSetting import readMRtrixConfSetting global cleanup, mrtrixQuiet, numArgs, tempDir, verbosity, workingDir #if not numArgs: # sys.stderr.write('Must set numArgs value before calling initialise()\n') # exit(1) numArgs = n workingDir = os.getcwd() for option in sys.argv[numArgs+1:]: if '-verbose'.startswith(option): verbosity = 2 mrtrixQuiet = '' elif '-quiet'.startswith(option): verbosity = 0 mrtrixQuiet = '-quiet' elif '-nocleanup'.startswith(option): cleanup = False else: sys.stderr.write('Unknown option: ' + option + '\n') exit(1) # Create the temporary directory dir_path = readMRtrixConfSetting('TmpFileDir') if not dir_path: if os.name == 'posix': dir_path = '/tmp' else: dir_path = '.' prefix = readMRtrixConfSetting('TmpFilePrefix') if not prefix: prefix = os.path.basename(sys.argv[0]) + '-tmp-' tempDir = dir_path while os.path.isdir(tempDir): random_string = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6)) tempDir = os.path.join(dir_path, prefix + random_string) + os.sep os.makedirs(tempDir) printMessage('Generated temporary directory: ' + tempDir)
def makeTempDir(): import os, random, string, sys from lib.errorMessage import errorMessage from lib.printMessage import printMessage from lib.readMRtrixConfSetting import readMRtrixConfSetting global args, tempDir if args.cont: printMessage( 'Skipping temporary directory creation due to use of -continue option' ) return if tempDir: errorMessage('Script error: Cannot use multiple temporary directories') if args.tempdir: dir_path = os.path.abspath(args.tempdir) else: dir_path = readMRtrixConfSetting('TmpFileDir') if not dir_path: if os.name == 'posix': dir_path = '/tmp' else: dir_path = '.' prefix = readMRtrixConfSetting('TmpFilePrefix') if not prefix: prefix = os.path.basename(sys.argv[0]) + '-tmp-' tempDir = dir_path while os.path.isdir(tempDir): random_string = ''.join( random.choice(string.ascii_uppercase + string.digits) for x in range(6)) tempDir = os.path.join(dir_path, prefix + random_string) + os.sep os.makedirs(tempDir) printMessage('Generated temporary directory: ' + tempDir) with open(os.path.join(tempDir, 'cwd.txt'), 'w') as outfile: outfile.write(workingDir + '\n') with open(os.path.join(tempDir, 'command.txt'), 'w') as outfile: outfile.write(' '.join(sys.argv) + '\n')
def initialise(): import os, sys from lib.errorMessage import errorMessage from lib.printMessage import printMessage from lib.readMRtrixConfSetting import readMRtrixConfSetting global args, citationList, cleanup, externalCitations, lastFile, mrtrixNThreads, mrtrixQuiet, parser, tempDir, verbosity, workingDir global colourClear, colourConsole, colourError, colourPrint, colourWarn if not parser: errorMessage('Script error: Command-line parser must be initialised before app') if len(sys.argv) == 1: parser.print_help() sys.exit(0) if sys.argv[-1] == '__print_usage_rst__': parser.printUsageRst() exit(0) workingDir = os.getcwd() args = parser.parse_args() if args.help: parser.print_help() sys.exit(0) use_colour = readMRtrixConfSetting('TerminalColor') if use_colour: use_colour = use_colour.lower() in ('yes', 'true', '1') else: # Windows now also gets coloured text terminal support, so make this the default use_colour = True if use_colour: colourClear = '\033[0m' colourConsole = '\033[03;34m' colourError = '\033[01;31m' colourPrint = '\033[03;32m' colourWarn = '\033[00;31m' if args.nocleanup: cleanup = False if args.nthreads: mrtrixNThreads = ' -nthreads ' + args.nthreads if args.quiet: verbosity = 0 mrtrixQuiet = ' -quiet' elif args.verbose: verbosity = 2 mrtrixQuiet = '' if citationList: printMessage('') citation_warning = 'Note that this script makes use of commands / algorithms that have relevant articles for citation' if externalCitations: citation_warning += '; INCLUDING FROM EXTERNAL SOFTWARE PACKAGES' citation_warning += '. Please consult the help page (-help option) for more information.' printMessage(citation_warning) printMessage('') if args.cont: tempDir = os.path.abspath(args.cont[0]) lastFile = args.cont[1]
def initialise(): import argparse, os, random, string, sys from lib.printMessage import printMessage from lib.printUsageMarkdown import printUsageMarkdown from lib.printUsageRst import printUsageRst from lib.readMRtrixConfSetting import readMRtrixConfSetting global args, author, cleanup, lastFile, mrtrixNThreads, mrtrixQuiet, standardOptions, parser, refList, tempDir, verbosity, workingDir global colourClear, colourConsole, colourError, colourPrint, colourWarn if len(sys.argv) == 2 and sys.argv[1] == '__print_usage_markdown__': printUsageMarkdown(parser, standardOptions, refList, author) exit(0) if len(sys.argv) == 2 and sys.argv[1] == '__print_usage_rst__': printUsageRst(parser, standardOptions, refList, author) exit(0) workingDir = os.getcwd() args = parser.parse_args() if args.help or len(sys.argv) == 1: parser.print_help() sys.exit(0) use_colour = readMRtrixConfSetting('TerminalColor') if use_colour: use_colour = use_colour.lower() in ('yes', 'true', '1') else: use_colour = not sys.platform.startswith('win') if use_colour: colourClear = '\033[0m' colourConsole = '\033[03;34m' colourError = '\033[01;31m' colourPrint = '\033[03;32m' colourWarn = '\033[00;31m' if args.nocleanup: cleanup = False if args.nthreads: mrtrixNThreads = '-nthreads ' + args.nthreads if args.quiet: verbosity = 0 mrtrixQuiet = '-quiet' if args.verbose: verbosity = 2 mrtrixQuiet = '' if citationWarning: printMessage('') printMessage(citationWarning) printMessage('') if args.cont: tempDir = os.path.abspath(args.cont[0]) lastFile = args.cont[1] else: if args.tempdir: dir_path = os.path.abspath(args.tempdir) else: dir_path = readMRtrixConfSetting('TmpFileDir') if not dir_path: if os.name == 'posix': dir_path = '/tmp' else: dir_path = '.' prefix = readMRtrixConfSetting('TmpFilePrefix') if not prefix: prefix = os.path.basename(sys.argv[0]) + '-tmp-' tempDir = dir_path while os.path.isdir(tempDir): random_string = ''.join( random.choice(string.ascii_uppercase + string.digits) for x in range(6)) tempDir = os.path.join(dir_path, prefix + random_string) + os.sep os.makedirs(tempDir) printMessage('Generated temporary directory: ' + tempDir) with open(os.path.join(tempDir, 'cwd.txt'), 'w') as outfile: outfile.write(workingDir + '\n') with open(os.path.join(tempDir, 'command.txt'), 'w') as outfile: outfile.write(' '.join(sys.argv) + '\n')
def initialise(): import os, sys from lib.errorMessage import errorMessage from lib.printMessage import printMessage from lib.readMRtrixConfSetting import readMRtrixConfSetting global args, citationList, cleanup, externalCitations, lastFile, mrtrixNThreads, mrtrixQuiet, parser, tempDir, verbosity, workingDir global colourClear, colourConsole, colourError, colourPrint, colourWarn if not parser: errorMessage( 'Script error: Command-line parser must be initialised before app') if len(sys.argv) == 1: parser.print_help() sys.exit(0) if sys.argv[-1] == '__print_usage_rst__': parser.printUsageRst() exit(0) workingDir = os.getcwd() args = parser.parse_args() if args.help: parser.print_help() sys.exit(0) use_colour = readMRtrixConfSetting('TerminalColor') if use_colour: use_colour = use_colour.lower() in ('yes', 'true', '1') else: # Windows now also gets coloured text terminal support, so make this the default use_colour = True if use_colour: colourClear = '\033[0m' colourConsole = '\033[03;34m' colourError = '\033[01;31m' colourPrint = '\033[03;32m' colourWarn = '\033[00;31m' if args.nocleanup: cleanup = False if args.nthreads: mrtrixNThreads = ' -nthreads ' + args.nthreads if args.quiet: verbosity = 0 mrtrixQuiet = ' -quiet' elif args.verbose: verbosity = 2 mrtrixQuiet = '' if citationList: printMessage('') citation_warning = 'Note that this script makes use of commands / algorithms that have relevant articles for citation' if externalCitations: citation_warning += '; INCLUDING FROM EXTERNAL SOFTWARE PACKAGES' citation_warning += '. Please consult the help page (-help option) for more information.' printMessage(citation_warning) printMessage('') if args.cont: tempDir = os.path.abspath(args.cont[0]) lastFile = args.cont[1]
def print_help(self): import subprocess, textwrap import lib.app from lib.readMRtrixConfSetting import readMRtrixConfSetting global author, citationList, copyright def bold(text): return ''.join( c + chr(0x08) + c for c in text) def underline(text): return ''.join( '_' + chr(0x08) + c for c in text) w = textwrap.TextWrapper(width=80, initial_indent=' ', subsequent_indent=' ') w_arg = textwrap.TextWrapper(width=80, initial_indent='', subsequent_indent=' ') s = ' ' + bold(self.prog) + ': Script using the MRtrix3 Python libraries\n' s += '\n' s += bold('SYNOPSIS') + '\n' s += '\n' synopsis = self.prog + ' [ options ]' # Compulsory sub-parser algorithm selection (if present) if self._subparsers: synopsis += ' ' + self._subparsers._group_actions[0].dest + ' ...' # Find compulsory input arguments for arg in self._positionals._group_actions: if arg.metavar: synopsis += ' ' + arg.metavar else: synopsis += ' ' + arg.dest # Unfortunately this can line wrap early because textwrap is counting each # underlined character as 3 characters when calculating when to wrap # Fix by underlining after the fact s += w.fill(synopsis).replace(self.prog, underline(self.prog), 1) + '\n' s += '\n' if self._subparsers: s += ' ' + w_arg.fill(self._subparsers._group_actions[0].dest + ' '*(max(13-len(self._subparsers._group_actions[0].dest), 1)) + self._subparsers._group_actions[0].help).replace (self._subparsers._group_actions[0].dest, underline(self._subparsers._group_actions[0].dest), 1) + '\n' s += '\n' for arg in self._positionals._group_actions: line = ' ' if arg.metavar: name = arg.metavar else: name = arg.dest line += name + ' '*(max(13-len(name), 1)) + arg.help s += w_arg.fill(line).replace(name, underline(name), 1) + '\n' s += '\n' s += bold('DESCRIPTION') + '\n' s += '\n' s += w.fill(self.description) + '\n' s += '\n' # Option groups for group in reversed(self._action_groups): # * Don't display empty groups # * Don't display the subparser option; that's dealt with in the synopsis # * Don't re-display any compulsory positional arguments; they're also dealt with in the synopsis if group._group_actions and not (len(group._group_actions) == 1 and isinstance(group._group_actions[0], argparse._SubParsersAction)) and not group == self._positionals: s += bold(group.title) + '\n' s += '\n' for option in group._group_actions: s += ' ' + underline('/'.join(option.option_strings)) if option.metavar: s += ' ' if isinstance(option.metavar, tuple): s += ' '.join(option.metavar) else: s += option.metavar elif option.nargs: s += (' ' + option.dest.upper())*option.nargs elif option.type is not None: s += ' ' + option.type.__name__.upper() elif option.default is None: s += ' ' + option.dest.upper() # Any options that haven't tripped one of the conditions above should be a store_true or store_false, and # therefore there's nothing to be appended to the option instruction s += '\n' s += w.fill(option.help) + '\n' s += '\n' s += bold('AUTHOR') + '\n' s += w.fill(lib.app.author) + '\n' s += '\n' s += bold('COPYRIGHT') + '\n' s += w.fill(lib.app.copyright) + '\n' if lib.app.citationList: s += '\n' s += bold('REFERENCES') + '\n' s += '\n' for entry in lib.app.citationList: if entry[0]: s += w.fill('* ' + entry[0] + ':') + '\n' s += w.fill(entry[1]) + '\n' s += '\n' command = readMRtrixConfSetting('HelpCommand') if not command: command = 'less -X' if command: try: process = subprocess.Popen(command.split(' '), stdin=subprocess.PIPE) process.communicate(s.encode()) except: print (s) else: print (s)
def print_help(self): import subprocess, textwrap import lib.app from lib.readMRtrixConfSetting import readMRtrixConfSetting global author, citationList, copyright def bold(text): return ''.join(c + chr(0x08) + c for c in text) def underline(text): return ''.join('_' + chr(0x08) + c for c in text) w = textwrap.TextWrapper(width=80, initial_indent=' ', subsequent_indent=' ') w_arg = textwrap.TextWrapper(width=80, initial_indent='', subsequent_indent=' ') s = ' ' + bold( self.prog) + ': Script using the MRtrix3 Python libraries\n' s += '\n' s += bold('SYNOPSIS') + '\n' s += '\n' synopsis = self.prog + ' [ options ]' # Compulsory sub-parser algorithm selection (if present) if self._subparsers: synopsis += ' ' + self._subparsers._group_actions[0].dest + ' ...' # Find compulsory input arguments for arg in self._positionals._group_actions: if arg.metavar: synopsis += ' ' + arg.metavar else: synopsis += ' ' + arg.dest # Unfortunately this can line wrap early because textwrap is counting each # underlined character as 3 characters when calculating when to wrap # Fix by underlining after the fact s += w.fill(synopsis).replace(self.prog, underline(self.prog), 1) + '\n' s += '\n' if self._subparsers: s += ' ' + w_arg.fill( self._subparsers._group_actions[0].dest + ' ' * (max(13 - len(self._subparsers._group_actions[0].dest), 1)) + self._subparsers._group_actions[0].help).replace( self._subparsers._group_actions[0].dest, underline(self._subparsers._group_actions[0].dest), 1) + '\n' s += '\n' for arg in self._positionals._group_actions: line = ' ' if arg.metavar: name = arg.metavar else: name = arg.dest line += name + ' ' * (max(13 - len(name), 1)) + arg.help s += w_arg.fill(line).replace(name, underline(name), 1) + '\n' s += '\n' s += bold('DESCRIPTION') + '\n' s += '\n' s += w.fill(self.description) + '\n' s += '\n' # Option groups for group in reversed(self._action_groups): # * Don't display empty groups # * Don't display the subparser option; that's dealt with in the synopsis # * Don't re-display any compulsory positional arguments; they're also dealt with in the synopsis if group._group_actions and not ( len(group._group_actions) == 1 and isinstance( group._group_actions[0], argparse._SubParsersAction) ) and not group == self._positionals: s += bold(group.title) + '\n' s += '\n' for option in group._group_actions: s += ' ' + underline('/'.join(option.option_strings)) if option.metavar: s += ' ' if isinstance(option.metavar, tuple): s += ' '.join(option.metavar) else: s += option.metavar elif option.nargs: s += (' ' + option.dest.upper()) * option.nargs elif option.type is not None: s += ' ' + option.type.__name__.upper() elif option.default is None: s += ' ' + option.dest.upper() # Any options that haven't tripped one of the conditions above should be a store_true or store_false, and # therefore there's nothing to be appended to the option instruction s += '\n' s += w.fill(option.help) + '\n' s += '\n' s += bold('AUTHOR') + '\n' s += w.fill(lib.app.author) + '\n' s += '\n' s += bold('COPYRIGHT') + '\n' s += w.fill(lib.app.copyright) + '\n' if lib.app.citationList: s += '\n' s += bold('REFERENCES') + '\n' s += '\n' for entry in lib.app.citationList: if entry[0]: s += w.fill('* ' + entry[0] + ':') + '\n' s += w.fill(entry[1]) + '\n' s += '\n' command = readMRtrixConfSetting('HelpCommand') if not command: command = 'less -X' if command: try: process = subprocess.Popen(command.split(' '), stdin=subprocess.PIPE) process.communicate(s.encode()) except: print(s) else: print(s)
def initialise(): import argparse, os, random, string, sys from lib.printMessage import printMessage from lib.printUsageMarkdown import printUsageMarkdown from lib.printUsageRst import printUsageRst from lib.readMRtrixConfSetting import readMRtrixConfSetting global args, author, cleanup, lastFile, mrtrixNThreads, mrtrixQuiet, standardOptions, parser, refList, tempDir, verbosity, workingDir global colourClear, colourConsole, colourError, colourPrint, colourWarn if len(sys.argv) == 2 and sys.argv[1] == '__print_usage_markdown__': printUsageMarkdown(parser, standardOptions, refList, author) exit(0) if len(sys.argv) == 2 and sys.argv[1] == '__print_usage_rst__': printUsageRst(parser, standardOptions, refList, author) exit(0) workingDir = os.getcwd() args = parser.parse_args() if args.help or len(sys.argv) == 1: parser.print_help() sys.exit(0) use_colour = readMRtrixConfSetting('TerminalColor') if use_colour: use_colour = use_colour.lower() in ('yes', 'true', '1') else: use_colour = not sys.platform.startswith('win') if use_colour: colourClear = '\033[0m' colourConsole = '\033[03;34m' colourError = '\033[01;31m' colourPrint = '\033[03;32m' colourWarn = '\033[00;31m' if args.nocleanup: cleanup = False if args.nthreads: mrtrixNThreads = ' -nthreads ' + args.nthreads if args.quiet: verbosity = 0 mrtrixQuiet = ' -quiet' if args.verbose: verbosity = 2 mrtrixQuiet = '' if citationWarning: printMessage('') printMessage(citationWarning) printMessage('') if args.cont: tempDir = os.path.abspath(args.cont[0]) lastFile = args.cont[1] else: if args.tempdir: dir_path = os.path.abspath(args.tempdir) else: dir_path = readMRtrixConfSetting('TmpFileDir') if not dir_path: if os.name == 'posix': dir_path = '/tmp' else: dir_path = '.' prefix = readMRtrixConfSetting('TmpFilePrefix') if not prefix: prefix = os.path.basename(sys.argv[0]) + '-tmp-' tempDir = dir_path while os.path.isdir(tempDir): random_string = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6)) tempDir = os.path.join(dir_path, prefix + random_string) + os.sep os.makedirs(tempDir) printMessage('Generated temporary directory: ' + tempDir) with open(os.path.join(tempDir, 'cwd.txt'), 'w') as outfile: outfile.write(workingDir + '\n') with open(os.path.join(tempDir, 'command.txt'), 'w') as outfile: outfile.write(' '.join(sys.argv) + '\n')