Best Python code snippet using localstack_python
fileset.py
Source:fileset.py
1# JUBE Benchmarking Environment2# Copyright (C) 2008-20163# Forschungszentrum Juelich GmbH, Juelich Supercomputing Centre4# http://www.fz-juelich.de/jsc/jube5#6# This program is free software: you can redistribute it and/or modify7# it under the terms of the GNU General Public License as published by8# the Free Software Foundation, either version 3 of the License, or9# any later version.10#11# This program is distributed in the hope that it will be useful,12# but WITHOUT ANY WARRANTY; without even the implied warranty of13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14# GNU General Public License for more details.15#16# You should have received a copy of the GNU General Public License17# along with this program. If not, see <http://www.gnu.org/licenses/>.18"""Fileset related classes"""19from __future__ import (print_function,20 unicode_literals,21 division)22import os23import shutil24import xml.etree.ElementTree as ET25import jube2.util.util26import jube2.conf27import jube2.step28import jube2.log29import glob30LOGGER = jube2.log.get_logger(__name__)31class Fileset(list):32 """Container for file copy, link and prepare operations"""33 def __init__(self, name):34 list.__init__(self)35 self._name = name36 @property37 def name(self):38 """Return fileset name"""39 return self._name40 def etree_repr(self):41 """Return etree object representation"""42 fileset_etree = ET.Element("fileset")43 fileset_etree.attrib["name"] = self._name44 for file_handle in self:45 fileset_etree.append(file_handle.etree_repr())46 return fileset_etree47 def create(self, work_dir, parameter_dict, alt_work_dir=None,48 environment=None, file_path_ref=""):49 """Copy/load/prepare all files in fileset"""50 for file_handle in self:51 if type(file_handle) is Prepare:52 file_handle.execute(53 parameter_dict=parameter_dict,54 work_dir=alt_work_dir if alt_work_dir55 is not None else work_dir,56 environment=environment)57 else:58 file_handle.create(59 work_dir=work_dir, parameter_dict=parameter_dict,60 alt_work_dir=alt_work_dir, file_path_ref=file_path_ref,61 environment=environment)62class File(object):63 """Generic file access"""64 def __init__(self, path, name=None, is_internal_ref=False, active="true",65 source_dir="", target_dir=""):66 self._path = path67 self._source_dir = source_dir68 self._target_dir = target_dir69 self._name = name70 self._file_path_ref = ""71 self._active = active72 self._is_internal_ref = is_internal_ref73 def create(self, work_dir, parameter_dict, alt_work_dir=None,74 file_path_ref="", environment=None):75 """Create file access"""76 # Check active status77 active = jube2.util.util.eval_bool(jube2.util.util.substitution(78 self._active, parameter_dict))79 if not active:80 return81 pathname = jube2.util.util.substitution(self._path, parameter_dict)82 pathname = os.path.expanduser(pathname)83 source_dir = jube2.util.util.substitution(self._source_dir,84 parameter_dict)85 source_dir = os.path.expanduser(source_dir)86 target_dir = jube2.util.util.substitution(self._target_dir,87 parameter_dict)88 target_dir = os.path.expanduser(target_dir)89 if environment is not None:90 pathname = jube2.util.util.substitution(pathname, environment)91 source_dir = jube2.util.util.substitution(source_dir, environment)92 target_dir = jube2.util.util.substitution(target_dir, environment)93 else:94 pathname = os.path.expandvars(pathname)95 source_dir = os.path.expandvars(source_dir)96 target_dir = os.path.expandvars(target_dir)97 # Add source prefix directory if needed98 pathname = os.path.join(source_dir, pathname)99 if self._is_internal_ref:100 pathname = os.path.join(work_dir, pathname)101 else:102 pathname = os.path.join(self._file_path_ref, pathname)103 pathname = os.path.join(file_path_ref, pathname)104 pathname = os.path.normpath(pathname)105 if self._name is None:106 name = os.path.basename(pathname)107 else:108 name = jube2.util.util.substitution(self._name, parameter_dict)109 name = os.path.expanduser(name)110 if environment is not None:111 name = jube2.util.util.substitution(name, environment)112 else:113 name = os.path.expandvars(name)114 if alt_work_dir is not None:115 work_dir = alt_work_dir116 # Shell expansion117 pathes = glob.glob(pathname)118 if (len(pathes) == 0) and (not jube2.conf.DEBUG_MODE):119 raise RuntimeError("no files found using \"{0}\""120 .format(pathname))121 for path in pathes:122 # When using shell extensions, alternative filenames are not123 # allowed for multiple matches.124 if (len(pathes) > 1) or ((pathname != path) and125 (name == os.path.basename(pathname))):126 name = os.path.basename(path)127 # Add target prefix directory if needed128 name = os.path.join(target_dir, name)129 new_file_path = os.path.join(work_dir, name)130 # Create target_dir if needed131 if (len(os.path.dirname(new_file_path)) > 0 and132 not os.path.exists(os.path.dirname(new_file_path)) and133 not jube2.conf.DEBUG_MODE):134 os.makedirs(os.path.dirname(new_file_path))135 self.create_action(path, name, new_file_path)136 def create_action(self, path, name, new_file_path):137 """File access type specific creation"""138 raise NotImplementedError()139 def etree_repr(self):140 """Return etree object representation"""141 raise NotImplementedError()142 @property143 def path(self):144 """Return filepath"""145 return self._path146 @property147 def file_path_ref(self):148 """Get file path reference"""149 return self._file_path_ref150 @file_path_ref.setter151 def file_path_ref(self, file_path_ref):152 """Set file path reference"""153 self._file_path_ref = file_path_ref154 @property155 def is_internal_ref(self):156 """Return path is internal ref"""157 return self._is_internal_ref158 def __repr__(self):159 return self._path160class Link(File):161 """A link to a given path. Which can be used inside steps."""162 def create_action(self, path, name, new_file_path):163 """Create link to file in work_dir"""164 # Manipulate target_path if a new relative name path was selected165 if os.path.isabs(path):166 target_path = path167 else:168 target_path = os.path.relpath(path, os.path.dirname(new_file_path))169 LOGGER.debug(" link \"{0}\" <- \"{1}\"".format(target_path, name))170 if not jube2.conf.DEBUG_MODE and not os.path.exists(new_file_path):171 os.symlink(target_path, new_file_path)172 def etree_repr(self):173 """Return etree object representation"""174 link_etree = ET.Element("link")175 link_etree.text = self._path176 if self._name is not None:177 link_etree.attrib["name"] = self._name178 if self._active != "true":179 link_etree.attrib["active"] = self._active180 if self._source_dir != "":181 link_etree.attrib["source_dir"] = self._source_dir182 if self._target_dir != "":183 link_etree.attrib["target_dir"] = self._target_dir184 if self._is_internal_ref:185 link_etree.attrib["rel_path_ref"] = "internal"186 if self._file_path_ref != "":187 link_etree.attrib["file_path_ref"] = self._file_path_ref188 return link_etree189class Copy(File):190 """A file or directory given by path. Which can be copied to the work_dir191 inside steps.192 """193 def create_action(self, path, name, new_file_path):194 """Copy file/directory to work_dir"""195 LOGGER.debug(" copy \"{0}\" -> \"{1}\"".format(path, name))196 if not jube2.conf.DEBUG_MODE and not os.path.exists(new_file_path):197 if os.path.isdir(path):198 shutil.copytree(path, new_file_path, symlinks=True)199 else:200 shutil.copy2(path, new_file_path)201 def etree_repr(self):202 """Return etree object representation"""203 copy_etree = ET.Element("copy")204 copy_etree.text = self._path205 if self._name is not None:206 copy_etree.attrib["name"] = self._name207 if self._active != "true":208 copy_etree.attrib["active"] = self._active209 if self._source_dir != "":210 copy_etree.attrib["source_dir"] = self._source_dir211 if self._target_dir != "":212 copy_etree.attrib["target_dir"] = self._target_dir213 if self._is_internal_ref:214 copy_etree.attrib["rel_path_ref"] = "internal"215 if self._file_path_ref != "":216 copy_etree.attrib["file_path_ref"] = self._file_path_ref217 return copy_etree218class Prepare(jube2.step.Operation):219 """Prepare the workpackage work directory"""220 def __init__(self, cmd, stdout_filename=None, stderr_filename=None,221 work_dir=None, active="true"):222 jube2.step.Operation.__init__(self,223 do=cmd,224 stdout_filename=stdout_filename,225 stderr_filename=stderr_filename,226 active=active,227 work_dir=work_dir)228 def execute(self, parameter_dict, work_dir, only_check_pending=False,229 environment=None):230 """Execute the prepare command"""231 jube2.step.Operation.execute(232 self, parameter_dict=parameter_dict, work_dir=work_dir,233 only_check_pending=only_check_pending, environment=environment)234 def etree_repr(self):235 """Return etree object representation"""236 do_etree = ET.Element("prepare")237 do_etree.text = self._do238 if self._stdout_filename is not None:239 do_etree.attrib["stdout"] = self._stdout_filename240 if self._stderr_filename is not None:241 do_etree.attrib["stderr"] = self._stderr_filename242 if self._active != "true":243 do_etree.attrib["active"] = self._active244 if self._work_dir is not None:245 do_etree.attrib["work_dir"] = self._work_dir...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!