lidavidm commented on a change in pull request #125:
URL: https://github.com/apache/arrow-cookbook/pull/125#discussion_r788048675
##########
File path: java/ext/javadoctest.py
##########
@@ -0,0 +1,57 @@
+import subprocess
+import os
+from sphinx.ext.doctest import TestcodeDirective, TestoutputDirective,
DocTestBuilder, Any, Dict, doctest, sphinx
+from sphinx.locale import __
+from subprocess import Popen, PIPE
+
+class JavaDocTestBuilder(DocTestBuilder):
+ """
+ Runs java test snippets in the documentation.
+ """
+ name = 'javadoctest'
+ epilog = __('Java testing of doctests in the sources finished, look at the
'
+ 'results in %(outdir)s/output.txt.')
+ def compile(self, code: str, name: str, type: str, flags: Any,
dont_inherit: bool) -> Any:
+ # go to project that contains all your arrow maven dependencies
+ dir_arrow_maven_project =
os.path.dirname(os.getcwd())+'/java/source/demo'
+
+ # create list of all arrow jar dependencies
+ proc_mvn_dependency = subprocess.Popen(['mvn', '-q',
'dependency:build-classpath', '-DincludeTypes=jar',
'-Dmdep.outputFile=.cp.tmp'], cwd=dir_arrow_maven_project,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
+ stdout_mvn, stderr_mvn = proc_mvn_dependency.communicate()
+ if not os.path.exists(dir_arrow_maven_project + '/.cp.tmp'):
+ raise RuntimeError(__('invalid process to create jshell
dependencies library'))
+
+ # get list of all arrow jar dependencies
+ proc_get_dependency = subprocess.Popen(['cat', '.cp.tmp'],
cwd=dir_arrow_maven_project, stdout=subprocess.PIPE, text=True)
Review comment:
…we can just read the file directly, no?
##########
File path: java/ext/javadoctest.py
##########
@@ -0,0 +1,57 @@
+import subprocess
Review comment:
Overall, this file should probably be formatted according to Arrow
Python guidelines.
https://arrow.apache.org/docs/dev/developers/python.html#coding-style
Though that tool won't quite work here, I think, but we just use flake8:
https://github.com/apache/arrow/blob/9938f708a718a1ad735a15d6ce2f61d7c5185365/dev/archery/archery/utils/lint.py#L216
##########
File path: java/ext/javadoctest.py
##########
@@ -0,0 +1,57 @@
+import subprocess
+import os
+from sphinx.ext.doctest import TestcodeDirective, TestoutputDirective,
DocTestBuilder, Any, Dict, doctest, sphinx
+from sphinx.locale import __
+from subprocess import Popen, PIPE
+
+class JavaDocTestBuilder(DocTestBuilder):
+ """
+ Runs java test snippets in the documentation.
+ """
+ name = 'javadoctest'
+ epilog = __('Java testing of doctests in the sources finished, look at the
'
+ 'results in %(outdir)s/output.txt.')
+ def compile(self, code: str, name: str, type: str, flags: Any,
dont_inherit: bool) -> Any:
+ # go to project that contains all your arrow maven dependencies
+ dir_arrow_maven_project =
os.path.dirname(os.getcwd())+'/java/source/demo'
Review comment:
We should try to use pathlib or at least os.path.join throughout.
##########
File path: java/ext/javadoctest.py
##########
@@ -0,0 +1,57 @@
+import subprocess
+import os
+from sphinx.ext.doctest import TestcodeDirective, TestoutputDirective,
DocTestBuilder, Any, Dict, doctest, sphinx
+from sphinx.locale import __
+from subprocess import Popen, PIPE
+
+class JavaDocTestBuilder(DocTestBuilder):
+ """
+ Runs java test snippets in the documentation.
+ """
+ name = 'javadoctest'
+ epilog = __('Java testing of doctests in the sources finished, look at the
'
+ 'results in %(outdir)s/output.txt.')
+ def compile(self, code: str, name: str, type: str, flags: Any,
dont_inherit: bool) -> Any:
+ # go to project that contains all your arrow maven dependencies
+ dir_arrow_maven_project =
os.path.dirname(os.getcwd())+'/java/source/demo'
+
+ # create list of all arrow jar dependencies
+ proc_mvn_dependency = subprocess.Popen(['mvn', '-q',
'dependency:build-classpath', '-DincludeTypes=jar',
'-Dmdep.outputFile=.cp.tmp'], cwd=dir_arrow_maven_project,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
+ stdout_mvn, stderr_mvn = proc_mvn_dependency.communicate()
+ if not os.path.exists(dir_arrow_maven_project + '/.cp.tmp'):
Review comment:
what actually generates this `.cp.tmp`?
##########
File path: java/ext/javadoctest.py
##########
@@ -0,0 +1,57 @@
+import subprocess
+import os
+from sphinx.ext.doctest import TestcodeDirective, TestoutputDirective,
DocTestBuilder, Any, Dict, doctest, sphinx
+from sphinx.locale import __
+from subprocess import Popen, PIPE
+
+class JavaDocTestBuilder(DocTestBuilder):
+ """
+ Runs java test snippets in the documentation.
+ """
+ name = 'javadoctest'
+ epilog = __('Java testing of doctests in the sources finished, look at the
'
+ 'results in %(outdir)s/output.txt.')
+ def compile(self, code: str, name: str, type: str, flags: Any,
dont_inherit: bool) -> Any:
+ # go to project that contains all your arrow maven dependencies
+ dir_arrow_maven_project =
os.path.dirname(os.getcwd())+'/java/source/demo'
+
+ # create list of all arrow jar dependencies
+ proc_mvn_dependency = subprocess.Popen(['mvn', '-q',
'dependency:build-classpath', '-DincludeTypes=jar',
'-Dmdep.outputFile=.cp.tmp'], cwd=dir_arrow_maven_project,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
+ stdout_mvn, stderr_mvn = proc_mvn_dependency.communicate()
+ if not os.path.exists(dir_arrow_maven_project + '/.cp.tmp'):
+ raise RuntimeError(__('invalid process to create jshell
dependencies library'))
+
+ # get list of all arrow jar dependencies
+ proc_get_dependency = subprocess.Popen(['cat', '.cp.tmp'],
cwd=dir_arrow_maven_project, stdout=subprocess.PIPE, text=True)
+ stdout_dependency, stderr_dependency =
proc_get_dependency.communicate()
+ if not stdout_dependency:
+ raise RuntimeError(__('invalid process to list jshell dependencies
library'))
+
+ # execute java testing code thru jshell and read output
+ proc_java_arrow_code = subprocess.Popen(['echo', '' + code],
stdout=subprocess.PIPE)
+ proc_jshell_process = subprocess.Popen(['jshell', '--class-path',
stdout_dependency, '-'], stdin=proc_java_arrow_code.stdout,
stdout=subprocess.PIPE, text=True)
Review comment:
Is Jshell really unable to take in a file?
Regardless, there's no need to use echo. `subprocess` lets you pass in
`subprocess.PIPE` to `stdin` and then we can write to it directly.
##########
File path: java/ext/javadoctest.py
##########
@@ -0,0 +1,64 @@
+from sphinx.ext.doctest import *
+from sphinx.locale import __
+import subprocess
+from subprocess import Popen,PIPE
+import os
+
+class JavaTestDirective(TestDirective):
+ pass
+
+class JavaDocTestBuilder(DocTestBuilder):
+ """
+ Runs java test snippets in the documentation.
+ """
+ name = 'javadoctest'
+ epilog = __('Java testing of doctests in the sources finished, look at the
'
+ 'results in %(outdir)s/output.txt.')
+ def compile(self, code: str, name: str, type: str, flags: Any,
dont_inherit: bool) -> Any:
+ # go to project that contains all your arrow maven dependencies
+ cwd = os.getcwd()
+ os.chdir('./source/demo')
+
+ # create list of all arrow jar dependencies
+ proc_mvn_dependency = subprocess.Popen(['mvn', '-q',
'dependency:build-classpath', '-DincludeTypes=jar',
'-Dmdep.outputFile=.cp.tmp'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True)
+ stdout_mvn, stderr_mvn = proc_mvn_dependency.communicate()
+ if not os.path.exists('.cp.tmp'):
+ raise RuntimeError(__('invalid process to create jshell
dependencies library'))
+
+ # get list of all arrow jar dependencies
+ proc_get_dependency = subprocess.Popen(['cat', '.cp.tmp'],
stdout=subprocess.PIPE, text=True)
+ stdout_dependency, stderr_dependency =
proc_get_dependency.communicate()
+ if not stdout_dependency:
+ raise RuntimeError(__('invalid process to list jshell dependencies
library'))
+
+ # execute java testing code thru jshell and read output
+ proc_java_arrow_code = subprocess.Popen(['echo', '' + code],
stdout=subprocess.PIPE)
+ proc_jshell_process = subprocess.Popen(['jshell', '--class-path',
stdout_dependency, '-'], stdin=proc_java_arrow_code.stdout,
stdout=subprocess.PIPE, text=True)
+ proc_java_arrow_code.stdout.close()
+ stdout_java_arrow, stderr_java_arrow =
proc_jshell_process.communicate()
+ if stderr_java_arrow:
+ raise RuntimeError(__('invalid process to run jshell por arrow
project'))
+
+ # continue with python logic code to do java output validation battle
tested
+ output = 'print('+stdout_java_arrow+')'
+
+ # go to default directory
+ os.chdir(cwd)
+
+ # continue with sphinx default logic
+ return compile(output, name, self.type, flags, dont_inherit)
+
+def setup(app: "ArrowSphinx") -> Dict[str, Any]:
+ app.add_directive('testcode', TestcodeDirective)
+ app.add_directive('testoutput', TestoutputDirective)
Review comment:
Ah, sorry, I missed the import at top, thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]