def printlog(message, logpath=False):
# I think the logging module is great, but this will be used for the time being
# Eventually, we will want to write out multiple output formats: xml,json, etc
if logpath:
# Next iteration of project will include a more secure logger,
# for now, we will just write results to a file directly.
# flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
# mode = stat.S_IRUSR | stat.S_IWUSR
# umask_original = os.umask(0)
# try:
# fdesc = os.open(logpath, flags, mode)
# # except(OSError):
# # print("[-] Log file exists. Remove it, or change log filename")
# # raise SystemExit
# finally:
# os.umask(umask_original)
# with os.fdopen(fdesc, 'w') as fout:
with open(logpath, 'a') as fout:
fout.write("{}\n".format(message))
print("{}".format(message))
python类S_IWUSR的实例源码
def _get_netrc(cls):
# Buffer the '.netrc' path. Use an empty file if it doesn't exist.
path = cls.get_netrc_path()
content = ''
if os.path.exists(path):
st = os.stat(path)
if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO):
print >> sys.stderr, (
'WARNING: netrc file %s cannot be used because its file '
'permissions are insecure. netrc file permissions should be '
'600.' % path)
with open(path) as fd:
content = fd.read()
# Load the '.netrc' file. We strip comments from it because processing them
# can trigger a bug in Windows. See crbug.com/664664.
content = '\n'.join(l for l in content.splitlines()
if l.strip() and not l.strip().startswith('#'))
with tempdir() as tdir:
netrc_path = os.path.join(tdir, 'netrc')
with open(netrc_path, 'w') as fd:
fd.write(content)
os.chmod(netrc_path, (stat.S_IRUSR | stat.S_IWUSR))
return cls._get_netrc_from_path(netrc_path)
def make_writeable(self, filename):
"""
Make sure that the file is writeable.
Useful if our source is read-only.
"""
if sys.platform.startswith('java'):
# On Jython there is no os.access()
return
if not os.access(filename, os.W_OK):
st = os.stat(filename)
new_permissions = stat.S_IMODE(st.st_mode) | stat.S_IWUSR
os.chmod(filename, new_permissions)
def updateFactorio():
file_name = "/tmp/latestFactorio.tar.gz"
print("Downloading %s" % file_name)
r = requests.get(DOWNLOADURL, stream=True)
total_length = int(r.headers.get('content-length'))
if not os.path.isfile(file_name) or total_length != os.path.getsize(file_name):
with open(file_name, 'wb') as f:
for chunk in progress.bar(r.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1):
if chunk:
f.write(chunk)
f.flush()
#os.chmod(file_name, stat.S_IWUSR | stat.S_IRUSR)
else:
print("File already exists and file sizes match. Skipping download.")
if os.access(FACTORIOPATH, os.W_OK):
if os.path.isfile(file_name):
tar = tarfile.open(file_name, "r:gz")
tar.extractall(path="/tmp")
tar.close()
copytree("/tmp/factorio", FACTORIOPATH)
print("Success.")
else:
print("Help! Can't find %s, but I should have!" % (file_name))
sys.exit(1)
else:
print("Can't write to %s" % (FACTORIOPATH))
sys.exit(1)
def write_pid_file(pid_file, pid):
import fcntl
import stat
try:
fd = os.open(pid_file, os.O_RDWR | os.O_CREAT,
stat.S_IRUSR | stat.S_IWUSR)
except OSError as e:
shell.print_exception(e)
return -1
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
assert flags != -1
flags |= fcntl.FD_CLOEXEC
r = fcntl.fcntl(fd, fcntl.F_SETFD, flags)
assert r != -1
# There is no platform independent way to implement fcntl(fd, F_SETLK, &fl)
# via fcntl.fcntl. So use lockf instead
try:
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB, 0, 0, os.SEEK_SET)
except IOError:
r = os.read(fd, 32)
if r:
logging.error('already started at pid %s' % common.to_str(r))
else:
logging.error('already started')
os.close(fd)
return -1
os.ftruncate(fd, 0)
os.write(fd, common.to_bytes(str(pid)))
return 0
def begin_handover(self, fdtx_dir='/tmp/ave'):
# call this function on the broker that is going to be replaced.
# steps:
# stop listening for new connections from clients, stop the notifier,
# disconnect all shares, disable further allocation (a session will
# receive a Restarting exception if it manages to allocate at precisely
# this moment and is expected to try again once or twice), create a
# UNIX domain socket to transfer file descriptors (in a separate step),
# serialize the state of the local allocator and all live sessions
# (PIDs and RPC keys). finally return serialized data and the path to
# UNIX domain socket.
# first of all check that fdtx_dir is writable. otherwise the socket
# will not be created in it and everything fails
if os.path.exists(fdtx_dir) and os.path.isdir(fdtx_dir):
if not os.access(fdtx_dir, os.R_OK | os.X_OK | os.W_OK):
raise Exception('directory not writable: %s' % fdtx_dir)
self.stop_listening()
self.stop_sharing()
self.drop_all_shares()
self.stop_listers()
self.allocating = False
self.fdtx = FdTx(None)
uds_path = self.fdtx.listen(fdtx_dir, 'handover-%s' % rand_authkey())
# make sure the caller will be able to interact with the new socket by
# making it world readable and world writable
mode = (stat.S_IRUSR # owner has read permission
| stat.S_IWUSR # owner has write permission
| stat.S_IRGRP # group has read permission
| stat.S_IWGRP # group has write permission
| stat.S_IROTH # others have read permission
| stat.S_IWOTH) # others have write permission
os.chmod(uds_path, mode)
return self.serialize(), self.config, uds_path
def begin_handover(self, fdtx_dir='/tmp/ave'):
# call this function on the broker that is going to be replaced.
# steps:
# stop listening for new connections from clients, stop the notifier,
# disconnect all shares, disable further allocation (a session will
# receive a Restarting exception if it manages to allocate at precisely
# this moment and is expected to try again once or twice), create a
# UNIX domain socket to transfer file descriptors (in a separate step),
# serialize the state of the local allocator and all live sessions
# (PIDs and RPC keys). finally return serialized data and the path to
# UNIX domain socket.
# first of all check that fdtx_dir is writable. otherwise the socket
# will not be created in it and everything fails
if os.path.exists(fdtx_dir) and os.path.isdir(fdtx_dir):
if not os.access(fdtx_dir, os.R_OK | os.X_OK | os.W_OK):
raise Exception('directory not writable: %s' % fdtx_dir)
self.stop_listening()
self.stop_sharing()
self.drop_all_shares()
self.stop_listers()
self.allocating = False
self.fdtx = FdTx(None)
uds_path = self.fdtx.listen(fdtx_dir, 'handover-%s' % rand_authkey())
# make sure the caller will be able to interact with the new socket by
# making it world readable and world writable
mode = (stat.S_IRUSR # owner has read permission
| stat.S_IWUSR # owner has write permission
| stat.S_IRGRP # group has read permission
| stat.S_IWGRP # group has write permission
| stat.S_IROTH # others have read permission
| stat.S_IWOTH) # others have write permission
os.chmod(uds_path, mode)
return self.serialize(), self.config, uds_path
def make_writeable(self, filename):
"""
Make sure that the file is writeable.
Useful if our source is read-only.
"""
if sys.platform.startswith('java'):
# On Jython there is no os.access()
return
if not os.access(filename, os.W_OK):
st = os.stat(filename)
new_permissions = stat.S_IMODE(st.st_mode) | stat.S_IWUSR
os.chmod(filename, new_permissions)
def delete_extension(extension):
def onerror(func, path, exc_info):
"""`shutil.rmtree` error handler that helps deleting read-only files on Windows."""
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
return shutil.rmtree('dwarf/' + extension, onerror=onerror)
def make_writeable(self, filename):
"""
Make sure that the file is writeable.
Useful if our source is read-only.
"""
if sys.platform.startswith('java'):
# On Jython there is no os.access()
return
if not os.access(filename, os.W_OK):
st = os.stat(filename)
new_permissions = stat.S_IMODE(st.st_mode) | stat.S_IWUSR
os.chmod(filename, new_permissions)
def write_pid_file(pid_file, pid):
import fcntl
import stat
try:
fd = os.open(pid_file, os.O_RDWR | os.O_CREAT,
stat.S_IRUSR | stat.S_IWUSR)
except OSError as e:
shell.print_exception(e)
return -1
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
assert flags != -1
flags |= fcntl.FD_CLOEXEC
r = fcntl.fcntl(fd, fcntl.F_SETFD, flags)
assert r != -1
# There is no platform independent way to implement fcntl(fd, F_SETLK, &fl)
# via fcntl.fcntl. So use lockf instead
try:
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB, 0, 0, os.SEEK_SET)
except IOError:
r = os.read(fd, 32)
if r:
logging.error('already started at pid %s' % common.to_str(r))
else:
logging.error('already started')
os.close(fd)
return -1
os.ftruncate(fd, 0)
os.write(fd, common.to_bytes(str(pid)))
return 0
def rmtree(path):
"""shutil.rmtree() with error handler.
Handle 'access denied' from trying to delete read-only files.
"""
def onerror(func, path, exc_info):
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
return shutil.rmtree(path, onerror=onerror)
def write_pid_file(pid_file, pid):
import fcntl
import stat
try:
fd = os.open(pid_file, os.O_RDWR | os.O_CREAT,
stat.S_IRUSR | stat.S_IWUSR)
except OSError as e:
shell.print_exception(e)
return -1
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
assert flags != -1
flags |= fcntl.FD_CLOEXEC
r = fcntl.fcntl(fd, fcntl.F_SETFD, flags)
assert r != -1
# There is no platform independent way to implement fcntl(fd, F_SETLK, &fl)
# via fcntl.fcntl. So use lockf instead
try:
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB, 0, 0, os.SEEK_SET)
except IOError:
r = os.read(fd, 32)
if r:
logging.error('already started at pid %s' % common.to_str(r))
else:
logging.error('already started')
os.close(fd)
return -1
os.ftruncate(fd, 0)
os.write(fd, common.to_bytes(str(pid)))
return 0
def write_pid_file(pid_file, pid):
try:
fd = os.open(pid_file, os.O_RDWR | os.O_CREAT,
stat.S_IRUSR | stat.S_IWUSR)
except OSError as e:
LOG.exception(e)
return -1
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
assert flags != -1
flags |= fcntl.FD_CLOEXEC
r = fcntl.fcntl(fd, fcntl.F_SETFD, flags)
assert r != -1
# There is no platform independent way to implement fcntl(fd, F_SETLK, &fl)
# via fcntl.fcntl. So use lockf instead
try:
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB, 0, 0, os.SEEK_SET)
except IOError:
r = os.read(fd, 32)
if r:
logging.error('already started at pid %s' % utils.to_str(r))
else:
logging.error('already started')
os.close(fd)
return -1
os.ftruncate(fd, 0)
os.write(fd, utils.to_bytes(str(pid)))
return 0
def make_writeable(self, filename):
"""
Make sure that the file is writeable.
Useful if our source is read-only.
"""
if sys.platform.startswith('java'):
# On Jython there is no os.access()
return
if not os.access(filename, os.W_OK):
st = os.stat(filename)
new_permissions = stat.S_IMODE(st.st_mode) | stat.S_IWUSR
os.chmod(filename, new_permissions)
def _MakeWritable(dir_path):
for root, dirs, files in os.walk(dir_path):
for path in itertools.chain(dirs, files):
st = os.stat(os.path.join(root, path))
os.chmod(os.path.join(root, path), st.st_mode | stat.S_IWUSR)
# E.g. turn "base_1p" into "base"
def RmTree(dir):
"""Delete dir."""
def ChmodAndRetry(func, path, _):
# Subversion can leave read-only files around.
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
return func(path)
raise
shutil.rmtree(dir, onerror=ChmodAndRetry)
def copy_template_file(self, src, dest, ctx={}):
template = self.env.get_template(src)
template.stream(ctx).dump(dest, encoding='utf-8')
# Make new file writable.
if os.access(dest, os.W_OK):
st = os.stat(dest)
new_permissions = stat.S_IMODE(st.st_mode) | stat.S_IWUSR
os.chmod(dest, new_permissions)
def make_writeable(self, filename):
"""
Make sure that the file is writeable.
Useful if our source is read-only.
"""
if sys.platform.startswith('java'):
# On Jython there is no os.access()
return
if not os.access(filename, os.W_OK):
st = os.stat(filename)
new_permissions = stat.S_IMODE(st.st_mode) | stat.S_IWUSR
os.chmod(filename, new_permissions)
def rmtree(path):
"""shutil.rmtree() with error handler.
Handle 'access denied' from trying to delete read-only files.
"""
def onerror(func, path, exc_info):
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
return shutil.rmtree(path, onerror=onerror)