def safe_filepath(file_path_name, dir_sep=None): ''' Input the full path and filename, splits on directory separator and calls safe_filename_leaf for each part of the path. dir_sep allows coder to force a directory separate to a particular character .. versionadded:: 2017.7.2 :codeauthor: Damon Atkins <https://github.com/damon-atkins> ''' if not dir_sep: dir_sep = os.sep # Normally if file_path_name or dir_sep is Unicode then the output will be Unicode # This code ensure the output type is the same as file_path_name if not isinstance(file_path_name, six.text_type) and isinstance( dir_sep, six.text_type): dir_sep = dir_sep.encode( 'ascii') # This should not be executed under PY3 # splitdrive only set drive on windows platform (drive, path) = os.path.splitdrive(file_path_name) path = dir_sep.join([ safe_filename_leaf(file_section) for file_section in path.rsplit(dir_sep) ]) if drive: path = dir_sep.join([drive, path]) return path
def test_module_name(self): ''' Make sure all test modules conform to the test_*.py naming scheme ''' excluded_dirs, included_dirs = tuple(EXCLUDED_DIRS), tuple( INCLUDED_DIRS) tests_dir = os.path.join(RUNTIME_VARS.CODE_DIR, 'tests') bad_names = [] for root, _, files in salt.utils.path.os_walk(tests_dir): reldir = os.path.relpath(root, RUNTIME_VARS.CODE_DIR) if (reldir.startswith(excluded_dirs) and not self._match_dirs(reldir, included_dirs)) \ or reldir.endswith('__pycache__'): continue for fname in files: if fname in ('__init__.py', 'conftest.py') or not fname.endswith('.py'): continue relpath = os.path.join(reldir, fname) if relpath in EXCLUDED_FILES: continue if not fname.startswith('test_'): bad_names.append(relpath) error_msg = '\n\nPlease rename the following files:\n' for path in bad_names: directory, filename = path.rsplit(os.sep, 1) filename, _ = os.path.splitext(filename) error_msg += ' {} -> {}/test_{}.py\n'.format( path, directory, filename.split('_test')[0]) error_msg += '\nIf you believe one of the entries above should be ignored, please add it to either\n' error_msg += '\'EXCLUDED_DIRS\' or \'EXCLUDED_FILES\' in \'tests/unit/test_module_names.py\'.\n' error_msg += 'If it is a tests module, then please rename as suggested.' self.assertEqual([], bad_names, error_msg)
def get_dir(self, path, dest=u'', saltenv=u'base', gzip=None, cachedir=None): ''' Get a directory recursively from the salt-master ''' ret = [] # Strip trailing slash path = self._check_proto(path).rstrip(u'/') # Break up the path into a list containing the bottom-level directory # (the one being recursively copied) and the directories preceding it separated = path.rsplit(u'/', 1) if len(separated) != 2: # No slashes in path. (This means all files in saltenv will be # copied) prefix = u'' else: prefix = separated[0] # Copy files from master for fn_ in self.file_list(saltenv, prefix=path): # Prevent files in "salt://foobar/" (or salt://foo.sh) from # matching a path of "salt://foo" try: if fn_[len(path)] != u'/': continue except IndexError: continue # Remove the leading directories from path to derive # the relative path on the minion. minion_relpath = fn_[len(prefix):].lstrip(u'/') ret.append( self.get_file( salt.utils.url.create(fn_), u'{0}/{1}'.format(dest, minion_relpath), True, saltenv, gzip ) ) # Replicate empty dirs from master try: for fn_ in self.file_list_emptydirs(saltenv, prefix=path): # Prevent an empty dir "salt://foobar/" from matching a path of # "salt://foo" try: if fn_[len(path)] != u'/': continue except IndexError: continue # Remove the leading directories from path to derive # the relative path on the minion. minion_relpath = fn_[len(prefix):].lstrip(u'/') minion_mkdir = u'{0}/{1}'.format(dest, minion_relpath) if not os.path.isdir(minion_mkdir): os.makedirs(minion_mkdir) ret.append(minion_mkdir) except TypeError: pass ret.sort() return ret