import sys
if sys.version_info[0] < 3:
    raise Exception("Please use python3, not {}!".format(sys.version))

import os
import glob
import pickle
import argparse
from multiprocessing import Pool

import numpy as np
import corner
import pymultinest as pmn
import filelock
import binarytree as bt

from sdf import fitting
from sdf import plotting
from sdf import db
from sdf import www
from sdf import config as cfg


# command line
if __name__ == '__main__':
    """Run it, with options."""

    # inputs
    parser1 = argparse.ArgumentParser(description='Fit SED models to sdb    \
                                                   rawphot files')
    parser = parser1.add_mutually_exclusive_group(required=True)
    parser.add_argument('--file','-f',nargs='+',action='append',
                        help='Fit SED to file or files')
    parser.add_argument('--dir','-d',nargs='+',action='append',
                        help='Fit SED to *-rawphot.txt files in path(s)')
    parser.add_argument('--samples','-s',nargs='+',
                        help='Restrict to target samples')

    parser1.add_argument('--subset',nargs='+',default='*',
                         help='Restrict to subset of targets (e.g. public)')

    parser1.add_argument('--www','-w',action='store_true',
                         help='www material',default=False)
    parser1.add_argument('--update-www',action='store_true',
                         help='Force update of www')
                         
    parser1.add_argument('--dbwrite','-b',action='store_true',
                         help='Write results to db',default=False)
    parser1.add_argument('--update-db',action='store_true',
                         help='Force udpate of db')

    parser1.add_argument('--no-spectra',action='store_true',
                         help='Exclude spectra from fitting')

    parser1.add_argument('--update-all','-u',action='store_true',
                         help='Force udpate of everything')

    parser1.add_argument('--update-analysis','-a',action='store_true',
                         help='Force udpate of post-multinest analysis')

    parser1.add_argument('--quick-update-check','-q',action='store_true',
                         help='Skip if index.html most recent file')

    args = parser1.parse_args()
    
    # collect the files
    if args.file is not None:
        files = args.file[0]

    elif args.dir is not None:
        for d in args.dir[0]:
            d.rstrip('/')
            files = glob.glob(os.path.abspath(d)+'/**/'\
                              +args.subset[0]+'/*-rawphot.txt',
                              recursive=True)

    elif args.samples is not None:
        ids = []
        for s in args.samples:
            ids += db.sample_targets(s)
        files = []
        for id in ids:
            files += glob.glob( cfg.file['sdb_root']+'masters/'\
                               +id+'/**/*-rawphot.txt' )

    # one at a time, locking before we start
    for f in files:
        
        lock = filelock.FileLock(os.path.dirname(f)
                                 +'/.sdf_lock-'
                                 +os.path.basename(f))
        try:
            with lock.acquire(timeout = 0):
                
                print(f)

                # quick check if ANY files are more recently modified
                # than index.html
                if args.quick_update_check and \
                    os.path.exists(os.path.dirname(f)+'/index.html'):
                    print(" Quick check")
                    t_index = os.path.getmtime(os.path.dirname(f)+'/index.html')
                    t_max = np.max([os.path.getmtime(i) for i in \
                                    glob.glob(os.path.dirname(f)+'**/*',
                                              recursive=True)])
                    if t_index >= t_max:
                        print("   no files more recent than {}".format(os.path.dirname(f)+'/index.html'))
                        continue
                    else:
                        print("   index.html out of date, continuing")
            
                # evidence-sorted list of results
                results = fitting.fit_results(os.path.abspath(f),
                                              update_mn=args.update_all,
                                              update_an=args.update_analysis,
                                              nospec=args.no_spectra)
                if results is None:
                    continue

                if args.www or args.update_www:
                    www.www_all(results,update=args.update_www)

                # write best model to db
                if args.dbwrite or args.update_db:
                    db.write_all(results[0],update=args.update_db)

        except filelock.Timeout:
            pass

@@ -160,185 +160,44 @@
 rse%0A
-from multiprocessing import Pool%0A%0Aimport numpy as np%0Aimport corner%0Aimport pymultinest as pmn%0Aimport filelock%0Aimport binarytree as bt%0A%0Afrom sdf import fitting%0Afrom sdf import plo
+%0Aimport filelock%0A%0Afrom sdf import fi
 ttin
@@ -1978,152 +1978,8 @@
 ')%0A%0A
-    parser1.add_argument('--quick-update-check','-q',action='store_true',%0A                         help='Skip if index.html most recent file')%0A%0A
     
@@ -2971,848 +2971,8 @@
 f)%0A%0A
-                # quick check if ANY files are more recently modified%0A                # than index.html%0A                if args.quick_update_check and %5C%0A                    os.path.exists(os.path.dirname(f)+'/index.html'):%0A                    print(%22 Quick check%22)%0A                    t_index = os.path.getmtime(os.path.dirname(f)+'/index.html')%0A                    t_max = np.max(%5Bos.path.getmtime(i) for i in %5C%0A                                    glob.glob(os.path.dirname(f)+'**/*',%0A                                              recursive=True)%5D)%0A                    if t_index %3E= t_max:%0A                        print(%22   no files more recent than %7B%7D%22.format(os.path.dirname(f)+'/index.html'))%0A                        continue%0A                    else:%0A                        print(%22   index.html out of date, continuing%22)%0A            %0A
     

clean out unused modules and obsolete quick check