forked from ideawu/cpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpy.py
executable file
·73 lines (60 loc) · 1.57 KB
/
cpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# encoding=utf-8
#################################
# Author: ideawu
# Link: http://www.ideawu.net/
#################################
import sys, os
import signal
def __sigint__(n, f):
sys.exit(0)
signal.signal(signal.SIGINT, __sigint__);
def usage():
print ('Cpy - A C-like scripting language.')
print ('Copyright (c) 2012 ideawu.com')
print ('')
print ('Usage:')
print (' cpy source_file')
# 不然管道时报错
reload(sys)
sys.setdefaultencoding('utf-8')
from engine import CpyEngine
cpy = CpyEngine()
if len(sys.argv) < 2:
usage()
sys.exit(0)
is_compile = False;
if sys.argv[1] == '-c':
is_compile = True
if len(sys.argv) >= 3:
srcfile = sys.argv[2]
else:
usage()
sys.exit(0)
else:
srcfile = sys.argv[1]
if not srcfile.endswith('.cpy'):
srcfile += '.cpy'
if not os.path.exists(srcfile):
print ("File not found!: " + srcfile)
sys.exit(0)
base_dir, tail = os.path.split(srcfile)
if len(base_dir) == 0:
base_dir = '.'
dstfile = cpy.compile(srcfile, base_dir, base_dir + '/_cpy_')
#print ('-----')
#print (''.join(open(dstfile, 'r').readlines()))
#print ('-----')
dstfile = os.path.abspath(dstfile)
sys.path.append(os.path.dirname(os.path.abspath(srcfile)));
sys.path.append(os.path.dirname(os.path.abspath(dstfile)));
os.chdir(os.path.dirname(os.path.abspath(srcfile)));
#print os.getcwd();
if not is_compile:
sys.argv = sys.argv[1 :]
sys.path.insert(0, os.path.dirname(dstfile))
try:
execfile(dstfile)
except Exception:
import traceback
sys.stderr.write(traceback.format_exc())
pass