~maddevelopers/mg5amcnlo/2.5.5_NLOandLOmerging

« back to all changes in this revision

Viewing changes to models/import_ufo.py

  • Committer: olivier Mattelaer
  • Date: 2017-11-26 16:35:21 UTC
  • mfrom: (271.1.38 2.6.1_bk)
  • Revision ID: olivier.mattelaer@uclouvain.be-20171126163521-2q5otmkxwluwsp68
merge with latest 2.6.1 

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
    """ a class for invalid Model """
61
61
 
62
62
last_model_path =''
63
 
def find_ufo_path(model_name):
 
63
def find_ufo_path(model_name, web_search=True):
64
64
    """ find the path to a model """
65
65
 
66
66
    global last_model_path
80
80
    if os.path.isdir(model_name):
81
81
        logger.warning('No model %s found in default path. Did you mean \'import model ./%s\'',
82
82
                       model_name, model_name)
83
 
        raise UFOImportError("Path %s is not a valid pathname" % model_name)    
 
83
        if os.path.sep in model_name:
 
84
            raise UFOImportError("Path %s is not a valid pathname" % model_name)    
 
85
    elif web_search and '-' not in model_name:
 
86
        found = import_model_from_db(model_name)
 
87
        if found:
 
88
            return find_ufo_path(model_name, web_search=False)
 
89
        else:
 
90
            raise UFOImportError("Path %s is not a valid pathname" % model_name)    
84
91
    else:
85
92
        raise UFOImportError("Path %s is not a valid pathname" % model_name)    
86
93
    
87
94
 
88
95
    return
89
96
 
 
97
def import_model_from_db(model_name):
 
98
 
 
99
    data_path = ['http://madgraph.phys.ucl.ac.be/models_db.dat',
 
100
                     'http://madgraph.physics.illinois.edu/models_db.dat']
 
101
    import random
 
102
    import urllib
 
103
    r = random.randint(0,1)
 
104
    r = [r, (1-r)]
 
105
 
 
106
    for index in r:
 
107
        cluster_path = data_path[index]
 
108
        try:
 
109
            data = urllib.urlopen(cluster_path)
 
110
        except Exception:
 
111
            continue
 
112
        break
 
113
    else:
 
114
        raise MadGraph5Error, '''Model not found locally and Impossible to connect any of us servers.
 
115
        Please check your internet connection or retry later'''
 
116
    
 
117
    link = None
 
118
    for line in data:
 
119
        split = line.split()
 
120
        if model_name == split[0]:
 
121
            link = split[1]
 
122
            break
 
123
    else:
 
124
        logger.debug('no model with that name found online')
 
125
        return False
 
126
    
 
127
    #get target directory
 
128
    # 1. PYTHONPATH containing UFO
 
129
    # 2. models directory
 
130
    target = None 
 
131
    if 'PYTHONPATH' in os.environ:
 
132
        for directory in os.environ['PYTHONPATH'].split(':'):
 
133
            if 'UFO' in os.path.basename(directory) and os.path.exists(directory):
 
134
                target= directory 
 
135
    if target is None:
 
136
        target = pjoin(MG5DIR, 'models')    
 
137
    try:
 
138
        os.remove(pjoin(target, 'tmp.tgz'))
 
139
    except Exception:
 
140
        pass
 
141
    logger.info("download model from %s to the following directory: %s", link, target, '$MG:color:BLACK')
 
142
    if sys.platform == "darwin":
 
143
        misc.call(['curl', link, '-otmp.tgz'], cwd=target)
 
144
    else:
 
145
        misc.call(['wget', link, '--output-document=tmp.tgz'], cwd=target)
 
146
 
 
147
    #untar the file.
 
148
    # .tgz
 
149
    if link.endswith(('.tgz','.tar.gz')):
 
150
        try:
 
151
            proc = misc.call('tar -xzpvf tmp.tgz', shell=True, cwd=target)#, stdout=devnull, stderr=devnull)
 
152
            if proc: raise Exception
 
153
        except:
 
154
            proc = misc.call('tar -xpvf tmp.tgz', shell=True, cwd=target)#, stdout=devnull, stderr=devnull)
 
155
    # .zip
 
156
    if link.endswith(('.zip')):
 
157
        try:
 
158
            proc = misc.call('unzip tmp.tgz', shell=True, cwd=target)#, stdout=devnull, stderr=devnull)
 
159
            if proc: raise Exception
 
160
        except:
 
161
            proc = misc.call('tar -xzpvf tmp.tgz', shell=True, cwd=target)#, stdout=devnull, stderr=devnull)
 
162
    if proc:
 
163
        raise Exception, "Impossible to unpack the model. Please install it manually"
 
164
    return True
 
165
 
90
166
def import_model(model_name, decay=False, restrict=True, prefix='mdl_',
91
167
                                                    complex_mass_scheme = None):
92
168
    """ a practical and efficient way to import a model"""