Python os.O_NOINHERIT() Examples

The following are code examples for showing how to use os.O_NOINHERIT(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 7 votes vote down vote up
def h_file_win32(fname):
	try:
		fd = os.open(fname, os.O_BINARY | os.O_RDONLY | os.O_NOINHERIT)
	except OSError:
		raise IOError('Cannot read from %r' % fname)
	f = os.fdopen(fd, 'rb')
	m = md5()
	try:
		while fname:
			fname = f.read(200000)
			m.update(fname)
	finally:
		f.close()
	return m.digest()

# always save these 
Example 2
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
def writef_win32(f, data, m='w', encoding='ISO8859-1'):
	if sys.hexversion > 0x3000000 and not 'b' in m:
		data = data.encode(encoding)
		m += 'b'
	flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | os.O_NOINHERIT
	if 'b' in m:
		flags |= os.O_BINARY
	if '+' in m:
		flags |= os.O_RDWR
	try:
		fd = os.open(f, flags)
	except OSError:
		raise IOError('Cannot write to %r' % f)
	f = os.fdopen(fd, m)
	try:
		f.write(data)
	finally:
		f.close() 
Example 3
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
def writef_win32(f, data, m='w', encoding='ISO8859-1'):
	if sys.hexversion > 0x3000000 and not 'b' in m:
		data = data.encode(encoding)
		m += 'b'
	flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | os.O_NOINHERIT
	if 'b' in m:
		flags |= os.O_BINARY
	if '+' in m:
		flags |= os.O_RDWR
	try:
		fd = os.open(f, flags)
	except OSError:
		raise IOError('Cannot write to %r' % f)
	f = os.fdopen(fd, m)
	try:
		f.write(data)
	finally:
		f.close() 
Example 4
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
def h_file_win32(fname):
	try:
		fd = os.open(fname, os.O_BINARY | os.O_RDONLY | os.O_NOINHERIT)
	except OSError:
		raise IOError('Cannot read from %r' % fname)
	f = os.fdopen(fd, 'rb')
	m = md5()
	try:
		while fname:
			fname = f.read(200000)
			m.update(fname)
	finally:
		f.close()
	return m.digest()

# always save these 
Example 5
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
def writef_win32(f, data, m='w', encoding='ISO8859-1'):
	if sys.hexversion > 0x3000000 and not 'b' in m:
		data = data.encode(encoding)
		m += 'b'
	flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | os.O_NOINHERIT
	if 'b' in m:
		flags |= os.O_BINARY
	if '+' in m:
		flags |= os.O_RDWR
	try:
		fd = os.open(f, flags)
	except OSError:
		raise IOError('Cannot write to %r' % f)
	f = os.fdopen(fd, m)
	try:
		f.write(data)
	finally:
		f.close() 
Example 6
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
def h_file_win32(fname):
	try:
		fd = os.open(fname, os.O_BINARY | os.O_RDONLY | os.O_NOINHERIT)
	except OSError:
		raise IOError('Cannot read from %r' % fname)
	f = os.fdopen(fd, 'rb')
	m = md5()
	try:
		while fname:
			fname = f.read(200000)
			m.update(fname)
	finally:
		f.close()
	return m.digest()

# always save these 
Example 7
Project: SoCFoundationFlow   Author: mattaw   File: build_logs.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def init(ctx):
	global LOGFILE
	filename = os.path.abspath(LOGFILE)
	try:
		os.makedirs(os.path.dirname(os.path.abspath(filename)))
	except OSError:
		pass

	if hasattr(os, 'O_NOINHERIT'):
		fd = os.open(LOGFILE, os.O_CREAT | os.O_TRUNC | os.O_WRONLY | os.O_NOINHERIT)
		fileobj = os.fdopen(fd, 'w')
	else:
		fileobj = open(LOGFILE, 'w')
	old_stderr = sys.stderr

	# sys.stdout has already been replaced, so __stdout__ will be faster
	#sys.stdout = log_to_file(sys.stdout, fileobj, filename)
	#sys.stderr = log_to_file(sys.stderr, fileobj, filename)
	def wrap(stream):
		if stream.isatty():
			return ansiterm.AnsiTerm(stream)
		return stream
	sys.stdout = log_to_file(wrap(sys.__stdout__), fileobj, filename)
	sys.stderr = log_to_file(wrap(sys.__stderr__), fileobj, filename)

	# now mess with the logging module...
	for x in Logs.log.handlers:
		try:
			stream = x.stream
		except AttributeError:
			pass
		else:
			if id(stream) == id(old_stderr):
				x.stream = sys.stderr 
Example 8
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def readf_win32(f, m='r', encoding='ISO8859-1'):
	flags = os.O_NOINHERIT | os.O_RDONLY
	if 'b' in m:
		flags |= os.O_BINARY
	if '+' in m:
		flags |= os.O_RDWR
	try:
		fd = os.open(f, flags)
	except OSError:
		raise IOError('Cannot read from %r' % f)

	if sys.hexversion > 0x3000000 and not 'b' in m:
		m += 'b'
		f = os.fdopen(fd, m)
		try:
			txt = f.read()
		finally:
			f.close()
		if encoding:
			txt = txt.decode(encoding)
		else:
			txt = txt.decode()
	else:
		f = os.fdopen(fd, m)
		try:
			txt = f.read()
		finally:
			f.close()
	return txt 
Example 9
Project: SoCFoundationFlow   Author: mattaw   File: build_logs.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def init(ctx):
	global LOGFILE
	filename = os.path.abspath(LOGFILE)
	try:
		os.makedirs(os.path.dirname(os.path.abspath(filename)))
	except OSError:
		pass

	if hasattr(os, 'O_NOINHERIT'):
		fd = os.open(LOGFILE, os.O_CREAT | os.O_TRUNC | os.O_WRONLY | os.O_NOINHERIT)
		fileobj = os.fdopen(fd, 'w')
	else:
		fileobj = open(LOGFILE, 'w')
	old_stderr = sys.stderr

	# sys.stdout has already been replaced, so __stdout__ will be faster
	#sys.stdout = log_to_file(sys.stdout, fileobj, filename)
	#sys.stderr = log_to_file(sys.stderr, fileobj, filename)
	def wrap(stream):
		if stream.isatty():
			return ansiterm.AnsiTerm(stream)
		return stream
	sys.stdout = log_to_file(wrap(sys.__stdout__), fileobj, filename)
	sys.stderr = log_to_file(wrap(sys.__stderr__), fileobj, filename)

	# now mess with the logging module...
	for x in Logs.log.handlers:
		try:
			stream = x.stream
		except AttributeError:
			pass
		else:
			if id(stream) == id(old_stderr):
				x.stream = sys.stderr 
Example 10
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def readf_win32(f, m='r', encoding='ISO8859-1'):
	flags = os.O_NOINHERIT | os.O_RDONLY
	if 'b' in m:
		flags |= os.O_BINARY
	if '+' in m:
		flags |= os.O_RDWR
	try:
		fd = os.open(f, flags)
	except OSError:
		raise IOError('Cannot read from %r' % f)

	if sys.hexversion > 0x3000000 and not 'b' in m:
		m += 'b'
		f = os.fdopen(fd, m)
		try:
			txt = f.read()
		finally:
			f.close()
		if encoding:
			txt = txt.decode(encoding)
		else:
			txt = txt.decode()
	else:
		f = os.fdopen(fd, m)
		try:
			txt = f.read()
		finally:
			f.close()
	return txt 
Example 11
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def readf_win32(f, m='r', encoding='ISO8859-1'):
	flags = os.O_NOINHERIT | os.O_RDONLY
	if 'b' in m:
		flags |= os.O_BINARY
	if '+' in m:
		flags |= os.O_RDWR
	try:
		fd = os.open(f, flags)
	except OSError:
		raise IOError('Cannot read from %r' % f)

	if sys.hexversion > 0x3000000 and not 'b' in m:
		m += 'b'
		f = os.fdopen(fd, m)
		try:
			txt = f.read()
		finally:
			f.close()
		if encoding:
			txt = txt.decode(encoding)
		else:
			txt = txt.decode()
	else:
		f = os.fdopen(fd, m)
		try:
			txt = f.read()
		finally:
			f.close()
	return txt 
Example 12
Project: SoCFoundationFlow   Author: mattaw   File: win32_opts.py    (Apache License 2.0) View Source Project 4 votes vote down vote up
def cached_hash_file(self):
		try:
			cache = self.ctx.cache_listdir_cache_hash_file
		except AttributeError:
			cache = self.ctx.cache_listdir_cache_hash_file = {}

		if id(self.parent) in cache:
			try:
				t = cache[id(self.parent)][self.name]
			except KeyError:
				raise IOError('Not a file')
		else:
			# an opportunity to list the files and the timestamps at once
			findData = ctypes.wintypes.WIN32_FIND_DATAW()
			find     = FindFirstFile(TP % self.parent.abspath(), ctypes.byref(findData))

			if find == INVALID_HANDLE_VALUE:
				cache[id(self.parent)] = {}
				raise IOError('Not a file')

			cache[id(self.parent)] = lst_files = {}
			try:
				while True:
					if findData.cFileName not in UPPER_FOLDERS:
						thatsadir = findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
						if not thatsadir:
							ts = findData.ftLastWriteTime
							d = (ts.dwLowDateTime << 32) | ts.dwHighDateTime
							lst_files[str(findData.cFileName)] = d
					if not FindNextFile(find, ctypes.byref(findData)):
						break
			except Exception:
				cache[id(self.parent)] = {}
				raise IOError('Not a file')
			finally:
				FindClose(find)
			t = lst_files[self.name]

		fname = self.abspath()
		if fname in Build.hashes_md5_tstamp:
			if Build.hashes_md5_tstamp[fname][0] == t:
				return Build.hashes_md5_tstamp[fname][1]

		try:
			fd = os.open(fname, os.O_BINARY | os.O_RDONLY | os.O_NOINHERIT)
		except OSError:
			raise IOError('Cannot read from %r' % fname)
		f = os.fdopen(fd, 'rb')
		m = Utils.md5()
		rb = 1
		try:
			while rb:
				rb = f.read(200000)
				m.update(rb)
		finally:
			f.close()

		# ensure that the cache is overwritten
		Build.hashes_md5_tstamp[fname] = (t, m.digest())
		return m.digest() 
Example 13
Project: SoCFoundationFlow   Author: mattaw   File: win32_opts.py    (Apache License 2.0) View Source Project 4 votes vote down vote up
def cached_hash_file(self):
		try:
			cache = self.ctx.cache_listdir_cache_hash_file
		except AttributeError:
			cache = self.ctx.cache_listdir_cache_hash_file = {}

		if id(self.parent) in cache:
			try:
				t = cache[id(self.parent)][self.name]
			except KeyError:
				raise IOError('Not a file')
		else:
			# an opportunity to list the files and the timestamps at once
			findData = ctypes.wintypes.WIN32_FIND_DATAW()
			find     = FindFirstFile(TP % self.parent.abspath(), ctypes.byref(findData))

			if find == INVALID_HANDLE_VALUE:
				cache[id(self.parent)] = {}
				raise IOError('Not a file')

			cache[id(self.parent)] = lst_files = {}
			try:
				while True:
					if findData.cFileName not in UPPER_FOLDERS:
						thatsadir = findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
						if not thatsadir:
							ts = findData.ftLastWriteTime
							d = (ts.dwLowDateTime << 32) | ts.dwHighDateTime
							lst_files[str(findData.cFileName)] = d
					if not FindNextFile(find, ctypes.byref(findData)):
						break
			except Exception as e:
				cache[id(self.parent)] = {}
				raise IOError('Not a file')
			finally:
				FindClose(find)
			t = lst_files[self.name]

		fname = self.abspath()
		if fname in Build.hashes_md5_tstamp:
			if Build.hashes_md5_tstamp[fname][0] == t:
				return Build.hashes_md5_tstamp[fname][1]

		try:
			fd = os.open(fname, os.O_BINARY | os.O_RDONLY | os.O_NOINHERIT)
		except OSError:
			raise IOError('Cannot read from %r' % fname)
		f = os.fdopen(fd, 'rb')
		m = Utils.md5()
		rb = 1
		try:
			while rb:
				rb = f.read(200000)
				m.update(rb)
		finally:
			f.close()

		# ensure that the cache is overwritten
		Build.hashes_md5_tstamp[fname] = (t, m.digest())
		return m.digest()