-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathnodejs.py
More file actions
executable file
·108 lines (86 loc) · 2.54 KB
/
nodejs.py
File metadata and controls
executable file
·108 lines (86 loc) · 2.54 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# NodeJS Wrapper for PythonJS
# by Brett Hartshorn - copyright 2013
# License: "New BSD"
# tested with NodeJS v0.6.19
import os, sys, subprocess
PATHS = dict(
pythonscript = os.path.abspath('pythonjs'),
nodejs_bindings = os.path.abspath('nodejs/bindings'),
runtime = os.path.abspath('pythonjs.js'),
module_cache = '/tmp',
)
def python_to_pythonjs( src, module=None ):
cmdheader = '#!' + PATHS['module_cache']
if module:
assert '.' not in module
cmdheader += ';' + module
cmdheader += '\n'
cmd = ['python2', os.path.join( PATHS['pythonscript'], 'python_to_pythonjs.py')]
p = subprocess.Popen(
cmd,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE
)
stdout, stderr = p.communicate( (cmdheader + src).encode('utf-8') )
return stdout.decode('utf-8')
def pythonjs_to_javascript( src ):
p = subprocess.Popen(
['python2', os.path.join( PATHS['pythonscript'],'pythonjs.py')],
stdin = subprocess.PIPE,
stdout = subprocess.PIPE
)
stdout, stderr = p.communicate( src.encode('utf-8') )
a = stdout.decode('utf-8')
return a
def python_to_javascript( src, module=None, debug=False ):
a = python_to_pythonjs( src, module=module )
if debug: print( a )
return pythonjs_to_javascript( a )
def get_nodejs_bindings(source):
bindings = []
for line in source.splitlines():
if line.strip().startswith('from nodejs.'):
if line.strip().endswith('*'):
name = line.split('.')[-1].split()[0]
bindings.append( name )
return bindings
if __name__ == '__main__':
if len(sys.argv) == 1: ## interactive nodejs console
nodejs = subprocess.Popen(
['nodejs'],
stdin = sys.stdin,
stdout = sys.stdout,
)
nodejs.wait()
else:
cmd = ['nodejs', '/tmp/nodejs-input.js']
if len(sys.argv) > 2:
for arg in sys.argv[2:]:
print 'ARG', arg
cmd.append( arg )
script = sys.argv[1]
assert script.endswith('.py')
print 'translating script to javascript:', script
runtime = open( PATHS['runtime'], 'rb').read()
header = []
source = open(script, 'rb').read()
bindings = get_nodejs_bindings( source )
for binding in bindings:
data = open(
os.path.join( PATHS['nodejs_bindings'], binding + '.py' ),
'rb'
).read()
header.append( data )
data = python_to_javascript( '\n'.join(header) + '\n' + source )
f = open( '/tmp/nodejs-input.js', 'wb')
f.write( runtime + '\n' + data )
f.close()
#print subprocess.check_output( ['nodejs', '/tmp/nodejs-input.js'] )
print '<<running nodejs>>'
nodejs = subprocess.Popen(
cmd,
stdout = sys.stdout,
)
nodejs.wait()
print 'nodejs.py exit'