def startRun(self): time.sleep(0.2) if sys.platform not in mp_platforms: self.processingRunning=False if self.processingRunning: print('Processing already running.') elif self.processingRunning==False: self.update_settings() if os.path.exists(self.acolite_settings['inputfile']): self.processingRunning=True self.processingComplete=False ## test output directory if os.path.exists(self.acolite_settings['output']) is False: try: os.makedirs(self.acolite_settings['output']) except: print('Could not make output directory {}'.format(self.acolite_settings['output'])) self.processingRunning=False ## test logfile if 'runid' not in self.acolite_settings: self.acolite_settings['runid'] = datetime.datetime.now().strftime('%Y%m%d_%H%M%S') logfile = '{}/{}'.format(self.acolite_settings['output'],'acolite_run_{}_log.txt'.format(self.acolite_settings['runid'])) try: with open(logfile, 'w') as f: f.write('') except: print('Cannot write to output directory {}'.format(self.acolite_settings['output'])) self.processingRunning=False if self.processingRunning: print('Running ACOLITE processing on {}'.format(sys.platform)) print('Logging to file {}'.format(logfile)) ## Running acolite directly works - but no interruption possible #acolite(settings=self.acolite_settings) if sys.platform in mp_platforms: ## stop stdout redirection - otherwise Process doesn't finish ## probably a conflict between multithreading and the GUI loop or the button threading ## too confusing to figure out right now #print('Logging disabled in GUI window until processing is complete.') self.logging.__del__() self.logging = None self.logging=LogTee(logfile) ## use custom Process class that returns exceptions self.process = Process(target=acolite_run, kwargs={'settings':self.acolite_settings, 'gui':True}) self.process.start() ## run processing until finished or interrupted while(self.process.join() and self.processingRunning): time.sleep(0.1) self.update() if not self.process.is_alive(): self.processingRunning=False else: # no interruption acolite_run(settings=self.acolite_settings, gui=True) ## processing running has stopped self.processingRunning=False ## redirect stdout to the GUI again if (sys.platform in mp_platforms): if (not self.processingRunning): self.logging.__del__() self.logging = None self.logging=WidgetTee(self.text) #self.wt.__init__(self.text) ## did we finish normally or did the user interrupt? if sys.platform in mp_platforms: #if True: if self.process.exitcode == 0: self.processingComplete=True print('Finished processing.') else: self.processingComplete=False if(self.process.exitcode == -15): print('Processing interrupted by user.') else: if self.process.exception is None: print('Processing error, see log file for details.') else: print('Processing error:') for i in self.process.exception: print(i) else: #self.processingComplete=True print('Finished processing.') #self.update() else: print('Could not start ACOLITE processing.')
def acolite_cli(*args): import os ## object for logging stdout to log file when processing class LogTee(object): def __init__(self, name): self.name = name ## make new file if os.path.exists(os.path.dirname(self.name)) is False: os.makedirs(os.path.dirname(self.name)) self.file = open(self.name, 'w') self.file.close() self.mode = 'a' self.stdout = sys.stdout sys.stdout = self def __del__(self): sys.stdout = self.stdout def write(self, data): self.stdout.write(data) data = data.strip() if len(data) > 0: self.file = open(self.name, self.mode) self.file.write('{}: {}\n'.format( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data)) self.file.close() def flush(self): pass import argparse parser = argparse.ArgumentParser(description='ACOLITE CLI') parser.add_argument('--settings', help='settings file') parser.add_argument('--images', help='list of images') #args = parser.parse_args() args, unknown = parser.parse_known_args() if args.settings is None: print('No settings file given.') return (1) import sys, datetime from acolite.acolite import acolite_run, settings_read acolite_settings = settings_read(args.settings) if 'runid' not in acolite_settings: acolite_settings['runid'] = datetime.datetime.now().strftime( '%Y%m%d_%H%M%S') if 'output' not in acolite_settings: print('No output specified in settings file.') return (1) logfile = '{}/{}'.format( acolite_settings['output'], 'acolite_run_{}_log.txt'.format(acolite_settings['runid'])) log = LogTee(logfile) print('Running ACOLITE') if args.images is None: acolite_run(settings=acolite_settings) else: images = args.images if ',' in images: images = images.split(',') if type(images) is not list: if not os.path.isdir(images): with open(images, 'r') as f: images = [line.strip() for line in f.readlines()] else: images = [images] acolite_settings['inputfile'] = images acolite_run(settings=acolite_settings)