3 from functools import wraps
9 from django.contrib.auth.decorators import login_required
10 from django.core.cache import caches
11 from django.core.paginator import Paginator
12 from django.utils.formats import date_format
13 from django.utils.timezone import localtime
14 from django.views.decorators.cache import cache_control
16 from pyqcg.description import JobDescription
18 from filex.ftp import FTPOperation
19 from qcg import constants
22 from django.utils import encoding
24 resources_cache = caches['resources']
27 def get_attributes(obj, attrs):
28 return {name: getattr(obj, name) for name in attrs if getattr(obj, name) is not None}
31 def username_from_dn(dn):
32 _, username = dn.rsplit('=', 1)
37 def try_parse_int(s, default=None, base=10):
40 except (TypeError, ValueError):
44 def paginator_context(request, objects, per_page=constants.PER_PAGE):
45 paginator = Paginator(objects, per_page)
47 page_num = try_parse_int(request.GET.get('page'), 1)
48 if not (1 <= page_num <= paginator.num_pages):
51 pages_range = range(max(2, min(page_num - 2, paginator.num_pages - 4)),
52 min(max(page_num + 2, 5), paginator.num_pages) + 1)
54 return {'page': paginator.page(page_num), 'num_pages': paginator.num_pages, 'pages_range': pages_range}
57 def localtime_str(datetime):
58 return date_format(localtime(datetime), 'DATETIME_FORMAT')
61 def random_id(size=8, chars=string.ascii_uppercase + string.digits):
62 return ''.join(random.choice(chars) for _ in range(size))
65 def chunks(seq, size):
66 return (seq[pos:pos + size] for pos in xrange(0, len(seq), size))
68 def generate_md_inputfile(params):
70 # Opis pliku wyjsciowego
71 opis=params['note'][:80]
72 md_input.append(encoding.smart_str(opis, encoding='ascii', errors='ignore'))
73 # Dane kontrolne obliczen
74 md_input.append('SEED=-3059743 PDBREF ONE_LETTER MD EXTCONF RESCALE_MODE=2')
75 ctl_data='nstep='+str(params['nstep'])+' ntwe='+str(params['ntwe'])
76 ctl_data+=' ntwx='+str(params['ntwx'])+' dt='+str(params['dt'])+' damax='+str(params['damax'])+'lang=0 tbf'
77 md_input.append('{:<79}&'.format(ctl_data))
78 md_input.append('tau_bath=1.0 t_bath=300 reset_vel=10000 respa ntime_split=1 maxtime_split=512')
79 # Paramatry pól siłowych
80 if params['force_field'] == 'GAB':
82 md_input.append('WLONG=1.35279 WSCP=1.59304 WELEC=0.71534 WBOND=1.00000 WANG=1.13873 &')
83 md_input.append('WSCLOC=0.16258 WTOR=1.98599 WTORD=1.57069 WCORRH=0.42887 WCORR5=0.00000 &')
84 md_input.append('WCORR6=0.00000 WEL_LOC=0.16036 WTURN3=1.68722 WTURN4=0.66230 WTURN6=0.00000 &')
85 md_input.append('WVDWPP=0.11371 WHPB=1.00000 &')
86 md_input.append('CUTOFF=7.00000 WCORR4=0.00000 WSCCOR=0.0')
89 md_input.append('WLONG=1.00000 WSCP=1.23315 WELEC=0.84476 WBOND=1.00000 WANG=0.62954 &')
90 md_input.append('WSCLOC=0.10554 WTOR=1.84316 WTORD=1.26571 WCORRH=0.19212 WCORR5=0.00000 &')
91 md_input.append('WCORR6=0.00000 WEL_LOC=0.37357 WTURN3=1.40323 WTURN4=0.64673 WTURN6=0.00000 &')
92 md_input.append('WVDWPP=0.23173 WHPB=1.00000 WSCCOR=0.0 &')
93 md_input.append('CUTOFF=7.00000 WCORR4=0.00000')
95 md_input.append(params['pdb_file'].split('/')[-1])
96 # Sekwencja aminokwasów
97 md_input.append(len(params['sequence']))
98 seq_str=params['sequence']
100 md_input.append(seq_str[:80])
102 md_input.append(' 0')
103 md_input.append(' 0')
108 def to_job_desc(params, proxy):
110 desc = JobDescription()
112 direct_map = ('env_variables', 'executable', 'arguments', 'note', 'grant', 'hosts', 'properties', 'queue', 'procs',
113 'wall_time', 'memory', 'memory_per_slot', 'modules', 'input', 'stage_in', 'native', 'notify',
114 'preprocess', 'postprocess', 'persistent')
116 for name in direct_map:
118 setattr(desc, name, params[name])
120 if params['application']:
121 desc.set_application(*params['application'])
122 desc.stage_in += [params['master_file']]
123 desc.arguments.insert(0, os.path.basename(params['master_file']))
125 ftp = FTPOperation(proxy)
127 ftp.mkdir(constants.QCG_DATA_URL, parents=True)
128 url = os.path.join(constants.QCG_DATA_URL, 'script.{}.sh'.format(random_id()))
131 for chunk in chunks(params['script'], 4096):
132 ftp.stream.put(chunk)
136 desc.executable = url
138 desc.set_nodes(*params['nodes'])
139 if params['reservation']:
140 desc.set_reservation(params['reservation'])
141 if params['watch_output']:
142 desc.set_watch_output(params['watch_output'], params['watch_output_pattern'])
149 def to_form_data(xml):
150 # prevent circular import errors
151 from forms import JobDescriptionForm
154 desc = JobDescription()
155 desc.xml_description = xml
157 direct_map = ('env_variables', 'executable', 'arguments', 'note', 'grant', 'hosts', 'properties', 'queue', 'procs',
158 'wall_time', 'memory', 'memory_per_slot', 'modules', 'input', 'stage_in', 'native', 'persistent')
161 for name in direct_map:
162 attr = getattr(desc, name)
163 if isinstance(attr, bool) or attr:
166 if desc.application is not None:
167 app_name, app_ver = desc.application
168 params['application'] = app_name if app_ver is None else app_name + '/' + app_name
169 stage_in = params['stage_in']
170 params['stage_in'], params['master_file'] = stage_in[:-1], stage_in[-1]
171 params['arguments'] = params['arguments'][1:]
172 if desc.nodes is not None:
173 params['nodes'] = ':'.join(map(str, desc.nodes))
174 if desc.reservation is not None:
175 res_id, res_type = desc.reservation
176 params['reservation'] = res_id
177 if desc.notify is not None:
178 params['notify_type'], params['notify_address'] = desc.notify.split(':')
179 if desc.watch_output is not None:
180 watch_output, params['watch_output_pattern'] = desc.watch_output
181 params['watch_output_type'], params['watch_output_address'] = watch_output.split(':')
182 if desc.preprocess is not None:
183 if desc.preprocess.startswith('gsiftp://'):
184 params['preprocess_type'] = JobDescriptionForm.Process.SCRIPT
185 params['preprocess_script'] = desc.preprocess
187 params['preprocess_type'] = JobDescriptionForm.Process.CMD
188 params['preprocess_cmd'] = desc.preprocess
189 if desc.postprocess is not None:
190 if desc.postprocess.startswith('gsiftp://'):
191 params['postprocess_type'] = JobDescriptionForm.Process.SCRIPT
192 params['postprocess_script'] = desc.postprocess
194 params['postprocess_type'] = JobDescriptionForm.Process.CMD
195 params['postprocess_cmd'] = desc.postprocess
200 def restricted(view):
201 return wraps(view)(cache_control(no_cache=True, must_revalidate=True, no_store=True)(login_required(view)))
204 def cached_resources(proxy):
205 hosts = resources_cache.get('hosts')
207 # prevent circular import errors
208 from qcg.service import fetch_resources
210 hosts, _, applications, modules = map(make_choices, fetch_resources(proxy))
211 resources_cache.set('hosts', hosts)
212 resources_cache.set('applications', applications)
213 resources_cache.set('modules', modules)
215 applications = resources_cache.get('applications')
216 modules = resources_cache.get('modules')
218 return hosts, applications, modules
221 def make_choices(iterable):
222 return ((None, ''),) + tuple((item, item) for item in sorted(iterable))