def listSimFolders(id): simfolders = getSimFolders() for i in id: simfolder = simfolders[i] print render('%(BLUE)s' + simfolder + ':%(NORMAL)s') print render('%(BLUE)s' + '\t' + '\n\t'.join([ name for name in os.listdir(os.path.join(spParams['home'], simfolder)) if name.startswith(spParams['name']) ]) + '%(NORMAL)s')
def __init__(self, argv): self.initialize() self.parameters["runlevel"] = {"value":0 , "name": "Run level"} self.parameters["stopat"] = {"value":len(self.actions), "name": "Stop at"} self.parse_parameters(argv) if not str(self.value_of("stopat")).isdigit(): self.parameters["stopat"] = {"value": int(self.get_runlevel_of(self.value_of("stopat"))), "name": "Stop at"} if str(self.value_of("runlevel")).isdigit(): self.start(self.value_of("runlevel")) else: runlevel = self.get_runlevel_of(self.value_of("runlevel")) self.start(runlevel) print render('%(RED)s%(BOLD)sFinished!%(NORMAL)s')
def showSimInfo(id): info = OrderedDict() info['project name'] = spParams['name'] info['project path'] = spParams['home'] scaninfo = [] simfolders = getSimFolders() for i in range(0, len(simfolders)): if spParams['name'] != 'Gene': var, scan = parseScanName(simfolders[i]) dic = OrderedDict() dic['id'] = i dic['project name'] = spParams['name'] dic['project path'] = spParams['home'] dic['simfolder path'] = os.path.join(spParams['home'], simfolders[i]) # if no scan, show 'No Scan' if var: vardic = OrderedDict() for j in range(0, len(var)): try: float(scan[j]) vardic[var[j]] = scan[j] + ", num=1" except ValueError: start, end, step = tuple(scan[j].split(':')) num = runCounter(start, end, step) vardic[var[j]] = scan[j] + ", num=%i" % num dic['scaned variables'] = vardic else: dic['scaned variables'] = 'No Scan' scaninfo.append(dic) else: dic = OrderedDict() dic['id'] = i dic['project name'] = spParams['name'] dic['project path'] = spParams['home'] dic['simfolder path'] = os.path.join(spParams['home'], simfolders[i]) scaninfo.append(dic) info['simulation folders'] = scaninfo # print chosen sim's info for i in id: dic = info['simulation folders'][i] print render('%(BLUE)s' + json.dumps( dic, sort_keys=False, indent=4, separators=(',', ': ')) + '%(NORMAL)s')
def mergeIDs(id): idlist = range(len(getSimFolders())) if not id: return idlist else: idset = [] for s in id: try: part = idlist[getSlice(s)] except IndexError: print render('%(RED)s' + "id=%s doesn't exist!" % s + '%(NORMAL)s') continue if isinstance(part, int): idset.append(part) else: idset += part idset = sorted(set(idset)) return idset
def showSimParas(): try: filename = os.path.join(spParams['home'], spParams['name'] + '.data') f = file(filename) dic = OrderedDict() while True: line = f.readline() if len(line) == 0: break elif not line.startswith('#'): s = line.split() try: dic[s[0]] = s[1] except IndexError: pass f.close() print render('%(BLUE)s' + json.dumps( dic, sort_keys=False, indent=4, separators=(',', ': ')) + '%(NORMAL)s') except IOError: print ".data file doesn't exist!"
def magic( self , action) : print render('%(BOLD)s%(BG_YELLOW)s%(BLACK)s ' + str(self.current_runlevel) + ' %(NORMAL)s%(BOLD)s%(YELLOW)s%(BG_BLACK)s ' + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ' %(NORMAL)s%(BG_GREEN)s%(WHITE)s%(BOLD)s ' + foo.__name__ + ' %(NORMAL)s%(BOLD)s%(BLACK)s%(BG_WHITE)s ' + action['description'] + ' %(NORMAL)s') print render("%(BG_BLACK)s%(BLUE)s") result = foo( self ) print render("%(NORMAL)s") return result
def check(id, details=False): logs = [] logpasses = [] summaries = [] simfolders = getSimFolders() # backup single folder with a specified id=i. def singleCheck(i): ifpass = True log = [] logpass = [] summary = "" numfail = 0 numtotal = 0 simfolder = simfolders[i] simdir = os.path.join(spParams['home'], simfolder) namelist = [ d for d in os.listdir(simdir) if os.path.isdir(os.path.join(simdir, d)) and d.startswith(spParams['name']) ] size = len(namelist) # consider the simulation folder is a single or a scan if size: numtotal = size for name in namelist: _simdir = os.path.join(simdir, name) statfile = [f for f in os.listdir(_simdir) if f[-4:] == 'stat'] if not statfile: numfail += 1 log.append(_simdir) else: logpass.append(_simdir) summary = "id=%s, pass/total: %i/%i" % (i, numtotal - numfail, numtotal) if numfail: ifpass = False else: statfile = [f for f in os.listdir(simdir) if f[-4:] == 'stat'] statsize = len(statfile) numtotal = statsize if statsize: logpass.append(simdir) summary = "id=%s, pass/total: %i/%i" % (i, 1, numtotal) else: log.append(simdir) summary = "id=%s, pass/total: %i/%i" % (i, 0, numtotal) ifpass = False return log, logpass, summary, ifpass # check chosen folders for i in id: log, logpass, summary, ifpass = singleCheck(i) if log: log = "id=%s, fail:" % i + '\n\t' + '\n\t'.join(log) log = render('%(RED)s' + log + '%(NORMAL)s') logs.append(log) else: logs.append(None) if logpass: logpass = "******" % i + '\n\t' + '\n\t'.join(logpass) logpass = render('%(GREEN)s' + logpass + '%(NORMAL)s') logpasses.append(logpass) else: logs.append(None) if ifpass or details: summary = render('%(BLUE)s' + summary + '%(NORMAL)s') else: summary = render('%(RED)s' + summary + '%(NORMAL)s') summaries.append(summary) for i in range(len(summaries)): print summaries[i] if details: if logs[i]: print logs[i] if logpass[i]: print logpasses[i]
def main(): # user friendly command line arguments manual intro = """hello there, I'm sscout, a small code equiped some useful features to help you dealing with the .stat file that generated by OPAL.""" parser = argparse.ArgumentParser(description=intro) parser.add_argument('-v', '--version', action='version', version='sscout 1.1') parser.add_argument('id', nargs='*', help="""every simfolder has an id (you will know when you run this code without any arguments), so we specify the simfolders we care by specified the id of them. 'id' should be a list of slice. if not specified, the default value is ':'""") parser.add_argument('-bu', '--backup', action='store_true', help="""back up the .stat files in simfolders which are specified by 'id' argument""") parser.add_argument('-ck', '--check', action='store_true', help="""check out if the .stat files exist in simfolders which are specified by 'id' argument""") parser.add_argument('-ls', '--list', action='store_true', help="""list the folder name of every subfolder in simfolders which are specified by 'id' argument""") parser.add_argument('-ge', '--geneplot', action='append', nargs='+', dest='plotgene', metavar='arg', help=""" draw 2D plot for the rerun generation simulations""") parser.add_argument('-2d', '--2dplot', action='append', nargs='+', dest='plot2d', metavar='arg', help=""" draw 2D plots of specified simulations in target simfolder. it will show the emittance, beamsize, energy spread, etc vs. longitudinal position on the plots. notice that the target simfolder in which we draw 2D plots is not specified by 'id' argument, but by the first argument follows '-2d' commmand, which is an required argument, and must be an integer (not a slice or something). which simulations in that simfolder we will draw are specified by the rest args, which should be some slices. if not specified, the default is '0 ...', that means just plot the first simulation in target simfolder. you can add several '-2d' commands to draw 2D plots in different simfolders, just change the args follow each '-2d' commands""") parser.add_argument('-3d', '--3dplot', action='append', nargs='+', dest='plot3d', metavar='arg', help=""" draw 3D plots of specified simulations in target simfolder. it will show the ferrario point trace on the plots. notice that the target simfolder in which we draw 3D plots is not specified by 'id' argument, but by the first argument follows '-3d' commmand, which is an required arg, and must be an integer (not a slice or something). also notice that you can draw 3D plots only when the target simfolder contains simulation results of scan for at least 2 parameters. which simulations in that simfolder we will draw are specified by the rest args, which should be some slices. but in case we are plotting 3D plots, only 2 parameters can be treat as variables. so if you have scan 3 or more parameters, you can only specified 2 of them as slice, and the rest as position (integer). if not specified, the default is ': : 0 ...', which means use the first 2 parameters as variables and draw the plots when other parameters take their first value. you can add several '-3d' commands to draw 3D plots in different simfolders, just change the args follow each '-3d' commands. you can also get a json file which contains list of all ferrario points in range specified by args follow '-3d', by adding '-f' or '--fpoint' argument""") parser.add_argument('-co', '--coplot', action='append', nargs='+', dest='plotco', metavar='arg', help=""" comparison plots""") parser.add_argument('-z', '--zpos', action='store', nargs='+', dest='zpos', metavar='pos', help=""" specify the z position where we draw comparison plots""" ) parser.add_argument('-s', '--save', action='store_true', help="""save all figures generated by argument '-2d' and '-3d' in pdf format""") parser.add_argument('-f', '--fpoint', action='store_true', help="""generate json file that contains all ferrario points in ranges that specified by arguments follow '-3d', must be used with '-3d' argument""") parser.add_argument('-d', '--detail', action='store_true', help="""show details of the result of '.stat' files check""") parser.add_argument('-p', '--showparas', action='store_true', help="""show simulation parameters under root simfolder, in fact it shows the parameters in projname.data""") parser.add_argument('-ho', '--home', nargs=1, dest='home', metavar='dir', help=""" set simulation home directory, sscout will search for simualtion folders under the specified directory 'dir', if not specified, sscout will set current directory as the simulation root directory.""") parser.add_argument('-n', '--name', nargs=1, dest='name', metavar='name', help=""" set the simulation project name, ffinder will search for the simulation folders of which the name starts with the project name 'name'. If not specified, sscout will try to point out the project name automatically.""") # parse command line and get all arguments args = parser.parse_args() # set init flag noarg_flag = 1 # set sim homedir and name if args.home: spParams['home'] = args.home[0] if args.name: spParams['name'] = args.name[0] else: autoDetectProj() # take actions #pb = ProgressBar(color='blue') pb = ProgressBar(color='black', block='=', width=40) id = mergeIDs(args.id) if args.list: listSimFolders(id) noarg_flag = 0 if args.showparas: showSimParas() noarg_flag = 0 if args.backup: easyBackup(id, pb) noarg_flag = 0 if args.check: check(id, args.detail) noarg_flag = 0 if args.plotgene: for arg in args.plotgene: plotGene2D(arg[1:], int(arg[0]), args.save, pb) if args.plot2d: for arg in args.plot2d: plotSim2D(arg[1:], int(arg[0]), args.save, pb) if args.plot3d: for arg in args.plot3d: plotSim3D(arg[1:], int(arg[0]), args.save, args.fpoint, pb) if args.plotco: if args.zpos: for z in args.zpos: for i, arg in enumerate(args.plotco): if i == 0: fig, ax = plotComp(arg[1:], int(arg[0]), z, pb=pb) else: plotComp(arg[1:], int(arg[0]), z, fig, ax, pb) else: for i, arg in enumerate(args.plotco): if i == 0: if len(arg) > 1: fig, ax = plotComp(arg[1:], int(arg[0]), args.zpos, pb=pb) else: fig, ax = plotComp([ ':', ], int(arg[0]), args.zpos, pb=pb) else: plotComp(arg[1:], int(arg[0]), args.zpos, fig, ax, pb) if args.plot2d or args.plot3d or args.plotco or args.plotgene: plt.show() elif noarg_flag and (not args.save) and (not args.fpoint) and\ (not args.detail) and (not args.zpos): showSimInfo(id) if not args.id and (not args.home) and (not args.name): print render('%(GREEN)s' + "getting help by adding argument '-h'" + '%(NORMAL)s')
def check_back (): from terminal import render if len(sys.argv) > 1 and sys.argv[1] == "-h": print render('%(BOLD)sNAME') print render ('%(NORMAL)s rbackup - creates a incremental backup using rsync to a mounted device') print " " print render ('%(BOLD)sSYNOPSIS') print render ('%(NORMAL)s rbackup [source directory or file] [destination directory or file]') print "" print render ('%(BOLD)sEXAMPLE') print render ('%(NORMAL)s rbackup /tmp/rbackup /tmp2/rbackup') return if (len(sys.argv) > 2 and len(sys.argv) < 4): source = sys.argv[1] dest = sys.argv[2] print rbackup (source,dest) else: print "Usage: rbackup [Source] [Destination] or -h for help"
def render_blue(msg): print render('%(BLUE)s{}%(NORMAL)s'.format(msg))
def render_red(msg): print render('%(RED)s{}%(NORMAL)s'.format(msg))
def make_title(text): """Make a markdown-ish header in bold blue""" string = "%(BLUE)s%(BOLD)s" + text + "%(NORMAL)s" print render(string) string = "%(BLUE)s%(BOLD)s" + "=" * len(text) + "%(NORMAL)s" print render(string)
def print_x(): """Prints a red x-mark""" print render("%(RED)s✘%(NORMAL)s"),
def print_tick(): """Prints a green tick""" print render("%(GREEN)s✔%(NORMAL)s"),