mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #43172 from rallytime/move-utils-funcs
Move new utils/__init__.py funcs to utils.files.py
This commit is contained in:
commit
d48938e6b4
2 changed files with 38 additions and 34 deletions
|
@ -16,7 +16,6 @@ import json
|
|||
import logging
|
||||
import numbers
|
||||
import os
|
||||
import os.path
|
||||
import posixpath
|
||||
import random
|
||||
import re
|
||||
|
@ -33,7 +32,6 @@ import warnings
|
|||
import string
|
||||
import subprocess
|
||||
import getpass
|
||||
import urllib
|
||||
|
||||
# Import 3rd-party libs
|
||||
from salt.ext import six
|
||||
|
@ -165,38 +163,6 @@ def is_empty(filename):
|
|||
return False
|
||||
|
||||
|
||||
def safe_filename_leaf(file_basename):
|
||||
'''
|
||||
input the basename of a file, without the directory tree, and returns a safe name to use
|
||||
i.e. only the required characters are converted by urllib.quote
|
||||
If the input is a PY2 String, output a PY2 String. If input is Unicode output Unicode.
|
||||
For consistency all platforms are treated the same. Hard coded to utf8 as its ascii compatible
|
||||
windows is \\ / : * ? " < > | posix is /
|
||||
'''
|
||||
def _replace(re_obj):
|
||||
return urllib.quote(re_obj.group(0), safe=u'')
|
||||
if not isinstance(file_basename, six.text_type):
|
||||
# the following string is not prefixed with u
|
||||
return re.sub('[\\\\:/*?"<>|]',
|
||||
_replace,
|
||||
six.text_type(file_basename, 'utf8').encode('ascii', 'backslashreplace'))
|
||||
# the following string is prefixed with u
|
||||
return re.sub(u'[\\\\:/*?"<>|]', _replace, file_basename, flags=re.UNICODE)
|
||||
|
||||
|
||||
def safe_filepath(file_path_name):
|
||||
'''
|
||||
input the full path and filename, splits on directory separator and calls safe_filename_leaf for
|
||||
each part of the path.
|
||||
'''
|
||||
(drive, path) = os.path.splitdrive(file_path_name)
|
||||
path = os.sep.join([safe_filename_leaf(file_section) for file_section in file_path_name.rsplit(os.sep)])
|
||||
if drive:
|
||||
return os.sep.join([drive, path])
|
||||
else:
|
||||
return path
|
||||
|
||||
|
||||
def is_hex(value):
|
||||
'''
|
||||
Returns True if value is a hexidecimal string, otherwise returns False
|
||||
|
|
|
@ -7,9 +7,11 @@ import contextlib
|
|||
import errno
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import time
|
||||
import urllib
|
||||
|
||||
# Import salt libs
|
||||
import salt.utils
|
||||
|
@ -258,3 +260,39 @@ def set_umask(mask):
|
|||
yield
|
||||
finally:
|
||||
os.umask(orig_mask)
|
||||
|
||||
|
||||
def safe_filename_leaf(file_basename):
|
||||
'''
|
||||
Input the basename of a file, without the directory tree, and returns a safe name to use
|
||||
i.e. only the required characters are converted by urllib.quote
|
||||
If the input is a PY2 String, output a PY2 String. If input is Unicode output Unicode.
|
||||
For consistency all platforms are treated the same. Hard coded to utf8 as its ascii compatible
|
||||
windows is \\ / : * ? " < > | posix is /
|
||||
|
||||
.. versionadded:: 2017.7.2
|
||||
'''
|
||||
def _replace(re_obj):
|
||||
return urllib.quote(re_obj.group(0), safe=u'')
|
||||
if not isinstance(file_basename, six.text_type):
|
||||
# the following string is not prefixed with u
|
||||
return re.sub('[\\\\:/*?"<>|]',
|
||||
_replace,
|
||||
six.text_type(file_basename, 'utf8').encode('ascii', 'backslashreplace'))
|
||||
# the following string is prefixed with u
|
||||
return re.sub(u'[\\\\:/*?"<>|]', _replace, file_basename, flags=re.UNICODE)
|
||||
|
||||
|
||||
def safe_filepath(file_path_name):
|
||||
'''
|
||||
Input the full path and filename, splits on directory separator and calls safe_filename_leaf for
|
||||
each part of the path.
|
||||
|
||||
.. versionadded:: 2017.7.2
|
||||
'''
|
||||
(drive, path) = os.path.splitdrive(file_path_name)
|
||||
path = os.sep.join([safe_filename_leaf(file_section) for file_section in file_path_name.rsplit(os.sep)])
|
||||
if drive:
|
||||
return os.sep.join([drive, path])
|
||||
else:
|
||||
return path
|
||||
|
|
Loading…
Add table
Reference in a new issue