Das Modul »slr«
slr.py
"""
Stefan Ram's utilities module.
Version: 2020-02-02T01:11:29+01:00.
This module contains some utilties by Stefan Ram.
Mathematical Functions
----------------------
================== ==================================================
Function Description
================== ==================================================
sign the sign (signum) of the argument as -1, 0, or +1
================== ==================================================
Exceptions
----------
No exception is defined.
"""
import subprocess
import sys
import os.path
import inspect
import datetime
import os
import tempfile
def stamp():
return datetime.datetime.now().replace( microsecond=0 ).isoformat( 'T' )
def caller_name():
return inspect.currentframe().f_back.f_back.f_code.co_name
class default_context_class:
def __init__( __self__ ):
pass
def report( __self__, *s, **x ):
"""report, in a somewhat verbose manner, what is happening."""
print( ':'+ caller_name() + ' ' + stamp()+ ":report:", *s, **x )
def debug( __self__, *s, **x ):
print( ':'+ caller_name() + ' ' + stamp()+ ":debug:", *s, **x )
def error( __self__, *s, **x ):
print( ':'+ caller_name() + ' ' + stamp()+ ":error:", *s, **x )
default_context = default_context_class()
def mswin_execute_command( *args, context=default_context ):
print(args)
batch_file = tempfile.NamedTemporaryFile( 'wb', suffix='.cmd', delete=False, dir='.' )
batch_file.write( b"@chcp 65001 > nul\r\n" )
batch_file.write( b"@" )
command = subprocess.list2cmdline( args ).encode( "utf-8" )
batch_file.write( command )
batch_file.close()
try:
context.report( f'EXECUTING {command}' )
with subprocess.Popen \
( 'cmd.exe /c ' + batch_file.name,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT ) as p:
for line in p.stdout:
print( line.decode( 'utf-8' ), end='' )
context.report( 'returncode = ', p.returncode )
finally:
os.remove( batch_file.name )
return p.returncode
mswin_execute_command( "dir" )
def mswin_install_python_package( name ):
'''
e.g., pywin32
'''
mswin_execute_command( sys.executable, '-m', 'pip', 'install', name, '--force-reinstall' )
def execute_pywin32_postinstall(x):
"""# im scripts folder aufrufen:"""
execute_os_command( sys.executable, os.path.dirname(sys.executable)+r'\Scripts\pywin32_postinstall.py', '-install' )
print( 'You now need to restart your Python interpreter!' )