mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
refactor
This commit is contained in:
parent
8924b5a911
commit
cb3dfee969
1 changed files with 23 additions and 31 deletions
|
@ -234,6 +234,7 @@ A more complex ``recurse`` example:
|
|||
|
||||
# Import python libs
|
||||
import difflib
|
||||
import itertools
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
|
@ -385,34 +386,23 @@ def _clean_dir(root, keep, exclude_pat):
|
|||
if fn_ in ['/', ''.join([os.path.splitdrive(fn_)[0], '\\'])]:
|
||||
break
|
||||
|
||||
for roots, dirs, files in os.walk(root):
|
||||
for name in dirs:
|
||||
nfn = os.path.join(roots, name)
|
||||
if os.path.islink(nfn):
|
||||
# the "directory" is in fact a symlink and cannot be
|
||||
# removed by shutil.rmtree
|
||||
files.append(nfn)
|
||||
continue
|
||||
if nfn not in real_keep:
|
||||
# -- check if this is a part of exclude_pat(only). No need to
|
||||
# check include_pat
|
||||
if not salt.utils.check_include_exclude(
|
||||
os.path.relpath(nfn, root), None, exclude_pat):
|
||||
continue
|
||||
removed.add(nfn)
|
||||
if not __opts__['test']:
|
||||
shutil.rmtree(nfn)
|
||||
for name in files:
|
||||
nfn = os.path.join(roots, name)
|
||||
if nfn not in real_keep:
|
||||
# -- check if this is a part of exclude_pat(only). No need to
|
||||
# check include_pat
|
||||
if not salt.utils.check_include_exclude(
|
||||
os.path.relpath(nfn, root), None, exclude_pat):
|
||||
continue
|
||||
removed.add(nfn)
|
||||
if not __opts__['test']:
|
||||
def _delete_not_kept(nfn):
|
||||
if nfn not in real_keep:
|
||||
# -- check if this is a part of exclude_pat(only). No need to
|
||||
# check include_pat
|
||||
if not salt.utils.check_include_exclude(
|
||||
os.path.relpath(nfn, root), None, exclude_pat):
|
||||
return
|
||||
removed.add(nfn)
|
||||
if not __opts__['test']:
|
||||
try:
|
||||
os.remove(nfn)
|
||||
except OSError:
|
||||
shutil.rmtree(nfn)
|
||||
|
||||
for roots, dirs, files in os.walk(root):
|
||||
for name in itertools.chain(dirs, files):
|
||||
_delete_not_kept(os.path.join(roots, name))
|
||||
return list(removed)
|
||||
|
||||
|
||||
|
@ -498,12 +488,14 @@ def _check_directory(name,
|
|||
|
||||
def _check_changes(fname):
|
||||
path = os.path.join(root, fname)
|
||||
if path not in keep:
|
||||
if path in keep:
|
||||
return {}
|
||||
else:
|
||||
if not salt.utils.check_include_exclude(
|
||||
path[len(name) + 1:], None, exclude_pat):
|
||||
os.path.relpath(path, name), None, exclude_pat):
|
||||
return {}
|
||||
return {path: {'removed': 'Removed due to clean'}}
|
||||
return {}
|
||||
else:
|
||||
return {path: {'removed': 'Removed due to clean'}}
|
||||
|
||||
for root, dirs, files in os.walk(name):
|
||||
for fname in files:
|
||||
|
|
Loading…
Add table
Reference in a new issue