Skip to content

Commit 1fc1ce7

Browse files
committed
Use package_data and pkg_resources instead.
1 parent 7c3da7a commit 1fc1ce7

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ dist/
44
*.egg-info/
55
build/
66
.tox
7+
javac_parser/*.jar

MANIFEST.in

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
include pom.xml
2-
recursive-include src *.java
31
include javac_parser.pyi
42
include target/*.jar

javac_parser/javac_parser.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import sys
2121
import time
2222
import unittest
23+
import pkg_resources
2324
from bisect import bisect_right
2425

2526
import msgpack
@@ -28,22 +29,32 @@
2829

2930

3031
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)
3135

3236

3337
def find_jar_path():
3438
"Tries to find where the lexer JAR is."
3539
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+
3748
# 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))
3950
# 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))
4152

4253
for path in paths:
4354
if os.path.exists(path):
4455
return path
4556
# 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
4758

4859

4960
JAR_PATH = find_jar_path()
@@ -53,15 +64,19 @@ class Java(object):
5364
@staticmethod
5465
def _build_jar():
5566
"""
56-
Rebuilds the jar.
67+
Rebuilds the JAR from source, using Maven.
5768
"""
69+
from shutil import copyfile
5870
py4j_jar = os.path.join(sys.prefix, 'share/py4j/py4j0.10.6.jar')
5971
subprocess.check_call("mvn install:install-file -Dfile=" + py4j_jar +
6072
" -DgroupId=py4j -DartifactId=py4j"
6173
" -Dversion=0.10.6 -Dpackaging=jar"
6274
" -DgeneratePom=true", shell=True)
6375
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))
6580

6681
def __init__(self):
6782
if not os.path.isfile(JAR_PATH):

setup.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
HERE = os.path.dirname(os.path.abspath(__file__))
1414
TEST_PATH = os.path.join(HERE, 'tests')
15-
JAR_PATH = os.path.join(HERE, "target", "lex-java-1.0-SNAPSHOT-jar-with-dependencies.jar")
15+
RELATIVE_JAR_PATH = os.path.join("target", "lex-java-1.0-SNAPSHOT-jar-with-dependencies.jar")
16+
JAR_PATH = os.path.join(HERE, RELATIVE_JAR_PATH)
1617

1718

1819
class PostDevelopCommand(develop):
@@ -69,8 +70,10 @@ def version():
6970
'py4j==0.10.6',
7071
'msgpack-python>=0.4.8'
7172
],
72-
include_package_data=True,
73-
data_files=[('share/javac-parser', [JAR_PATH])],
73+
package_data={
74+
NAME: [RELATIVE_JAR_PATH]
75+
},
76+
test_suite='setup.simple_test_suite',
7477

7578
license='AGPL3+',
7679
keywords='java javac parser scanner lexer tokenizer',
@@ -81,5 +84,4 @@ def version():
8184
cmdclass={
8285
'develop': PostDevelopCommand,
8386
},
84-
test_suite='setup.simple_test_suite',
8587
)

0 commit comments

Comments
 (0)