Skip to content

Commit

Permalink
Update all references for moved functions to use "files" util
Browse files Browse the repository at this point in the history
- fopen
- flopen
- fpopen
- safe_rm
- is_empty
- is_fcntl_available
  • Loading branch information
rallytime committed Jul 18, 2017
1 parent fe972f5 commit ccf790a
Show file tree
Hide file tree
Showing 363 changed files with 1,777 additions and 1,571 deletions.
5 changes: 3 additions & 2 deletions doc/topics/development/tests/integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ on a minion event bus.
.. code-block:: python
import tests.integration as integration
import salt.utils.event
class TestEvent(integration.SaltEventAssertsMixin):
'''
Expand Down Expand Up @@ -443,7 +444,7 @@ to test states:
from tests.support.mixins import SaltReturnAssertsMixin
# Import salt libs
import salt.utils
import salt.utils.files
HFILE = os.path.join(TMP, 'hosts')
Expand All @@ -470,7 +471,7 @@ to test states:
ip = '10.10.10.10'
ret = self.run_state('host.present', name=name, ip=ip)
self.assertSaltTrueReturn(ret)
with salt.utils.fopen(HFILE) as fp_:
with salt.utils.files.fopen(HFILE) as fp_:
output = fp_.read()
self.assertIn('{0}\t\t{1}'.format(ip, name), output)
Expand Down
2 changes: 1 addition & 1 deletion doc/topics/spm/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ This function will not generally be more complex than:
.. code-block:: python
def hash_file(path, hashobj, conn=None):
with salt.utils.fopen(path, 'r') as f:
with salt.utils.files.fopen(path, 'r') as f:
hashobj.update(f.read())
return hashobj.hexdigest()
Expand Down
6 changes: 3 additions & 3 deletions salt/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def mk_token(self, load):

try:
with salt.utils.files.set_umask(0o177):
with salt.utils.fopen(t_path, 'w+b') as fp_:
with salt.utils.files.fopen(t_path, 'w+b') as fp_:
fp_.write(self.serial.dumps(tdata))
except (IOError, OSError):
log.warning('Authentication failure: can not write token file "{0}".'.format(t_path))
Expand All @@ -245,7 +245,7 @@ def get_tok(self, tok):
if not os.path.isfile(t_path):
return {}
try:
with salt.utils.fopen(t_path, 'rb') as fp_:
with salt.utils.files.fopen(t_path, 'rb') as fp_:
tdata = self.serial.loads(fp_.read())
except (IOError, OSError):
log.warning('Authentication failure: can not read token file "{0}".'.format(t_path))
Expand Down Expand Up @@ -670,7 +670,7 @@ def token_cli(self, eauth, load):
return tdata
try:
with salt.utils.files.set_umask(0o177):
with salt.utils.fopen(self.opts['token_file'], 'w+') as fp_:
with salt.utils.files.fopen(self.opts['token_file'], 'w+') as fp_:
fp_.write(tdata['token'])
except (IOError, OSError):
pass
Expand Down
3 changes: 2 additions & 1 deletion salt/auth/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@

# Import salt utils
import salt.utils
import salt.utils.files

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -158,7 +159,7 @@ def _text(username, password, **kwargs):
username_field = kwargs['username_field']-1
password_field = kwargs['password_field']-1

with salt.utils.fopen(filename, 'r') as pwfile:
with salt.utils.files.fopen(filename, 'r') as pwfile:
for line in pwfile.readlines():
fields = line.strip().split(field_separator)

Expand Down
4 changes: 2 additions & 2 deletions salt/auth/pki.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# pylint: enable=import-error

# Import salt libs
import salt.utils
import salt.utils.files

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -74,7 +74,7 @@ def auth(username, password, **kwargs):
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, pem)

cacert_file = __salt__['config.get']('external_auth:pki:ca_file')
with salt.utils.fopen(cacert_file) as f:
with salt.utils.files.fopen(cacert_file) as f:
cacert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, f.read())

log.debug('Attempting to authenticate via pki.')
Expand Down
4 changes: 2 additions & 2 deletions salt/beacons/btmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import struct

# Import Salt Libs
import salt.utils
import salt.utils.files

__virtualname__ = 'btmp'
BTMP = '/var/log/btmp'
Expand Down Expand Up @@ -71,7 +71,7 @@ def beacon(config):
btmp: {}
'''
ret = []
with salt.utils.fopen(BTMP, 'rb') as fp_:
with salt.utils.files.fopen(BTMP, 'rb') as fp_:
loc = __context__.get(LOC_KEY, 0)
if loc == 0:
fp_.seek(0, 2)
Expand Down
3 changes: 2 additions & 1 deletion salt/beacons/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# Import salt libs
import salt.utils
import salt.utils.files


try:
Expand Down Expand Up @@ -79,7 +80,7 @@ def beacon(config):
ret.append(event)
return ret

with salt.utils.fopen(config['file'], 'r') as fp_:
with salt.utils.files.fopen(config['file'], 'r') as fp_:
loc = __context__.get(LOC_KEY, 0)
if loc == 0:
fp_.seek(0, 2)
Expand Down
4 changes: 2 additions & 2 deletions salt/beacons/wtmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import struct

# Import salt libs
import salt.utils
import salt.utils.files

__virtualname__ = 'wtmp'
WTMP = '/var/log/wtmp'
Expand Down Expand Up @@ -73,7 +73,7 @@ def beacon(config):
wtmp: {}
'''
ret = []
with salt.utils.fopen(WTMP, 'rb') as fp_:
with salt.utils.files.fopen(WTMP, 'rb') as fp_:
loc = __context__.get(LOC_KEY, 0)
if loc == 0:
fp_.seek(0, 2)
Expand Down
5 changes: 3 additions & 2 deletions salt/cache/localfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from salt.exceptions import SaltCacheError
import salt.utils
import salt.utils.atomicfile
import salt.utils.files

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -58,7 +59,7 @@ def store(bank, key, data, cachedir):
tmpfh, tmpfname = tempfile.mkstemp(dir=base)
os.close(tmpfh)
try:
with salt.utils.fopen(tmpfname, 'w+b') as fh_:
with salt.utils.files.fopen(tmpfname, 'w+b') as fh_:
fh_.write(__context__['serial'].dumps(data))
# On Windows, os.rename will fail if the destination file exists.
salt.utils.atomicfile.atomic_rename(tmpfname, outfile)
Expand All @@ -85,7 +86,7 @@ def fetch(bank, key, cachedir):
log.debug('Cache file "%s" does not exist', key_file)
return {}
try:
with salt.utils.fopen(key_file, 'rb') as fh_:
with salt.utils.files.fopen(key_file, 'rb') as fh_:
if inkey:
return __context__['serial'].load(fh_)[key]
else:
Expand Down
3 changes: 2 additions & 1 deletion salt/cli/caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import salt.payload
import salt.transport
import salt.utils.args
import salt.utils.files
import salt.utils.jid
import salt.utils.minion
import salt.defaults.exitcodes
Expand Down Expand Up @@ -189,7 +190,7 @@ def call(self):
no_parse=self.opts.get('no_parse', [])),
data=sdata)
try:
with salt.utils.fopen(proc_fn, 'w+b') as fp_:
with salt.utils.files.fopen(proc_fn, 'w+b') as fp_:
fp_.write(self.serial.dumps(sdata))
except NameError:
# Don't require msgpack with local
Expand Down
3 changes: 2 additions & 1 deletion salt/cli/salt.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ def run(self):
'show_jid': self.options.show_jid}

if 'token' in self.config:
import salt.utils.files
try:
with salt.utils.fopen(os.path.join(self.config['cachedir'], '.root_key'), 'r') as fp_:
with salt.utils.files.fopen(os.path.join(self.config['cachedir'], '.root_key'), 'r') as fp_:
kwargs['key'] = fp_.readline()
except IOError:
kwargs['token'] = self.config['token']
Expand Down
3 changes: 2 additions & 1 deletion salt/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import salt.utils
import salt.utils.args
import salt.utils.event
import salt.utils.files
import salt.utils.minions
import salt.utils.verify
import salt.utils.jid
Expand Down Expand Up @@ -193,7 +194,7 @@ def __read_master_key(self):
self.skip_perm_errors)

try:
with salt.utils.fopen(keyfile, 'r') as key:
with salt.utils.files.fopen(keyfile, 'r') as key:
return key.read()
except (OSError, IOError):
# Fall back to eauth
Expand Down
15 changes: 8 additions & 7 deletions salt/client/ssh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
import salt.state
import salt.utils
import salt.utils.args
import salt.utils.event
import salt.utils.atomicfile
import salt.utils.event
import salt.utils.files
import salt.utils.network
import salt.utils.thin
import salt.utils.url
import salt.utils.verify
import salt.utils.network
from salt.utils import is_windows
from salt.utils.process import MultiprocessingProcess

Expand Down Expand Up @@ -195,7 +196,7 @@
if not os.path.exists(shim_file):
# On esky builds we only have the .pyc file
shim_file += "c"
with salt.utils.fopen(shim_file) as ssh_py_shim:
with salt.utils.files.fopen(shim_file) as ssh_py_shim:
SSH_PY_SHIM = ssh_py_shim.read()

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -337,7 +338,7 @@ def get_pubkey(self):
)
)
pub = '{0}.pub'.format(priv)
with salt.utils.fopen(pub, 'r') as fp_:
with salt.utils.files.fopen(pub, 'r') as fp_:
return '{0} rsa root@master'.format(fp_.read().split()[1])

def key_deploy(self, host, ret):
Expand Down Expand Up @@ -941,12 +942,12 @@ def run_wfunc(self):
'grains': opts_pkg['grains'],
'pillar': pillar_data}
if data_cache:
with salt.utils.fopen(datap, 'w+b') as fp_:
with salt.utils.files.fopen(datap, 'w+b') as fp_:
fp_.write(
self.serial.dumps(data)
)
if not data and data_cache:
with salt.utils.fopen(datap, 'rb') as fp_:
with salt.utils.files.fopen(datap, 'rb') as fp_:
data = self.serial.load(fp_)
opts = data.get('opts', {})
opts['grains'] = data.get('grains')
Expand Down Expand Up @@ -1412,7 +1413,7 @@ def mod_data(fsclient):
return mods
tfp = tarfile.open(ext_tar_path, 'w:gz')
verfile = os.path.join(fsclient.opts['cachedir'], 'ext_mods.ver')
with salt.utils.fopen(verfile, 'w+') as fp_:
with salt.utils.files.fopen(verfile, 'w+') as fp_:
fp_.write(ver)
tfp.add(verfile, 'ext_version')
for ref in ret:
Expand Down
8 changes: 4 additions & 4 deletions salt/client/ssh/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# Import salt libs
import salt.client.ssh.shell
import salt.client.ssh
import salt.utils
import salt.utils.files
import salt.utils.thin
import salt.utils.url
import salt.utils.verify
import salt.roster
import salt.state
import salt.loader
Expand Down Expand Up @@ -177,13 +177,13 @@ def prep_trans_tar(opts, file_client, chunks, file_refs, pillar=None, id_=None,
[salt.utils.url.create('_output')],
[salt.utils.url.create('_utils')],
]
with salt.utils.fopen(lowfn, 'w+') as fp_:
with salt.utils.files.fopen(lowfn, 'w+') as fp_:
fp_.write(json.dumps(chunks))
if pillar:
with salt.utils.fopen(pillarfn, 'w+') as fp_:
with salt.utils.files.fopen(pillarfn, 'w+') as fp_:
fp_.write(json.dumps(pillar))
if roster_grains:
with salt.utils.fopen(roster_grainsfn, 'w+') as fp_:
with salt.utils.files.fopen(roster_grainsfn, 'w+') as fp_:
fp_.write(json.dumps(roster_grains))

if id_ is None:
Expand Down
10 changes: 6 additions & 4 deletions salt/client/ssh/wrapper/cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
'''
Wrap the cp module allowing for managed ssh file transfers
'''
# Import Python libs
from __future__ import absolute_import
import logging
import os

# Import salt libs
import salt.client.ssh
import salt.utils.files
import logging
import os
import salt.utils.templates
from salt.exceptions import CommandExecutionError

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -138,14 +140,14 @@ def _render(contents):
'''
# write out path to temp file
tmp_path_fn = salt.utils.files.mkstemp()
with salt.utils.fopen(tmp_path_fn, 'w+') as fp_:
with salt.utils.files.fopen(tmp_path_fn, 'w+') as fp_:
fp_.write(contents)
data = salt.utils.templates.TEMPLATE_REGISTRY[template](
tmp_path_fn,
to_str=True,
**kwargs
)
salt.utils.safe_rm(tmp_path_fn)
salt.utils.files.safe_rm(tmp_path_fn)
if not data['result']:
# Failed to render the template
raise CommandExecutionError(
Expand Down
4 changes: 2 additions & 2 deletions salt/cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ def run_profile(self, profile, names, vm_overrides=None):
vm_overrides = {}

try:
with salt.utils.fopen(self.opts['conf_file'], 'r') as mcc:
with salt.utils.files.fopen(self.opts['conf_file'], 'r') as mcc:
main_cloud_config = yaml.safe_load(mcc)
if not main_cloud_config:
main_cloud_config = {}
Expand Down Expand Up @@ -2106,7 +2106,7 @@ def run_map(self, dmap):
# Generate the fingerprint of the master pubkey in order to
# mitigate man-in-the-middle attacks
master_temp_pub = salt.utils.files.mkstemp()
with salt.utils.fopen(master_temp_pub, 'w') as mtp:
with salt.utils.files.fopen(master_temp_pub, 'w') as mtp:
mtp.write(pub)
master_finger = salt.utils.pem_finger(master_temp_pub, sum_type=self.opts['hash_type'])
os.unlink(master_temp_pub)
Expand Down
3 changes: 2 additions & 1 deletion salt/cloud/clouds/azurearm.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import salt.config as config
import salt.utils
import salt.utils.cloud
import salt.utils.files
import salt.ext.six as six
import salt.version
from salt.exceptions import (
Expand Down Expand Up @@ -1002,7 +1003,7 @@ def request_instance(call=None, kwargs=None): # pylint: disable=unused-argument
)
else:
if os.path.exists(userdata_file):
with salt.utils.fopen(userdata_file, 'r') as fh_:
with salt.utils.files.fopen(userdata_file, 'r') as fh_:
userdata = fh_.read()

userdata = salt.utils.cloud.userdata_template(__opts__, vm_, userdata)
Expand Down
Loading

0 comments on commit ccf790a

Please sign in to comment.