20
20
import sys
21
21
import time
22
22
import unittest
23
+ import pkg_resources
23
24
from bisect import bisect_right
24
25
25
26
import msgpack
28
29
29
30
30
31
SOURCE_PATH = os .path .dirname (os .path .abspath (__file__ ))
32
+ JAR_FILE = 'lex-java-1.0-SNAPSHOT-jar-with-dependencies.jar'
33
+ GENERATED_JAR_PATH = os .path .join (os .path .dirname (SOURCE_PATH ),
34
+ "target" , JAR_FILE )
31
35
32
36
33
37
def find_jar_path ():
34
38
"Tries to find where the lexer JAR is."
35
39
paths = []
36
- jar_file = 'lex-java-1.0-SNAPSHOT-jar-with-dependencies.jar'
40
+
41
+ # Try to find the JAR file as part of the package.
42
+ try :
43
+ return pkg_resources .resource_filename (__name__ , JAR_FILE )
44
+ except OSError :
45
+ from warnings import warn
46
+ warn ("Could not find " + JAR_FILE + " in package. " )
47
+
37
48
# setup.py local installation
38
- paths .append (os .path .join (SOURCE_PATH , "share/javac-parser" , jar_file ))
49
+ paths .append (os .path .join (SOURCE_PATH , "share/javac-parser" , JAR_FILE ))
39
50
# pip install
40
- paths .append (os .path .join (sys .prefix , "share/javac-parser" , jar_file ))
51
+ paths .append (os .path .join (sys .prefix , "share/javac-parser" , JAR_FILE ))
41
52
42
53
for path in paths :
43
54
if os .path .exists (path ):
44
55
return path
45
56
# Maven. The default in case the JAR must be rebuilt.
46
- return os . path . join ( os . path . dirname ( SOURCE_PATH ), "target" , jar_file )
57
+ return GENERATED_JAR_PATH
47
58
48
59
49
60
JAR_PATH = find_jar_path ()
@@ -53,15 +64,19 @@ class Java(object):
53
64
@staticmethod
54
65
def _build_jar ():
55
66
"""
56
- Rebuilds the jar .
67
+ Rebuilds the JAR from source, using Maven .
57
68
"""
69
+ from shutil import copyfile
58
70
py4j_jar = os .path .join (sys .prefix , 'share/py4j/py4j0.10.6.jar' )
59
71
subprocess .check_call ("mvn install:install-file -Dfile=" + py4j_jar +
60
72
" -DgroupId=py4j -DartifactId=py4j"
61
73
" -Dversion=0.10.6 -Dpackaging=jar"
62
74
" -DgeneratePom=true" , shell = True )
63
75
subprocess .check_call ("mvn package" , shell = True )
64
- assert os .path .isfile (JAR_PATH )
76
+ assert os .path .isfile (GENERATED_JAR_PATH )
77
+ # The JAR file NEEDS to be in the package directory for distribution,
78
+ # so copy it over there.
79
+ copyfile (GENERATED_JAR_PATH , os .path .join (SOURCE_PATH , JAR_FILE ))
65
80
66
81
def __init__ (self ):
67
82
if not os .path .isfile (JAR_PATH ):
0 commit comments