28 from robot.utils import (abspath, ConnectionCache, console_decode, del_env_var,
29 get_env_var, get_env_vars, get_time, is_truthy,
30 is_string, normpath, parse_time, plural_or_not,
31 safe_str, secs_to_timestamp, secs_to_timestr, seq2str,
32 set_env_var, timestr_to_secs, CONSOLE_ENCODING, WINDOWS)
35 PROCESSES = ConnectionCache(
'No active processes.')
155 ROBOT_LIBRARY_SCOPE =
'GLOBAL'
156 ROBOT_LIBRARY_VERSION = __version__
194 return self.
_run_run(command)[1]
221 return self.
_run_run(command)[0]
243 return self.
_run_run(command)
247 self.
_info_info(
"Running command '%s'." % process)
248 stdout = process.read()
277 def get_file(self, path, encoding='UTF-8', encoding_errors='strict'):
279 self.
_link_link(
"Getting file '%s'.", path)
286 with open(path, encoding=encoding, errors=encoding_errors, newline=
'')
as f:
287 return f.read().replace(
'\r\n',
'\n')
290 return {
'SYSTEM':
None,
291 'CONSOLE': CONSOLE_ENCODING}.get(encoding.upper(), encoding)
300 self.
_link_link(
"Getting file '%s'.", path)
301 with open(path,
'rb')
as f:
338 def grep_file(self, path, pattern, encoding='UTF-8', encoding_errors='strict',
342 pattern = fnmatch.translate(f
'{pattern}*')
343 reobj = re.compile(pattern)
347 self.
_link_link(
"Reading file '%s'.", path)
348 with open(path, encoding=encoding, errors=encoding_errors)
as file:
351 line = line.rstrip(
'\r\n')
352 if reobj.search(line):
354 self.
_info_info(
'%d out of %d lines matched' % (len(lines), total_lines))
355 return '\n'.join(lines)
366 def log_file(self, path, encoding='UTF-8', encoding_errors='strict'):
367 content = self.
get_fileget_file(path, encoding, encoding_errors)
368 self.
_info_info(content)
382 if not self.
_glob_glob(path):
383 self.
_fail_fail(msg,
"Path '%s' does not exist." % path)
384 self.
_link_link(
"Path '%s' exists.", path)
395 matches = self.
_glob_glob(path)
398 self.
_link_link(
"Path '%s' does not exist.", path)
401 return glob.glob(path)
if not os.path.exists(path)
else [path]
405 return "%s '%s' exists." % (what, path)
406 return "%s '%s' matches %s." % (what, path,
seq2str(sorted(matches)))
409 return '*' in path
or '?' in path
or (
'[' in path
and ']' in path)
420 matches = [p
for p
in self.
_glob_glob(path)
if os.path.isfile(p)]
422 self.
_fail_fail(msg,
"File '%s' does not exist." % path)
423 self.
_link_link(
"File '%s' exists.", path)
434 matches = [p
for p
in self.
_glob_glob(path)
if os.path.isfile(p)]
437 self.
_link_link(
"File '%s' does not exist.", path)
448 matches = [p
for p
in self.
_glob_glob(path)
if os.path.isdir(p)]
450 self.
_fail_fail(msg,
"Directory '%s' does not exist." % path)
451 self.
_link_link(
"Directory '%s' exists.", path)
462 matches = [p
for p
in self.
_glob_glob(path)
if os.path.isdir(p)]
465 self.
_link_link(
"Directory '%s' does not exist.", path)
487 maxtime = time.time() + timeout
488 while self.
_glob_glob(path):
489 if timeout >= 0
and time.time() > maxtime:
490 self.
_fail_fail(
"'%s' was not removed in %s."
493 self.
_link_link(
"'%s' was removed.", path)
513 maxtime = time.time() + timeout
514 while not self.
_glob_glob(path):
515 if timeout >= 0
and time.time() > maxtime:
516 self.
_fail_fail(
"'%s' was not created in %s."
519 self.
_link_link(
"'%s' was created.", path)
531 self.
_fail_fail(msg,
"Directory '%s' is not empty. Contents: %s."
532 % (path,
seq2str(items, lastsep=
', ')))
533 self.
_link_link(
"Directory '%s' is empty.", path)
543 self.
_fail_fail(msg,
"Directory '%s' is empty." % path)
544 self.
_link_link(
"Directory '%%s' contains %d item%s."
553 if not os.path.isfile(path):
554 self.
_error_error(
"File '%s' does not exist." % path)
555 size = os.stat(path).st_size
558 "File '%s' is not empty. Size: %d bytes." % (path, size))
559 self.
_link_link(
"File '%s' is empty.", path)
567 if not os.path.isfile(path):
568 self.
_error_error(
"File '%s' does not exist." % path)
569 size = os.stat(path).st_size
571 self.
_fail_fail(msg,
"File '%s' is empty." % path)
572 self.
_link_link(
"File '%%s' contains %d bytes." % size, path)
600 self.
_link_link(
"Created file '%s'.", path)
604 parent = os.path.dirname(path)
605 if not os.path.exists(parent):
609 with open(path, mode, encoding=encoding)
as f:
635 content = bytes(ord(c)
for c
in content)
636 path = self.
_write_to_file_write_to_file(path, content, mode=
'wb')
637 self.
_link_link(
"Created binary file '%s'.", path)
649 path = self.
_write_to_file_write_to_file(path, content, encoding, mode=
'a')
650 self.
_link_link(
"Appended to file '%s'.", path)
663 matches = self.
_glob_glob(path)
665 self.
_link_link(
"File '%s' does not exist.", path)
666 for match
in matches:
667 if not os.path.isfile(match):
668 self.
_error_error(
"Path '%s' is not a file." % match)
670 self.
_link_link(
"Removed file '%s'.", match)
689 for item
in self.
_list_dir_list_dir(path, absolute=
True):
690 if os.path.isdir(item):
694 self.
_link_link(
"Emptied directory '%s'.", path)
704 if os.path.isdir(path):
705 self.
_link_link(
"Directory '%s' already exists.", path )
706 elif os.path.exists(path):
707 self.
_error_error(
"Path '%s' is not a directory." % path)
710 self.
_link_link(
"Created directory '%s'.", path)
723 if not os.path.exists(path):
724 self.
_link_link(
"Directory '%s' does not exist.", path)
725 elif not os.path.isdir(path):
726 self.
_error_error(
"Path '%s' is not a directory." % path)
732 path,
"Directory '%s' is not empty." % path)
734 self.
_link_link(
"Removed directory '%s'.", path)
765 source, destination = \
768 source, destination = self.
_atomic_copy_atomic_copy(source, destination)
769 self.
_link_link(
"Copied file from '%s' to '%s'.", source, destination)
775 if os.path.isdir(destination):
776 destination = os.path.join(destination, os.path.basename(source))
777 return source, destination
780 source = self.
_absnorm_absnorm(source)
781 sources = self.
_glob_glob(source)
783 self.
_error_error(
"Multiple matches with source pattern '%s'." % source)
786 if not os.path.exists(source):
787 self.
_error_error(
"Source file '%s' does not exist." % source)
788 if not os.path.isfile(source):
789 self.
_error_error(
"Source file '%s' is not a regular file." % source)
793 if isinstance(destination, pathlib.Path):
794 destination = str(destination)
795 is_dir = os.path.isdir(destination)
or destination.endswith((
'/',
'\\'))
796 destination = self.
_absnorm_absnorm(destination)
797 directory = destination
if is_dir
else os.path.dirname(destination)
802 if not os.path.exists(path):
804 elif not os.path.isdir(path):
805 self.
_error_error(
"Destination '%s' exists and is not a directory." % path)
809 self.
_link_link(
"Source '%s' and destination '%s' point to the same "
810 "file.", source, destination)
817 return os.path.realpath(
normpath(path, case_normalize=
True))
836 temp_directory = tempfile.mkdtemp(dir=os.path.dirname(destination))
837 temp_file = os.path.join(temp_directory, os.path.basename(source))
839 shutil.copy(source, temp_file)
840 if os.path.exists(destination):
841 os.remove(destination)
842 shutil.move(temp_file, destination)
844 shutil.rmtree(temp_directory)
845 return source, destination
859 source, destination = \
862 shutil.move(source, destination)
863 self.
_link_link(
"Moved file from '%s' to '%s'.", source, destination)
882 sources, destination \
884 for source
in sources:
885 self.
copy_filecopy_file(source, destination)
889 self.
_error_error(
'Must contain destination and at least one source.')
891 destination = self.
_absnorm_absnorm(items[-1])
893 return sources, destination
897 for pattern
in patterns:
898 files.extend(self.
_glob_glob(self.
_absnorm_absnorm(pattern)))
908 sources, destination \
910 for source
in sources:
911 self.
move_filemove_file(source, destination)
921 shutil.copytree(source, destination)
922 self.
_link_link(
"Copied directory from '%s' to '%s'.", source, destination)
925 source = self.
_absnorm_absnorm(source)
926 destination = self.
_absnorm_absnorm(destination)
927 if not os.path.exists(source):
928 self.
_error_error(
"Source '%s' does not exist." % source)
929 if not os.path.isdir(source):
930 self.
_error_error(
"Source '%s' is not a directory." % source)
931 if os.path.exists(destination)
and not os.path.isdir(destination):
932 self.
_error_error(
"Destination '%s' is not a directory." % destination)
933 if os.path.exists(destination):
934 base = os.path.basename(source)
935 destination = os.path.join(destination, base)
937 parent = os.path.dirname(destination)
938 if not os.path.exists(parent):
940 return source, destination
949 source, destination \
951 shutil.move(source, destination)
952 self.
_link_link(
"Moved directory from '%s' to '%s'.", source, destination)
971 self.
_error_error(
"Environment variable '%s' does not exist." % name)
981 self.
_info_info(
"Environment variable '%s' set to value '%s'."
1007 if initial
is not sentinel:
1008 values = (initial,) + values
1009 separator = config.pop(
'separator', os.pathsep)
1011 config = [
'='.join(i)
for i
in sorted(config.items())]
1012 self.
_error_error(
'Configuration %s not accepted.'
1013 %
seq2str(config, lastsep=
' or '))
1027 self.
_info_info(
"Environment variable '%s' deleted." % name)
1029 self.
_info_info(
"Environment variable '%s' does not exist." % name)
1038 self.
_fail_fail(msg,
"Environment variable '%s' is not set." % name)
1039 self.
_info_info(
"Environment variable '%s' is set to '%s'." % (name, value))
1048 self.
_fail_fail(msg,
"Environment variable '%s' is set to '%s'."
1050 self.
_info_info(
"Environment variable '%s' is not set." % name)
1068 for name
in sorted(variables, key=
lambda item: item.lower()):
1069 self.
_log_log(
'%s = %s' % (name, variables[name]), level)
1094 parts = [str(p)
if isinstance(p, pathlib.Path)
else p.replace(
'/', os.sep)
1095 for p
in (base,) + parts]
1112 return [self.
join_pathjoin_path(base, path)
for path
in paths]
1138 if isinstance(path, pathlib.Path):
1141 path = path.replace(
'/', os.sep)
1142 path = os.path.normpath(os.path.expanduser(path))
1148 path = os.path.normcase(path)
1197 basename = os.path.basename(path)
1198 if basename.startswith(
'.' * basename.count(
'.')):
1200 if path.endswith(
'.'):
1201 path2 = path.rstrip(
'.')
1202 trailing_dots =
'.' * (len(path) - len(path2))
1206 basepath, extension = os.path.splitext(path)
1207 if extension.startswith(
'.'):
1208 extension = extension[1:]
1210 extension += trailing_dots
1212 basepath += trailing_dots
1213 return basepath, extension
1253 if not os.path.exists(path):
1254 self.
_error_error(
"Path '%s' does not exist." % path)
1255 mtime =
get_time(format, os.stat(path).st_mtime)
1256 self.
_link_link(
"Last modified time of '%%s' is %s." % mtime, path)
1297 if not os.path.exists(path):
1298 self.
_error_error(
"File '%s' does not exist." % path)
1299 if not os.path.isfile(path):
1300 self.
_error_error(
"Path '%s' is not a regular file." % path)
1301 os.utime(path, (mtime, mtime))
1304 self.
_link_link(
"Set modified time of '%%s' to %s." % tstamp, path)
1309 if not os.path.isfile(path):
1310 self.
_error_error(
"File '%s' does not exist." % path)
1311 size = os.stat(path).st_size
1313 self.
_link_link(
"Size of file '%%s' is %d byte%s." % (size, plural), path)
1339 items = self.
_list_dir_list_dir(path, pattern, absolute)
1354 self.
_info_info(
'%d director%s:\n%s' % (len(dirs),
1355 'y' if len(dirs) == 1
else 'ies',
1366 count = len(self.
_list_dir_list_dir(path, pattern))
1379 self.
_info_info(
"%s director%s." % (count,
'y' if count == 1
else 'ies'))
1384 self.
_link_link(
"Listing contents of directory '%s'.", path)
1385 if not os.path.isdir(path):
1386 self.
_error_error(
"Directory '%s' does not exist." % path)
1388 items = sorted(
safe_str(item)
for item
in os.listdir(path))
1390 items = [i
for i
in items
if fnmatch.fnmatchcase(i, pattern)]
1392 path = os.path.normpath(path)
1393 items = [os.path.join(path, item)
for item
in items]
1397 return [item
for item
in self.
_list_dir_list_dir(path, pattern, absolute)
1398 if os.path.isfile(os.path.join(path, item))]
1401 return [item
for item
in self.
_list_dir_list_dir(path, pattern, absolute)
1402 if os.path.isdir(os.path.join(path, item))]
1414 if os.path.isdir(path):
1415 self.
_error_error(
"Cannot touch '%s' because it is a directory." % path)
1416 if not os.path.exists(os.path.dirname(path)):
1417 self.
_error_error(
"Cannot touch '%s' because its parent directory does "
1418 "not exist." % path)
1419 if os.path.exists(path):
1420 mtime = round(time.time())
1421 os.utime(path, (mtime, mtime))
1422 self.
_link_link(
"Touched existing file '%s'.", path)
1424 open(path,
'w').close()
1425 self.
_link_link(
"Touched new file '%s'.", path)
1437 self.
_log_log(msg,
'INFO')
1440 paths = tuple(
'<a href="file://%s">%s</a>' % (p, p)
for p
in paths)
1441 self.
_log_log(msg % paths,
'HTML')
1444 self.
_log_log(msg,
'WARN')
1447 logger.write(msg, level)
1479 if '>' not in command:
1480 if command.endswith(
'&'):
1481 command = command[:-1] +
' 2>&1 &'
1487 if '\r\n' in output:
1488 output = output.replace(
'\r\n',
'\n')
1489 if output.endswith(
'\n'):
1490 output = output[:-1]
A library providing keywords for operating system related tasks.
def _are_source_and_destination_same_file(self, source, destination)
def should_exist(self, path, msg=None)
Fails unless the given path (file or directory) exists.
def get_environment_variable(self, name, default=None)
Returns the value of an environment variable with the given name.
def count_directories_in_directory(self, path, pattern=None)
Wrapper for Count Items In Directory returning only directory count.
def _normalize_copy_and_move_source(self, source)
def get_file(self, path, encoding='UTF-8', encoding_errors='strict')
Returns the contents of a specified file.
def _list_dirs_in_dir(self, path, pattern=None, absolute=False)
def list_files_in_directory(self, path, pattern=None, absolute=False)
Wrapper for List Directory that returns only files.
def copy_file(self, source, destination)
Copies the source file into the destination.
def grep_file(self, path, pattern, encoding='UTF-8', encoding_errors='strict', regexp=False)
Returns the lines of the specified file that match the pattern.
def directory_should_not_exist(self, path, msg=None)
Fails if the given path points to an existing file.
def set_modified_time(self, path, mtime)
Sets the file modification and access times.
def wait_until_created(self, path, timeout='1 minute')
Waits until the given file or directory is created.
def file_should_not_exist(self, path, msg=None)
Fails if the given path points to an existing file.
def log_environment_variables(self, level='INFO')
Logs all environment variables using the given log level.
def get_environment_variables(self)
Returns currently available environment variables as a dictionary.
def _fail(self, *messages)
def copy_directory(self, source, destination)
Copies the source directory into the destination.
def join_path(self, base, *parts)
Joins the given path part(s) to the given base path.
def list_directories_in_directory(self, path, pattern=None, absolute=False)
Wrapper for List Directory that returns only directories.
def join_paths(self, base, *paths)
Joins given paths with base and returns resulted paths.
def _log(self, msg, level)
def _ensure_destination_directory_exists(self, path)
def directory_should_not_be_empty(self, path, msg=None)
Fails if the specified directory is empty.
def create_file(self, path, content='', encoding='UTF-8')
Creates a file with the given content and encoding.
def _atomic_copy(self, source, destination)
Copy file atomically (or at least try to).
def _list_files_in_dir(self, path, pattern=None, absolute=False)
def log_file(self, path, encoding='UTF-8', encoding_errors='strict')
Wrapper for Get File that also logs the returned file.
def append_to_file(self, path, content, encoding='UTF-8')
Appends the given content to the specified file.
def touch(self, path)
Emulates the UNIX touch command.
def remove_file(self, path)
Removes a file with the given path.
def create_binary_file(self, path, content)
Creates a binary file with the given content.
def split_path(self, path)
Splits the given path from the last path separator (/ or \\).
def copy_files(self, *sources_and_destination)
Copies specified files to the target directory.
def _write_to_file(self, path, content, encoding=None, mode='w')
def _force_normalize(self, path)
def _get_matches_error(self, what, path, matches)
def _prepare_copy_and_move_files(self, items)
def remove_directory(self, path, recursive=False)
Removes the directory pointed to by the given path.
def move_files(self, *sources_and_destination)
Moves specified files to the target directory.
def _list_dir(self, path, pattern=None, absolute=False)
def split_extension(self, path)
Splits the extension from the given path.
def should_not_exist(self, path, msg=None)
Fails if the given path (file or directory) exists.
def _normalize_copy_and_move_destination(self, destination)
def get_binary_file(self, path)
Returns the contents of a specified file.
def list_directory(self, path, pattern=None, absolute=False)
Returns and logs items in a directory, optionally filtered with pattern.
def directory_should_be_empty(self, path, msg=None)
Fails unless the specified directory is empty.
def move_file(self, source, destination)
Moves the source file into the destination.
def get_modified_time(self, path, format='timestamp')
Returns the last modification time of a file or directory.
def get_file_size(self, path)
Returns and logs file size as an integer in bytes.
def empty_directory(self, path)
Deletes all the content from the given directory.
def environment_variable_should_not_be_set(self, name, msg=None)
Fails if the specified environment variable is set.
def environment_variable_should_be_set(self, name, msg=None)
Fails if the specified environment variable is not set.
def file_should_be_empty(self, path, msg=None)
Fails unless the specified file is empty.
def _prepare_copy_and_move_file(self, source, destination)
def run_and_return_rc_and_output(self, command)
Runs the given command in the system and returns the RC and output.
def count_items_in_directory(self, path, pattern=None)
Returns and logs the number of all items in the given directory.
def _map_encoding(self, encoding)
def count_files_in_directory(self, path, pattern=None)
Wrapper for Count Items In Directory returning only file count.
def set_environment_variable(self, name, value)
Sets an environment variable to a specified value.
def wait_until_removed(self, path, timeout='1 minute')
Waits until the given file or directory is removed.
def append_to_environment_variable(self, name, *values, **config)
Appends given values to environment variable name.
def _prepare_copy_and_move_directory(self, source, destination)
def create_directory(self, path)
Creates the specified directory.
def run(self, command)
Runs the given command in the system and returns the output.
def normalize_path(self, path, case_normalize=False)
Normalizes the given path.
def remove_environment_variable(self, *names)
Deletes the specified environment variable.
def _is_glob_path(self, path)
def remove_files(self, *paths)
Uses Remove File to remove multiple files one-by-one.
def file_should_not_be_empty(self, path, msg=None)
Fails if the specified file is empty.
def directory_should_exist(self, path, msg=None)
Fails unless the given path points to an existing directory.
def _link(self, msg, *paths)
def _glob_files(self, patterns)
def move_directory(self, source, destination)
Moves the source directory into a destination.
def run_and_return_rc(self, command)
Runs the given command in the system and returns the return code.
def file_should_exist(self, path, msg=None)
Fails unless the given path points to an existing file.
def _process_output(self, output)
def _process_command(self, command)
def __init__(self, command)
def console_decode(string, encoding=CONSOLE_ENCODING)
Decodes bytes from console encoding to Unicode.
def seq2str(sequence, quote="'", sep=', ', lastsep=' and ')
Returns sequence in format ‘'item 1’, 'item 2' and 'item 3'`.
def get_env_var(name, default=None)
def set_env_var(name, value)
def get_env_vars(upper=os.sep !='/')
def abspath(path, case_normalize=False)
Replacement for os.path.abspath with some enhancements and bug fixes.
def normpath(path, case_normalize=False)
Replacement for os.path.normpath with some enhancements.
def get_time(format='timestamp', time_=None)
Return the given or current time in requested format.
def secs_to_timestamp(secs, seps=None, millis=False)
def secs_to_timestr(secs, compact=False)
Converts time in seconds to a string representation.
def parse_time(timestr)
Parses the time string and returns its value as seconds since epoch.
def timestr_to_secs(timestr, round_to=3, accept_plain_values=True)
Parses time strings like '1h 10s', '01:00:10' and '42' and returns seconds.
def is_truthy(item)
Returns True or False depending on is the item considered true or not.
def get_version(naked=False)