Skip to content

Instantly share code, notes, and snippets.

@zaburo-ch
Created May 2, 2018 08:33
Show Gist options
  • Save zaburo-ch/9ae540efe8b0082c6d04efa8fd0d31c7 to your computer and use it in GitHub Desktop.
Save zaburo-ch/9ae540efe8b0082c6d04efa8fd0d31c7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import argparse
import os
import subprocess
import sys
parser = argparse.ArgumentParser(description='This sctipt quickly run a c++ program.')
parser.add_argument('file_path', help='path to c++ program')
# parser.add_argument('-i', metavar='input_file',
# default=None, help='path to standard input file')
parser.add_argument('-fc', action='store_true', help='force to compile')
compile_cmd = 'c++ -std=c++11 -o a.out {}'
ts_dir = os.path.expanduser("~/.qr_timestamps/")
args = parser.parse_args()
home = os.path.expanduser("~")
file_path = args.file_path
ts_name = os.path.relpath(file_path, home)
ts_name = ts_name.replace(os.sep, "_") + ".ts"
ts_path = os.path.join(ts_dir, ts_name)
if args.fc or not os.path.exists(ts_path) or os.stat(ts_path).st_mtime < os.stat(file_path).st_mtime:
# Compile
retcode = subprocess.call(compile_cmd.format(file_path), shell=True,
stdout=sys.stdout, stderr=sys.stderr)
if retcode != 0:
print('Compilng was failed. return code:', retcode)
sys.exit(retcode)
# Update timestamp
if not os.path.exists(ts_path):
if not os.path.exists(ts_dir):
os.makedirs(ts_dir)
# Make .ts file
with open(ts_path, 'w'):
pass
os.utime(ts_path, None)
# Run
sys.stderr.write('Runing\n')
sys.stderr.flush()
retcode = subprocess.call("." + os.sep + "a.out", shell=True, stdin=sys.stdin,
stdout=sys.stdout, stderr=sys.stderr)
if retcode != 0:
print('Running program was failed. return code:', retcode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment