Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
global cfg, _root
cfg = pkconfig.init(
root=(None, _cfg_root, 'where database resides'),
)
v = cfg.root
if v:
assert os.path.isabs(v), \
'{}: SIREPO_SRDB_ROOT must be absolute'.format(v)
assert os.path.isdir(v), \
'{}: SIREPO_SRDB_ROOT must be a directory and exist'.format(v)
v = pkio.py_path(v)
else:
assert pkconfig.channel_in('dev'), \
'SIREPO_SRDB_ROOT must be configured except in DEV'
fn = sys.modules[pkinspect.root_package(_init_root)].__file__
root = pkio.py_path(pkio.py_path(pkio.py_path(fn).dirname).dirname)
# Check to see if we are in our dev directory. This is a hack,
# but should be reliable.
if not root.join('requirements.txt').check():
# Don't run from an install directory
root = pkio.py_path('.')
v = pkio.mkdir_parent(root.join(_DEFAULT_ROOT))
_root = v
return v
def _parent_file(cfg_dir, filename):
return str(pkio.py_path(cfg_dir.dirname).join(filename))
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.run_dir = pkio.py_path(self.msg.runDir)
self._is_compute = self.msg.jobCmd == 'compute'
if self._is_compute:
pkio.unchecked_remove(self.run_dir)
pkio.mkdir_parent(self.run_dir)
self._in_file = self._create_in_file()
self._process = _Process(self)
self._terminating = False
self._start_time = int(time.time())
self.jid = self.msg.computeJid
s = srwl_uti_smp.SRWLUtiSmp(
file_path=path,
area=None if not int(m['cropArea']) else (m['areaXStart'], m['areaXEnd'], m['areaYStart'], m['areaYEnd']),
rotate_angle=float(m['rotateAngle']),
rotate_reshape=int(m['rotateReshape']),
cutoff_background_noise=float(m['cutoffBackgroundNoise']),
background_color=int(m['backgroundColor']),
invert=int(m['invert']),
tile=None if not int(m['tileImage']) else (m['tileRows'], m['tileColumns']),
shift_x=m['shiftX'],
shift_y=m['shiftY'],
is_save_images=True,
prefix=str(tmp_dir),
output_image_format=m['outputImageFormat'],
)
return pkio.py_path(s.processed_image_name)
def _report_job_status(job_tracker, request):
pkdc('report_job_status: {}', request)
status = job_tracker.report_job_status(
#TODO(e-carlin): Find a common place to do pkio.py_path() these are littered around
pkio.py_path(request.run_dir), request.jhash
).value
return pkcollections.Dict({
'action': 'status_of_report_job',
'request_id': request.request_id,
'uid': request.uid,
'status': status,
})
def _python(data):
"""Generate python in current directory
Args:
data (dict): simulation
Returns:
py.path.Local: file to append
"""
import sirepo.template
import copy
template = sirepo.template.import_module(data)
res = pkio.py_path('run.py')
res.write(template.python_source_for_model(copy.deepcopy(data), None))
return res
async def _run_extract_job(job_tracker, request, io_loop):
pkdc('run_extrac_job: {}', request)
result = await job_tracker.run_extract_job(
io_loop,
pkio.py_path(request.run_dir),
request.jhash,
request.subcmd,
request.arg,
)
return pkcollections.Dict({
'action' : 'result_of_run_extract_job',
'request_id': request.request_id,
'uid': request.uid,
'result': result,
})
def __volumes(self):
res = []
def _res(src, tgt):
res.append('--volume={}:{}'.format(src, tgt))
if pkconfig.channel_in('dev'):
for v in '~/src', '~/.pyenv':
v = pkio.py_path('~/src')
# pyenv and src shouldn't be writable, only rundir
_res(v, v + ':ro')
_res(self.run_dir, self.run_dir)
return res