changeset 15:c0e925ae721e

Flush stdout during update; initial work on memory usage control
author Oleg Oshmyan <chortos@inbox.lv>
date Tue, 01 Jun 2010 21:57:05 +0000
parents 28b1f4853968
children f2279b7602d3
files test-svn.py
diffstat 1 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/test-svn.py	Sat Mar 13 00:04:57 2010 +0000
+++ b/test-svn.py	Tue Jun 01 21:57:05 2010 +0000
@@ -14,11 +14,24 @@
 parser.add_option('-x', '--auto-exit', dest='pause', action='store_false', default=True, help='do not wait for a key to be pressed when finished testing')
 parser.add_option('-p', '--python', action='store_true', default=False, help='always parse all positional arguments as a single Python expression (including the first argument even if it names an executable file)')
 parser.add_option('-t', '--detect-time', dest='autotime', action='store_true', default=False, help='spend a second detecting the most precise time measurement function')
+parser.add_option('-b', dest='builtin', action='store_true', default=False)
 
 options, args = parser.parse_args()
 parser.destroy()
 del parser
 
+if options.builtin:
+	try:
+		if args[0] == 'run':
+			import resource
+			maxmemory = int(args[1])
+			resource.setrlimit(resource.RLIMIT_AS, (maxmemory*1024**2, maxmemory*1024**2))
+			os.execv(args[2], args[2:])
+		else:
+			sys.exit(2)
+	except:
+		sys.exit(2)
+
 def update():
 	import urllib
 	latesttext = urllib.urlopen('http://chortos.selfip.net/~astiob/test.py/version.txt').read()
@@ -40,6 +53,7 @@
 		print 'Your copy of test.py is newer than the publicly available version.'
 		return
 	print 'A ' + update + ' update to test.py is available. Downloading...'
+	sys.stdout.flush()
 	urllib.urlretrieve('http://chortos.selfip.net/~astiob/test.py/test.py', 'test.py')
 	print 'Downloaded and installed. Now you are using test.py ' + latesttext + '.'
 
@@ -47,6 +61,19 @@
 	update()
 	sys.exit()
 
+try:
+	import resource
+	memlimit = True
+	def call(name):
+		pid = os.fork()
+		if not pid:
+			resource.setrlimit(resource.RLIMIT_AS, (maxmemory*1024**2, maxmemory*1024**2))
+			os.execl(name)
+		else:
+			return pid
+except ImportError:
+	memlimit = False
+
 globals1 = set(globals())
 
 # Initialize some configuration variables with default values