diff --git a/overviewer.py b/overviewer.py index 71bdb14..b33acfa 100755 --- a/overviewer.py +++ b/overviewer.py @@ -66,6 +66,7 @@ def main(): parser.add_option("-p", "--processes", dest="procs", action="store", type="int", help="The number of local worker processes to spawn. Defaults to the number of CPU cores your computer has") + parser.add_option("--pid", dest="pid", action="store", help="Specify the pid file to use.") # Options that only apply to the config-less render usage parser.add_option("--rendermodes", dest="rendermodes", action="store", help="If you're not using a config file, specify which rendermodes to render with this option. This is a comma-separated list.") @@ -138,6 +139,18 @@ def main(): print("(build info not found)") return 0 + if options.pid: + if os.path.exists(options.pid): + try: + with open(options.pid, 'r') as fpid: + pid = int(fpid.read()) + if util.pid_exists(pid): + print("Already running (pid exists) - exiting..") + return 0 + except IOError, ValueError: + pass + with open(options.pid,"w") as f: + f.write(str(os.getpid())) # if --check-terrain was specified, but we have NO config file, then we cannot # operate on a custom texture path. we do terrain checking with a custom texture # pack later on, after we've parsed the config file @@ -493,6 +506,8 @@ dir but you forgot to put quotes around the directory, since it contains spaces. logging.debug("Final cache stats:") for c in caches: logging.debug("\t%s: %s hits, %s misses", c.__class__.__name__, c.hits, c.misses) + if options.pid: + os.remove(options.pid) return 0 diff --git a/overviewer_core/util.py b/overviewer_core/util.py index d936cba..6b81d8b 100644 --- a/overviewer_core/util.py +++ b/overviewer_core/util.py @@ -24,7 +24,7 @@ import platform from string import hexdigits from subprocess import Popen, PIPE from itertools import cycle, islice, product - +import errno def get_program_path(): if hasattr(sys, "frozen") or imp.is_frozen("__main__"): return os.path.dirname(sys.executable) @@ -391,3 +391,14 @@ try: OrderedDict = collections.OrderedDict except (ImportError, AttributeError): pass + +def pid_exists(pid): # http://stackoverflow.com/a/6940314/1318435 + """Check whether pid exists in the current process table.""" + if pid < 0: + return False + try: + os.kill(pid, 0) + except OSError, e: + return e.errno != errno.ESRCH + else: + return True \ No newline at end of file