1 from __future__
import print_function
30 from .exceptions
import IrodsError, IrodsWarning
36 return socket.getaddrinfo(
37 socket.gethostname(), 0, 0, 0, 0, socket.AI_CANONNAME)[0][3]
40 if not isinstance(args, str)
and isinstance(args, six.text_type):
41 args = args.encode(
'ascii')
42 return shlex.split(args)
45 l = logging.getLogger(__name__)
46 if not use_unsafe_shell
and isinstance(args, six.string_types):
48 kwargs[
'stdout'] = stdout
49 kwargs[
'stderr'] = stderr
50 kwargs[
'shell'] = use_unsafe_shell
52 kwargs_without_env = copy.copy(kwargs)
53 kwargs_without_env[
'env'] =
'HIDDEN'
55 kwargs_without_env = kwargs
56 l.debug(
'Calling %s with options:\n%s', args, pprint.pformat(kwargs_without_env))
58 return subprocess.Popen(args, **kwargs)
60 six.reraise(IrodsError,
62 'Call to open process with {0} failed:'.format(
65 'Could not find the requested executable \'{0}\'; '
66 'please ensure \'{0}\' is installed and in the path.'.format(
72 start_time = time.time()
73 while time.time() < start_time + timeout:
74 if p.poll()
is not None:
86 'The call {0} did not complete within'
87 ' {1} seconds.'.format(args, timeout))
91 if 'stdin' in kwargs
and kwargs[
'stdin'] != subprocess.PIPE:
92 raise IrodsError(
'\'input\' option is mutually exclusive with a \'stdin\' '
93 'option that is not equal to \'subprocess.PIPE\'.')
94 kwargs[
'stdin'] = subprocess.PIPE
98 return (out, err, p.returncode)
101 if returncode
is not None and returncode != 0:
103 kwargs_without_env = copy.copy(kwargs)
104 kwargs_without_env[
'env'] =
'HIDDEN'
106 kwargs_without_env = kwargs
108 'Call to open process with {0} returned an error:'.format(
111 'Options passed to Popen:',
112 indent(*[
'{0}: {1}'.format(k, v)
for k, v
in kwargs_without_env.items()]),
113 'Return code: {0}'.format(returncode),
120 l = logging.getLogger(__name__)
121 out, err = [t.decode(
'utf_8')
for t
in p.communicate(input=(input.encode(
'ascii')
if input
is not None else None))]
122 message = [
'Command %s returned with code %s.' % (args, p.returncode)]
124 message.append(
'stdin:\n%s' %
indent(input))
126 message.append(
'stdout:\n%s' %
indent(out))
128 message.append(
'stderr:\n%s' %
indent(err))
129 l.debug(
'\n'.
join(message))
139 if 'indentation' in kwargs:
140 indentation = kwargs[
'indentation']
144 ''.
join([indentation,
'\n{0}'.format(indentation).
join(lines.splitlines())])
148 def get_exe(process):
149 if psutil.version_info >= (2,0):
152 abspath = os.path.abspath(binary_file_path)
154 for p
in psutil.process_iter():
156 if abspath == get_exe(p):
158 except (psutil.NoSuchProcess, psutil.AccessDenied):
163 p = psutil.Process(pid)
171 so_regex = re.compile(so_name)
173 if 'LD_PRELOAD' in os.environ:
174 for path
in os.environ[
'LD_PRELOAD'].split(
':'):
175 if path
not in paths
and os.path.exists(path)
and (
176 (regex
and so_regex.match(os.path.basename(path)))
or
177 (
not regex
and os.path.basename(path) == so_name)):
180 if 'LD_LIBRARY_PATH' in os.environ:
181 for directory
in os.environ[
'LD_LIBRARY_PATH'].split(
':'):
182 if regex
and os.path.exists(directory):
183 for name
in os.listdir(directory):
184 if so_regex.match(name)
and os.path.join(directory, name)
not in paths:
185 paths.append(os.path.join(directory, name))
186 elif os.path.exists(os.path.join(directory, so_name))
and os.path.join(directory, so_name)
not in paths:
187 paths.append(os.path.join(directory, so_name))
189 env = os.environ.copy()
190 env[
'PATH'] =
':'.
join([env[
'PATH'],
'/sbin'])
192 for directory
in [d.rstrip(
':')
for d
in out.splitlines()
if d
and d[0] ==
'/']:
193 if regex
and os.path.exists(directory):
194 for name
in os.listdir(directory):
195 if so_regex.match(name)
and os.path.join(directory, name)
not in paths:
196 paths.append(os.path.join(directory, name))
197 elif os.path.exists(os.path.join(directory, so_name))
and os.path.join(directory, so_name)
not in paths:
198 paths.append(os.path.join(directory, so_name))
200 for (directory, _, names)
in itertools.chain(*[os.walk(d)
for d
in itertools.chain(additional_directories, [
'/usr/lib/'])]):
203 if so_regex.match(os.path.basename(name))
and os.path.join(directory, name)
not in paths:
204 paths.append(os.path.join(directory, name))
205 elif os.path.exists(os.path.join(directory, so_name))
and os.path.join(directory, so_name)
not in paths:
206 paths.append(os.path.join(directory, so_name))
211 block_size = pow(2, 20)
212 hasher = hashlib.new(hash_type)
213 with open(filename,
'rb')
as f:
215 data = f.read(block_size)
219 if encoding ==
'hex':
220 return hasher.hexdigest()
221 if encoding ==
'base64':
222 return base64.b64encode(hasher.digest()).
decode()
223 if encoding
is None or encoding ==
'none':
224 return hasher.digest()
225 raise IrodsError(
'Unknown encoding %s for digest' % (encoding))
229 possible_shm_locations = [
'/var/run/shm',
'/dev/shm']
230 for l
in possible_shm_locations:
232 files = os.listdir(l)
234 if 'irods' in f.lower():
235 return os.path.join(l, f)
243 if not isinstance(i, str)
and isinstance(i, six.text_type):
244 i = i.encode(
'ascii')
245 elif isinstance(i, list):
252 for k, v
in d.items():
253 if not isinstance(k, str)
and isinstance(k, six.text_type):
254 k = k.encode(
'ascii')
255 if not isinstance(v, str)
and isinstance(v, six.text_type):
256 v = v.encode(
'ascii')
257 elif isinstance(v, list):
263 with open(filename,
'rt')
as f:
264 return json.load(f, object_hook=json_object_hook_dict)
268 env.update(update_dict)
269 with open(filename,
'wt')
as f:
270 json.dump(env, f, indent=4)
273 if not os.path.exists(directory_name_suffix):
274 os.mkdir(directory_name_suffix)
275 for i
in range(file_count):
276 with open(
'{0}/{1}'.format(directory_name_suffix, i),
'wt')
as f:
277 print(
"iglkg3fqfhwpwpo-" +
"A" * i, file=f, end=
'')
280 filepath = os.path.abspath(filename)
281 with open(filepath,
'wt')
as f:
282 print(
'TESTFILE -- [' + filepath +
']', file=f, end=
'')
286 with open(fname,
'at'):
287 os.utime(fname, times)
290 with open(fname,
'at')
as f:
291 print(string, file=f, end=
'')
293 def make_file(f_name, f_size, contents='zero', block_size_in_bytes=1000):
294 if contents
not in [
'arbitrary',
'random',
'zero']:
296 if contents ==
'arbitrary' or f_size == 0:
300 source = {
'zero':
'/dev/zero',
301 'random':
'/dev/urandom'}[contents]
303 count = f_size / block_size_in_bytes
305 execute_command([
'dd',
'if='+source,
'of='+f_name,
'count='+str(count),
'bs='+str(block_size_in_bytes)])
306 leftover_size = f_size % block_size_in_bytes
307 if leftover_size > 0:
308 execute_command([
'dd',
'if='+source,
'of='+f_name,
'count=1',
'bs='+str(leftover_size),
'oflag=append',
'conv=notrunc'])
310 execute_command([
'dd',
'if='+source,
'of='+f_name,
'count=1',
'bs='+str(f_size)])
315 os.makedirs(directory)
317 if e.errno == errno.EEXIST
and os.path.isdir(directory):
323 os.makedirs(dir_name)
324 for i
in range(file_count):
325 make_file(os.path.join(dir_name,
"junk" + str(i).zfill(4)),
327 local_files = os.listdir(dir_name)
328 if len(local_files) != file_count:
329 raise AssertionError(
"dd loop did not make all " + str(file_count) +
" files")
336 current_dir_name = root_name
337 for d
in range(depth):
342 directories[current_dir_name] = files
345 current_dir_name = os.path.join(current_dir_name,
'sub'+str(d))
350 for file
in os.listdir(path):
351 if os.path.isfile(os.path.join(path, file)):
356 'python -c "from __future__ import print_function; import os; import json; print(json.dumps(dict(os.environ)))"'])
357 return json.loads(out)
362 pw_record = pwd.getpwnam(user)
363 os.environ[
'HOME'] = pw_record.pw_dir
364 os.environ[
'LOGNAME'] = pw_record.pw_name
365 os.environ[
'USER'] = pw_record.pw_name
366 os.setgid(pw_record.pw_gid
if group
is None else grp.getgrnam(group).gr_gid)
367 os.setuid(pw_record.pw_uid)
369 @contextlib.contextmanager
371 with tempfile.NamedTemporaryFile(prefix=os.path.basename(filename))
as f:
372 shutil.copyfile(filename, f.name)
376 shutil.copyfile(f.name, filename)
378 @contextlib.contextmanager
383 shutil.rmtree(dirname)
393 with open(filename,
'r')
as f:
395 with open(filename,
'wt')
as f:
396 print(string, file=f, end=
'')
397 print(contents, file=f, end=
'')
400 irods_home = os.path.join(
'/', zone_name,
'home', username)
403 'irods_host': hostname,
405 'irods_default_resource':
'demoResc',
406 'irods_home': irods_home,
407 'irods_cwd': irods_home,
408 'irods_user_name': username,
409 'irods_zone_name': zone_name,
410 'irods_client_server_negotiation':
'request_server_negotiation',
411 'irods_client_server_policy':
'CS_NEG_REFUSE',
412 'irods_encryption_key_size': 32,
413 'irods_encryption_salt_size': 8,
414 'irods_encryption_num_hash_rounds': 16,
415 'irods_encryption_algorithm':
'AES-256-CBC',
416 'irods_default_hash_scheme':
'SHA256',
417 'irods_maximum_size_for_single_buffer_in_megabytes': 32,
418 'irods_default_number_of_transfer_threads': 4,
419 'irods_maximum_number_of_transfer_threads': 64,
420 'irods_transfer_buffer_size_for_parallel_transfer_in_megabytes': 4,
421 'irods_connection_pool_refresh_time_in_seconds': 300
426 'irods_client_server_policy':
'CS_NEG_REQUIRE',
427 'irods_ssl_verify_server':
'cert',
428 'irods_ssl_ca_certificate_file':
'/etc/irods/server.crt',
433 return platform.linux_distribution()[0]
436 return platform.linux_distribution()[1].split(
'.')[0]
439 if isinstance(ils_out, six.string_types):
440 ils_out = ils_out.strip().split()
443 if not item.startswith(
'C- /'):
447 return os.stat(path).st_size
450 with open(log_path,
'at')
as f:
451 print(message, file=f, end=
'')
456 if isinstance(pattern,(tuple,list)):
457 pattern = [pattern[0].
encode(
'ascii')] +
list(pattern[1:])
458 elif isinstance(pattern,re._pattern_type):
461 pattern = [pattern.encode(
'ascii')]
462 if target
is None: target=re.compile(*pattern)
463 with open(log_path)
as f:
464 m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
465 occurrences.extend(j
for j
in target.finditer(m[start_index:]))
470 with open(log_path)
as f:
471 m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
473 target = string.encode(
'ascii')
474 i = m.find(target, start_index)
477 i = m.find(target, i + 1)
482 return tuple(map(int, version_string.split(
'.')))
486 os.path.join( paths.server_bin_directory(),
'hostname_resolves_to_local_address'),
492 raise IrodsError(
'Error encountered in hostname_resolves_to_local_address for hostname [{0}]:\n{1}'.format(hostname, err))
495 lines = [l.strip()
for l
in message.splitlines()]
498 length = max(length, len(line))
499 edge =
'+' +
'-' * (length + 2) +
'+'
500 format_string =
'{0:<' + str(length) +
'}'
501 header_lines = [
'', edge]
503 header_lines.append(
'| ' + format_string.format(line) +
' |')
504 header_lines.append(edge)
505 header_lines.append(
'')
506 return '\n'.
join(header_lines)
509 for k, v
in u.items():
510 d[k] =
nested_update(d.get(k, {}), v)
if isinstance(v, collections.Mapping)
else u[k]
514 echo = kwargs.get(
'echo',
True)
515 end = kwargs.get(
'end',
': ')
516 input_filter = kwargs.get(
'input_filter',
lambda x: x)
518 l = logging.getLogger(__name__)
519 message =
''.
join([args[0] % tuple(args[1:]), end])
523 print(message, end=
'')
525 user_input = sys.stdin.readline().rstrip(
'\n')
526 l.debug(
'User input: %s', user_input)
528 if sys.stdin.isatty():
529 user_input = getpass.getpass(message)
531 print(
'Warning: Cannot control echo output on the terminal (stdin is not a tty). Input may be echoed.', file=sys.stderr)
532 user_input = sys.stdin.readline().rstrip(
'\n')
533 if not sys.stdin.isatty():
536 return input_filter(user_input)
537 except InputFilterError
as e:
538 l.debug(
'Error encountered in user input:', exc_info=sys.exc_info())
539 l.warning(e.args[0]
if len(e.args)
else "User input error.")
542 l = logging.getLogger(__name__)
543 default = kwargs.pop(
'default', [])
544 input_filter = kwargs.pop(
'input_filter',
lambda x: x)
548 if len(default) == 1:
550 args[0] % tuple(args[1:]),
551 ' [%s]' % (default[0])])
552 user_input =
prompt(message, **kwargs)
554 user_input = default[0]
557 args[0] % tuple(args[1:]),
':\n',
558 '\n'.
join([
'%d. %s' % (i + 1, default[i])
for i
in range(0, len(default))]),
559 '\nPlease select a number or choose 0 to enter a new value'])
562 i =
int(user_input) - 1
563 except (TypeError, ValueError):
565 if i
in range(0, len(default)):
566 user_input = default[i]
568 user_input =
prompt(
'New value', **kwargs)
570 user_input =
prompt(*args, **kwargs)
572 return input_filter(user_input)
573 except InputFilterError
as e:
574 l.debug(
'Error encountered in user input:', exc_info=sys.exc_info())
575 l.warning(e.args[0]
if len(e.args)
else "User input error.")
581 except ValueError
as e:
594 if (minimum
is None or len(x) >= minimum)
and (maximum
is None or len(x) <= maximum):
596 if minimum
is not None and minimum < 0:
599 new_minimum = minimum
600 if new_minimum
is not None and maximum
is not None:
601 if new_minimum == maximum:
602 raise InputFilterError(
'%s must be exactly %s character%s in length.' % (field, maximum,
'' if maximum == 1
else 's'))
603 if new_minimum < maximum:
604 raise InputFilterError(
'%s must be between %s and %s characters in length.' % (field, new_minimum, maximum))
605 raise IrodsError(
'Minimum character count %s must not be greater than maximum character count %s.' % (new_minimum, maximum))
606 if new_minimum
is not None:
607 raise InputFilterError(
'%s must be at least %s character%s in length.' % (field, new_minimum,
'' if new_minimum == 1
else 's'))
608 raise InputFilterError(
'%s may be at most %s character%s in length.' % (field, maximum,
'' if maximum == 1
else 's'))
614 class callback_on_change_dict(dict):
621 super(callback_on_change_dict, self).
__init__(*args, **kwargs)
624 super(callback_on_change_dict, self).
__setitem__(*args, **kwargs)
628 super(callback_on_change_dict, self).
__delitem__(*args, **kwargs)
632 super(callback_on_change_dict, self).
update(*args, **kwargs)
636 super(callback_on_change_dict, self).
clear(*args, **kwargs)
639 def pop(self, *args, **kwargs):
640 super(callback_on_change_dict, self).
pop(*args, **kwargs)
644 super(callback_on_change_dict, self).
popitem(*args, **kwargs)
648 super(callback_on_change_dict, self).
setdefault(*args, **kwargs)
652 for _
in range(maxrep):
660 if server_log_path
is None:
661 server_log_path=paths.server_log_path()
665 if server_log_path
is None:
666 server_log_path=paths.server_log_path()
670 if server_log_path
is None:
671 server_log_path=paths.server_log_path()
675 if server_log_path
is None:
676 server_log_path=paths.server_log_path()