Here are the examples of the python api sys.platform.startswith taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
123 Examples
5
Example 1
def os_name():
if sys.platform.startswith('darwin'):
return 'mac'
elif sys.platform.startswith('linux'):
return 'linux'
elif sys.platform.startswith('freebsd'):
return 'freebsd'
elif sys.platform.startswith('sunos'):
return 'solaris'
else:
return sys.platform
5
Example 2
def get_platform():
if sys.platform.startswith('win') or sys.platform.startswith('nt'):
return 'win'
elif sys.platform.startswith('linux'):
return 'linux'
elif sys.platform.startswith('darwin'):
return 'osx'
else:
raise EnvironmentError("Unsupported platform: {}".format(sys.platform))
5
Example 3
def detect_os():
""" Returns a string according to the OS host """
if sys.platform.startswith('linux'):
return OS_LINUX
elif sys.platform.startswith('freebsd'):
return OS_FREEBSD
elif sys.platform.startswith('dragonfly'):
return OS_DFLY
elif sys.platform.startswith('win32'):
return OS_WINDOWS
elif sys.platform.startswith('darwin'):
return OS_MAC
elif sys.platform.startswith('java'):
return OS_JAVA
else:
return OS_UNKNOWN
5
Example 4
Project: python-qinfer Source File: config.py
def preffilename():
if sys.platform.startswith('linux') or sys.platform.startswith('darwin') or sys.platform.startswith('cygwin'):
# Unix-y
configfile = os.path.expanduser('~/.config/qinfer.conf')
elif sys.platform.startswith('win'):
# Windows-y
configfile = os.path.join(os.path.expandvars('%APPDATA%'), 'qinfer.conf')
else:
return NotImplemented
ensuredir(configfile)
return configfile
5
Example 5
def load_library(libname):
# numpy 1.6 has bug in ctypeslib.load_library, see numpy/distutils/misc_util.py
if '1.6' in numpy.__version__:
if (sys.platform.startswith('linux') or
sys.platform.startswith('gnukfreebsd')):
so_ext = '.so'
elif sys.platform.startswith('darwin'):
so_ext = '.dylib'
elif sys.platform.startswith('win'):
so_ext = '.dll'
else:
raise OSError('Unknown platform')
libname_so = libname + so_ext
return ctypes.CDLL(os.path.join(os.path.dirname(__file__), libname_so))
else:
_loaderpath = os.path.dirname(__file__)
return numpy.ctypeslib.load_library(libname, _loaderpath)
5
Example 6
Project: reggata Source File: cx_setup.py
def get_short_sys_platform():
if sys.platform.startswith("linux"):
return "linux"
elif sys.platform.startswith("win"):
return "win"
elif sys.platform.startswith("darwin"):
return "mac"
else:
raise UnsupportedPlatform()
5
Example 7
def get_platform_name():
if sys.platform.startswith("win"):
return PLATFORM_WINDOWS
elif sys.platform.startswith('darwin'):
return PLATFORM_DARWIN
elif sys.platform.startswith('linux'):
return PLATFORM_LINUX
elif sys.platform.startswith('bsd'):
return PLATFORM_BSD
else:
return PLATFORM_UNKNOWN
5
Example 8
Project: artiq Source File: comm_tcp.py
def set_keepalive(sock, after_idle, interval, max_fails):
if sys.platform.startswith("linux"):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, after_idle)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, max_fails)
elif sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
# setting max_fails is not supported, typically ends up being 5 or 10
# depending on Windows version
sock.ioctl(socket.SIO_KEEPALIVE_VALS,
(1, after_idle*1000, interval*1000))
else:
logger.warning("TCP keepalive not supported on platform '%s', ignored",
sys.platform)
2
Example 9
Project: expyriment Source File: _get_system_info.py
def get_system_info(as_string=False):
"""Print system information to standard out and return as a dictionary.
Parameters
----------
as_string : boolean, optional
Print as string instead of dict (default = False)
"""
info = {}
# Get platform specific info for Linux
if sys.platform.startswith("linux"):
os_platform = "Linux"
os_name = platform.linux_distribution()[0]
details = []
if os.environ.has_key("XDG_CURRENT_DESKTOP"):
details.append(os.environ["XDG_CURRENT_DESKTOP"])
if os.environ.has_key("DESKTOP_SESSION"):
details.append(os.environ["DESKTOP_SESSION"])
if details != []:
os_details = ", ".join(details)
else:
os_details = ""
os_version = platform.linux_distribution()[1]
try:
hardware_cpu_details = ""
with open('/proc/cpuinfo') as f:
for line in f:
if line.startswith("model name"):
hardware_cpu_details = line.split(":")[1].strip()
except:
hardware_cpu_details = ""
try:
with open('/proc/meminfo') as f:
for line in f:
if line.startswith("MemTotal"):
mem_total = \
int(line.split(":")[1].strip()[:-2].strip()) / 1024
if line.startswith("MemFree"):
mem_free = \
int(line.split(":")[1].strip()[:-2].strip()) / 1024
if line.startswith("Buffers"):
mem_buffers = \
int(line.split(":")[1].strip()[:-2].strip()) / 1024
if line.startswith("Cached"):
mem_cached = \
int(line.split(":")[1].strip()[:-2].strip()) / 1024
hardware_memory_total = str(mem_total) + " MB"
hardware_memory_free = str(mem_free + mem_buffers + mem_cached) + \
" MB"
except:
hardware_memory_total = ""
hardware_memory_free = ""
try:
hardware_audio_card = ""
cards = []
p = subprocess.Popen(['lspci'], stdout=subprocess.PIPE)
for line in p.stdout:
if "Audio" in line:
cards.append(line.split(":")[-1].strip())
p.wait()
hardware_audio_card = ", ".join(cards)
except:
try:
hardware_audio_card = ""
cards = []
with open('/proc/asound/cards') as f:
for line in f:
if line.startswith(" 0"):
cards.append(line.split(":")[1].strip())
hardware_audio_card = ", ".join(cards)
except:
hardware_audio_card = ""
try:
current_folder = os.path.split(os.path.realpath(sys.argv[0]))[0]
s = os.statvfs(current_folder)
disk_total = int((s.f_frsize * s.f_blocks) / 1024 ** 2)
disk_free = int((s.f_frsize * s.f_bavail) / 1024 ** 2)
hardware_disk_space_total = str(disk_total) + " MB"
hardware_disk_space_free = str(disk_free) + " MB"
except:
hardware_disk_space_total = ""
hardware_disk_space_free = ""
try:
hardware_video_card = ""
cards = []
p = subprocess.Popen(['lspci'], stdout=subprocess.PIPE)
for line in p.stdout:
if "VGA" in line:
cards.append(line.split(":")[-1].strip())
p.wait()
hardware_video_card = ", ".join(cards)
except:
hardware_video_card = ""
# Get platform specific info for Windows
elif sys.platform.startswith("win"):
os_platform = "Windows"
os_name = "Windows"
os_details = platform.win32_ver()[2]
os_version = platform.win32_ver()[1]
try:
hardware_cpu_details = str(_get_registry_value(
"HKEY_LOCAL_MACHINE",
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
"ProcessorNameString"))
except:
hardware_cpu_details = ""
try:
import ctypes
class MEMORYSTATUSEX(ctypes.Structure):
_fields_ = [("dwLength", ctypes.c_uint),
("dwMemoryLoad", ctypes.c_uint),
("ullTotalPhys", ctypes.c_ulonglong),
("ullAvailPhys", ctypes.c_ulonglong),
("ullTotalPageFile", ctypes.c_ulonglong),
("ullAvailPageFile", ctypes.c_ulonglong),
("ullTotalVirtual", ctypes.c_ulonglong),
("ullAvailVirtual", ctypes.c_ulonglong),
("sullAvailExtendedVirtual", ctypes.c_ulonglong), ]
def __init__(self):
# Initialize this to the size of MEMORYSTATUSEX
self.dwLength = 2 * 4 + 7 * 8 # size = 2 ints, 7 longs
return super(MEMORYSTATUSEX, self).__init__()
stat = MEMORYSTATUSEX()
ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(stat))
hardware_memory_total = str(stat.ullTotalPhys / 1024 ** 2) + " MB"
hardware_memory_free = str(stat.ullAvailPhys / 1024 ** 2) + " MB"
except:
hardware_memory_total = ""
hardware_memory_free = ""
try:
current_folder = os.path.split(os.path.realpath(sys.argv[0]))[0]
_, disk_total, disk_free = ctypes.c_int64(), ctypes.c_int64(), \
ctypes.c_int64()
ret = ctypes.windll.kernel32.GetDiskFreeSpaceExW(
unicode(current_folder), ctypes.byref(_),
ctypes.byref(disk_total), ctypes.byref(disk_free))
hardware_disk_space_total = str(disk_total.value / 1024 ** 2) + " MB"
hardware_disk_space_free = str(disk_free.value / 1024 ** 2) + " MB"
except:
hardware_disk_space_total = ""
hardware_disk_space_free = ""
try:
tmp = str(_get_registry_value("HKEY_LOCAL_MACHINE",
"HARDWARE\\DEVICEMAP\\VIDEO",
"\Device\Video0"))
idx = tmp.find("System")
card = str(_get_registry_value("HKEY_LOCAL_MACHINE",
tmp[idx:].replace("\\", "\\\\"),
"Device Description"))
hardware_video_card = card
except:
hardware_video_card = ""
hardware_audio_card = "" # TODO
# Get platform specific info for OS X
elif sys.platform.startswith("darwin"):
os_platform = "Darwin"
os_name = "Mac OS X"
os_details = platform.mac_ver()[1][1]
os_version = platform.mac_ver()[0]
try:
proc = subprocess.Popen(['sysctl', '-a', 'hw.model'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
hardware_cpu_details = \
proc.stdout.readline().split(":")[1].strip()
except:
hardware_cpu_details = ""
try:
proc = subprocess.Popen(['sysctl', '-a', 'hw.memsize'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
mem_total = int(proc.stdout.readline().split(":")[1].strip()) / 1024 ** 2
hardware_memory_total = str(mem_total) + " MB"
except:
hardware_memory_total = ""
try:
import re
proc = subprocess.Popen(['vm_stat'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
output = proc.stdout.read().split("\n")
for item in output:
x = item.find("Pages active:")
y = item.find("page size of")
if x > -1:
non_decimal = re.compile(r'[^\d.]+')
active = int(non_decimal.sub('', item).strip("."))
if y > -1:
non_decimal = re.compile(r'[^\d.]+')
page = int(non_decimal.sub('', item).strip("."))
hardware_memory_free = str(mem_total - (active * page) / 1024 ** 2) + " MB"
except:
hardware_memory_free = ""
try:
current_folder = os.path.split(os.path.realpath(sys.argv[0]))[0]
s = os.statvfs(current_folder)
disk_total = int((s.f_frsize * s.f_blocks) / 1024 ** 2)
disk_free = int((s.f_frsize * s.f_bavail) / 1024 ** 2)
hardware_disk_space_total = str(disk_total) + " MB"
hardware_disk_space_free = str(disk_free) + " MB"
except:
hardware_disk_space_total = ""
hardware_disk_space_free = ""
try:
proc = subprocess.Popen(['system_profiler', 'SPAudioDataType'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
hardware_audio_card = \
proc.stdout.read().split("\n")[2].strip(":").strip()
except:
hardware_audio_card = ""
try:
import plistlib
proc = subprocess.Popen(['system_profiler', 'SPDisplaysDataType',
'-xml'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
pl = plistlib.readPlist(proc.stdout)
hardware_video_card = []
for card in pl[0]['_items']:
hardware_video_card.append(card['sppci_model'])
except:
hardware_video_card = ""
# Fill in info
info["os_architecture"] = platform.architecture()[0]
info["os_name"] = os_name
info["os_details"] = os_details
info["os_platform"] = os_platform
info["os_release"] = platform.release()
info["os_version"] = os_version
info["settings_folder"] = expyriment._importer_functions.get_settings_folder()
info["python_expyriment_build_date"] = __date__
info["python_expyriment_revision"] = __revision__
info["python_expyriment_version"] = __version__
if _numpy is not None:
numpy_version = _numpy.version.version
else:
numpy_version = ""
info["python_numpy_version"] = numpy_version
if _pil is not None:
pil_version = _pil.VERSION
else:
pil_version = ""
info["python_pil_version"] = pil_version
info["python_pygame_version"] = pygame.version.ver
if _ogl is not None:
pyopengl_version = _ogl.version.__version__
else:
pyopengl_version = ""
info["python_pyopengl_version"] = pyopengl_version
if _parallel is not None:
parallel_version = _parallel.VERSION
else:
parallel_version = ""
info["python_pyparallel_version"] = parallel_version
if _serial is not None:
serial_version = _serial.VERSION
else:
serial_version = ""
info["python_pyserial_version"] = serial_version
info["python_version"] = "{0}.{1}.{2}".format(sys.version_info[0],
sys.version_info[1],
sys.version_info[2])
info["hardware_audio_card"] = hardware_audio_card
info["hardware_cpu_architecture"] = platform.machine()
info["hardware_cpu_details"] = hardware_cpu_details
info["hardware_cpu_type"] = platform.processor()
info["hardware_disk_space_free"] = hardware_disk_space_free
info["hardware_disk_space_total"] = hardware_disk_space_total
try:
socket.gethostbyname("expyriment.googlecode.com")
hardware_internet_connection = "Yes"
except:
hardware_internet_connection = "No"
info["hardware_internet_connection"] = hardware_internet_connection
info["hardware_memory_total"] = hardware_memory_total
info["hardware_memory_free"] = hardware_memory_free
info["hardware_ports_parallel"] = \
expyriment.io.ParallelPort.get_available_ports()
info["hardware_ports_serial"] = \
expyriment.io.SerialPort.get_available_ports()
info["hardware_video_card"] = hardware_video_card
if as_string:
sorted_keys = info.keys()
sorted_keys.sort()
rtn = ""
for key in sorted_keys:
tabs = "\t" * (4 - int((len(key) + 1) / 8))
try:
rtn += key + ":" + tabs + info[key] + "\n"
except TypeError:
rtn += key + ":" + tabs + repr(info[key]) + "\n"
else:
rtn = info
return rtn
2
Example 10
Project: babble Source File: test_mmap.py
def test_both():
"Test mmap module on Unix systems and Windows"
# Create a file to be mmap'ed.
if os.path.exists(TESTFN):
os.unlink(TESTFN)
f = open(TESTFN, 'w+')
try: # unlink TESTFN no matter what
# Write 2 pages worth of data to the file
f.write('\0'* PAGESIZE)
f.write('foo')
f.write('\0'* (PAGESIZE-3) )
f.flush()
m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
f.close()
# Simple sanity checks
print type(m) # SF bug 128713: segfaulted on Linux
print ' Position of foo:', m.find('foo') / float(PAGESIZE), 'pages'
vereq(m.find('foo'), PAGESIZE)
print ' Length of file:', len(m) / float(PAGESIZE), 'pages'
vereq(len(m), 2*PAGESIZE)
print ' Contents of byte 0:', repr(m[0])
vereq(m[0], '\0')
print ' Contents of first 3 bytes:', repr(m[0:3])
vereq(m[0:3], '\0\0\0')
# Modify the file's content
print "\n Modifying file's content..."
m[0] = '3'
m[PAGESIZE +3: PAGESIZE +3+3] = 'bar'
# Check that the modification worked
print ' Contents of byte 0:', repr(m[0])
vereq(m[0], '3')
print ' Contents of first 3 bytes:', repr(m[0:3])
vereq(m[0:3], '3\0\0')
print ' Contents of second page:', repr(m[PAGESIZE-1 : PAGESIZE + 7])
vereq(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0')
m.flush()
# Test doing a regular expression match in an mmap'ed file
match = re.search('[A-Za-z]+', m)
if match is None:
print ' ERROR: regex match on mmap failed!'
else:
start, end = match.span(0)
length = end - start
print ' Regex match on mmap (page start, length of match):',
print start / float(PAGESIZE), length
vereq(start, PAGESIZE)
vereq(end, PAGESIZE + 6)
# test seeking around (try to overflow the seek implementation)
m.seek(0,0)
print ' Seek to zeroth byte'
vereq(m.tell(), 0)
m.seek(42,1)
print ' Seek to 42nd byte'
vereq(m.tell(), 42)
m.seek(0,2)
print ' Seek to last byte'
vereq(m.tell(), len(m))
print ' Try to seek to negative position...'
try:
m.seek(-1)
except ValueError:
pass
else:
verify(0, 'expected a ValueError but did not get it')
print ' Try to seek beyond end of mmap...'
try:
m.seek(1,2)
except ValueError:
pass
else:
verify(0, 'expected a ValueError but did not get it')
print ' Try to seek to negative position...'
try:
m.seek(-len(m)-1,2)
except ValueError:
pass
else:
verify(0, 'expected a ValueError but did not get it')
# Try resizing map
print ' Attempting resize()'
try:
m.resize(512)
except SystemError:
# resize() not supported
# No messages are printed, since the output of this test suite
# would then be different across platforms.
pass
else:
# resize() is supported
verify(len(m) == 512,
"len(m) is %d, but expecting 512" % (len(m),) )
# Check that we can no longer seek beyond the new size.
try:
m.seek(513,0)
except ValueError:
pass
else:
verify(0, 'Could seek beyond the new size')
# Check that the underlying file is truncated too
# (bug #728515)
f = open(TESTFN)
f.seek(0, 2)
verify(f.tell() == 512, 'Underlying file not truncated')
f.close()
verify(m.size() == 512, 'New size not reflected in file')
m.close()
finally:
try:
f.close()
except OSError:
pass
try:
os.unlink(TESTFN)
except OSError:
pass
# Test for "access" keyword parameter
try:
mapsize = 10
print " Creating", mapsize, "byte test data file."
open(TESTFN, "wb").write("a"*mapsize)
print " Opening mmap with access=ACCESS_READ"
f = open(TESTFN, "rb")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
verify(m[:] == 'a'*mapsize, "Readonly memory map data incorrect.")
print " Ensuring that readonly mmap can't be slice assigned."
try:
m[:] = 'b'*mapsize
except TypeError:
pass
else:
verify(0, "Able to write to readonly memory map")
print " Ensuring that readonly mmap can't be item assigned."
try:
m[0] = 'b'
except TypeError:
pass
else:
verify(0, "Able to write to readonly memory map")
print " Ensuring that readonly mmap can't be write() to."
try:
m.seek(0,0)
m.write('abc')
except TypeError:
pass
else:
verify(0, "Able to write to readonly memory map")
print " Ensuring that readonly mmap can't be write_byte() to."
try:
m.seek(0,0)
m.write_byte('d')
except TypeError:
pass
else:
verify(0, "Able to write to readonly memory map")
print " Ensuring that readonly mmap can't be resized."
try:
m.resize(2*mapsize)
except SystemError: # resize is not universally supported
pass
except TypeError:
pass
else:
verify(0, "Able to resize readonly memory map")
del m, f
verify(open(TESTFN, "rb").read() == 'a'*mapsize,
"Readonly memory map data file was modified")
print " Opening mmap with size too big"
import sys
f = open(TESTFN, "r+b")
try:
m = mmap.mmap(f.fileno(), mapsize+1)
except ValueError:
# we do not expect a ValueError on Windows
# CAUTION: This also changes the size of the file on disk, and
# later tests assume that the length hasn't changed. We need to
# repair that.
if sys.platform.startswith('win'):
verify(0, "Opening mmap with size+1 should work on Windows.")
else:
# we expect a ValueError on Unix, but not on Windows
if not sys.platform.startswith('win'):
verify(0, "Opening mmap with size+1 should raise ValueError.")
m.close()
f.close()
if sys.platform.startswith('win'):
# Repair damage from the resizing test.
f = open(TESTFN, 'r+b')
f.truncate(mapsize)
f.close()
print " Opening mmap with access=ACCESS_WRITE"
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
print " Modifying write-through memory map."
m[:] = 'c'*mapsize
verify(m[:] == 'c'*mapsize,
"Write-through memory map memory not updated properly.")
m.flush()
m.close()
f.close()
f = open(TESTFN, 'rb')
stuff = f.read()
f.close()
verify(stuff == 'c'*mapsize,
"Write-through memory map data file not updated properly.")
print " Opening mmap with access=ACCESS_COPY"
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
print " Modifying copy-on-write memory map."
m[:] = 'd'*mapsize
verify(m[:] == 'd' * mapsize,
"Copy-on-write memory map data not written correctly.")
m.flush()
verify(open(TESTFN, "rb").read() == 'c'*mapsize,
"Copy-on-write test data file should not be modified.")
try:
print " Ensuring copy-on-write maps cannot be resized."
m.resize(2*mapsize)
except TypeError:
pass
else:
verify(0, "Copy-on-write mmap resize did not raise exception.")
del m, f
try:
print " Ensuring invalid access parameter raises exception."
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=4)
except ValueError:
pass
else:
verify(0, "Invalid access code should have raised exception.")
if os.name == "posix":
# Try incompatible flags, prot and access parameters.
f = open(TESTFN, "r+b")
try:
m = mmap.mmap(f.fileno(), mapsize, flags=mmap.MAP_PRIVATE,
prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
except ValueError:
pass
else:
verify(0, "Incompatible parameters should raise ValueError.")
f.close()
finally:
try:
os.unlink(TESTFN)
except OSError:
pass
print ' Try opening a bad file descriptor...'
try:
mmap.mmap(-2, 4096)
except mmap.error:
pass
else:
verify(0, 'expected a mmap.error but did not get it')
# Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
# searching for data with embedded \0 bytes didn't work.
f = open(TESTFN, 'w+')
try: # unlink TESTFN no matter what
data = 'aabaac\x00deef\x00\x00aa\x00'
n = len(data)
f.write(data)
f.flush()
m = mmap.mmap(f.fileno(), n)
f.close()
for start in range(n+1):
for finish in range(start, n+1):
slice = data[start : finish]
vereq(m.find(slice), data.find(slice))
vereq(m.find(slice + 'x'), -1)
m.close()
finally:
os.unlink(TESTFN)
# make sure a double close doesn't crash on Solaris (Bug# 665913)
f = open(TESTFN, 'w+')
try: # unlink TESTFN no matter what
f.write(2**16 * 'a') # Arbitrary character
f.close()
f = open(TESTFN)
mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ)
mf.close()
mf.close()
f.close()
finally:
os.unlink(TESTFN)
# test mapping of entire file by passing 0 for map length
if hasattr(os, "stat"):
print " Ensuring that passing 0 as map length sets map size to current file size."
f = open(TESTFN, "w+")
try:
f.write(2**16 * 'm') # Arbitrary character
f.close()
f = open(TESTFN, "rb+")
mf = mmap.mmap(f.fileno(), 0)
verify(len(mf) == 2**16, "Map size should equal file size.")
vereq(mf.read(2**16), 2**16 * "m")
mf.close()
f.close()
finally:
os.unlink(TESTFN)
# test mapping of entire file by passing 0 for map length
if hasattr(os, "stat"):
print " Ensuring that passing 0 as map length sets map size to current file size."
f = open(TESTFN, "w+")
try:
f.write(2**16 * 'm') # Arbitrary character
f.close()
f = open(TESTFN, "rb+")
mf = mmap.mmap(f.fileno(), 0)
verify(len(mf) == 2**16, "Map size should equal file size.")
vereq(mf.read(2**16), 2**16 * "m")
mf.close()
f.close()
finally:
os.unlink(TESTFN)
# make move works everywhere (64-bit format problem earlier)
f = open(TESTFN, 'w+')
try: # unlink TESTFN no matter what
f.write("ABCDEabcde") # Arbitrary character
f.flush()
mf = mmap.mmap(f.fileno(), 10)
mf.move(5, 0, 5)
verify(mf[:] == "ABCDEABCDE", "Map move should have duplicated front 5")
mf.close()
f.close()
finally:
os.unlink(TESTFN)
# Test that setting access to PROT_READ gives exception
# rather than crashing
if hasattr(mmap, "PROT_READ"):
try:
mapsize = 10
open(TESTFN, "wb").write("a"*mapsize)
f = open(TESTFN, "rb")
m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ)
try:
m.write("foo")
except TypeError:
pass
else:
verify(0, "PROT_READ is not working")
finally:
os.unlink(TESTFN)
2
Example 11
def main():
"""
Entry point for GNS3 GUI.
"""
# Sometimes (for example at first launch) the OSX app service launcher add
# an extra argument starting with -psn_. We filter it
if sys.platform.startswith("darwin"):
sys.argv = [a for a in sys.argv if not a.startswith("-psn_")]
parser = argparse.ArgumentParser()
parser.add_argument("project", help="load a GNS3 project (.gns3)", metavar="path", nargs="?")
parser.add_argument("--version", help="show the version", action="version", version=__version__)
parser.add_argument("--debug", help="print out debug messages", action="store_true", default=False)
parser.add_argument("--config", help="Configuration file")
options = parser.parse_args()
exception_file_path = "exceptions.log"
if options.config:
LocalConfig.instance(config_file=options.config)
else:
LocalConfig.instance()
if options.project:
options.project = os.path.abspath(options.project)
if hasattr(sys, "frozen"):
# We add to the path where the OS search executable our binary location starting by GNS3
# packaged binary
frozen_dir = os.path.dirname(os.path.abspath(sys.executable))
if sys.platform.startswith("darwin"):
frozen_dirs = [
frozen_dir,
os.path.normpath(os.path.join(frozen_dir, '..', 'Resources'))
]
elif sys.platform.startswith("win"):
frozen_dirs = [
frozen_dir,
os.path.normpath(os.path.join(frozen_dir, 'dynamips')),
os.path.normpath(os.path.join(frozen_dir, 'vpcs'))
]
os.environ["PATH"] = os.pathsep.join(frozen_dirs) + os.pathsep + os.environ.get("PATH", "")
if options.project:
os.chdir(frozen_dir)
def exceptionHook(exception, value, tb):
if exception == KeyboardInterrupt:
sys.exit(0)
lines = traceback.format_exception(exception, value, tb)
print("cuem** Exception detected, traceback information saved in {} ******".format(exception_file_path))
print("\nPLEASE REPORT ON https://www.gns3.com\n")
print("".join(lines))
try:
curdate = time.strftime("%d %b %Y %H:%M:%S")
logfile = open(exception_file_path, "a", encoding="utf-8")
logfile.write("=== GNS3 {} traceback on {} ===\n".format(__version__, curdate))
logfile.write("".join(lines))
logfile.close()
except OSError as e:
print("Could not save traceback to {}: {}".format(os.path.normpath(exception_file_path), e))
if not sys.stdout.isatty():
# if stdout is not a tty (redirected to the console view),
# then print the exception on stderr too.
print("".join(lines), file=sys.stderr)
if exception is MemoryError:
print("YOUR SYSTEM IS OUT OF MEMORY!")
else:
CrashReport.instance().captureException(exception, value, tb)
# catch exceptions to write them in a file
sys.excepthook = exceptionHook
current_year = datetime.date.today().year
print("GNS3 GUI version {}".format(__version__))
print("Copyright (c) 2007-{} GNS3 Technologies Inc.".format(current_year))
# we only support Python 3 version >= 3.4
if sys.version_info < (3, 4):
raise SystemExit("Python 3.4 or higher is required")
if parse_version(QtCore.QT_VERSION_STR) < parse_version("5.0.0"):
raise SystemExit("Requirement is PyQt5 version 5.0.0 or higher, got version {}".format(QtCore.QT_VERSION_STR))
if parse_version(psutil.__version__) < parse_version("2.2.1"):
raise SystemExit("Requirement is psutil version 2.2.1 or higher, got version {}".format(psutil.__version__))
# check for the correct locale
# (UNIX/Linux only)
locale_check()
try:
os.getcwd()
except FileNotFoundError:
log.critical("the current working directory doesn't exist")
return
# always use the INI format on Windows and OSX (because we don't like the registry and plist files)
if sys.platform.startswith('win') or sys.platform.startswith('darwin'):
QtCore.QSettings.setDefaultFormat(QtCore.QSettings.IniFormat)
if sys.platform.startswith('win') and hasattr(sys, "frozen"):
try:
import win32console
import win32con
import win32gui
except ImportError:
raise SystemExit("Python for Windows extensions must be installed.")
if not options.debug:
try:
# hide the console
console_window = win32console.GetConsoleWindow()
win32gui.ShowWindow(console_window, win32con.SW_HIDE)
except win32console.error as e:
print("warning: could not allocate console: {}".format(e))
global app
app = Application(sys.argv)
# save client logging info to a file
logfile = os.path.join(LocalConfig.configDirectory(), "gns3_gui.log")
# on debug enable logging to stdout
if options.debug:
root_logger = init_logger(logging.DEBUG, logfile)
else:
root_logger = init_logger(logging.INFO, logfile)
# update the exception file path to have it in the same directory as the settings file.
exception_file_path = os.path.join(LocalConfig.configDirectory(), exception_file_path)
global mainwindow
mainwindow = MainWindow()
# On OSX we can receive the file to open from a system event
# loadPath is smart and will load only if a path is present
mainwindow.ready_signal.connect(lambda: mainwindow.loadPath(app.open_file_at_startup))
mainwindow.ready_signal.connect(lambda: mainwindow.loadPath(options.project))
app.file_open_signal.connect(lambda path: mainwindow.loadPath(path))
# Manage Ctrl + C or kill command
def sigint_handler(*args):
log.info("Signal received exiting the application")
mainwindow.setSoftExit(False)
app.closeAllWindows()
orig_sigint = signal.signal(signal.SIGINT, sigint_handler)
orig_sigterm = signal.signal(signal.SIGTERM, sigint_handler)
mainwindow.show()
exit_code = app.exec_()
signal.signal(signal.SIGINT, orig_sigint)
signal.signal(signal.SIGTERM, orig_sigterm)
delattr(MainWindow, "_instance")
# We force deleting the app object otherwise it's segfault on Fedora
del app
# We force a full garbage collect before exit
# for unknow reason otherwise Qt Segfault on OSX in some
# conditions
import gc
gc.collect()
sys.exit(exit_code)
2
Example 12
Project: cadquery Source File: __init__.py
def _fc_path():
"""Find FreeCAD"""
_PATH = ""
if _PATH:
return _PATH
#look for FREECAD_LIB env variable
if os.environ.has_key('FREECAD_LIB'):
_PATH = os.environ.get('FREECAD_LIB')
if os.path.exists( _PATH):
return _PATH
if sys.platform.startswith('linux'):
#Make some dangerous assumptions...
for _PATH in [
os.path.join(os.path.expanduser("~"), "lib/freecad/lib"),
"/usr/local/lib/freecad/lib",
"/usr/lib/freecad/lib",
"/opt/freecad/lib/",
"/usr/bin/freecad/lib",
"/usr/lib/freecad",
]:
if os.path.exists(_PATH):
return _PATH
elif sys.platform.startswith('win'):
#try all the usual suspects
for _PATH in [
"c:/Program Files/FreeCAD0.12/bin",
"c:/Program Files/FreeCAD0.13/bin",
"c:/Program Files/FreeCAD0.14/bin",
"c:/Program Files/FreeCAD0.15/bin",
"c:/Program Files/FreeCAD0.16/bin",
"c:/Program Files/FreeCAD0.17/bin",
"c:/Program Files (x86)/FreeCAD0.12/bin",
"c:/Program Files (x86)/FreeCAD0.13/bin",
"c:/Program Files (x86)/FreeCAD0.14/bin",
"c:/Program Files (x86)/FreeCAD0.15/bin",
"c:/Program Files (x86)/FreeCAD0.16/bin",
"c:/Program Files (x86)/FreeCAD0.17/bin",
"c:/apps/FreeCAD0.12/bin",
"c:/apps/FreeCAD0.13/bin",
"c:/apps/FreeCAD0.14/bin",
"c:/apps/FreeCAD0.15/bin",
"c:/apps/FreeCAD0.16/bin",
"c:/apps/FreeCAD0.17/bin",
"c:/Program Files/FreeCAD 0.12/bin",
"c:/Program Files/FreeCAD 0.13/bin",
"c:/Program Files/FreeCAD 0.14/bin",
"c:/Program Files/FreeCAD 0.15/bin",
"c:/Program Files/FreeCAD 0.16/bin",
"c:/Program Files/FreeCAD 0.17/bin",
"c:/Program Files (x86)/FreeCAD 0.12/bin",
"c:/Program Files (x86)/FreeCAD 0.13/bin",
"c:/Program Files (x86)/FreeCAD 0.14/bin",
"c:/Program Files (x86)/FreeCAD 0.15/bin",
"c:/Program Files (x86)/FreeCAD 0.16/bin",
"c:/Program Files (x86)/FreeCAD 0.17/bin",
"c:/apps/FreeCAD 0.12/bin",
"c:/apps/FreeCAD 0.13/bin",
"c:/apps/FreeCAD 0.14/bin",
"c:/apps/FreeCAD 0.15/bin",
"c:/apps/FreeCAD 0.16/bin",
"c:/apps/FreeCAD 0.17/bin",
]:
if os.path.exists(_PATH):
return _PATH
elif sys.platform.startswith('darwin'):
#Assume we're dealing with a Mac
for _PATH in [
"/Applications/FreeCAD.app/Contents/lib",
os.path.join(os.path.expanduser("~"), "Library/Application Support/FreeCAD/lib"),
]:
if os.path.exists(_PATH):
return _PATH
2
Example 13
Project: PyFITS Source File: hdulist.py
def _flush_resize(self):
"""
Implements flushing changes in update mode when parts of one or more HDU
need to be resized.
"""
old_name = self._file.name
old_memmap = self._file.memmap
name = _tmp_name(old_name)
if not self._file.file_like:
old_mode = os.stat(old_name).st_mode
# The underlying file is an actual file object. The HDUList is
# resized, so we need to write it to a tmp file, delete the
# original file, and rename the tmp file to the original file.
if self._file.compression == 'gzip':
new_file = gzip.GzipFile(name, mode='ab+')
elif self._file.compression == 'bzip2':
new_file = bz2.BZ2File(name, mode='w')
else:
new_file = name
hdulist = self.fromfile(new_file, mode='append')
for hdu in self:
hdu._writeto(hdulist._file, inplace=True, copy=True)
if sys.platform.startswith('win'):
# Collect a list of open mmaps to the data; this well be used
# later. See below.
mmaps = [(idx, _get_array_mmap(hdu.data), hdu.data)
for idx, hdu in enumerate(self) if hdu._has_data]
hdulist._file.close()
self._file.close()
if sys.platform.startswith('win'):
# Close all open mmaps to the data. This is only necessary on
# Windows, which will not allow a file to be renamed or deleted
# until all handles to that file have been closed.
for idx, mmap, arr in mmaps:
if mmap is not None:
mmap.close()
os.remove(self._file.name)
# reopen the renamed new file with "update" mode
os.rename(name, old_name)
os.chmod(old_name, old_mode)
if isinstance(new_file, gzip.GzipFile):
old_file = gzip.GzipFile(old_name, mode='rb+')
else:
old_file = old_name
ffo = _File(old_file, mode='update', memmap=old_memmap)
self._file = ffo
for hdu in self:
# Need to update the _file attribute and close any open mmaps
# on each HDU
if hdu._has_data and _get_array_mmap(hdu.data) is not None:
del hdu.data
hdu._file = ffo
if sys.platform.startswith('win'):
# On Windows, all the original data mmaps were closed above.
# However, it's possible that the user still has references to
# the old data which would no longer work (possibly even cause
# a segfault if they try to access it). This replaces the
# buffers used by the original arrays with the buffers of mmap
# arrays created from the new file. This seems to work, but
# it's a flaming hack and carries no guarantees that it won't
# lead to odd behavior in practice. Better to just not keep
# references to data from files that had to be resized upon
# flushing (on Windows--again, this is no problem on Linux).
for idx, mmap, arr in mmaps:
if mmap is not None:
arr.data = self[idx].data.data
del mmaps # Just to be sure
else:
# The underlying file is not a file object, it is a file like
# object. We can't write out to a file, we must update the file
# like object in place. To do this, we write out to a temporary
# file, then delete the contents in our file like object, then
# write the contents of the temporary file to the now empty file
# like object.
self.writeto(name)
hdulist = self.fromfile(name)
ffo = self._file
ffo.truncate(0)
ffo.seek(0)
for hdu in hdulist:
hdu._writeto(ffo, inplace=True, copy=True)
# Close the temporary file and delete it.
hdulist.close()
os.remove(hdulist._file.name)
# reset the resize attributes after updating
self._resize = False
self._truncate = False
for hdu in self:
hdu._header._modified = False
hdu._new = False
hdu._file = ffo
2
Example 14
Project: jhbuild Source File: environment.py
def addpath(envvar, path, prepend=True):
'''Adds a path to an environment variable.'''
if envvar in [ 'LDFLAGS', 'CFLAGS', 'CXXFLAGS' ]:
if sys.platform.startswith('win'):
path = subprocess_win32.fix_path_for_msys(path)
envval = os.environ.get(envvar)
if envval:
envval = path + ' ' + envval
else:
envval = path
else:
if envvar == 'PATH':
# PATH is special cased on Windows to allow execution without
# sh.exe. The other env vars (like LD_LIBRARY_PATH) don't mean
# anything to native Windows so they stay in UNIX format, but
# PATH is kept in Windows format (; separated, c:/ or c:\ format
# paths) so native Popen works.
pathsep = os.pathsep
else:
pathsep = ':'
if sys.platform.startswith('win'):
path = subprocess_win32.fix_path_for_msys(path)
if sys.platform.startswith('win') and len(path) > 1 and \
path[1] == ':':
# Windows: Don't allow c:/ style paths in :-separated env vars
# for obvious reasons. /c/ style paths are valid - if a var is
# separated by : it will only be of interest to programs inside
# MSYS anyway.
path='/'+path[0]+path[2:]
envval = os.environ.get(envvar, path)
parts = envval.split(pathsep)
if prepend:
parts.insert(0, path)
else:
parts.append(path)
# remove duplicate entries:
i = 1
while i < len(parts):
if parts[i] in parts[:i]:
del parts[i]
elif envvar == 'PYTHONPATH' and parts[i] == "":
del parts[i]
else:
i += 1
envval = pathsep.join(parts)
os.environ[envvar] = envval
2
Example 15
Project: vlc-python Source File: header.py
def find_lib():
dll = None
plugin_path = None
if sys.platform.startswith('linux'):
p = find_library('vlc')
try:
dll = ctypes.CDLL(p)
except OSError: # may fail
dll = ctypes.CDLL('libvlc.so.5')
elif sys.platform.startswith('win'):
p = find_library('libvlc.dll')
if p is None:
try: # some registry settings
import _winreg as w # leaner than win32api, win32con
for r in w.HKEY_LOCAL_MACHINE, w.HKEY_CURRENT_USER:
try:
r = w.OpenKey(r, 'Software\\VideoLAN\\VLC')
plugin_path, _ = w.QueryValueEx(r, 'InstallDir')
w.CloseKey(r)
break
except w.error:
pass
except ImportError: # no PyWin32
pass
if plugin_path is None:
# try some standard locations.
for p in ('Program Files\\VideoLan\\', 'VideoLan\\',
'Program Files\\', ''):
p = 'C:\\' + p + 'VLC\\libvlc.dll'
if os.path.exists(p):
plugin_path = os.path.dirname(p)
break
if plugin_path is not None: # try loading
p = os.getcwd()
os.chdir(plugin_path)
# if chdir failed, this will raise an exception
dll = ctypes.CDLL('libvlc.dll')
# restore cwd after dll has been loaded
os.chdir(p)
else: # may fail
dll = ctypes.CDLL('libvlc.dll')
else:
plugin_path = os.path.dirname(p)
dll = ctypes.CDLL(p)
elif sys.platform.startswith('darwin'):
# FIXME: should find a means to configure path
d = '/Applications/VLC.app/Contents/MacOS/'
p = d + 'lib/libvlc.dylib'
if os.path.exists(p):
dll = ctypes.CDLL(p)
d += 'modules'
if os.path.isdir(d):
plugin_path = d
else: # hope, some PATH is set...
dll = ctypes.CDLL('libvlc.dylib')
else:
raise NotImplementedError('%s: %s not supported' % (sys.argv[0], sys.platform))
return (dll, plugin_path)
2
Example 16
Project: rituals Source File: which.py
def whichgen(command, path=None, verbose=0, exts=None): # pylint: disable=too-many-branches, too-many-statements
"""Return a generator of full paths to the given command.
"command" is a the name of the executable to search for.
"path" is an optional alternate path list to search. The default it
to use the PATH environment variable.
"verbose", if true, will cause a 2-tuple to be returned for each
match. The second element is a textual description of where the
match was found.
"exts" optionally allows one to specify a list of extensions to use
instead of the standard list for this system. This can
effectively be used as an optimization to, for example, avoid
stat's of "foo.vbs" when searching for "foo" and you know it is
not a VisualBasic script but ".vbs" is on PATHEXT. This option
is only supported on Windows.
This method returns a generator which yields either full paths to
the given command or, if verbose, tuples of the form (<path to
command>, <where path found>).
"""
matches = []
if path is None:
using_given_path = 0
path = os.environ.get("PATH", "").split(os.pathsep)
if sys.platform.startswith("win"):
path.insert(0, os.curdir) # implied by Windows shell
else:
using_given_path = 1
# Windows has the concept of a list of extensions (PATHEXT env var).
if sys.platform.startswith("win"):
if exts is None:
exts = os.environ.get("PATHEXT", "").split(os.pathsep)
# If '.exe' is not in exts then obviously this is Win9x and
# or a bogus PATHEXT, then use a reasonable default.
for ext in exts:
if ext.lower() == ".exe":
break
else:
exts = ['.COM', '.EXE', '.BAT']
elif not isinstance(exts, list):
raise TypeError("'exts' argument must be a list or None")
else:
if exts is not None:
raise WhichError("'exts' argument is not supported on platform '%s'" % sys.platform)
exts = []
# File name cannot have path separators because PATH lookup does not
# work that way.
if os.sep in command or os.altsep and os.altsep in command:
pass
else:
for i in range(len(path)):
dir_name = path[i]
# On windows the dir_name *could* be quoted, drop the quotes
if sys.platform.startswith("win") and len(dir_name) >= 2 and dir_name[0] == '"' and dir_name[-1] == '"':
dir_name = dir_name[1:-1]
for ext in ['']+exts:
abs_name = os.path.abspath(os.path.normpath(os.path.join(dir_name, command+ext)))
if os.path.isfile(abs_name):
if using_given_path:
from_where = "from given path element %d" % i
elif not sys.platform.startswith("win"):
from_where = "from PATH element %d" % i
elif i == 0:
from_where = "from current directory"
else:
from_where = "from PATH element %d" % (i-1)
match = _cull((abs_name, from_where), matches, verbose)
if match:
if verbose:
yield match
else:
yield match[0]
match = _get_registered_executable(command)
if match is not None:
match = _cull(match, matches, verbose)
if match:
if verbose:
yield match
else:
yield match[0]
2
Example 17
Project: gns3-server Source File: vmware_vm.py
@asyncio.coroutine
def _add_ubridge_connection(self, nio, adapter_number):
"""
Creates a connection in uBridge.
:param nio: NIO instance
:param adapter_number: adapter number
"""
block_host_traffic = self.manager.config.get_section_config("VMware").getboolean("block_host_traffic", False)
vnet = "ethernet{}.vnet".format(adapter_number)
if vnet not in self._vmx_pairs:
raise VMwareError("vnet {} not in VMX file".format(vnet))
yield from self._ubridge_hypervisor.send("bridge create {name}".format(name=vnet))
vmnet_interface = os.path.basename(self._vmx_pairs[vnet])
if sys.platform.startswith("linux"):
yield from self._ubridge_hypervisor.send('bridge add_nio_linux_raw {name} "{interface}"'.format(name=vnet,
interface=vmnet_interface))
elif sys.platform.startswith("win"):
windows_interfaces = interfaces()
npf = None
source_mac = None
for interface in windows_interfaces:
if "netcard" in interface and vmnet_interface in interface["netcard"]:
npf = interface["id"]
source_mac = interface["mac_address"]
elif vmnet_interface in interface["name"]:
npf = interface["id"]
source_mac = interface["mac_address"]
if npf:
yield from self._ubridge_hypervisor.send('bridge add_nio_ethernet {name} "{interface}"'.format(name=vnet,
interface=npf))
else:
raise VMwareError("Could not find NPF id for VMnet interface {}".format(vmnet_interface))
if block_host_traffic:
if source_mac:
yield from self._ubridge_hypervisor.send('bridge set_pcap_filter {name} "not ether src {mac}"'.format(name=vnet,
mac=source_mac))
else:
log.warn("Could not block host network traffic on {} (no MAC address found)".format(vmnet_interface))
elif sys.platform.startswith("darwin"):
yield from self._ubridge_hypervisor.send('bridge add_nio_fusion_vmnet {name} "{interface}"'.format(name=vnet,
interface=vmnet_interface))
else:
yield from self._ubridge_hypervisor.send('bridge add_nio_ethernet {name} "{interface}"'.format(name=vnet,
interface=vmnet_interface))
if isinstance(nio, NIOUDP):
yield from self._ubridge_hypervisor.send('bridge add_nio_udp {name} {lport} {rhost} {rport}'.format(name=vnet,
lport=nio.lport,
rhost=nio.rhost,
rport=nio.rport))
elif isinstance(nio, NIOTAP):
yield from self._ubridge_hypervisor.send('bridge add_nio_tap {name} {tap}'.format(name=vnet, tap=nio.tap_device))
if nio.capturing:
yield from self._ubridge_hypervisor.send('bridge start_capture {name} "{pcap_file}"'.format(name=vnet,
pcap_file=nio.pcap_output_file))
yield from self._ubridge_hypervisor.send('bridge start {name}'.format(name=vnet))
2
Example 18
Project: Icarra Source File: icarra2.py
def __init__(self, *args):
global prefs
self.started = False
self.isOSX = sys.platform.startswith("darwin")
self.isWindows = sys.platform.startswith("win")
self.isLinux = sys.platform.startswith("linux")
QApplication.__init__(self, *args)
self.setApplicationName('Icarra2')
self.setQuitOnLastWindowClosed(False)
# Set locale one more time, QT may override it
locale.setlocale(locale.LC_ALL, "")
# Set global app and path
if hasattr(sys, "frozen"):
appPath = os.path.dirname(sys.argv[0])
else:
appPath = os.path.abspath(os.path.dirname(sys.argv[0]))
appGlobal.setApp(self, appPath)
# For thread safe errors
self.errorMutex = threading.Lock()
self.errorList = []
# For checking tables
self.checkTableMutex = threading.Lock()
# For starting and ending big tasks
self.bigTask = False
self.bigTaskCondition = threading.Condition()
# The current statusUpdate dialog, if any
self.statusUpdate = False
# Initialize members
self.prefs = prefs
self.stockData = StockData()
self.ofxDebugFrame = False
self.portfolio = False
self.tool = False
self.toolWidget = False
# If we notified the user that we failed to connect to the icarra server
self.notifieldFailConnected = False
self.positiveColor = QColor(0, 153, 0)
self.negativeColor = QColor(204, 0, 0)
self.alternateRowColor = QColor(216, 216, 255)
# Make sure benchmarks have been created
checkBenchmarks(prefs)
# Dictionary of portfolios used for rebuilding portfolio menu
# So we don't have to open portfolios every time
# Key is portfolio name, value is portfolio type ("benchmark", "bank", "brokerage", "combined")
self.buildPortfolioMenuNames()
# Nothing else if regression
if "--regression" in args[0] or "--broker-info" in args[0] or "--rebuild" in args[0] or "--import" in args[0]:
return
self.plugins = PluginManager()
timesRun = prefs.getTimesRun()
splashTime = datetime.datetime.now()
if timesRun > 0:
# Start updater now so we can get stock data
autoUpdater.start(self.stockData, self.prefs)
self.splash = SplashScreenFrame()
else:
self.createSamplePortfolio()
# Set all stocks as not having been downloaded
# This is important incase the startup process failed
self.stockData.db.query("update stockInfo set lastDownload='1900-01-01 00:00:00'")
# Start after creating sample portfolio
autoUpdater.start(self.stockData, self.prefs)
# Load initial data, make sure we got it
self.splash = SplashScreenFrame(firstTime = True)
if not self.splash.running:
autoUpdater.stop()
self.splash.close()
self.started = False
message = QMessageBox(QMessageBox.Critical, "Connection error", "Unable to connect to the Icarra web server. Please check your internet connection and try again.", flags = Qt.Dialog | Qt.MSWindowsFixedSizeDialogHint | Qt.WindowStaysOnTopHint)
message.exec_()
return
self.main = MainWindow()
self.processEvents()
self.main.render()
self.processEvents()
self.rebuildPortfoliosMenu()
self.loadPortfolio(prefs.getLastPortfolio())
self.processEvents()
if self.prefs.getOfxDebug():
self.startOfxDebug()
if self.splash and timesRun == 0:
self.splash.progress.setValue(100)
self.checkVersion()
self.processEvents()
# Wait up to 2 seconds before hiding splash screen
elapsedMs = (datetime.datetime.now() - splashTime).microseconds / 1000
if self.splash and elapsedMs < 3000:
self.timer = QTimer()
interval = 3000 - elapsedMs
self.timer.setInterval(interval)
self.timer.setSingleShot(True)
self.connect(self.timer, SIGNAL("timeout()"), self.hideSplash)
self.timer.start()
else:
self.hideSplash()
# Start timer to check for failed connected
self.failTimer = QTimer()
self.failTimer.setInterval(3000)
self.connect(self.failTimer, SIGNAL("timeout()"), self.checkFailTimer)
self.failTimer.start()
self.prefs.incTimesRun()
appGlobal.getApp().started = True
2
Example 19
Project: idle_master_py Source File: idle-to-ready.py
def IdleBatch(appids):
try:
global process_idle1, process_idle2, process_idle3, process_idle4, process_idle5, process_idle6, process_idle7, process_idle8, process_idle9, process_idle10, process_idle11, process_idle12, process_idle13, process_idle14, process_idle15, process_idle16, process_idle17, process_idle18, process_idle19, process_idle20, process_idle21, process_idle22, process_idle23, process_idle24, process_idle25
global idle_time
idle_time = time.time()
if sys.platform.startswith('win32'):
start_text = "steam-idle.exe "
elif sys.platform.startswith('darwin'):
start_text = "./steam-idle "
elif sys.platform.startswith('linux'):
start_text = "./steam-idle.py "
process_idle1 = subprocess.Popen(start_text + str(appids[0][0]), shell=True)
process_idle2 = subprocess.Popen(start_text + str(appids[1][0]), shell=True)
process_idle3 = subprocess.Popen(start_text + str(appids[2][0]), shell=True)
process_idle4 = subprocess.Popen(start_text + str(appids[3][0]), shell=True)
process_idle5 = subprocess.Popen(start_text + str(appids[4][0]), shell=True)
process_idle6 = subprocess.Popen(start_text + str(appids[5][0]), shell=True)
process_idle7 = subprocess.Popen(start_text + str(appids[6][0]), shell=True)
process_idle8 = subprocess.Popen(start_text + str(appids[7][0]), shell=True)
process_idle9 = subprocess.Popen(start_text + str(appids[8][0]), shell=True)
process_idle10 = subprocess.Popen(start_text + str(appids[9][0]), shell=True)
process_idle11 = subprocess.Popen(start_text + str(appids[10][0]), shell=True)
process_idle12 = subprocess.Popen(start_text + str(appids[11][0]), shell=True)
process_idle13 = subprocess.Popen(start_text + str(appids[12][0]), shell=True)
process_idle14 = subprocess.Popen(start_text + str(appids[13][0]), shell=True)
process_idle15 = subprocess.Popen(start_text + str(appids[14][0]), shell=True)
process_idle16 = subprocess.Popen(start_text + str(appids[15][0]), shell=True)
process_idle17 = subprocess.Popen(start_text + str(appids[16][0]), shell=True)
process_idle18 = subprocess.Popen(start_text + str(appids[17][0]), shell=True)
process_idle19 = subprocess.Popen(start_text + str(appids[18][0]), shell=True)
process_idle20 = subprocess.Popen(start_text + str(appids[19][0]), shell=True)
process_idle21 = subprocess.Popen(start_text + str(appids[20][0]), shell=True)
process_idle22 = subprocess.Popen(start_text + str(appids[21][0]), shell=True)
process_idle23 = subprocess.Popen(start_text + str(appids[22][0]), shell=True)
process_idle24 = subprocess.Popen(start_text + str(appids[23][0]), shell=True)
process_idle25 = subprocess.Popen(start_text + str(appids[24][0]), shell=True)
except:
logging.warning("All games started")
2
Example 20
def test_access_parameter(self):
# Test for "access" keyword parameter
mapsize = 10
open(TESTFN, "wb").write("a"*mapsize)
f = open(TESTFN, "rb")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
self.assertEqual(m[:], 'a'*mapsize, "Readonly memory map data incorrect.")
# Ensuring that readonly mmap can't be slice assigned
try:
m[:] = 'b'*mapsize
except TypeError:
pass
else:
self.fail("Able to write to readonly memory map")
# Ensuring that readonly mmap can't be item assigned
try:
m[0] = 'b'
except TypeError:
pass
else:
self.fail("Able to write to readonly memory map")
# Ensuring that readonly mmap can't be write() to
try:
m.seek(0,0)
m.write('abc')
except TypeError:
pass
else:
self.fail("Able to write to readonly memory map")
# Ensuring that readonly mmap can't be write_byte() to
try:
m.seek(0,0)
m.write_byte('d')
except TypeError:
pass
else:
self.fail("Able to write to readonly memory map")
# Ensuring that readonly mmap can't be resized
try:
m.resize(2*mapsize)
except SystemError: # resize is not universally supported
pass
except TypeError:
pass
else:
self.fail("Able to resize readonly memory map")
f.close()
del m, f
self.assertEqual(open(TESTFN, "rb").read(), 'a'*mapsize,
"Readonly memory map data file was modified")
# Opening mmap with size too big
import sys
f = open(TESTFN, "r+b")
try:
m = mmap.mmap(f.fileno(), mapsize+1)
except ValueError:
# we do not expect a ValueError on Windows
# CAUTION: This also changes the size of the file on disk, and
# later tests assume that the length hasn't changed. We need to
# repair that.
if sys.platform.startswith('win'):
self.fail("Opening mmap with size+1 should work on Windows.")
else:
# we expect a ValueError on Unix, but not on Windows
if not sys.platform.startswith('win'):
self.fail("Opening mmap with size+1 should raise ValueError.")
m.close()
f.close()
if sys.platform.startswith('win'):
# Repair damage from the resizing test.
f = open(TESTFN, 'r+b')
f.truncate(mapsize)
f.close()
# Opening mmap with access=ACCESS_WRITE
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
# Modifying write-through memory map
m[:] = 'c'*mapsize
self.assertEqual(m[:], 'c'*mapsize,
"Write-through memory map memory not updated properly.")
m.flush()
m.close()
f.close()
f = open(TESTFN, 'rb')
stuff = f.read()
f.close()
self.assertEqual(stuff, 'c'*mapsize,
"Write-through memory map data file not updated properly.")
# Opening mmap with access=ACCESS_COPY
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
# Modifying copy-on-write memory map
m[:] = 'd'*mapsize
self.assertEqual(m[:], 'd' * mapsize,
"Copy-on-write memory map data not written correctly.")
m.flush()
self.assertEqual(open(TESTFN, "rb").read(), 'c'*mapsize,
"Copy-on-write test data file should not be modified.")
# Ensuring copy-on-write maps cannot be resized
self.assertRaises(TypeError, m.resize, 2*mapsize)
f.close()
del m, f
# Ensuring invalid access parameter raises exception
f = open(TESTFN, "r+b")
self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4)
f.close()
if os.name == "posix":
# Try incompatible flags, prot and access parameters.
f = open(TESTFN, "r+b")
self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize,
flags=mmap.MAP_PRIVATE,
prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
f.close()
# Try writing with PROT_EXEC and without PROT_WRITE
prot = mmap.PROT_READ | getattr(mmap, 'PROT_EXEC', 0)
with open(TESTFN, "r+b") as f:
m = mmap.mmap(f.fileno(), mapsize, prot=prot)
self.assertRaises(TypeError, m.write, b"abcdef")
self.assertRaises(TypeError, m.write_byte, 0)
m.close()
2
Example 21
Project: python-matlab-bridge Source File: make.py
def which_matlab():
try:
matlab_path = which('matlab').strip()
matlab_path = make_str(matlab_path)
return os.path.dirname(os.path.realpath(matlab_path))
except (OSError, subprocess.CalledProcessError):
def ensure_path(path, extension=''):
return os.path.isdir(path) and \
os.path.isfile(os.path.join(path, "matlab" + extension))
# need to guess the location of MATLAB
if sys.platform.startswith("darwin"):
MATLABs = [os.path.join("/Applications", i, "bin")
for i in os.listdir("/Applications")
if i.startswith("MATLAB_R")]
# only want ones with MATLAB executables
# sort so we can get the latest
MATLABs = list(sorted(filter(ensure_path, MATLABs)))
return MATLABs[-1] if len(MATLABs) > 0 else None
elif sys.platform.startswith("win32"):
MATLAB_loc = "C:\\Program Files\\MATLAB"
print(MATLAB_loc)
if not os.path.isdir(MATLAB_loc):
return None
MATLABs = [os.path.join(MATLAB_loc, i, "bin")
for i in os.listdir(MATLAB_loc)]
print(MATLABs)
print(i)
# only want ones with MATLAB executables
# sort so we can get the latest
MATLABs = list(sorted(filter(lambda x: ensure_path(x, ".exe"),
MATLABs)))
print(MATLABs)
return MATLABs[-1] if len(MATLABs) > 0 else None
elif sys.platform.startswith("linux"):
MATLAB_loc = "/usr/local/MATLAB/"
if not os.path.isdir(MATLAB_loc):
return None
MATLABs = [os.path.join(MATLAB_loc, i, "bin")
for i in os.listdir(MATLAB_loc)
if i.startswith("R")]
# only want ones with MATLAB executables
# sort so we can get the latest
MATLABs = list(sorted(filter(ensure_path, MATLABs)))
return MATLABs[-1] if len(MATLABs) > 0 else None
2
Example 22
Project: python-vlc Source File: header.py
def find_lib():
dll = None
plugin_path = None
if sys.platform.startswith('linux'):
p = find_library('vlc')
try:
dll = ctypes.CDLL(p)
except OSError: # may fail
dll = ctypes.CDLL('libvlc.so.5')
elif sys.platform.startswith('win'):
libname = 'libvlc.dll'
p = find_library(libname)
if p is None:
try: # some registry settings
# leaner than win32api, win32con
if PYTHON3:
import winreg as w
else:
import _winreg as w
for r in w.HKEY_LOCAL_MACHINE, w.HKEY_CURRENT_USER:
try:
r = w.OpenKey(r, 'Software\\VideoLAN\\VLC')
plugin_path, _ = w.QueryValueEx(r, 'InstallDir')
w.CloseKey(r)
break
except w.error:
pass
except ImportError: # no PyWin32
pass
if plugin_path is None:
# try some standard locations.
programfiles = os.environ["ProgramFiles"]
homedir = os.environ["HOMEDRIVE"]
for p in ('{programfiles}\\VideoLan{libname}', 'programtalk_com:\\VideoLan{libname}',
'{programfiles}{libname}', 'programtalk_com:{libname}'):
p = p.format(homedir = homedir,
programfiles = programfiles,
libname = '\\VLC\\' + libname)
if os.path.exists(p):
plugin_path = os.path.dirname(p)
break
if plugin_path is not None: # try loading
p = os.getcwd()
os.chdir(plugin_path)
# if chdir failed, this will raise an exception
dll = ctypes.CDLL(libname)
# restore cwd after dll has been loaded
os.chdir(p)
else: # may fail
dll = ctypes.CDLL(libname)
else:
plugin_path = os.path.dirname(p)
dll = ctypes.CDLL(p)
elif sys.platform.startswith('darwin'):
# FIXME: should find a means to configure path
d = '/Applications/VLC.app/Contents/MacOS/'
p = d + 'lib/libvlc.dylib'
if os.path.exists(p):
dll = ctypes.CDLL(p)
for p in ('modules', 'plugins'):
p = d + p
if os.path.isdir(p):
plugin_path = p
break
else: # hope, some PATH is set...
dll = ctypes.CDLL('libvlc.dylib')
else:
raise NotImplementedError('%s: %s not supported' % (sys.argv[0], sys.platform))
return (dll, plugin_path)
2
Example 23
Project: opentrons-api Source File: motor.py
def get_serial_ports_list(self):
""" Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
"""
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif (sys.platform.startswith('linux') or
sys.platform.startswith('cygwin')):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
port_filter = {'usbmodem', 'COM', 'ACM', 'USB'}
for port in ports:
try:
if any([f in port for f in port_filter]):
s = serial.Serial(port)
s.close()
result.append(port)
except Exception as e:
log.debug(
'Exception in testing port {}'.format(port))
log.debug(e)
return result
2
Example 24
Project: eol Source File: which.py
def whichgen(command, path=None, verbose=0, exts=None):
"""Return a generator of full paths to the given command.
"command" is a the name of the executable to search for.
"path" is an optional alternate path list to search. The default it
to use the PATH environment variable.
"verbose", if true, will cause a 2-tuple to be returned for each
match. The second element is a textual description of where the
match was found.
"exts" optionally allows one to specify a list of extensions to use
instead of the standard list for this system. This can
effectively be used as an optimization to, for example, avoid
stat's of "foo.vbs" when searching for "foo" and you know it is
not a VisualBasic script but ".vbs" is on PATHEXT. This option
is only supported on Windows.
This method returns a generator which yields either full paths to
the given command or, if verbose, tuples of the form (<path to
command>, <where path found>).
"""
matches = []
if path is None:
usingGivenPath = 0
path = os.environ.get("PATH", "").split(os.pathsep)
if sys.platform.startswith("win"):
path.insert(0, os.curdir) # implied by Windows shell
else:
usingGivenPath = 1
# Windows has the concept of a list of extensions (PATHEXT env var).
if sys.platform.startswith("win"):
if exts is None:
exts = os.environ.get("PATHEXT", "").split(os.pathsep)
# If '.exe' is not in exts then obviously this is Win9x and
# or a bogus PATHEXT, then use a reasonable default.
for ext in exts:
if ext.lower() == ".exe":
break
else:
exts = ['.COM', '.EXE', '.BAT']
elif not isinstance(exts, list):
raise TypeError("'exts' argument must be a list or None")
else:
if exts is not None:
raise WhichError("'exts' argument is not supported on "\
"platform '%s'" % sys.platform)
exts = []
# File name cannot have path separators because PATH lookup does not
# work that way.
if os.sep in command or os.altsep and os.altsep in command:
if os.path.exists(command):
match = _cull((command, "explicit path given"), matches, verbose)
if verbose:
yield match
else:
yield match[0]
else:
for i in range(len(path)):
dirName = path[i]
# On windows the dirName *could* be quoted, drop the quotes
if sys.platform.startswith("win") and len(dirName) >= 2\
and dirName[0] == '"' and dirName[-1] == '"':
dirName = dirName[1:-1]
for ext in ['']+exts:
absName = os.path.abspath(
os.path.normpath(os.path.join(dirName, command+ext)))
if os.path.isfile(absName):
if usingGivenPath:
fromWhere = "from given path element %d" % i
elif not sys.platform.startswith("win"):
fromWhere = "from PATH element %d" % i
elif i == 0:
fromWhere = "from current directory"
else:
fromWhere = "from PATH element %d" % (i-1)
match = _cull((absName, fromWhere), matches, verbose)
if match:
if verbose:
yield match
else:
yield match[0]
match = _getRegisteredExecutable(command)
if match is not None:
match = _cull(match, matches, verbose)
if match:
if verbose:
yield match
else:
yield match[0]
2
Example 25
def test_access_parameter(self):
# Test for "access" keyword parameter
mapsize = 10
with open(TESTFN, "wb") as fp:
fp.write(b"a"*mapsize)
with open(TESTFN, "rb") as f:
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
self.assertEqual(m[:], b'a'*mapsize, "Readonly memory map data incorrect.")
# Ensuring that readonly mmap can't be slice assigned
try:
m[:] = b'b'*mapsize
except TypeError:
pass
else:
self.fail("Able to write to readonly memory map")
# Ensuring that readonly mmap can't be item assigned
try:
m[0] = b'b'
except TypeError:
pass
else:
self.fail("Able to write to readonly memory map")
# Ensuring that readonly mmap can't be write() to
try:
m.seek(0,0)
m.write(b'abc')
except TypeError:
pass
else:
self.fail("Able to write to readonly memory map")
# Ensuring that readonly mmap can't be write_byte() to
try:
m.seek(0,0)
m.write_byte(b'd')
except TypeError:
pass
else:
self.fail("Able to write to readonly memory map")
# Ensuring that readonly mmap can't be resized
try:
m.resize(2*mapsize)
except SystemError: # resize is not universally supported
pass
except TypeError:
pass
else:
self.fail("Able to resize readonly memory map")
with open(TESTFN, "rb") as fp:
self.assertEqual(fp.read(), b'a'*mapsize,
"Readonly memory map data file was modified")
# Opening mmap with size too big
with open(TESTFN, "r+b") as f:
try:
m = mmap.mmap(f.fileno(), mapsize+1)
except ValueError:
# we do not expect a ValueError on Windows
# CAUTION: This also changes the size of the file on disk, and
# later tests assume that the length hasn't changed. We need to
# repair that.
if sys.platform.startswith('win'):
self.fail("Opening mmap with size+1 should work on Windows.")
else:
# we expect a ValueError on Unix, but not on Windows
if not sys.platform.startswith('win'):
self.fail("Opening mmap with size+1 should raise ValueError.")
m.close()
if sys.platform.startswith('win'):
# Repair damage from the resizing test.
with open(TESTFN, 'r+b') as f:
f.truncate(mapsize)
# Opening mmap with access=ACCESS_WRITE
with open(TESTFN, "r+b") as f:
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
# Modifying write-through memory map
m[:] = b'c'*mapsize
self.assertEqual(m[:], b'c'*mapsize,
"Write-through memory map memory not updated properly.")
m.flush()
m.close()
with open(TESTFN, 'rb') as f:
stuff = f.read()
self.assertEqual(stuff, b'c'*mapsize,
"Write-through memory map data file not updated properly.")
# Opening mmap with access=ACCESS_COPY
with open(TESTFN, "r+b") as f:
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
# Modifying copy-on-write memory map
m[:] = b'd'*mapsize
self.assertEqual(m[:], b'd' * mapsize,
"Copy-on-write memory map data not written correctly.")
m.flush()
with open(TESTFN, "rb") as fp:
self.assertEqual(fp.read(), b'c'*mapsize,
"Copy-on-write test data file should not be modified.")
# Ensuring copy-on-write maps cannot be resized
self.assertRaises(TypeError, m.resize, 2*mapsize)
m.close()
# Ensuring invalid access parameter raises exception
with open(TESTFN, "r+b") as f:
self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4)
if os.name == "posix":
# Try incompatible flags, prot and access parameters.
with open(TESTFN, "r+b") as f:
self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize,
flags=mmap.MAP_PRIVATE,
prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
# Try writing with PROT_EXEC and without PROT_WRITE
prot = mmap.PROT_READ | getattr(mmap, 'PROT_EXEC', 0)
with open(TESTFN, "r+b") as f:
m = mmap.mmap(f.fileno(), mapsize, prot=prot)
self.assertRaises(TypeError, m.write, b"abcdef")
self.assertRaises(TypeError, m.write_byte, 0)
m.close()
2
Example 26
Project: pyzmq Source File: setup.py
def settings_from_prefix(prefix=None, bundle_libzmq_dylib=False):
"""load appropriate library/include settings from ZMQ prefix"""
settings = {}
settings['libraries'] = []
settings['include_dirs'] = []
settings['library_dirs'] = []
settings['runtime_library_dirs'] = []
settings['extra_link_args'] = []
if sys.platform.startswith('win'):
settings['libraries'].append(libzmq_name)
if prefix:
settings['include_dirs'] += [pjoin(prefix, 'include')]
settings['library_dirs'] += [pjoin(prefix, 'lib')]
else:
# add pthread on freebsd
if sys.platform.startswith('freebsd'):
settings['libraries'].append('pthread')
if sys.platform.startswith('sunos'):
if platform.architecture()[0] == '32bit':
settings['extra_link_args'] += ['-m32']
else:
settings['extra_link_args'] += ['-m64']
if prefix:
settings['libraries'].append('zmq')
settings['include_dirs'] += [pjoin(prefix, 'include')]
if not bundle_libzmq_dylib:
if sys.platform.startswith('sunos') and platform.architecture()[0] == '64bit':
settings['library_dirs'] += [pjoin(prefix, 'lib/amd64')]
settings['library_dirs'] += [pjoin(prefix, 'lib')]
else:
# If prefix is not explicitly set, pull it from pkg-config by default.
# this is probably applicable across platforms, but i don't have
# sufficient test environments to confirm
pkgcfginfo = check_pkgconfig()
if pkgcfginfo is not None:
# we can get all the zmq-specific values from pkgconfg
for key, value in pkgcfginfo.items():
settings[key].extend(value)
else:
settings['libraries'].append('zmq')
if sys.platform == 'darwin' and os.path.isdir('/opt/local/lib'):
# allow macports default
settings['include_dirs'] += ['/opt/local/include']
settings['library_dirs'] += ['/opt/local/lib']
if os.environ.get('VIRTUAL_ENV', None):
# find libzmq installed in virtualenv
env = os.environ['VIRTUAL_ENV']
settings['include_dirs'] += [pjoin(env, 'include')]
settings['library_dirs'] += [pjoin(env, 'lib')]
if bundle_libzmq_dylib:
# bdist should link against bundled libzmq
settings['library_dirs'].append('zmq')
if sys.platform == 'darwin':
pass
# unused rpath args for OS X:
# settings['extra_link_args'] = ['-Wl,-rpath','-Wl,$ORIGIN/..']
else:
settings['runtime_library_dirs'] += ['$ORIGIN/..']
elif sys.platform != 'darwin':
info("%r" % settings)
settings['runtime_library_dirs'] += [
os.path.abspath(x) for x in settings['library_dirs']
]
return settings
2
Example 27
def _load_lib(self):
_lib = '' # os.getenv('XXX', '')
need_es2 = False
if _lib != '':
# Force a library
if sys.platform.startswith('win'):
_lib = ctypes.windll.LoadLibrary(_lib)
else:
_lib = ctypes.cdll.LoadLibrary(_lib)
elif need_es2:
# ES2 lib
es2_file = None
if 'ES2_LIBRARY' in os.environ: # todo: is this the correct name?
if os.path.exists(os.environ['ES2_LIBRARY']):
es2_file = os.path.realpath(os.environ['ES2_LIBRARY'])
if es2_file is None:
es2_file = ctypes.util.find_library('GLESv2')
if es2_file is None:
raise OSError('GL ES 2.0 library not found')
_lib = ctypes.CDLL(es2_file)
elif sys.platform.startswith('win'):
# Windows
_lib = ctypes.windll.opengl32
try:
wglGetProcAddress = _lib.wglGetProcAddress
wglGetProcAddress.restype = ctypes.CFUNCTYPE(
ctypes.POINTER(ctypes.c_int))
wglGetProcAddress.argtypes = [ctypes.c_char_p]
self._have_get_proc_address = True
except AttributeError:
pass
else:
# Unix-ish
if sys.platform.startswith('darwin'):
_fname = ctypes.util.find_library('OpenGL')
else:
_fname = ctypes.util.find_library('GL')
if not _fname:
raise RuntimeError('Could not load OpenGL library.')
# Load lib
_lib = ctypes.cdll.LoadLibrary(_fname)
self._lib = _lib
2
Example 28
Project: jhbuild Source File: environment.py
def setup_env(prefix):
'''set environment variables for using prefix'''
os.environ['JHBUILD_PREFIX'] = prefix
addpath('JHBUILD_PREFIXES', prefix)
# LD_LIBRARY_PATH
libdir = os.path.join(prefix, 'lib')
addpath('LD_LIBRARY_PATH', libdir)
os.environ['JHBUILD_LIBDIR'] = libdir
# LDFLAGS and C_INCLUDE_PATH are required for autoconf configure
# scripts to find modules that do not use pkg-config (such as guile
# looking for gmp, or wireless-tools for NetworkManager)
# (see bug #377724 and bug #545018)
# This path doesn't always get passed to addpath so we fix it here
if sys.platform.startswith('win'):
libdir = subprocess_win32.fix_path_for_msys(libdir)
os.environ['LDFLAGS'] = ('-L%s ' % libdir) + os.environ.get('LDFLAGS', '')
includedir = os.path.join(prefix, 'include')
addpath('C_INCLUDE_PATH', includedir)
addpath('OBJC_INCLUDE_PATH', includedir)
addpath('CPLUS_INCLUDE_PATH', includedir)
# On Mac OS X, we use DYLD_FALLBACK_LIBRARY_PATH
if sys.platform == 'darwin':
addpath('DYLD_FALLBACK_LIBRARY_PATH', libdir)
# PATH
bindir = os.path.join(prefix, 'bin')
addpath('PATH', bindir)
# MANPATH
manpathdir = os.path.join(prefix, 'share', 'man')
addpath('MANPATH', '')
addpath('MANPATH', manpathdir)
# Setting MANPATH on *BSD causes man to ignore its default search path,
# so we need to add the default search path to MANPATH.
if sys.platform.startswith('freebsd') or sys.platform.startswith('dragonfly'):
systemmanpath = get_output('manpath -q', extra_env={'MANPATH': ''})
systemmanpath = systemmanpath.strip().split(':')
elif sys.platform.startswith('netbsd'):
# Running 'man -p' without specifying a manual page name causes it to
# exit with status 1.
systemmanpath = get_output('man -p || true', extra_env={'MANPATH': ''})
systemmanpath = map(os.path.dirname, systemmanpath.strip().split('\n'))
elif sys.platform.startswith('openbsd'):
# I cannot find a command that prints the default search path on
# OpenBSD, so I add paths found in the default /etc/man.conf here.
systemmanpath = [ '/usr/share/man', '/usr/X11R6/man', '/usr/local/man' ]
else:
systemmanpath = []
for systemmanpathdir in systemmanpath:
addpath('MANPATH', systemmanpathdir, prepend=False)
# INFOPATH
infopathdir = os.path.join(prefix, 'share', 'info')
addpath('INFOPATH', infopathdir)
# PKG_CONFIG_PATH
pkgconfigdatadir = os.path.join(prefix, 'share', 'pkgconfig')
pkgconfigdir = os.path.join(libdir, 'pkgconfig')
addpath('PKG_CONFIG_PATH', pkgconfigdatadir)
addpath('PKG_CONFIG_PATH', pkgconfigdir)
# GI_TYPELIB_PATH
typelibpath = os.path.join(libdir, 'girepository-1.0')
addpath('GI_TYPELIB_PATH', typelibpath)
# XDG_DATA_DIRS
xdgdatadir = os.path.join(prefix, 'share')
addpath('XDG_DATA_DIRS', xdgdatadir)
# XDG_CONFIG_DIRS
xdgconfigdir = os.path.join(prefix, 'etc', 'xdg')
addpath('XDG_CONFIG_DIRS', xdgconfigdir)
# XCURSOR_PATH
xcursordir = os.path.join(prefix, 'share', 'icons')
addpath('XCURSOR_PATH', xcursordir)
# GST_PLUGIN_PATH
gstplugindir = os.path.join(libdir , 'gstreamer-0.10')
if os.path.exists(gstplugindir):
addpath('GST_PLUGIN_PATH', gstplugindir)
# GST_PLUGIN_PATH_1_0
gstplugindir = os.path.join(libdir , 'gstreamer-1.0')
if os.path.exists(gstplugindir):
addpath('GST_PLUGIN_PATH_1_0', gstplugindir)
# GST_REGISTRY
gstregistry = os.path.join(prefix, '_jhbuild', 'gstreamer-0.10.registry')
os.environ['GST_REGISTRY'] = gstregistry
# GST_REGISTRY_1_0
gstregistry = os.path.join(prefix, '_jhbuild', 'gstreamer-1.0.registry')
os.environ['GST_REGISTRY_1_0'] = gstregistry
# ACLOCAL_PATH
aclocalpath = os.path.join(prefix, 'share', 'aclocal')
addpath('ACLOCAL_PATH', aclocalpath)
# PERL5LIB
perl5lib = os.path.join(prefix, 'lib', 'perl5')
addpath('PERL5LIB', perl5lib)
# These two variables are so that people who use "jhbuild shell"
# can tweak their shell prompts and such to show "I'm under jhbuild".
# The first variable is the obvious one to look for; the second
# one is for historical reasons.
os.environ['UNDER_JHBUILD'] = 'true'
os.environ['CERTIFIED_GNOMIE'] = 'yes'
# PYTHONPATH
# We use a sitecustomize script to make sure we get the correct path
# with the various versions of python that the user may run.
if PKGDATADIR:
addpath('PYTHONPATH', os.path.join(PKGDATADIR, 'sitecustomize'))
else:
addpath('PYTHONPATH', os.path.join(SRCDIR, 'jhbuild', 'sitecustomize'))
2
Example 29
Project: exaproxy Source File: daemon.py
def __init__ (self,configuration):
self.filemax = 0
self.daemonize = configuration.daemon.daemonize
self.user = configuration.daemon.user
self.log = Logger('daemon', configuration.log.daemon)
#mask = os.umask(0137)
if configuration.web.debug:
self.log.critical('WARNING: python remote execution via the web server is enabled')
if configuration.daemon.reactor == 'epoll' and not sys.platform.startswith('linux'):
self.log.error('exaproxy.daemon.reactor can only be epoll only on Linux')
sys.exit(1)
if configuration.daemon.reactor == 'kqueue' and not sys.platform.startswith('freebsd') and not sys.platform.startswith('darwin'):
self.log.error('exaproxy.daemon.reactor can only be kqueue only on FreeBSD or OS X')
sys.exit(1)
self.nb_descriptors = 40 # some to be safe ...
self.nb_descriptors += configuration.http.connections*2 # one socket for client and server connection
self.nb_descriptors += configuration.web.connections # one socket per web client connection
self.nb_descriptors += configuration.redirector.maximum*2 # one socket per pipe to the thread and one for the forked process
self.nb_descriptors += configuration.dns.retries*10 # some sockets for the DNS
if configuration.daemon.reactor == 'select':
if self.nb_descriptors > 1024:
self.log.critical('the select reactor is not very scalable, and can only handle 1024 simultaneous descriptors')
self.log.critical('your configuration requires %d file descriptors' % self.nb_descriptors)
self.log.critical('please increase your system maximum limit, alternatively you can reduce')
self.log.critical('exaproxy.http.connections, exaproxy.web.connections and/or configuration.redirector.maximum')
self.log.critical('shutting down, we can not safely run with these settings')
sys.exit(1)
soft,hard = resource.getrlimit(resource.RLIMIT_NOFILE)
if soft < self.nb_descriptors:
try:
self.log.warning('not enough file descriptor available, increasing the limit from %d to %d' % (soft,self.nb_descriptors))
soft_limit,hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
wanted_limit = min(self.nb_descriptors, hard_limit if hard_limit > 0 else self.nb_descriptors)
# default on mac are (256,-1)
resource.setrlimit(resource.RLIMIT_NOFILE, (wanted_limit, hard_limit))
except (resource.error,ValueError),e:
self.log.warning('problem when trying to increase resource limit : %s' % str(e))
soft,hard = resource.getrlimit(resource.RLIMIT_NOFILE)
if soft < self.nb_descriptors:
self.log.critical('could not increase file descriptor limit to %d, limit is still %d' % (self.nb_descriptors,signed(soft)))
self.log.critical('on Linux you may want to increase the value in /proc/sys/fs/file-max')
self.log.critical('please increase your system maximum limit, alternatively you can reduce')
self.log.critical('exaproxy.http.connections, exaproxy.web.connections and/or configuration.redirector.maximum')
return
# on linux :
# need to get page size, and box memory, read /proc/sys/net/ipv4/tcp_mem and make sure the values are same.
# look at /proc/sys/net/ipv4/tcp_max_orphans and make sure the value is high enough for the number of FD
# > free -m
# > getconf PAGESIZE
# we should monitor and graph some of the values of /proc/net/sockstat and add it to our web page
# like TCP inuse, orphan, etc.
self.log.info('for information, your configuration requires %d available file descriptors' % self.nb_descriptors)
self.filemax = self.nb_descriptors
2
Example 30
Project: pyzmq Source File: setup.py
def bundle_libzmq_extension(self):
bundledir = "bundled"
ext_modules = self.distribution.ext_modules
if ext_modules and any(m.name == 'zmq.libzmq' for m in ext_modules):
# I've already been run
return
line()
info("Using bundled libzmq")
# fetch sources for libzmq extension:
if not os.path.exists(bundledir):
os.makedirs(bundledir)
fetch_libzmq(bundledir)
stage_platform_hpp(pjoin(bundledir, 'zeromq'))
sources = [pjoin('buildutils', 'initlibzmq.c')]
sources += glob(pjoin(bundledir, 'zeromq', 'src', '*.cpp'))
includes = [
pjoin(bundledir, 'zeromq', 'include')
]
if bundled_version < (4, 2, 0):
tweetnacl = pjoin(bundledir, 'zeromq', 'tweetnacl')
tweetnacl_sources = glob(pjoin(tweetnacl, 'src', '*.c'))
randombytes = pjoin(tweetnacl, 'contrib', 'randombytes')
if sys.platform.startswith('win'):
tweetnacl_sources.append(pjoin(randombytes, 'winrandom.c'))
else:
tweetnacl_sources.append(pjoin(randombytes, 'devurandom.c'))
sources += tweetnacl_sources
includes.append(pjoin(tweetnacl, 'src'))
includes.append(randombytes)
else:
# >= 4.2
sources += glob(pjoin(bundledir, 'zeromq', 'src', 'tweetnacl.c'))
# construct the Extensions:
libzmq = Extension(
'zmq.libzmq',
sources=sources,
include_dirs=includes,
)
# register the extension:
self.distribution.ext_modules.insert(0, libzmq)
# use tweetnacl to provide CURVE support
libzmq.define_macros.append(('ZMQ_HAVE_CURVE', 1))
libzmq.define_macros.append(('ZMQ_USE_TWEETNACL', 1))
# select polling subsystem based on platform
if sys.platform == 'darwin' or 'bsd' in sys.platform:
libzmq.define_macros.append(('ZMQ_USE_KQUEUE', 1))
elif 'linux' in sys.platform:
libzmq.define_macros.append(('ZMQ_USE_EPOLL', 1))
elif sys.platform.startswith('win'):
libzmq.define_macros.append(('ZMQ_USE_SELECT', 1))
else:
# this may not be sufficiently precise
libzmq.define_macros.append(('ZMQ_USE_POLL', 1))
if sys.platform.startswith('win'):
# include defines from zeromq msvc project:
libzmq.define_macros.append(('FD_SETSIZE', 16384))
libzmq.define_macros.append(('DLL_EXPORT', 1))
libzmq.define_macros.append(('_CRT_SECURE_NO_WARNINGS', 1))
# When compiling the C++ code inside of libzmq itself, we want to
# avoid "warning C4530: C++ exception handler used, but unwind
# semantics are not enabled. Specify /EHsc".
if self.compiler_type == 'msvc':
libzmq.extra_compile_args.append('/EHsc')
elif self.compiler_type == 'mingw32':
libzmq.define_macros.append(('ZMQ_HAVE_MINGW32', 1))
# And things like sockets come from libraries that must be named.
libzmq.libraries.extend(['rpcrt4', 'ws2_32', 'advapi32'])
# bundle MSCVP redist
if self.config['bundle_msvcp']:
cc = new_compiler(compiler=self.compiler_type)
cc.initialize()
# get vc_redist location via private API
try:
cc._vcruntime_redist
except AttributeError:
# fatal error if env set, warn otherwise
msg = fatal if os.environ.get("PYZMQ_BUNDLE_CRT") else warn
msg("Failed to get cc._vcruntime via private API, not bundling CRT")
if cc._vcruntime_redist:
redist_dir, dll = os.path.split(cc._vcruntime_redist)
to_bundle = [
pjoin(redist_dir, dll.replace('vcruntime', name))
for name in ('msvcp', 'concrt')
]
for src in to_bundle:
dest = localpath('zmq', basename(src))
info("Copying %s -> %s" % (src, dest))
# copyfile to avoid permission issues
shutil.copyfile(src, dest)
else:
libzmq.include_dirs.append(bundledir)
# check if we need to link against Realtime Extensions library
cc = new_compiler(compiler=self.compiler_type)
cc.output_dir = self.build_temp
if not sys.platform.startswith(('darwin', 'freebsd')):
line()
info("checking for timer_create")
if not cc.has_function('timer_create'):
info("no timer_create, linking librt")
libzmq.libraries.append('rt')
else:
info("ok")
if pypy:
# seem to need explicit libstdc++ on linux + pypy
# not sure why
libzmq.libraries.append("stdc++")
# copy the header files to the source tree.
bundledincludedir = pjoin('zmq', 'include')
if not os.path.exists(bundledincludedir):
os.makedirs(bundledincludedir)
if not os.path.exists(pjoin(self.build_lib, bundledincludedir)):
os.makedirs(pjoin(self.build_lib, bundledincludedir))
for header in glob(pjoin(bundledir, 'zeromq', 'include', '*.h')):
shutil.copyfile(header, pjoin(bundledincludedir, basename(header)))
shutil.copyfile(header, pjoin(self.build_lib, bundledincludedir, basename(header)))
# update other extensions, with bundled settings
self.config['libzmq_extension'] = True
self.init_settings_from_config()
self.save_config('config', self.config)
2
Example 31
Project: nupic Source File: __init__.py
def getUserDocuementsPath():
"""
Find the user's "Docuements" directory (OS X), "My Docuements" directory
(Windows), or home directory (Unix).
"""
# OS X and Windows code from:
# http://www.blueskyonmars.com/2005/08/05
# /finding-a-users-my-docuements-folder-on-windows/
# Alternate Windows code from:
# http://bugs.python.org/issue1763
if sys.platform.startswith('win'):
if sys.platform.startswith('win32'):
# Try the primary method on 32-bit windows
try:
from win32com.shell import shell
alt = False
except ImportError:
try:
import ctypes
dll = ctypes.windll.shell32
alt = True
except:
raise Exception("Could not find 'My Docuements'")
else:
# Use the alternate method on 64-bit Windows
alt = True
if not alt:
# Primary method using win32com
df = shell.SHGetDesktopFolder()
pidl = df.ParseDisplayName(0, None,
"::{450d8fba-ad25-11d0-98a8-0800361b1103}")[1]
path = shell.SHGetPathFromIDList(pidl)
else:
# Alternate method using ctypes rather than win32com
buf = ctypes.create_string_buffer(300)
dll.SHGetSpecialFolderPathA(None, buf, 0x0005, False)
path = buf.value
elif sys.platform.startswith('darwin'):
from Carbon import Folder, Folders
folderref = Folder.FSFindFolder(Folders.kUserDomain,
Folders.kDocuementsFolderType,
False)
path = folderref.as_pathname()
else:
path = os.getenv('HOME')
return path
2
Example 32
Project: cmdln Source File: which.py
def whichgen(command, path=None, verbose=0, exts=None):
"""Return a generator of full paths to the given command.
"command" is a the name of the executable to search for.
"path" is an optional alternate path list to search. The default it
to use the PATH environment variable.
"verbose", if true, will cause a 2-tuple to be returned for each
match. The second element is a textual description of where the
match was found.
"exts" optionally allows one to specify a list of extensions to use
instead of the standard list for this system. This can
effectively be used as an optimization to, for example, avoid
stat's of "foo.vbs" when searching for "foo" and you know it is
not a VisualBasic script but ".vbs" is on PATHEXT. This option
is only supported on Windows.
This method returns a generator which yields either full paths to
the given command or, if verbose, tuples of the form (<path to
command>, <where path found>).
"""
matches = []
if path is None:
usingGivenPath = 0
path = os.environ.get("PATH", "").split(os.pathsep)
if sys.platform.startswith("win"):
path.insert(0, os.curdir) # implied by Windows shell
else:
usingGivenPath = 1
# Windows has the concept of a list of extensions (PATHEXT env var).
if sys.platform.startswith("win"):
if exts is None:
exts = os.environ.get("PATHEXT", "").split(os.pathsep)
# If '.exe' is not in exts then obviously this is Win9x and
# or a bogus PATHEXT, then use a reasonable default.
for ext in exts:
if ext.lower() == ".exe":
break
else:
exts = ['.COM', '.EXE', '.BAT']
elif not isinstance(exts, list):
raise TypeError("'exts' argument must be a list or None")
else:
if exts is not None:
raise WhichError("'exts' argument is not supported on "\
"platform '%s'" % sys.platform)
exts = []
# File name cannot have path separators because PATH lookup does not
# work that way.
if os.sep in command or os.altsep and os.altsep in command:
pass
else:
for i in range(len(path)):
dirName = path[i]
# On windows the dirName *could* be quoted, drop the quotes
if sys.platform.startswith("win") and len(dirName) >= 2\
and dirName[0] == '"' and dirName[-1] == '"':
dirName = dirName[1:-1]
for ext in ['']+exts:
absName = os.path.abspath(
os.path.normpath(os.path.join(dirName, command+ext)))
if os.path.isfile(absName):
if usingGivenPath:
fromWhere = "from given path element %d" % i
elif not sys.platform.startswith("win"):
fromWhere = "from PATH element %d" % i
elif i == 0:
fromWhere = "from current directory"
else:
fromWhere = "from PATH element %d" % (i-1)
match = _cull((absName, fromWhere), matches, verbose)
if match:
if verbose:
yield match
else:
yield match[0]
match = _getRegisteredExecutable(command)
if match is not None:
match = _cull(match, matches, verbose)
if match:
if verbose:
yield match
else:
yield match[0]
2
Example 33
Project: pyzmq Source File: setup.py
def run(self):
cfg = self.config
if cfg['libzmq_extension']:
self.bundle_libzmq_extension()
self.finish_run()
return
# When cross-compiling and zmq is given explicitly, we can't testbuild
# (as we can't testrun the binary), we assume things are alright.
if cfg['skip_check_zmq'] or self.cross_compiling:
warn("Skipping zmq version check")
self.finish_run()
return
zmq_prefix = cfg['zmq_prefix']
# There is no available default on Windows, so start with fallback unless
# zmq was given explicitly, or libzmq extension was explicitly prohibited.
if sys.platform.startswith("win") and \
not cfg['no_libzmq_extension'] and \
not zmq_prefix:
self.fallback_on_bundled()
self.finish_run()
return
if zmq_prefix and self.bundle_libzmq_dylib and not sys.platform.startswith('win'):
copy_and_patch_libzmq(zmq_prefix, libzmq_name+lib_ext)
# first try with given config or defaults
try:
self.check_zmq_version()
except LibZMQVersionError as e:
info("\nBad libzmq version: %s\n" % e)
except Exception as e:
# print the error as distutils would if we let it raise:
info("\nerror: %s\n" % e)
else:
self.finish_run()
return
# try fallback on /usr/local on *ix if no prefix is given
if not zmq_prefix and not sys.platform.startswith('win'):
info("Failed with default libzmq, trying again with /usr/local")
time.sleep(1)
zmq_prefix = cfg['zmq_prefix'] = '/usr/local'
self.init_settings_from_config()
try:
self.check_zmq_version()
except LibZMQVersionError as e:
info("\nBad libzmq version: %s\n" % e)
except Exception as e:
# print the error as distutils would if we let it raise:
info("\nerror: %s\n" % e)
else:
# if we get here the second run succeeded, so we need to update compiler
# settings for the extensions with /usr/local prefix
self.finish_run()
return
# finally, fallback on bundled
if cfg['no_libzmq_extension']:
fatal("Falling back on bundled libzmq,"
" but config has explicitly prohibited building the libzmq extension."
)
self.fallback_on_bundled()
self.finish_run()
0
Example 34
Project: tpot Source File: decorators.py
def _timeout(func):
"""Runs a function with time limit
Parameters
----------
time_minute: int
Time limit in minutes
func: Python function
Function to run
args: tuple
Function args
kw: dict
Function keywords
Returns
-------
limitedTime: function
Wrapped function that raises a timeout exception if the time limit is exceeded
"""
def convert_mins_to_secs(time_minute):
"""Convert time from minutes to seconds"""
second = int(time_minute * 60)
# time limit should be at least 1 second
return max(second, 1)
class TIMEOUT(RuntimeError):
"""
Inhertis from RuntimeError
"""
pass
def timeout_signal_handler(signum, frame):
"""
signal handler for _timeout function
rasie TIMEOUT exception
"""
raise TIMEOUT("Time Out!")
if not sys.platform.startswith('win'):
from signal import SIGXCPU, signal, getsignal
from resource import getrlimit, setrlimit, RLIMIT_CPU, getrusage, RUSAGE_SELF
# timeout uses the CPU time
@wraps(func)
def limitedTime(self,*args, **kw):
# don't show traceback
sys.tracebacklimit=0
# save old signal
old_signal_hander = getsignal(SIGXCPU)
# change signal
signal(SIGXCPU, timeout_signal_handler)
max_time_second = convert_mins_to_secs(self.max_eval_time_mins)
r = getrusage(RUSAGE_SELF)
cpu_time = r.ru_utime + r.ru_stime
current = getrlimit(RLIMIT_CPU)
try:
setrlimit(RLIMIT_CPU, (cpu_time+max_time_second, current[1]))
ret = func(*args, **kw)
except RuntimeError:
if self.verbosity > 1:
self._pbar.write('Timeout during evaluation of pipeline #{0}. Skipping to the next pipeline.'.format(self._pbar.n + 1))
ret = None
finally:
# reset cpu time limit and trackback
setrlimit(RLIMIT_CPU, current)
sys.tracebacklimit=1000
# reset signal
signal(SIGXCPU, old_signal_hander)
return ret
else:
from threading import Thread, current_thread
class InterruptableThread(Thread):
def __init__(self, args, kwargs):
Thread.__init__(self)
self.args = args
self.kwargs = kwargs
self.result = None
self.daemon = True
def stop(self):
self._stop()
def run(self):
try:
# Note: changed name of the thread to "MainThread" to avoid such warning from joblib (maybe bugs)
# Note: Need attention if using parallel execution model of scikit-learn
current_thread().name = 'MainThread'
self.result = func(*self.args, **self.kwargs)
except Exception:
pass
@wraps(func)
def limitedTime(self, *args, **kw):
sys.tracebacklimit = 0
max_time_seconds = convert_mins_to_secs(self.max_eval_time_mins)
# start thread
tmp_it = InterruptableThread(args, kw)
tmp_it.start()
#timer = Timer(max_time_seconds, interrupt_main)
tmp_it.join(max_time_seconds)
if tmp_it.isAlive():
if self.verbosity > 1:
self._pbar.write('Timeout during evaluation of pipeline #{0}. Skipping to the next pipeline.'.format(self._pbar.n + 1))
sys.tracebacklimit=1000
return tmp_it.result
tmp_it.stop()
# return func
return limitedTime
0
Example 35
def main():
# parse command-line args
parser = argparse.ArgumentParser(description="Automatically generates config files for YouCompleteMe")
parser.add_argument("-v", "--verbose", action="store_true", help="Show output from build process")
parser.add_argument("-f", "--force", action="store_true", help="Overwrite the file if it exists.")
parser.add_argument("-m", "--make", default="make", help="Use the specified executable for make.")
parser.add_argument("-b", "--build-system", choices=["cmake", "autotools", "qmake", "make"], help="Force use of the specified build system rather than trying to autodetect.")
parser.add_argument("-c", "--compiler", help="Use the specified executable for clang. It should be the same version as the libclang used by YCM. The executable for clang++ will be inferred from this.")
parser.add_argument("-C", "--configure_opts", default="", help="Additional flags to pass to configure/cmake/etc. e.g. --configure_opts=\"--enable-FEATURE\"")
parser.add_argument("-F", "--format", choices=["ycm", "cc"], default="ycm", help="Format of output file (YouCompleteMe or color_coded). Default: ycm")
parser.add_argument("-M", "--make-flags", help="Flags to pass to make when fake-building. Default: -M=\"{}\"".format(" ".join(default_make_flags)))
parser.add_argument("-o", "--output", help="Save the config file as OUTPUT. Default: .ycm_extra_conf.py, or .color_coded if --format=cc.")
parser.add_argument("-x", "--language", choices=["c", "c++"], help="Only output flags for the given language. This defaults to whichever language has its compiler invoked the most.")
parser.add_argument("--out-of-tree", action="store_true", help="Build autotools projects out-of-tree. This is a no-op for other project types.")
parser.add_argument("--qt-version", choices=["4", "5"], default="5", help="Use the given Qt version for qmake. (Default: 5)")
parser.add_argument("-e", "--preserve-environment", action="store_true", help="Pass environment variables to build processes.")
parser.add_argument("PROJECT_DIR", help="The root directory of the project.")
args = vars(parser.parse_args())
project_dir = os.path.abspath(args["PROJECT_DIR"])
# verify that project_dir exists
if(not os.path.exists(project_dir)):
print("ERROR: '{}' does not exist".format(project_dir))
return 1
# verify the clang is installed, and infer the correct name for both the C and C++ compilers
try:
cc = args["compiler"] or "clang"
args["cc"] = subprocess.check_output(["which", cc]).strip()
except subprocess.CalledProcessError:
print("ERROR: Could not find clang at '{}'. Please make sure it is installed and is either in your path, or specified with --compiler.".format(cc))
return 1
try:
h, t = os.path.split(args["compiler"] or "clang")
cxx = os.path.join(h, t.replace("clang", "clang++"))
args["cxx"] = subprocess.check_output(["which", cxx]).strip()
except subprocess.CalledProcessError:
print("ERROR: Could not find clang++ at '{}'. Please make sure it is installed and specified appropriately.".format(cxx))
return 1
# sanity check - remove this after we add Windows support
if(sys.platform.startswith("win32")):
print("ERROR: Windows is not supported")
# prompt user to overwrite existing file (if necessary)
config_file = {
None: args["output"],
"cc": os.path.join(project_dir, ".color_coded"),
"ycm": os.path.join(project_dir, ".ycm_extra_conf.py"),
}[args["format"] if args["output"] is None else None]
if(os.path.exists(config_file) and not args["force"]):
print("'{}' already exists. Overwrite? [y/N] ".format(config_file)),
response = sys.stdin.readline().strip().lower()
if(response != "y" and response != "yes"):
return 1
# command-line args to pass to fake_build() using kwargs
args["make_cmd"] = args.pop("make")
args["configure_opts"] = shlex.split(args["configure_opts"])
args["make_flags"] = default_make_flags if args["make_flags"] is None else shlex.split(args["make_flags"])
force_lang = args.pop("language")
output_format = args.pop("format")
del args["compiler"]
del args["force"]
del args["output"]
del args["PROJECT_DIR"]
generate_conf = {
"ycm": generate_ycm_conf,
"cc": generate_cc_conf,
}[output_format]
# temporary files to hold build logs
with tempfile.NamedTemporaryFile(mode="rw") as c_build_log:
with tempfile.NamedTemporaryFile(mode="rw") as cxx_build_log:
# perform the actual compilation of flags
fake_build(project_dir, c_build_log.name, cxx_build_log.name, **args)
(c_count, c_skip, c_flags) = parse_flags(c_build_log)
(cxx_count, cxx_skip, cxx_flags) = parse_flags(cxx_build_log)
print("Collected {} relevant entries for C compilation ({} discarded).".format(c_count, c_skip))
print("Collected {} relevant entries for C++ compilation ({} discarded).".format(cxx_count, cxx_skip))
# select the language to compile for. If -x was used, zero all other options (so we don't need to repeat the error code)
if(force_lang == "c"):
cxx_count = 0
elif(force_lang == "c++"):
c_count = 0
if(c_count == 0 and cxx_count == 0):
print("")
print("ERROR: No commands were logged to the build logs (C: {}, C++: {}).".format(c_build_log.name, cxx_build_log.name))
print("Your build system may not be compatible.")
if(not args["verbose"]):
print("")
print("Try running with the --verbose flag to see build system output - the most common cause of this is a hardcoded compiler path.")
c_build_log.delete = False
cxx_build_log.delete = False
return 3
elif(c_count > cxx_count):
lang, flags = ("c", c_flags)
else:
lang, flags = ("c++", cxx_flags)
generate_conf(["-x", lang] + flags, config_file)
print("Created {} config file with {} {} flags".format(output_format.upper(), len(flags), lang.upper()))
0
Example 36
def get_config_vars(*args):
"""With no arguments, return a dictionary of all configuration
variables relevant for the current platform. Generally this includes
everything needed to build extensions and install both pure modules and
extensions. On Unix, this means every variable defined in Python's
installed Makefile; on Windows and Mac OS it's a much smaller set.
With arguments, return a list of values that result from looking up
each argument in the configuration variable dictionary.
"""
global _config_vars
if _config_vars is None:
if sys.platform.startswith('java'):
# Jython might pose as a different os.name, but we always
# want _init_jython regardless
func = _init_jython
else:
func = globals().get("_init_" + os.name)
if func:
func()
else:
_config_vars = {}
# Normalized versions of prefix and exec_prefix are handy to have;
# in fact, these are the standard versions used most places in the
# Distutils.
_config_vars['prefix'] = PREFIX
_config_vars['exec_prefix'] = EXEC_PREFIX
if sys.platform == 'darwin':
kernel_version = os.uname()[2] # Kernel version (8.4.3)
major_version = int(kernel_version.split('.')[0])
if major_version < 8:
# On Mac OS X before 10.4, check if -arch and -isysroot
# are in CFLAGS or LDFLAGS and remove them if they are.
# This is needed when building extensions on a 10.3 system
# using a universal build of python.
for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
# a number of derived variables. These need to be
# patched up as well.
'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
flags = _config_vars[key]
flags = re.sub('-arch\s+\w+\s', ' ', flags)
flags = re.sub('-isysroot [^ \t]*', ' ', flags)
_config_vars[key] = flags
else:
# Allow the user to override the architecture flags using
# an environment variable.
# NOTE: This name was introduced by Apple in OSX 10.5 and
# is used by several scripting languages distributed with
# that OS release.
if 'ARCHFLAGS' in os.environ:
arch = os.environ['ARCHFLAGS']
for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
# a number of derived variables. These need to be
# patched up as well.
'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
flags = _config_vars[key]
flags = re.sub('-arch\s+\w+\s', ' ', flags)
flags = flags + ' ' + arch
_config_vars[key] = flags
# If we're on OSX 10.5 or later and the user tries to
# compiles an extension using an SDK that is not present
# on the current machine it is better to not use an SDK
# than to fail.
#
# The major usecase for this is users using a Python.org
# binary installer on OSX 10.6: that installer uses
# the 10.4u SDK, but that SDK is not installed by default
# when you install Xcode.
#
m = re.search('-isysroot\s+(\S+)', _config_vars['CFLAGS'])
if m is not None:
sdk = m.group(1)
if not os.path.exists(sdk):
for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
# a number of derived variables. These need to be
# patched up as well.
'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
flags = _config_vars[key]
flags = re.sub('-isysroot\s+\S+(\s|$)', ' ', flags)
_config_vars[key] = flags
if args:
vals = []
for name in args:
vals.append(_config_vars.get(name))
return vals
else:
return _config_vars
0
Example 37
Project: pymo Source File: test_kqueue.py
def test_queue_event(self):
serverSocket = socket.socket()
serverSocket.bind(('127.0.0.1', 0))
serverSocket.listen(1)
client = socket.socket()
client.setblocking(False)
try:
client.connect(('127.0.0.1', serverSocket.getsockname()[1]))
except socket.error, e:
self.assertEqual(e.args[0], errno.EINPROGRESS)
else:
#raise AssertionError("Connect should have raised EINPROGRESS")
pass # FreeBSD doesn't raise an exception here
server, addr = serverSocket.accept()
if sys.platform.startswith("darwin"):
flags = select.KQ_EV_ADD | select.KQ_EV_ENABLE
else:
flags = 0
kq = select.kqueue()
kq2 = select.kqueue.fromfd(kq.fileno())
ev = select.kevent(server.fileno(),
select.KQ_FILTER_WRITE,
select.KQ_EV_ADD | select.KQ_EV_ENABLE)
kq.control([ev], 0)
ev = select.kevent(server.fileno(),
select.KQ_FILTER_READ,
select.KQ_EV_ADD | select.KQ_EV_ENABLE)
kq.control([ev], 0)
ev = select.kevent(client.fileno(),
select.KQ_FILTER_WRITE,
select.KQ_EV_ADD | select.KQ_EV_ENABLE)
kq2.control([ev], 0)
ev = select.kevent(client.fileno(),
select.KQ_FILTER_READ,
select.KQ_EV_ADD | select.KQ_EV_ENABLE)
kq2.control([ev], 0)
events = kq.control(None, 4, 1)
events = [(e.ident, e.filter, e.flags) for e in events]
events.sort()
self.assertEqual(events, [
(client.fileno(), select.KQ_FILTER_WRITE, flags),
(server.fileno(), select.KQ_FILTER_WRITE, flags)])
client.send("Hello!")
server.send("world!!!")
# We may need to call it several times
for i in range(10):
events = kq.control(None, 4, 1)
if len(events) == 4:
break
time.sleep(1.0)
else:
self.fail('timeout waiting for event notifications')
events = [(e.ident, e.filter, e.flags) for e in events]
events.sort()
self.assertEqual(events, [
(client.fileno(), select.KQ_FILTER_WRITE, flags),
(client.fileno(), select.KQ_FILTER_READ, flags),
(server.fileno(), select.KQ_FILTER_WRITE, flags),
(server.fileno(), select.KQ_FILTER_READ, flags)])
# Remove completely client, and server read part
ev = select.kevent(client.fileno(),
select.KQ_FILTER_WRITE,
select.KQ_EV_DELETE)
kq.control([ev], 0)
ev = select.kevent(client.fileno(),
select.KQ_FILTER_READ,
select.KQ_EV_DELETE)
kq.control([ev], 0)
ev = select.kevent(server.fileno(),
select.KQ_FILTER_READ,
select.KQ_EV_DELETE)
kq.control([ev], 0, 0)
events = kq.control([], 4, 0.99)
events = [(e.ident, e.filter, e.flags) for e in events]
events.sort()
self.assertEqual(events, [
(server.fileno(), select.KQ_FILTER_WRITE, flags)])
client.close()
server.close()
serverSocket.close()
0
Example 38
Project: YCM-Generator Source File: config_gen.py
def fake_build(project_dir, c_build_log_path, cxx_build_log_path, verbose, make_cmd, build_system, cc, cxx, out_of_tree, configure_opts, make_flags, preserve_environment, qt_version):
'''Builds the project using the fake toolchain, to collect the compiler flags.
project_dir: the directory containing the source files
build_log_path: the file to log commands to
verbose: show the build process output
make_cmd: the path of the make executable
cc: the path of the clang executable
cxx: the path of the clang++ executable
out_of_tree: perform an out-of-tree build (autotools only)
configure_opts: additional flags for configure stage
make_flags: additional flags for make
preserve_environment: pass environment variables to build processes
qt_version: The Qt version to use when building with qmake.
'''
# TODO: add Windows support
assert(not sys.platform.startswith("win32"))
fake_path = os.path.join(ycm_generator_dir, "fake-toolchain", "Unix")
# environment variables and arguments for build process
started = time.time()
FNULL = open(os.devnull, "w")
proc_opts = {} if verbose else {
"stdin": FNULL,
"stdout": FNULL,
"stderr": FNULL
}
proc_opts["cwd"] = project_dir
if(preserve_environment):
env = os.environ
else:
# Preserve HOME, since Cmake needs it to find some packages and it's
# normally there anyway. See #26.
env = dict(map(lambda x: (x, os.environ[x]), ["HOME"]))
env["PATH"] = "{}:{}".format(fake_path, os.environ["PATH"])
env["CC"] = "clang"
env["CXX"] = "clang++"
env["YCM_CONFIG_GEN_CC_LOG"] = c_build_log_path
env["YCM_CONFIG_GEN_CXX_LOG"] = cxx_build_log_path
# used during configuration stage, so that cmake, etc. can verify what the compiler supports
env_config = env.copy()
env_config["YCM_CONFIG_GEN_CC_PASSTHROUGH"] = cc
env_config["YCM_CONFIG_GEN_CXX_PASSTHROUGH"] = cxx
# use -i (ignore errors), since the makefile may include scripts which
# depend upon the existence of various output files
make_args = [make_cmd] + make_flags
# Used for the qmake build system below
pro_files = glob.glob(os.path.join(project_dir, "*.pro"))
# sanity check - make sure the toolchain is available
assert os.path.exists(fake_path), "Could not find toolchain at '{}'".format(fake_path)
# helper function to display exact commands used
def run(cmd, *args, **kwargs):
print("$ " + " ".join(cmd))
subprocess.call(cmd, *args, **kwargs)
if build_system is None:
if os.path.exists(os.path.join(project_dir, "CMakeLists.txt")):
build_system = "cmake"
elif os.path.exists(os.path.join(project_dir, "configure")):
build_system = "autotools"
elif pro_files:
build_system = "qmake"
elif any([os.path.exists(os.path.join(project_dir, x)) for x in ["GNUmakefile", "makefile", "Makefile"]]):
build_system = "make"
# execute the build system
if build_system == "cmake":
# cmake
# run cmake in a temporary directory, then compile the project as usual
build_dir = tempfile.mkdtemp()
proc_opts["cwd"] = build_dir
# if the project was built in-tree, we need to hide the cache file so that cmake
# populates the build dir instead of just re-generating the existing files
cache_path = os.path.join(project_dir, "CMakeCache.txt")
if(os.path.exists(cache_path)):
fd, cache_tmp = tempfile.mkstemp()
os.close(fd)
shutil.move(cache_path, cache_tmp)
else:
cache_tmp = None
print("Running cmake in '{}'...".format(build_dir))
sys.stdout.flush()
run(["cmake", project_dir] + configure_opts, env=env_config, **proc_opts)
print("\nRunning make...")
sys.stdout.flush()
run(make_args, env=env, **proc_opts)
print("\nCleaning up...")
print("")
sys.stdout.flush()
shutil.rmtree(build_dir)
if(cache_tmp):
shutil.move(cache_tmp, cache_path)
elif build_system == "autotools":
# autotools
# perform build in-tree, since not all projects handle out-of-tree builds correctly
if(out_of_tree):
build_dir = tempfile.mkdtemp()
proc_opts["cwd"] = build_dir
print("Configuring autotools in '{}'...".format(build_dir))
else:
print("Configuring autotools...")
run([os.path.join(project_dir, "configure")] + configure_opts, env=env_config, **proc_opts)
print("\nRunning make...")
run(make_args, env=env, **proc_opts)
print("\nCleaning up...")
if(out_of_tree):
print("")
shutil.rmtree(build_dir)
else:
run([make_cmd, "maintainer-clean"], env=env, **proc_opts)
elif build_system == "qmake":
# qmake
# make sure there is only one .pro file
if len(pro_files) != 1:
print("ERROR: Found {} .pro files (expected one): {}.".format(
len(pro_files), ', '.join(pro_files)))
sys.exit(1)
# run qmake in a temporary directory, then compile the project as usual
build_dir = tempfile.mkdtemp()
proc_opts["cwd"] = build_dir
env_config["QT_SELECT"] = qt_version
# QMAKESPEC is platform dependent - valid mkspecs are in
# /usr/share/qt4/mkspecs, /usr/lib64/qt5/mkspecs
env_config["QMAKESPEC"] = {
("Linux", True): "unsupported/linux-clang",
("Linux", False): "linux-clang",
("Darwin", True): "unsupported/macx-clang",
("Darwin", False): "macx-clang",
("FreeBSD", False): "unsupported/freebsd-clang",
}[(os.uname()[0], qt_version == "4")]
print("Running qmake in '{}' with Qt {}...".format(build_dir, qt_version))
run(["qmake"] + configure_opts + [pro_files[0]], env=env_config,
**proc_opts)
print("\nRunning make...")
run(make_args, env=env, **proc_opts)
print("\nCleaning up...")
print("")
shutil.rmtree(build_dir)
elif build_system == "make":
# make
# needs to be handled last, since other build systems can generate Makefiles
print("Preparing build directory...")
run([make_cmd, "clean"], env=env, **proc_opts)
print("\nRunning make...")
run(make_args, env=env, **proc_opts)
elif(os.path.exists(os.path.join(project_dir, "Make/options"))):
print("Found OpenFOAM Make/options")
# OpenFOAM build system
make_args = ["wmake"]
# Since icpc could not find directory in which g++ resides,
# set environmental variables to gcc to make fake_build operate normally.
env['WM_COMPILER']='Gcc'
env['WM_CC']='gcc'
env['WM_CXX']='g++'
print("\nRunning wmake...")
run(make_args, env=env, **proc_opts)
else:
print("ERROR: Unknown build system")
sys.exit(2)
print("Build completed in {} sec".format(round(time.time() - started, 2)))
print("")
0
Example 39
Project: imagrium Source File: config.py
def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
"""
Start up a socket server on the specified port, and listen for new
configurations.
These will be sent as a file suitable for processing by fileConfig().
Returns a Thread object on which you can call start() to start the server,
and which you can join() when appropriate. To stop the server, call
stopListening().
"""
if not thread:
raise NotImplementedError("listen() needs threading to work")
class ConfigStreamHandler(StreamRequestHandler):
"""
Handler for a logging configuration request.
It expects a completely new logging configuration and uses fileConfig
to install it.
"""
def handle(self):
"""
Handle a request.
Each request is expected to be a 4-byte length, packed using
struct.pack(">L", n), followed by the config file.
Uses fileConfig() to do the grunt work.
"""
import tempfile
try:
conn = self.connection
chunk = conn.recv(4)
if len(chunk) == 4:
slen = struct.unpack(">L", chunk)[0]
chunk = self.connection.recv(slen)
while len(chunk) < slen:
chunk = chunk + conn.recv(slen - len(chunk))
try:
import json
d =json.loads(chunk)
assert isinstance(d, dict)
dictConfig(d)
except:
#Apply new configuration.
file = cStringIO.StringIO(chunk)
try:
fileConfig(file)
except (KeyboardInterrupt, SystemExit):
raise
except:
traceback.print_exc()
if self.server.ready:
self.server.ready.set()
except socket.error, e:
if not isinstance(e.args, tuple):
raise
else:
errcode = e.args[0]
if errcode != RESET_ERROR:
raise
class ConfigSocketReceiver(ThreadingTCPServer):
"""
A simple TCP socket-based logging config receiver.
"""
allow_reuse_address = 1
def __init__(self, host='localhost', port=DEFAULT_LOGGING_CONFIG_PORT,
handler=None, ready=None):
ThreadingTCPServer.__init__(self, (host, port), handler)
logging._acquireLock()
self.abort = 0
logging._releaseLock()
self.timeout = 1
self.ready = ready
def serve_until_stopped(self):
if sys.platform.startswith('java'):
from select import cpython_compatible_select as select
else:
from select import select
abort = 0
while not abort:
rd, wr, ex = select([self.socket.fileno()],
[], [],
self.timeout)
if rd:
self.handle_request()
logging._acquireLock()
abort = self.abort
logging._releaseLock()
self.socket.close()
class Server(threading.Thread):
def __init__(self, rcvr, hdlr, port):
super(Server, self).__init__()
self.rcvr = rcvr
self.hdlr = hdlr
self.port = port
self.ready = threading.Event()
def run(self):
server = self.rcvr(port=self.port, handler=self.hdlr,
ready=self.ready)
if self.port == 0:
self.port = server.server_address[1]
self.ready.set()
global _listener
logging._acquireLock()
_listener = server
logging._releaseLock()
server.serve_until_stopped()
return Server(ConfigSocketReceiver, ConfigStreamHandler, port)
0
Example 40
Project: pymo Source File: py2_test_grammar.py
def testFuncdef(self):
### 'def' NAME parameters ':' suite
### parameters: '(' [varargslist] ')'
### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
### | ('**'|'*' '*') NAME)
### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
### fpdef: NAME | '(' fplist ')'
### fplist: fpdef (',' fpdef)* [',']
### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
### argument: [test '='] test # Really [keyword '='] test
def f1(): pass
f1()
f1(*())
f1(*(), **{})
def f2(one_argument): pass
def f3(two, arguments): pass
def f4(two, (compound, (argument, list))): pass
def f5((compound, first), two): pass
self.assertEquals(f2.func_code.co_varnames, ('one_argument',))
self.assertEquals(f3.func_code.co_varnames, ('two', 'arguments'))
if sys.platform.startswith('java'):
self.assertEquals(f4.func_code.co_varnames,
('two', '(compound, (argument, list))', 'compound', 'argument',
'list',))
self.assertEquals(f5.func_code.co_varnames,
('(compound, first)', 'two', 'compound', 'first'))
else:
self.assertEquals(f4.func_code.co_varnames,
('two', '.1', 'compound', 'argument', 'list'))
self.assertEquals(f5.func_code.co_varnames,
('.0', 'two', 'compound', 'first'))
def a1(one_arg,): pass
def a2(two, args,): pass
def v0(*rest): pass
def v1(a, *rest): pass
def v2(a, b, *rest): pass
def v3(a, (b, c), *rest): return a, b, c, rest
f1()
f2(1)
f2(1,)
f3(1, 2)
f3(1, 2,)
f4(1, (2, (3, 4)))
v0()
v0(1)
v0(1,)
v0(1,2)
v0(1,2,3,4,5,6,7,8,9,0)
v1(1)
v1(1,)
v1(1,2)
v1(1,2,3)
v1(1,2,3,4,5,6,7,8,9,0)
v2(1,2)
v2(1,2,3)
v2(1,2,3,4)
v2(1,2,3,4,5,6,7,8,9,0)
v3(1,(2,3))
v3(1,(2,3),4)
v3(1,(2,3),4,5,6,7,8,9,0)
# ceval unpacks the formal arguments into the first argcount names;
# thus, the names nested inside tuples must appear after these names.
if sys.platform.startswith('java'):
self.assertEquals(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))
else:
self.assertEquals(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
self.assertEquals(v3(1, (2, 3), 4), (1, 2, 3, (4,)))
def d01(a=1): pass
d01()
d01(1)
d01(*(1,))
d01(**{'a':2})
def d11(a, b=1): pass
d11(1)
d11(1, 2)
d11(1, **{'b':2})
def d21(a, b, c=1): pass
d21(1, 2)
d21(1, 2, 3)
d21(*(1, 2, 3))
d21(1, *(2, 3))
d21(1, 2, *(3,))
d21(1, 2, **{'c':3})
def d02(a=1, b=2): pass
d02()
d02(1)
d02(1, 2)
d02(*(1, 2))
d02(1, *(2,))
d02(1, **{'b':2})
d02(**{'a': 1, 'b': 2})
def d12(a, b=1, c=2): pass
d12(1)
d12(1, 2)
d12(1, 2, 3)
def d22(a, b, c=1, d=2): pass
d22(1, 2)
d22(1, 2, 3)
d22(1, 2, 3, 4)
def d01v(a=1, *rest): pass
d01v()
d01v(1)
d01v(1, 2)
d01v(*(1, 2, 3, 4))
d01v(*(1,))
d01v(**{'a':2})
def d11v(a, b=1, *rest): pass
d11v(1)
d11v(1, 2)
d11v(1, 2, 3)
def d21v(a, b, c=1, *rest): pass
d21v(1, 2)
d21v(1, 2, 3)
d21v(1, 2, 3, 4)
d21v(*(1, 2, 3, 4))
d21v(1, 2, **{'c': 3})
def d02v(a=1, b=2, *rest): pass
d02v()
d02v(1)
d02v(1, 2)
d02v(1, 2, 3)
d02v(1, *(2, 3, 4))
d02v(**{'a': 1, 'b': 2})
def d12v(a, b=1, c=2, *rest): pass
d12v(1)
d12v(1, 2)
d12v(1, 2, 3)
d12v(1, 2, 3, 4)
d12v(*(1, 2, 3, 4))
d12v(1, 2, *(3, 4, 5))
d12v(1, *(2,), **{'c': 3})
def d22v(a, b, c=1, d=2, *rest): pass
d22v(1, 2)
d22v(1, 2, 3)
d22v(1, 2, 3, 4)
d22v(1, 2, 3, 4, 5)
d22v(*(1, 2, 3, 4))
d22v(1, 2, *(3, 4, 5))
d22v(1, *(2, 3), **{'d': 4})
def d31v((x)): pass
d31v(1)
def d32v((x,)): pass
d32v((1,))
# keyword arguments after *arglist
def f(*args, **kwargs):
return args, kwargs
self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
{'x':2, 'y':5}))
self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
# Check ast errors in *args and *kwargs
check_syntax_error(self, "f(*g(1=2))")
check_syntax_error(self, "f(**g(1=2))")
0
Example 41
Project: retext Source File: window.py
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.resize(950, 700)
screenRect = QDesktopWidget().screenGeometry()
if globalSettings.windowGeometry:
self.restoreGeometry(globalSettings.windowGeometry)
else:
self.move((screenRect.width()-self.width())/2, (screenRect.height()-self.height())/2)
if not screenRect.contains(self.geometry()):
self.showMaximized()
if sys.platform.startswith('darwin'):
# https://github.com/retext-project/retext/issues/198
searchPaths = QIcon.themeSearchPaths()
searchPaths.append('/opt/local/share/icons')
QIcon.setThemeSearchPaths(searchPaths)
if globalSettings.iconTheme:
QIcon.setThemeName(globalSettings.iconTheme)
if QIcon.themeName() in ('hicolor', ''):
if not QFile.exists(icon_path + 'docuement-new.png'):
QIcon.setThemeName(get_icon_theme())
if QFile.exists(icon_path+'retext.png'):
self.setWindowIcon(QIcon(icon_path+'retext.png'))
elif QFile.exists('/usr/share/pixmaps/retext.png'):
self.setWindowIcon(QIcon('/usr/share/pixmaps/retext.png'))
else:
self.setWindowIcon(QIcon.fromTheme('retext',
QIcon.fromTheme('accessories-text-editor')))
self.tabWidget = QTabWidget(self)
self.initTabWidget()
self.setCentralWidget(self.tabWidget)
self.tabWidget.currentChanged.connect(self.changeIndex)
self.tabWidget.tabCloseRequested.connect(self.closeTab)
toolBar = QToolBar(self.tr('File toolbar'), self)
self.addToolBar(Qt.TopToolBarArea, toolBar)
self.editBar = QToolBar(self.tr('Edit toolbar'), self)
self.addToolBar(Qt.TopToolBarArea, self.editBar)
self.searchBar = QToolBar(self.tr('Search toolbar'), self)
self.addToolBar(Qt.BottomToolBarArea, self.searchBar)
toolBar.setVisible(not globalSettings.hideToolBar)
self.editBar.setVisible(not globalSettings.hideToolBar)
self.actionNew = self.act(self.tr('New'), 'docuement-new',
self.createNew, shct=QKeySequence.New)
self.actionNew.setPriority(QAction.LowPriority)
self.actionOpen = self.act(self.tr('Open'), 'docuement-open',
self.openFile, shct=QKeySequence.Open)
self.actionOpen.setPriority(QAction.LowPriority)
self.actionSetEncoding = self.act(self.tr('Set encoding'),
trig=self.showEncodingDialog)
self.actionSetEncoding.setEnabled(False)
self.actionReload = self.act(self.tr('Reload'), 'view-refresh',
lambda: self.currentTab.readTextFromFile())
self.actionReload.setEnabled(False)
self.actionSave = self.act(self.tr('Save'), 'docuement-save',
self.saveFile, shct=QKeySequence.Save)
self.actionSave.setEnabled(False)
self.actionSave.setPriority(QAction.LowPriority)
self.actionSaveAs = self.act(self.tr('Save as'), 'docuement-save-as',
self.saveFileAs, shct=QKeySequence.SaveAs)
self.actionNextTab = self.act(self.tr('Next tab'), 'go-next',
lambda: self.switchTab(1), shct=Qt.CTRL+Qt.Key_PageDown)
self.actionPrevTab = self.act(self.tr('Previous tab'), 'go-previous',
lambda: self.switchTab(-1), shct=Qt.CTRL+Qt.Key_PageUp)
self.actionPrint = self.act(self.tr('Print'), 'docuement-print',
self.printFile, shct=QKeySequence.Print)
self.actionPrint.setPriority(QAction.LowPriority)
self.actionPrintPreview = self.act(self.tr('Print preview'), 'docuement-print-preview',
self.printPreview)
self.actionViewHtml = self.act(self.tr('View HTML code'), 'text-html', self.viewHtml)
self.actionChangeEditorFont = self.act(self.tr('Change editor font'),
trig=self.changeEditorFont)
self.actionChangePreviewFont = self.act(self.tr('Change preview font'),
trig=self.changePreviewFont)
self.actionSearch = self.act(self.tr('Find text'), 'edit-find', shct=QKeySequence.Find)
self.actionSearch.setCheckable(True)
self.actionSearch.triggered[bool].connect(self.searchBar.setVisible)
self.searchBar.visibilityChanged.connect(self.searchBarVisibilityChanged)
self.actionPreview = self.act(self.tr('Preview'), shct=Qt.CTRL+Qt.Key_E,
trigbool=self.preview)
if QIcon.hasThemeIcon('docuement-preview'):
self.actionPreview.setIcon(QIcon.fromTheme('docuement-preview'))
elif QIcon.hasThemeIcon('preview-file'):
self.actionPreview.setIcon(QIcon.fromTheme('preview-file'))
elif QIcon.hasThemeIcon('x-office-docuement'):
self.actionPreview.setIcon(QIcon.fromTheme('x-office-docuement'))
else:
self.actionPreview.setIcon(QIcon(icon_path+'docuement-preview.png'))
self.actionLivePreview = self.act(self.tr('Live preview'), shct=Qt.CTRL+Qt.Key_L,
trigbool=self.enableLivePreview)
menuPreview = QMenu()
menuPreview.addAction(self.actionLivePreview)
self.actionPreview.setMenu(menuPreview)
self.actionTableMode = self.act(self.tr('Table editing mode'),
shct=Qt.CTRL+Qt.Key_T,
trigbool=lambda x: self.currentTab.editBox.enableTableMode(x))
if ReTextFakeVimHandler:
self.actionFakeVimMode = self.act(self.tr('FakeVim mode'),
shct=Qt.CTRL+Qt.ALT+Qt.Key_V, trigbool=self.enableFakeVimMode)
if globalSettings.useFakeVim:
self.actionFakeVimMode.setChecked(True)
self.enableFakeVimMode(True)
self.actionFullScreen = self.act(self.tr('Fullscreen mode'), 'view-fullscreen',
shct=Qt.Key_F11, trigbool=self.enableFullScreen)
self.actionFullScreen.setPriority(QAction.LowPriority)
self.actionConfig = self.act(self.tr('Preferences'), icon='preferences-system',
trig=self.openConfigDialog)
self.actionConfig.setMenuRole(QAction.PreferencesRole)
self.actionSaveHtml = self.act('HTML', 'text-html', self.saveFileHtml)
self.actionPdf = self.act('PDF', 'application-pdf', self.savePdf)
self.actionOdf = self.act('ODT', 'x-office-docuement', self.saveOdf)
self.getExportExtensionsList()
self.actionQuit = self.act(self.tr('Quit'), 'application-exit', shct=QKeySequence.Quit)
self.actionQuit.setMenuRole(QAction.QuitRole)
self.actionQuit.triggered.connect(self.close)
self.actionUndo = self.act(self.tr('Undo'), 'edit-undo',
lambda: self.currentTab.editBox.undo(), shct=QKeySequence.Undo)
self.actionRedo = self.act(self.tr('Redo'), 'edit-redo',
lambda: self.currentTab.editBox.redo(), shct=QKeySequence.Redo)
self.actionCopy = self.act(self.tr('Copy'), 'edit-copy',
lambda: self.currentTab.editBox.copy(), shct=QKeySequence.Copy)
self.actionCut = self.act(self.tr('Cut'), 'edit-cut',
lambda: self.currentTab.editBox.cut(), shct=QKeySequence.Cut)
self.actionPaste = self.act(self.tr('Paste'), 'edit-paste',
lambda: self.currentTab.editBox.paste(), shct=QKeySequence.Paste)
self.actionUndo.setEnabled(False)
self.actionRedo.setEnabled(False)
self.actionCopy.setEnabled(False)
self.actionCut.setEnabled(False)
qApp = QApplication.instance()
qApp.clipboard().dataChanged.connect(self.clipboardDataChanged)
self.clipboardDataChanged()
if enchant is not None:
self.actionEnableSC = self.act(self.tr('Enable'), trigbool=self.enableSpellCheck)
self.actionSetLocale = self.act(self.tr('Set locale'), trig=self.changeLocale)
self.actionWebKit = self.act(self.tr('Use WebKit renderer'), trigbool=self.enableWebKit)
if ReTextWebPreview is None:
globalSettings.useWebKit = False
self.actionWebKit.setEnabled(False)
self.actionWebKit.setChecked(globalSettings.useWebKit)
self.actionShow = self.act(self.tr('Show directory'), 'system-file-manager', self.showInDir)
self.actionFind = self.act(self.tr('Next'), 'go-next', self.find,
shct=QKeySequence.FindNext)
self.actionFindPrev = self.act(self.tr('Previous'), 'go-previous',
lambda: self.find(back=True), shct=QKeySequence.FindPrevious)
self.actionReplace = self.act(self.tr('Replace'), 'edit-find-replace',
lambda: self.find(replace=True))
self.actionReplaceAll = self.act(self.tr('Replace all'), trig=self.replaceAll)
menuReplace = QMenu()
menuReplace.addAction(self.actionReplaceAll)
self.actionReplace.setMenu(menuReplace)
self.actionCloseSearch = self.act(self.tr('Close'), 'window-close',
lambda: self.searchBar.setVisible(False))
self.actionCloseSearch.setPriority(QAction.LowPriority)
self.actionHelp = self.act(self.tr('Get help online'), 'help-contents', self.openHelp)
self.aboutWindowTitle = self.tr('About ReText')
self.actionAbout = self.act(self.aboutWindowTitle, 'help-about', self.aboutDialog)
self.actionAbout.setMenuRole(QAction.AboutRole)
self.actionAboutQt = self.act(self.tr('About Qt'))
self.actionAboutQt.setMenuRole(QAction.AboutQtRole)
self.actionAboutQt.triggered.connect(qApp.aboutQt)
availableMarkups = markups.get_available_markups()
if not availableMarkups:
print('Warning: no markups are available!')
if len(availableMarkups) > 1:
self.chooseGroup = QActionGroup(self)
markupActions = []
for markup in availableMarkups:
markupAction = self.act(markup.name, trigbool=self.markupFunction(markup))
if markup.name == globalSettings.defaultMarkup:
markupAction.setChecked(True)
self.chooseGroup.addAction(markupAction)
markupActions.append(markupAction)
self.actionBold = self.act(self.tr('Bold'), shct=QKeySequence.Bold,
trig=lambda: self.insertFormatting('bold'))
self.actionItalic = self.act(self.tr('Italic'), shct=QKeySequence.Italic,
trig=lambda: self.insertFormatting('italic'))
self.actionUnderline = self.act(self.tr('Underline'), shct=QKeySequence.Underline,
trig=lambda: self.insertFormatting('underline'))
self.usefulTags = ('header', 'italic', 'bold', 'underline', 'numbering',
'bullets', 'image', 'link', 'inline code', 'code block', 'blockquote')
self.usefulChars = ('deg', 'divide', 'dollar', 'hellip', 'laquo', 'larr',
'lsquo', 'mdash', 'middot', 'minus', 'nbsp', 'ndash', 'raquo',
'rarr', 'rsquo', 'times')
self.formattingBox = QComboBox(self.editBar)
self.formattingBox.addItem(self.tr('Formatting'))
self.formattingBox.addItems(self.usefulTags)
self.formattingBox.activated[str].connect(self.insertFormatting)
self.symbolBox = QComboBox(self.editBar)
self.symbolBox.addItem(self.tr('Symbols'))
self.symbolBox.addItems(self.usefulChars)
self.symbolBox.activated.connect(self.insertSymbol)
self.updateStyleSheet()
menubar = self.menuBar()
menuFile = menubar.addMenu(self.tr('File'))
menuEdit = menubar.addMenu(self.tr('Edit'))
menuHelp = menubar.addMenu(self.tr('Help'))
menuFile.addAction(self.actionNew)
menuFile.addAction(self.actionOpen)
self.menuRecentFiles = menuFile.addMenu(self.tr('Open recent'))
self.menuRecentFiles.aboutToShow.connect(self.updateRecentFiles)
menuFile.addAction(self.actionShow)
menuFile.addAction(self.actionSetEncoding)
menuFile.addAction(self.actionReload)
menuFile.addSeparator()
menuFile.addAction(self.actionSave)
menuFile.addAction(self.actionSaveAs)
menuFile.addSeparator()
menuFile.addAction(self.actionNextTab)
menuFile.addAction(self.actionPrevTab)
menuFile.addSeparator()
menuExport = menuFile.addMenu(self.tr('Export'))
menuExport.addAction(self.actionSaveHtml)
menuExport.addAction(self.actionOdf)
menuExport.addAction(self.actionPdf)
if self.extensionActions:
menuExport.addSeparator()
for action, mimetype in self.extensionActions:
menuExport.addAction(action)
menuExport.aboutToShow.connect(self.updateExtensionsVisibility)
menuFile.addAction(self.actionPrint)
menuFile.addAction(self.actionPrintPreview)
menuFile.addSeparator()
menuFile.addAction(self.actionQuit)
menuEdit.addAction(self.actionUndo)
menuEdit.addAction(self.actionRedo)
menuEdit.addSeparator()
menuEdit.addAction(self.actionCut)
menuEdit.addAction(self.actionCopy)
menuEdit.addAction(self.actionPaste)
menuEdit.addSeparator()
if enchant is not None:
menuSC = menuEdit.addMenu(self.tr('Spell check'))
menuSC.addAction(self.actionEnableSC)
menuSC.addAction(self.actionSetLocale)
menuEdit.addAction(self.actionSearch)
menuEdit.addAction(self.actionChangeEditorFont)
menuEdit.addAction(self.actionChangePreviewFont)
menuEdit.addSeparator()
if len(availableMarkups) > 1:
self.menuMode = menuEdit.addMenu(self.tr('Default markup'))
for markupAction in markupActions:
self.menuMode.addAction(markupAction)
menuFormat = menuEdit.addMenu(self.tr('Formatting'))
menuFormat.addAction(self.actionBold)
menuFormat.addAction(self.actionItalic)
menuFormat.addAction(self.actionUnderline)
menuEdit.addAction(self.actionWebKit)
menuEdit.addSeparator()
menuEdit.addAction(self.actionViewHtml)
menuEdit.addAction(self.actionPreview)
menuEdit.addAction(self.actionTableMode)
if ReTextFakeVimHandler:
menuEdit.addAction(self.actionFakeVimMode)
menuEdit.addSeparator()
menuEdit.addAction(self.actionFullScreen)
menuEdit.addAction(self.actionConfig)
menuHelp.addAction(self.actionHelp)
menuHelp.addSeparator()
menuHelp.addAction(self.actionAbout)
menuHelp.addAction(self.actionAboutQt)
toolBar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
toolBar.addAction(self.actionNew)
toolBar.addSeparator()
toolBar.addAction(self.actionOpen)
toolBar.addAction(self.actionSave)
toolBar.addAction(self.actionPrint)
toolBar.addSeparator()
toolBar.addAction(self.actionPreview)
toolBar.addAction(self.actionFullScreen)
self.editBar.addAction(self.actionUndo)
self.editBar.addAction(self.actionRedo)
self.editBar.addSeparator()
self.editBar.addAction(self.actionCut)
self.editBar.addAction(self.actionCopy)
self.editBar.addAction(self.actionPaste)
self.editBar.addSeparator()
self.editBar.addWidget(self.formattingBox)
self.editBar.addWidget(self.symbolBox)
self.searchEdit = QLineEdit(self.searchBar)
self.searchEdit.setPlaceholderText(self.tr('Search'))
self.searchEdit.returnPressed.connect(self.find)
self.replaceEdit = QLineEdit(self.searchBar)
self.replaceEdit.setPlaceholderText(self.tr('Replace with'))
self.replaceEdit.returnPressed.connect(self.find)
self.csBox = QCheckBox(self.tr('Case sensitively'), self.searchBar)
self.searchBar.addWidget(self.searchEdit)
self.searchBar.addWidget(self.replaceEdit)
self.searchBar.addSeparator()
self.searchBar.addWidget(self.csBox)
self.searchBar.addAction(self.actionFindPrev)
self.searchBar.addAction(self.actionFind)
self.searchBar.addAction(self.actionReplace)
self.searchBar.addAction(self.actionCloseSearch)
self.searchBar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
self.searchBar.setVisible(False)
self.autoSaveEnabled = globalSettings.autoSave
if self.autoSaveEnabled:
timer = QTimer(self)
timer.start(60000)
timer.timeout.connect(self.saveAll)
self.ind = None
if enchant is not None:
self.sl = globalSettings.spellCheckLocale
try:
enchant.Dict(self.sl or None)
except enchant.errors.Error as e:
print(e, file=sys.stderr)
globalSettings.spellCheck = False
if globalSettings.spellCheck:
self.actionEnableSC.setChecked(True)
self.fileSystemWatcher = QFileSystemWatcher()
self.fileSystemWatcher.fileChanged.connect(self.fileChanged)
0
Example 42
def test_constructor(self):
# unicode(obj) tests (this maps to PyObject_Unicode() at C level)
self.assertEqual(
unicode(u'unicode remains unicode'),
u'unicode remains unicode'
)
class UnicodeSubclass(unicode):
pass
self.assertEqual(
unicode(UnicodeSubclass('unicode subclass becomes unicode')),
u'unicode subclass becomes unicode'
)
self.assertEqual(
unicode('strings are converted to unicode'),
u'strings are converted to unicode'
)
class UnicodeCompat:
def __init__(self, x):
self.x = x
def __unicode__(self):
return self.x
self.assertEqual(
unicode(UnicodeCompat('__unicode__ compatible objects are recognized')),
u'__unicode__ compatible objects are recognized')
class StringCompat:
def __init__(self, x):
self.x = x
def __str__(self):
return self.x
self.assertEqual(
unicode(StringCompat('__str__ compatible objects are recognized')),
u'__str__ compatible objects are recognized'
)
# unicode(obj) is compatible to str():
o = StringCompat('unicode(obj) is compatible to str()')
self.assertEqual(unicode(o), u'unicode(obj) is compatible to str()')
self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
for obj in (123, 123.45, 123L):
self.assertEqual(unicode(obj), unicode(str(obj)))
# unicode(obj, encoding, error) tests (this maps to
# PyUnicode_FromEncodedObject() at C level)
if not sys.platform.startswith('java'):
self.assertRaises(
TypeError,
unicode,
u'decoding unicode is not supported',
'utf-8',
'strict'
)
self.assertEqual(
unicode('strings are decoded to unicode', 'utf-8', 'strict'),
u'strings are decoded to unicode'
)
if not sys.platform.startswith('java'):
self.assertEqual(
unicode(
buffer('character buffers are decoded to unicode'),
'utf-8',
'strict'
),
u'character buffers are decoded to unicode'
)
self.assertRaises(TypeError, unicode, 42, 42, 42)
0
Example 43
def __init__(self):
Cleaner.__init__(self)
#
# options for Linux and BSD
#
if 'posix' == os.name:
# TRANSLATORS: desktop entries are .desktop files in Linux that
# make up the application menu (the menu that shows BleachBit,
# Firefox, and others. The .desktop files also associate file
# types, so clicking on an .html file in Nautilus brings up
# Firefox.
# More information:
# http://standards.freedesktop.org/menu-spec/latest/index.html#introduction
self.add_option('desktop_entry', _('Broken desktop files'), _(
'Delete broken application menu entries and file associations'))
self.add_option('cache', _('Cache'), _('Delete the cache'))
# TRANSLATORS: Localizations are files supporting specific
# languages, so applications appear in Spanish, etc.
self.add_option('localizations', _('Localizations'), _(
'Delete files for unwanted languages'))
self.set_warning(
'localizations', _("Configure this option in the preferences."))
# TRANSLATORS: 'Rotated logs' refers to old system log files.
# Linux systems often have a scheduled job to rotate the logs
# which means compress all except the newest log and then delete
# the oldest log. You could translate this 'old logs.'
self.add_option(
'rotated_logs', _('Rotated logs'), _('Delete old system logs'))
self.add_option('recent_docuements', _('Recent docuements list'), _(
'Delete the list of recently used docuements'))
self.add_option('trash', _('Trash'), _('Empty the trash'))
#
# options just for Linux
#
if sys.platform.startswith('linux'):
self.add_option('memory', _('Memory'),
# TRANSLATORS: 'free' means 'unallocated'
_('Wipe the swap and free memory'))
self.set_warning(
'memory', _('This option is experimental and may cause system problems.'))
#
# options just for Microsoft Windows
#
if 'nt' == os.name:
self.add_option('logs', _('Logs'), _('Delete the logs'))
self.add_option(
'memory_dump', _('Memory dump'), _('Delete the file memory.dmp'))
self.add_option('muicache', 'MUICache', _('Delete the cache'))
# TRANSLATORS: Prefetch is Microsoft Windows jargon.
self.add_option('prefetch', _('Prefetch'), _('Delete the cache'))
self.add_option(
'recycle_bin', _('Recycle bin'), _('Empty the recycle bin'))
# TRANSLATORS: 'Update' is a noun, and 'Update uninstallers' is an option to delete
# the uninstallers for software updates.
self.add_option('updates', _('Update uninstallers'), _(
'Delete uninstallers for Microsoft updates including hotfixes, service packs, and Internet Explorer updates'))
#
# options for GTK+
#
if HAVE_GTK:
self.add_option('clipboard', _('Clipboard'), _(
'The desktop environment\'s clipboard used for copy and paste operations'))
#
# options common to all platforms
#
# TRANSLATORS: "Custom" is an option allowing the user to specify which
# files and folders will be erased.
self.add_option('custom', _('Custom'), _(
'Delete user-specified files and folders'))
# TRANSLATORS: 'free' means 'unallocated'
self.add_option('free_disk_space', _('Free disk space'),
# TRANSLATORS: 'free' means 'unallocated'
_('Overwrite free disk space to hide deleted files'))
self.set_warning('free_disk_space', _('This option is very slow.'))
self.add_option(
'tmp', _('Temporary files'), _('Delete the temporary files'))
self.description = _("The system in general")
self.id = 'system'
self.name = _("System")
0
Example 44
Project: bleachbit Source File: Cleaner.py
def get_commands(self, option_id):
# This variable will collect fully expanded file names, and
# at the end of this function, they will be checked they exist
# and processed through Command.Delete().
files = []
# cache
if 'posix' == os.name and 'cache' == option_id:
dirname = os.path.expanduser("~/.cache/")
for filename in children_in_directory(dirname, True):
if self.whitelisted(filename):
continue
files += [filename]
# custom
if 'custom' == option_id:
for (c_type, c_path) in options.get_custom_paths():
if 'file' == c_type:
files += [c_path]
elif 'folder' == c_type:
files += [c_path]
for path in children_in_directory(c_path, True):
files += [path]
else:
raise RuntimeError(
'custom folder has invalid type %s' % c_type)
# menu
menu_dirs = ['~/.local/share/applications',
'~/.config/autostart',
'~/.gnome/apps/',
'~/.gnome2/panel2.d/default/launchers',
'~/.gnome2/vfolders/applications/',
'~/.kde/share/apps/RecentDocuements/',
'~/.kde/share/mimelnk',
'~/.kde/share/mimelnk/application/ram.desktop',
'~/.kde2/share/mimelnk/application/',
'~/.kde2/share/applnk']
if 'posix' == os.name and 'desktop_entry' == option_id:
for dirname in menu_dirs:
for filename in [fn for fn in children_in_directory(dirname, False)
if fn.endswith('.desktop')]:
if Unix.is_broken_xdg_desktop(filename):
yield Command.Delete(filename)
# unwanted locales
if 'posix' == os.name and 'localizations' == option_id:
for path in Unix.locales.localization_paths(locales_to_keep=options.get_languages()):
if os.path.isdir(path):
for f in FileUtilities.children_in_directory(path, True):
yield Command.Delete(f)
yield Command.Delete(path)
# Windows logs
if 'nt' == os.name and 'logs' == option_id:
paths = (
'$ALLUSERSPROFILE\\Application Data\\Microsoft\\Dr Watson\\*.log',
'$ALLUSERSPROFILE\\Application Data\\Microsoft\\Dr Watson\\user.dmp',
'$LocalAppData\\Microsoft\\Windows\\WER\\ReportArchive\\*\\*',
'$LocalAppData\\Microsoft\\Windows\WER\\ReportQueue\\*\\*',
'$programdata\\Microsoft\\Windows\\WER\\ReportArchive\\*\\*',
'$programdata\\Microsoft\\Windows\\WER\\ReportQueue\\*\\*',
'$localappdata\\Microsoft\\Internet Explorer\\brndlog.bak',
'$localappdata\\Microsoft\\Internet Explorer\\brndlog.txt',
'$windir\\*.log',
'$windir\\imsins.BAK',
'$windir\\OEWABLog.txt',
'$windir\\SchedLgU.txt',
'$windir\\ntbtlog.txt',
'$windir\\setuplog.txt',
'$windir\\REGLOCS.OLD',
'$windir\\Debug\\*.log',
'$windir\\Debug\\Setup\\UpdSh.log',
'$windir\\Debug\\UserMode\\*.log',
'$windir\\Debug\\UserMode\\ChkAcc.bak',
'$windir\\Debug\\UserMode\\userenv.bak',
'$windir\\Microsoft.NET\Framework\*\*.log',
'$windir\\pchealth\\helpctr\\Logs\\hcupdate.log',
'$windir\\security\\logs\\*.log',
'$windir\\security\\logs\\*.old',
'$windir\\SoftwareDistribution\\*.log',
'$windir\\SoftwareDistribution\\DataStore\\Logs\\*',
'$windir\\system32\\TZLog.log',
'$windir\\system32\\config\\systemprofile\\Application Data\\Microsoft\\Internet Explorer\\brndlog.bak',
'$windir\\system32\\config\\systemprofile\\Application Data\\Microsoft\\Internet Explorer\\brndlog.txt',
'$windir\\system32\\LogFiles\\AIT\\AitEventLog.etl.???',
'$windir\\system32\\LogFiles\\Firewall\\pfirewall.log*',
'$windir\\system32\\LogFiles\\Scm\\SCM.EVM*',
'$windir\\system32\\LogFiles\\WMI\\Terminal*.etl',
'$windir\\system32\\LogFiles\\WMI\\RTBackup\EtwRT.*etl',
'$windir\\system32\\wbem\\Logs\\*.lo_',
'$windir\\system32\\wbem\\Logs\\*.log', )
for path in paths:
expanded = expandvars(path)
for globbed in glob.iglob(expanded):
files += [globbed]
# memory
if sys.platform.startswith('linux') and 'memory' == option_id:
yield Command.Function(None, Memory.wipe_memory, _('Memory'))
# memory dump
# how to manually create this file
# http://www.pctools.com/guides/registry/detail/856/
if 'nt' == os.name and 'memory_dump' == option_id:
fname = expandvars('$windir\\memory.dmp')
if os.path.exists(fname):
files += [fname]
for fname in glob.iglob(expandvars('$windir\\Minidump\\*.dmp')):
files += [fname]
# most recently used docuements list
if 'posix' == os.name and 'recent_docuements' == option_id:
files += [os.path.expanduser("~/.recently-used")]
# GNOME 2.26 (as seen on Ubuntu 9.04) will retain the list
# in memory if it is simply deleted, so it must be shredded
# (or at least truncated).
#
# GNOME 2.28.1 (Ubuntu 9.10) and 2.30 (10.04) do not re-read
# the file after truncation, but do re-read it after
# shredding.
#
# https://bugzilla.gnome.org/show_bug.cgi?id=591404
for pathname in ["~/.recently-used.xbel", "~/.local/share/recently-used.xbel"]:
pathname = os.path.expanduser(pathname)
if os.path.lexists(pathname):
yield Command.Shred(pathname)
if HAVE_GTK:
gtk.RecentManager().purge_items()
if 'posix' == os.name and 'rotated_logs' == option_id:
for path in Unix.rotated_logs():
yield Command.Delete(path)
# temporary files
if 'posix' == os.name and 'tmp' == option_id:
dirnames = ['/tmp', '/var/tmp']
for dirname in dirnames:
for path in children_in_directory(dirname, True):
is_open = FileUtilities.openfiles.is_open(path)
ok = not is_open and os.path.isfile(path) and \
not os.path.islink(path) and \
FileUtilities.ego_owner(path) and \
not self.whitelisted(path)
if ok:
yield Command.Delete(path)
# temporary files
if 'nt' == os.name and 'tmp' == option_id:
dirname = expandvars(
"$USERPROFILE\\Local Settings\\Temp\\")
# whitelist the folder %TEMP%\Low but not its contents
# https://bugs.launchpad.net/bleachbit/+bug/1421726
low = os.path.join(dirname, 'low').lower()
for filename in children_in_directory(dirname, True):
if not low == filename.lower():
yield Command.Delete(filename)
dirname = expandvars("$windir\\temp\\")
for filename in children_in_directory(dirname, True):
yield Command.Delete(filename)
# trash
if 'posix' == os.name and 'trash' == option_id:
dirname = os.path.expanduser("~/.Trash")
for filename in children_in_directory(dirname, False):
yield Command.Delete(filename)
# fixme http://www.ramendik.ru/docs/trashspec.html
# http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
# ~/.local/share/Trash
# * GNOME 2.22, Fedora 9
# * KDE 4.1.3, Ubuntu 8.10
dirname = os.path.expanduser("~/.local/share/Trash/files")
for filename in children_in_directory(dirname, True):
yield Command.Delete(filename)
dirname = os.path.expanduser("~/.local/share/Trash/info")
for filename in children_in_directory(dirname, True):
yield Command.Delete(filename)
dirname = os.path.expanduser("~/.local/share/Trash/expunged")
# [email protected] tells me that the trash
# backend puts files in here temporary, but in some situations
# the files are stuck.
for filename in children_in_directory(dirname, True):
yield Command.Delete(filename)
# clipboard
if HAVE_GTK and 'clipboard' == option_id:
def clear_clipboard():
gtk.gdk.threads_enter()
clipboard = gtk.clipboard_get()
clipboard.set_text("")
gtk.gdk.threads_leave()
return 0
yield Command.Function(None, clear_clipboard, _('Clipboard'))
# overwrite free space
shred_drives = options.get_list('shred_drives')
if 'free_disk_space' == option_id and shred_drives:
for pathname in shred_drives:
# TRANSLATORS: 'Free' means 'unallocated.'
# %s expands to a path such as C:\ or /tmp/
display = _("Overwrite free disk space %s") % pathname
def wipe_path_func():
for ret in FileUtilities.wipe_path(pathname, idle=True):
# Yield control to GTK idle because this process
# is very slow. Also display progress.
yield ret
yield 0
yield Command.Function(None, wipe_path_func, display)
# MUICache
if 'nt' == os.name and 'muicache' == option_id:
keys = (
'HKCU\\Software\\Microsoft\\Windows\\ShellNoRoam\\MUICache',
'HKCU\\Software\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache')
for key in keys:
yield Command.Winreg(key, None)
# prefetch
if 'nt' == os.name and 'prefetch' == option_id:
for path in glob.iglob(expandvars('$windir\\Prefetch\\*.pf')):
yield Command.Delete(path)
# recycle bin
if 'nt' == os.name and 'recycle_bin' == option_id:
# This method allows shredding
for path in Windows.get_recycle_bin():
yield Command.Delete(path)
# If there were any files deleted, Windows XP will show the
# wrong icon for the recycle bin indicating it is not empty.
# The icon will be incorrect until logging in to Windows again
# or until it is emptied using the Windows API call for emptying
# the recycle bin.
# Windows 10 refreshes the recycle bin icon when the user
# opens the recycle bin folder.
# This is a hack to refresh the icon.
import tempfile
tmpdir = tempfile.mkdtemp()
Windows.move_to_recycle_bin(tmpdir)
try:
Windows.empty_recycle_bin(None, True)
except:
logger = logging.getLogger(__name__)
logger.info('error in empty_recycle_bin()', exc_info=True)
# Windows Updates
if 'nt' == os.name and 'updates' == option_id:
for wu in Windows.delete_updates():
yield wu
# return queued files
for filename in files:
if os.path.lexists(filename):
yield Command.Delete(filename)
0
Example 45
Project: rbtools Source File: git.py
def get_repository_info(self):
"""Get repository information for the current Git working tree.
This function changes the directory to the top level directory of the
current working tree.
"""
if not check_install(['git', '--help']):
# CreateProcess (launched via subprocess, used by check_install)
# does not automatically append .cmd for things it finds in PATH.
# If we're on Windows, and this works, save it for further use.
if (sys.platform.startswith('win') and
check_install(['git.cmd', '--help'])):
self.git = 'git.cmd'
else:
logging.debug('Unable to execute "git --help" or "git.cmd '
'--help": skipping Git')
return None
git_dir = execute([self.git, "rev-parse", "--git-dir"],
ignore_errors=True).rstrip("\n")
if git_dir.startswith("fatal:") or not os.path.isdir(git_dir):
return None
# Sometimes core.bare is not set, and generates an error, so ignore
# errors. Valid values are 'true' or '1'.
bare = execute([self.git, 'config', 'core.bare'],
ignore_errors=True).strip()
self.bare = bare in ('true', '1')
# If we are not working in a bare repository, then we will change
# directory to the top level working tree lose our original position.
# However, we need the original working directory for file exclusion
# patterns, so we save it here.
if self._original_cwd is None:
self._original_cwd = os.getcwd()
# Running in directories other than the top level of
# of a work-tree would result in broken diffs on the server
if not self.bare:
git_top = execute([self.git, "rev-parse", "--show-toplevel"],
ignore_errors=True).rstrip("\n")
# Top level might not work on old git version se we use git dir
# to find it.
if (git_top.startswith('fatal:') or not os.path.isdir(git_dir)
or git_top.startswith('cygdrive')):
git_top = git_dir
os.chdir(os.path.abspath(git_top))
self.head_ref = execute([self.git, 'symbolic-ref', '-q',
'HEAD'], ignore_errors=True).strip()
# We know we have something we can work with. Let's find out
# what it is. We'll try SVN first, but only if there's a .git/svn
# directory. Otherwise, it may attempt to create one and scan
# revisions, which can be slow. Also skip SVN detection if the git
# repository was specified on command line.
git_svn_dir = os.path.join(git_dir, 'svn')
if (not getattr(self.options, 'repository_url', None) and
os.path.isdir(git_svn_dir) and len(os.listdir(git_svn_dir)) > 0):
data = execute([self.git, "svn", "info"], ignore_errors=True)
m = re.search(r'^Repository Root: (.+)$', data, re.M)
if m:
path = m.group(1)
m = re.search(r'^URL: (.+)$', data, re.M)
if m:
base_path = m.group(1)[len(path):] or "/"
m = re.search(r'^Repository UUID: (.+)$', data, re.M)
if m:
uuid = m.group(1)
self.type = "svn"
# Get SVN tracking branch
if getattr(self.options, 'tracking', None):
self.upstream_branch = self.options.tracking
else:
data = execute([self.git, "svn", "rebase", "-n"],
ignore_errors=True)
m = re.search(r'^Remote Branch:\s*(.+)$', data,
re.M)
if m:
self.upstream_branch = m.group(1)
else:
sys.stderr.write('Failed to determine SVN '
'tracking branch. Defaulting'
'to "master"\n')
self.upstream_branch = 'master'
return SVNRepositoryInfo(path=path,
base_path=base_path,
uuid=uuid,
supports_parent_diffs=True)
else:
# Versions of git-svn before 1.5.4 don't (appear to) support
# 'git svn info'. If we fail because of an older git install,
# here, figure out what version of git is installed and give
# the user a hint about what to do next.
version = execute([self.git, "svn", "--version"],
ignore_errors=True)
version_parts = re.search('version (\d+)\.(\d+)\.(\d+)',
version)
svn_remote = execute(
[self.git, "config", "--get", "svn-remote.svn.url"],
ignore_errors=True)
if (version_parts and svn_remote and
not is_valid_version((int(version_parts.group(1)),
int(version_parts.group(2)),
int(version_parts.group(3))),
(1, 5, 4))):
die("Your installation of git-svn must be upgraded to "
"version 1.5.4 or later")
# Okay, maybe Perforce (git-p4).
git_p4_ref = os.path.join(git_dir, 'refs', 'remotes', 'p4', 'master')
if os.path.exists(git_p4_ref):
data = execute([self.git, 'config', '--get', 'git-p4.port'],
ignore_errors=True)
m = re.search(r'(.+)', data)
if m:
port = m.group(1)
else:
port = os.getenv('P4PORT')
if port:
self.type = 'perforce'
self.upstream_branch = 'remotes/p4/master'
return RepositoryInfo(path=port,
base_path='',
supports_parent_diffs=True)
# Nope, it's git then.
# Check for a tracking branch and determine merge-base
self.upstream_branch = ''
if self.head_ref:
short_head = self._strip_heads_prefix(self.head_ref)
merge = execute([self.git, 'config', '--get',
'branch.%s.merge' % short_head],
ignore_errors=True).strip()
remote = execute([self.git, 'config', '--get',
'branch.%s.remote' % short_head],
ignore_errors=True).strip()
merge = self._strip_heads_prefix(merge)
if remote and remote != '.' and merge:
self.upstream_branch = '%s/%s' % (remote, merge)
url = None
if getattr(self.options, 'repository_url', None):
url = self.options.repository_url
self.upstream_branch = self.get_origin(self.upstream_branch,
True)[0]
else:
self.upstream_branch, origin_url = \
self.get_origin(self.upstream_branch, True)
if not origin_url or origin_url.startswith("fatal:"):
self.upstream_branch, origin_url = self.get_origin()
url = origin_url.rstrip('/')
# Central bare repositories don't have origin URLs.
# We return git_dir instead and hope for the best.
if not url:
url = os.path.abspath(git_dir)
# There is no remote, so skip this part of upstream_branch.
self.upstream_branch = self.upstream_branch.split('/')[-1]
if url:
self.type = "git"
return RepositoryInfo(path=url, base_path='',
supports_parent_diffs=True)
return None
0
Example 46
Project: ReviewBot Source File: process.py
def execute(command,
env=None,
split_lines=False,
ignore_errors=False,
extra_ignore_errors=(),
translate_newlines=True,
with_errors=True,
none_on_ignored_error=False):
"""Execute a command and return the output.
Args:
command (list of unicode):
The command to run.
env (dict, optional):
The environment variables to use when running the process.
split_lines (bool, optional):
Whether to return the output as a list (split on newlines) or a
single string.
ignore_errors (bool, optional):
Whether to ignore non-zero return codes from the command.
extra_ignore_errors (tuple of int, optional):
Process return codes to ignore.
translate_newlines (bool, optional):
Whether to convert platform-specific newlines (such as \\r\\n) to
the regular newline (\\n) character.
with_errors (bool, optional):
Whether the stderr output should be merged in with the stdout
output or just ignored.
none_on_ignored_error (bool, optional):
Whether to return ``None`` if there was an ignored error (instead
of the process output).
Returns:
unicode or list of unicode:
Either the output of the process, or a list of lines in the output,
depending on the value of ``split_lines``.
"""
if isinstance(command, list):
logging.debug(subprocess.list2cmdline(command))
else:
logging.debug(command)
if env:
env.update(os.environ)
else:
env = os.environ.copy()
env['LC_ALL'] = 'en_US.UTF-8'
env['LANGUAGE'] = 'en_US.UTF-8'
if with_errors:
errors_output = subprocess.STDOUT
else:
errors_output = subprocess.PIPE
if sys.platform.startswith('win'):
p = subprocess.Popen(command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=errors_output,
shell=False,
universal_newlines=translate_newlines,
env=env)
else:
p = subprocess.Popen(command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=errors_output,
shell=False,
close_fds=True,
universal_newlines=translate_newlines,
env=env)
if split_lines:
data = p.stdout.readlines()
else:
data = p.stdout.read()
rc = p.wait()
if rc and not ignore_errors and rc not in extra_ignore_errors:
raise Exception('Failed to execute command: %s\n%s' % (command, data))
if rc and none_on_ignored_error:
return None
return data
0
Example 47
Project: powerline Source File: bat.py
def _fetch_battery_info(pl):
try:
import dbus
except ImportError:
pl.debug('Not using DBUS+UPower as dbus is not available')
else:
try:
bus = dbus.SystemBus()
except Exception as e:
pl.exception('Failed to connect to system bus: {0}', str(e))
else:
interface = 'org.freedesktop.UPower'
try:
up = bus.get_object(interface, '/org/freedesktop/UPower')
except dbus.exceptions.DBusException as e:
if getattr(e, '_dbus_error_name', '').endswith('ServiceUnknown'):
pl.debug('Not using DBUS+UPower as UPower is not available via dbus')
else:
pl.exception('Failed to get UPower service with dbus: {0}', str(e))
else:
devinterface = 'org.freedesktop.DBus.Properties'
devtype_name = interface + '.Device'
devices = []
for devpath in up.EnumerateDevices(dbus_interface=interface):
dev = bus.get_object(interface, devpath)
devget = lambda what: dev.Get(
devtype_name,
what,
dbus_interface=devinterface
)
if int(devget('Type')) != 2:
pl.debug('Not using DBUS+UPower with {0}: invalid type', devpath)
continue
if not bool(devget('IsPresent')):
pl.debug('Not using DBUS+UPower with {0}: not present', devpath)
continue
if not bool(devget('PowerSupply')):
pl.debug('Not using DBUS+UPower with {0}: not a power supply', devpath)
continue
devices.append(devpath)
pl.debug('Using DBUS+UPower with {0}', devpath)
if devices:
def _flatten_battery(pl):
energy = 0.0
energy_full = 0.0
state = True
for devpath in devices:
dev = bus.get_object(interface, devpath)
energy_full += float(
dbus.Interface(dev, dbus_interface=devinterface).Get(
devtype_name,
'EnergyFull'
),
)
energy += float(
dbus.Interface(dev, dbus_interface=devinterface).Get(
devtype_name,
'Energy'
),
)
state &= dbus.Interface(dev, dbus_interface=devinterface).Get(
devtype_name,
'State'
) != 2
return (energy * 100.0 / energy_full), state
return _flatten_battery
pl.debug('Not using DBUS+UPower as no batteries were found')
if os.path.isdir('/sys/class/power_supply'):
linux_energy_full_fmt = '/sys/class/power_supply/{0}/energy_full'
linux_energy_fmt = '/sys/class/power_supply/{0}/energy_now'
linux_status_fmt = '/sys/class/power_supply/{0}/status'
devices = []
for linux_supplier in os.listdir('/sys/class/power_supply'):
energy_path = linux_energy_fmt.format(linux_supplier)
if not os.path.exists(energy_path):
continue
pl.debug('Using /sys/class/power_supply with battery {0}', linux_supplier)
devices.append(linux_supplier)
if devices:
def _get_battery_status(pl):
energy = 0.0
energy_full = 0.0
state = True
for device in devices:
with open(linux_energy_full_fmt.format(device), 'r') as f:
energy_full += int(float(f.readline().split()[0]))
with open(linux_energy_fmt.format(device), 'r') as f:
energy += int(float(f.readline().split()[0]))
try:
with open(linux_status_fmt.format(device), 'r') as f:
state &= (f.readline().strip() != 'Discharging')
except IOError:
state = None
return (energy * 100.0 / energy_full), state
return _get_battery_status
pl.debug('Not using /sys/class/power_supply as no batteries were found')
else:
pl.debug('Not using /sys/class/power_supply: no directory')
try:
from shutil import which # Python-3.3 and later
except ImportError:
pl.info('Using dumb “which” which only checks for file in /usr/bin')
which = lambda f: (lambda fp: os.path.exists(fp) and fp)(os.path.join('/usr/bin', f))
if which('pmset'):
pl.debug('Using pmset')
BATTERY_PERCENT_RE = re.compile(r'(\d+)%')
def _get_battery_status(pl):
battery_summary = run_cmd(pl, ['pmset', '-g', 'batt'])
battery_percent = BATTERY_PERCENT_RE.search(battery_summary).group(1)
ac_charging = 'AC' in battery_summary
return int(battery_percent), ac_charging
return _get_battery_status
else:
pl.debug('Not using pmset: executable not found')
if sys.platform.startswith('win') or sys.platform == 'cygwin':
# From http://stackoverflow.com/a/21083571/273566, reworked
try:
from win32com.client import GetObject
except ImportError:
pl.debug('Not using win32com.client as it is not available')
else:
try:
wmi = GetObject('winmgmts:')
except Exception as e:
pl.exception('Failed to run GetObject from win32com.client: {0}', str(e))
else:
for battery in wmi.InstancesOf('Win32_Battery'):
pl.debug('Using win32com.client with Win32_Battery')
def _get_battery_status(pl):
# http://msdn.microsoft.com/en-us/library/aa394074(v=vs.85).aspx
return battery.EstimatedChargeRemaining, battery.BatteryStatus == 6
return _get_battery_status
pl.debug('Not using win32com.client as no batteries were found')
from ctypes import Structure, c_byte, c_ulong, byref
if sys.platform == 'cygwin':
pl.debug('Using cdll to communicate with kernel32 (Cygwin)')
from ctypes import cdll
library_loader = cdll
else:
pl.debug('Using windll to communicate with kernel32 (Windows)')
from ctypes import windll
library_loader = windll
class PowerClass(Structure):
_fields_ = [
('ACLineStatus', c_byte),
('BatteryFlag', c_byte),
('BatteryLifePercent', c_byte),
('Reserved1', c_byte),
('BatteryLifeTime', c_ulong),
('BatteryFullLifeTime', c_ulong)
]
def _get_battery_status(pl):
powerclass = PowerClass()
result = library_loader.kernel32.GetSystemPowerStatus(byref(powerclass))
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa372693(v=vs.85).aspx
if result:
return None
return powerclass.BatteryLifePercent, powerclass.ACLineStatus == 1
if _get_battery_status() is None:
pl.debug('Not using GetSystemPowerStatus because it failed')
else:
pl.debug('Using GetSystemPowerStatus')
return _get_battery_status
raise NotImplementedError
0
Example 48
Project: pymo Source File: test_grammar.py
def testFuncdef(self):
### 'def' NAME parameters ':' suite
### parameters: '(' [varargslist] ')'
### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
### | ('**'|'*' '*') NAME)
### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
### fpdef: NAME | '(' fplist ')'
### fplist: fpdef (',' fpdef)* [',']
### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
### argument: [test '='] test # Really [keyword '='] test
def f1(): pass
f1()
f1(*())
f1(*(), **{})
def f2(one_argument): pass
def f3(two, arguments): pass
# Silence Py3k warning
exec('def f4(two, (compound, (argument, list))): pass')
exec('def f5((compound, first), two): pass')
self.assertEqual(f2.func_code.co_varnames, ('one_argument',))
self.assertEqual(f3.func_code.co_varnames, ('two', 'arguments'))
if sys.platform.startswith('java'):
self.assertEqual(f4.func_code.co_varnames,
('two', '(compound, (argument, list))', 'compound', 'argument',
'list',))
self.assertEqual(f5.func_code.co_varnames,
('(compound, first)', 'two', 'compound', 'first'))
else:
self.assertEqual(f4.func_code.co_varnames,
('two', '.1', 'compound', 'argument', 'list'))
self.assertEqual(f5.func_code.co_varnames,
('.0', 'two', 'compound', 'first'))
def a1(one_arg,): pass
def a2(two, args,): pass
def v0(*rest): pass
def v1(a, *rest): pass
def v2(a, b, *rest): pass
# Silence Py3k warning
exec('def v3(a, (b, c), *rest): return a, b, c, rest')
f1()
f2(1)
f2(1,)
f3(1, 2)
f3(1, 2,)
f4(1, (2, (3, 4)))
v0()
v0(1)
v0(1,)
v0(1,2)
v0(1,2,3,4,5,6,7,8,9,0)
v1(1)
v1(1,)
v1(1,2)
v1(1,2,3)
v1(1,2,3,4,5,6,7,8,9,0)
v2(1,2)
v2(1,2,3)
v2(1,2,3,4)
v2(1,2,3,4,5,6,7,8,9,0)
v3(1,(2,3))
v3(1,(2,3),4)
v3(1,(2,3),4,5,6,7,8,9,0)
# ceval unpacks the formal arguments into the first argcount names;
# thus, the names nested inside tuples must appear after these names.
if sys.platform.startswith('java'):
self.assertEqual(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))
else:
self.assertEqual(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
self.assertEqual(v3(1, (2, 3), 4), (1, 2, 3, (4,)))
def d01(a=1): pass
d01()
d01(1)
d01(*(1,))
d01(**{'a':2})
def d11(a, b=1): pass
d11(1)
d11(1, 2)
d11(1, **{'b':2})
def d21(a, b, c=1): pass
d21(1, 2)
d21(1, 2, 3)
d21(*(1, 2, 3))
d21(1, *(2, 3))
d21(1, 2, *(3,))
d21(1, 2, **{'c':3})
def d02(a=1, b=2): pass
d02()
d02(1)
d02(1, 2)
d02(*(1, 2))
d02(1, *(2,))
d02(1, **{'b':2})
d02(**{'a': 1, 'b': 2})
def d12(a, b=1, c=2): pass
d12(1)
d12(1, 2)
d12(1, 2, 3)
def d22(a, b, c=1, d=2): pass
d22(1, 2)
d22(1, 2, 3)
d22(1, 2, 3, 4)
def d01v(a=1, *rest): pass
d01v()
d01v(1)
d01v(1, 2)
d01v(*(1, 2, 3, 4))
d01v(*(1,))
d01v(**{'a':2})
def d11v(a, b=1, *rest): pass
d11v(1)
d11v(1, 2)
d11v(1, 2, 3)
def d21v(a, b, c=1, *rest): pass
d21v(1, 2)
d21v(1, 2, 3)
d21v(1, 2, 3, 4)
d21v(*(1, 2, 3, 4))
d21v(1, 2, **{'c': 3})
def d02v(a=1, b=2, *rest): pass
d02v()
d02v(1)
d02v(1, 2)
d02v(1, 2, 3)
d02v(1, *(2, 3, 4))
d02v(**{'a': 1, 'b': 2})
def d12v(a, b=1, c=2, *rest): pass
d12v(1)
d12v(1, 2)
d12v(1, 2, 3)
d12v(1, 2, 3, 4)
d12v(*(1, 2, 3, 4))
d12v(1, 2, *(3, 4, 5))
d12v(1, *(2,), **{'c': 3})
def d22v(a, b, c=1, d=2, *rest): pass
d22v(1, 2)
d22v(1, 2, 3)
d22v(1, 2, 3, 4)
d22v(1, 2, 3, 4, 5)
d22v(*(1, 2, 3, 4))
d22v(1, 2, *(3, 4, 5))
d22v(1, *(2, 3), **{'d': 4})
# Silence Py3k warning
exec('def d31v((x)): pass')
exec('def d32v((x,)): pass')
d31v(1)
d32v((1,))
# keyword arguments after *arglist
def f(*args, **kwargs):
return args, kwargs
self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
{'x':2, 'y':5}))
self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
# Check ast errors in *args and *kwargs
check_syntax_error(self, "f(*g(1=2))")
check_syntax_error(self, "f(**g(1=2))")
0
Example 49
Project: telegram-send Source File: telegram_send.py
def configure(conf, channel=False, fm_integration=False):
"""Guide user to set up the bot, saves configuration at conf.
Args:
conf (str): Path where to save the configuration file. May contain '~' for user's home.
channel (Optional[bool]): Whether to configure a channel or not.
"""
conf = expanduser(conf) if conf else get_config_path()
prompt = "❯ " if not sys.platform.startswith("win32") else "> "
contact_url = "https://telegram.me/"
print("Talk with the {} on Telegram ({}), create a bot and insert the token"
.format(markup("BotFather", "cyan"), contact_url + "BotFather"))
token = input(markup(prompt, "magenta")).strip()
try:
bot = telegram.Bot(token)
bot_name = bot.getMe().username
except:
print(markup("Something went wrong, please try again.\n", "red"))
return configure()
print("Connected with {}.\n".format(markup(bot_name, "cyan")))
if channel:
print("Enter your channel's public name or link:"
.format(markup(bot_name, "cyan")))
chat_id = input(markup(prompt, "magenta")).strip()
if "telegram.me" in chat_id:
chat_id = '@' + chat_id.split('/')[-1]
elif chat_id.startswith('@'):
pass
else:
chat_id = '@' + chat_id
authorized = False
while not authorized:
try:
bot.sendChatAction(chat_id=chat_id, action="typing")
authorized = True
except telegram.error.Unauthorized:
input("Please add {} as administrator to {} and press Enter"
.format(markup(bot_name, "cyan"), markup(chat_id, "cyan")))
print(markup("\nCongratulations! telegram-send can now post to {}".format(chat_id), "green"))
else:
password = "".join([str(randint(0, 9)) for _ in range(5)])
print("Please add {} on Telegram ({})\nand send it the password: {}\n"
.format(markup(bot_name, "cyan"), contact_url + bot_name, markup(password, "bold")))
update, update_id = None, None
def get_user():
updates = bot.getUpdates(offset=update_id, timeout=10)
for update in updates:
# print(update.message.text)
if update.message.text.strip() == password:
return update, None
if len(updates) > 0:
return None, updates[-1].update_id + 1
else:
return None, None
while update is None:
try:
update, update_id = get_user()
except Exception as e:
print("Error! {}".format(e))
chat_id = update.message.chat_id
user = update.message.from_user.username or update.message.from_user.first_name
m = ("Congratulations {}! ".format(user), "\ntelegram-send is now ready for use!")
ball = "🎊"
print(markup("".join(m), "green"))
bot.sendMessage(chat_id=chat_id, text=ball + ' ' + m[0] + ball + m[1])
config = configparser.ConfigParser()
config.add_section("telegram")
config.set("telegram", "TOKEN", token)
config.set("telegram", "chat_id", str(chat_id))
# above 3 lines in py3: config["telegram"] = {"TOKEN": token, "chat_id": chat_id}
with open(conf, 'w') as f:
config.write(f)
if fm_integration:
return integrate_file_manager()
0
Example 50
Project: 2ping Source File: cli.py
def __init__(self, args):
now = clock()
self.args = args
self.time_start = now
self.fake_time_epoch = random_sys.random() * (2**32)
self.fake_time_generation = random_sys.randint(0, 65535)
self.sock_classes = []
self.poller = best_poller.best_poller()
self.pings_transmitted = 0
self.pings_received = 0
self.packets_transmitted = 0
self.packets_received = 0
self.lost_outbound = 0
self.lost_inbound = 0
self.errors_received = 0
self.rtt_total = 0
self.rtt_total_sq = 0
self.rtt_count = 0
self.rtt_min = 0
self.rtt_max = 0
self.rtt_ewma = 0
# Scheduled events
self.next_cleanup = now + 60.0
self.next_stats = 0
if self.args.stats:
self.next_stats = now + self.args.stats
self.old_age_interval = 60.0
# On Windows, KeyboardInterrupt during select() will not be trapped until a socket event or timeout, so we should set
# the timeout to a short value.
if sys.platform.startswith(('win32', 'cygwin')):
self.old_age_interval = 1.0
# Test for IPv6 functionality.
self.has_ipv6 = True
if not socket.has_ipv6:
self.has_ipv6 = False
else:
# BSD jails seem to have has_ipv6 = True, but will throw "Protocol not supported" on bind. Test for this.
try:
socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
except socket.error as e:
if e.errno == errno.EPROTONOSUPPORT:
self.has_ipv6 = False
else:
raise
if self.args.send_monotonic_clock and (not clock_info.monotonic):
self.args.send_monotonic_clock = False