Skip to content

Commit 05f6bb9

Browse files
committed
initial setup
0 parents  commit 05f6bb9

File tree

7 files changed

+135
-0
lines changed

7 files changed

+135
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_build
2+
*.install
3+
*.merlin

Makefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
INSTALL_ARGS := $(if $(PREFIX),--prefix $(PREFIX),)
2+
3+
build:
4+
time -p jbuilder build @install
5+
6+
install:
7+
jbuilder install $(INSTALL_ARGS)
8+
9+
uninstall:
10+
jbuilder uninstall $(INSTALL_ARGS)
11+
12+
reinstall: uninstall reinstall
13+
14+
clean:
15+
jbuilder clean
16+
17+
doc:
18+
jbuilder build @doc
19+
20+
test: build install
21+
ocaml-python
22+
23+
all: build test doc
24+
25+
.PHONY: build install uninstall reinstall clean doc test all

ocaml-python.opam

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
opam-version: "1.2"
2+
available: [ ocaml-version >= "4.05.0" ]
3+
version: "0.1"
4+
maintainer: "Steffen Smolka <[email protected]>"
5+
homepage: "http://www.cs.cornell.edu/~smolka"
6+
dev-repo: "https://github.com/smolkaj/ocaml-python.git"
7+
8+
build: ["jbuilder" "build" "-p" name "-j" jobs]
9+
build-doc: ["jbuilder" "build" "@doc" "-p" name "-j" jobs]
10+
build-test: ["jbuilder" "runtest" "-p" name "-j" jobs]
11+
install: ["jbuilder" "install" "-p" name "-j" jobs]
12+
13+
depends: [
14+
"jbuilder" {>="1.0+beta13"}
15+
"core" {>= "0.9.0"}
16+
"findlib"
17+
]

python/jbuild

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(jbuild_version 1)
2+
3+
(rule
4+
((targets (my-script.pyc))
5+
(deps (my-script.py))
6+
(action (run python3 -m compileall -b ${^}))
7+
)
8+
)
9+
10+
(install
11+
((section lib)
12+
(files (my-script.pyc))
13+
(package ocaml-python)
14+
))

python/my-script.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from __future__ import print_function
2+
import sys
3+
import fileinput
4+
5+
# just like print, but use stderr instead of stdout
6+
def eprint(*args, **kwargs):
7+
print(*args, file=sys.stderr, **kwargs)
8+
9+
def main():
10+
eprint("[python] waiting for input...")
11+
for line in sys.stdin:
12+
eprint("[python] received line: \"%s\"" % line.strip())
13+
# reply (and flush immediately)
14+
print("Python loves you, OCaml!", flush=True)
15+
eprint("[python] input pipe closed; shuttind down...")
16+
exit(0)
17+
18+
if __name__ == '__main__':
19+
main()

src/jbuild

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(jbuild_version 1)
2+
3+
(executable
4+
((name main)
5+
(public_name ocaml-python)
6+
(libraries (
7+
core
8+
findlib
9+
))
10+
))

src/main.ml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
open Core
2+
3+
(* The name of this package. Ideally, should not be hard-coded. *)
4+
let pkg_name = "ocaml-python"
5+
6+
(* The name of the python script to invoke. *)
7+
let script_name = "my-script.pyc"
8+
9+
(* At runtime, we look for the python script in the opam library folder of our
10+
package. *)
11+
let pyscript = match Findlib.package_directory pkg_name with
12+
| dir ->
13+
dir ^ "/" ^ script_name
14+
| exception Findlib.No_such_package _ ->
15+
failwith ("missing ocamlfind dependency: " ^ pkg_name)
16+
17+
18+
let () = begin
19+
(* start Python 3 process *)
20+
let cmd = "python3 " ^ pyscript in
21+
let (from_py, to_py) = Unix.open_process cmd in
22+
23+
(* send some stuff to python *)
24+
printf "[ocaml] sending message to python...\n%!";
25+
(* force out_channel to flush immediately with `%!` *)
26+
Out_channel.fprintf to_py
27+
"Hi there, Python! This is 🐪. My favorite number is %d.\n%!" 42;
28+
29+
(* receive stuff from python *)
30+
begin try while true do
31+
(* receive one line at a time *)
32+
printf "[ocaml] waiting for python response...\n%!";
33+
let line = In_channel.input_line_exn from_py in
34+
35+
(* Do something with line! *)
36+
(* As an example, we will just print the line to stdout and then close the
37+
pipe to Python. *)
38+
printf "[ocaml] received from Python: \"%s\"\n%!" line;
39+
40+
(* closing the pipe to Python will cause the script to terminate *)
41+
Out_channel.close to_py;
42+
done with
43+
| End_of_file -> ()
44+
end;
45+
46+
printf "[ocaml] All done. Bye bye!\n%!";
47+
end

0 commit comments

Comments
 (0)