Skip to content

Commit

Permalink
Switch build to setuptools. (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsteffensmeier committed May 10, 2022
1 parent cfca63f commit 0b0e234
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Some benefits of embedding CPython in a JVM:

Installation
------------
Simply run ``pip install jep`` or download the source and run ``python setup.py build install``.
Simply run ``pip install jep`` or download the source and run ``pip install .``.
Building and installing require the JDK, Python, and optionally numpy to be installed beforehand.

Dependencies
Expand Down
3 changes: 1 addition & 2 deletions commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import print_function
# https://github.com/pypa/setuptools/issues/2928
from distutils.command.build import build
from commands.java import build_java


class jep_build(build):
sub_commands = [
('setup_java', None),
Expand Down
4 changes: 2 additions & 2 deletions commands/build_ext.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
Fork of distutils' build_ext command to handle using a
Fork of setuptools' build_ext command to handle using a
patched version of the msvc9compiler.
"""

import os.path
from distutils.command.build_ext import build_ext as old_build_ext
from setuptools.command.build_ext import build_ext as old_build_ext


class build_ext (old_build_ext):
Expand Down
1 change: 0 additions & 1 deletion commands/clean.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from distutils.command.clean import clean
import shutil

Expand Down
2 changes: 1 addition & 1 deletion commands/dist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from distutils.dist import Distribution
from setuptools.dist import Distribution


class JepDistribution(Distribution):
Expand Down
4 changes: 2 additions & 2 deletions commands/install_lib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Fork of distutils' install_lib command that installs the native
Fork of setuptools' install_lib command that installs the native
jep library, the jar file, and the python files to the
install_dir + 'jep', typically site-packages/jep.
"""
Expand All @@ -8,7 +8,7 @@
from commands.util import is_windows
from commands.link_util import link_native_lib
from commands.python import get_python_lib_dir
from distutils.command.install_lib import install_lib
from setuptools.command.install_lib import install_lib
import os


Expand Down
27 changes: 12 additions & 15 deletions commands/java.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from __future__ import print_function

from distutils import log
from distutils.cmd import Command
from distutils.spawn import spawn, find_executable
from distutils.dep_util import newer_group
from setuptools import Command
from setuptools.dep_util import newer_group

import shutil
import os
import fnmatch
import traceback
import sys
import logging

from commands.util import configure_error
from commands.util import is_osx
Expand Down Expand Up @@ -171,7 +168,7 @@ def initialize_options(self):

def run(self):
if skip_java_build(self):
log.debug('skipping Java build (up-to-date)')
logging.debug('skipping Java build (up-to-date)')
return
warning('Using JAVA_HOME:', get_java_home())

Expand Down Expand Up @@ -220,8 +217,8 @@ def finalize_options(self):
def build(self, *jclasses):
jep = [x for x in list(*jclasses) if not x.startswith('src{0}test{0}java{0}'.format(os.sep))]
tests = [x for x in list(*jclasses) if x.startswith('src{0}test{0}java{0}'.format(os.sep))]
spawn([self.javac, '-deprecation', '-d', build_java.outdir, '-h', build_java.headeroutdir, '-classpath', 'src'] + jep)
spawn([self.javac, '-deprecation', '-d', build_java.testoutdir, '-classpath', '{0}{1}src'.format(build_java.outdir, os.pathsep)] + tests)
self.spawn([self.javac, '-deprecation', '-d', build_java.outdir, '-h', build_java.headeroutdir, '-classpath', 'src'] + jep)
self.spawn([self.javac, '-deprecation', '-d', build_java.testoutdir, '-classpath', '{0}{1}src'.format(build_java.outdir, os.pathsep)] + tests)
# Copy the source files over to the build directory to make src.jar's.
self.copySrc('jep', jep)
self.copySrc('jep.test', tests)
Expand All @@ -230,7 +227,7 @@ def run(self):
if not skip_java_build(self):
self.build(self.java_files)
else:
log.debug('skipping building .class files (up to date)')
logging.debug('skipping building .class files (up to date)')

def copySrc(self, app, files):
for src in files:
Expand Down Expand Up @@ -272,13 +269,13 @@ def build(self):
os.makedirs(dest_dir)
shutil.copy(src, dest)

spawn([self.jar, '-cf', 'build/java/jep-{0}-sources.jar'.format(self.version), '-C', 'build/java/jep.src/main/java', 'jep'])
spawn([self.jar, '-cfe', 'build/java/jep-{0}.jar'.format(self.version), 'jep.Run', '-C', 'build/java', 'jep'])
spawn([self.jar, '-cf', 'build/java/jep-{0}-test-sources.jar'.format(self.version), '-C', 'build/java/jep.test.src/test/java', 'jep'])
spawn([self.jar, '-cfe', 'build/java/jep-{0}-test.jar'.format(self.version), 'test.jep.Test', '-C', 'build/java/test', 'jep'])
self.spawn([self.jar, '-cf', 'build/java/jep-{0}-sources.jar'.format(self.version), '-C', 'build/java/jep.src/main/java', 'jep'])
self.spawn([self.jar, '-cfe', 'build/java/jep-{0}.jar'.format(self.version), 'jep.Run', '-C', 'build/java', 'jep'])
self.spawn([self.jar, '-cf', 'build/java/jep-{0}-test-sources.jar'.format(self.version), '-C', 'build/java/jep.test.src/test/java', 'jep'])
self.spawn([self.jar, '-cfe', 'build/java/jep-{0}-test.jar'.format(self.version), 'test.jep.Test', '-C', 'build/java/test', 'jep'])

def run(self):
if not skip_java_build(self):
self.build()
else:
log.debug('skipping packing jar files (up to date)')
logging.debug('skipping packing jar files (up to date)')
5 changes: 2 additions & 3 deletions commands/javadoc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from distutils.cmd import Command
from distutils.spawn import spawn
from setuptools import Command

import os.path

Expand All @@ -25,7 +24,7 @@ def finalize_options(self):
self.java_files = self.distribution.java_files

def run(self):
spawn([self.javadoc, '-public', '-notimestamp',
self.spawn([self.javadoc, '-public', '-notimestamp',
'-d', os.path.join(javadoc.outdir, self.version),
'-sourcepath', 'src/main/java',
'-subpackages', 'jep' ])
11 changes: 5 additions & 6 deletions commands/link_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

from commands.util import is_osx
from commands.util import is_windows
from distutils import sysconfig
from distutils.spawn import spawn

import os
import os.path
import shutil

import subprocess

def link_native_lib(output_dir, jep_lib_path):
"""
Expand All @@ -36,15 +35,15 @@ def link_native_lib(output_dir, jep_lib_path):
# Apple says to put the file at /Library/Java/Extensions/libjep.jnilib,
# which is good for a permanent install but not so good when using
# virtualenv or testing.
spawn(['ln',
subprocess.check_call(['ln',
'-sf',
'{0}'.format(jep_lib),
'{0}'.format(os.path.join(output_dir, 'libjep.jnilib')), ])

else:
# Otherwise, distutils outputs 'jep.so' which needs to be linked
# Otherwise, setuptools outputs 'jep.so' which needs to be linked
# to 'libjep.so'. Otherwise the JVM will not find the library.
spawn(['ln',
subprocess.check_call(['ln',
'-sf',
'{0}'.format(jep_lib),
'{0}'.format(os.path.join(output_dir, 'libjep.so')),
Expand Down
24 changes: 14 additions & 10 deletions commands/scripts.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"""
Fork of distutils' build_scripts command to handle creating
Fork of setuptools' build_scripts command to handle creating
jep shell script and/or jep.bat script with proper setup to ensure
the jep interpreter can be run from the command line.
"""

import logging
import os
import sys
import sysconfig

from stat import ST_MODE
from distutils.core import Command
from setuptools import Command
from distutils.dep_util import newer
from distutils.util import convert_path
from distutils import log
from distutils import sysconfig
from commands.util import is_osx
from commands.util import is_windows
from commands.python import get_libpython
Expand Down Expand Up @@ -59,6 +59,9 @@ def copy_scripts(self):
self.mkpath(self.build_dir)
outfiles = []


bdist_egg = self.get_finalized_command('bdist_egg')

install = self.get_finalized_command('install')
context = dict(
version=self.distribution.metadata.get_version(),
Expand All @@ -70,6 +73,7 @@ def copy_scripts(self):
ld_preload='',
pythonhome='',
pythonexecutable='',
egg_dir=os.path.basename(bdist_egg.egg_output),
)

if os.environ.get('VIRTUAL_ENV'):
Expand Down Expand Up @@ -108,12 +112,12 @@ def copy_scripts(self):
for script in self.scripts:
if is_windows():
script = '{0}.bat'.format(script)
script = convert_path(script)
script = os.path.normpath(script)
outfile = os.path.join(self.build_dir, os.path.basename(script))
outfiles.append(outfile)

if not self.force and not newer(script, outfile):
log.debug("not copying %s (up-to-date)", script)
logging.debug("not copying %s (up-to-date)", script)
continue

# Always open the file, but ignore failures in dry-run mode --
Expand All @@ -126,7 +130,7 @@ def copy_scripts(self):
raise
f = None

log.info("copying and adjusting %s -> %s", script,
logging.info("copying and adjusting %s -> %s", script,
self.build_dir)
if not self.dry_run:
outf = open(outfile, "w")
Expand All @@ -138,12 +142,12 @@ def copy_scripts(self):
if os.name == 'posix':
for file in outfiles:
if self.dry_run:
log.info("changing mode of %s", file)
logging.info("changing mode of %s", file)
else:
oldmode = os.stat(file)[ST_MODE] & 0o7777
newmode = (oldmode | 0o555) & 0o7777
if newmode != oldmode:
log.info("changing mode of %s from %o to %o",
logging.info("changing mode of %s from %o to %o",
file, oldmode, newmode)
os.chmod(file, newmode)

Expand Down
6 changes: 3 additions & 3 deletions commands/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import print_function
from distutils.cmd import Command
from distutils import sysconfig
from setuptools import Command
# https://github.com/pypa/setuptools/issues/2698
from distutils.errors import DistutilsExecError
from commands.util import configure_error
from commands.util import is_osx
Expand All @@ -11,6 +10,7 @@
import os
import os.path
import sys
import sysconfig


class test(Command):
Expand Down
3 changes: 1 addition & 2 deletions commands/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import print_function
from collections import namedtuple
from distutils.util import get_platform
from sysconfig import get_platform
import subprocess
import sys

Expand Down
8 changes: 2 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#!/usr/bin/env python
from __future__ import print_function
from __future__ import absolute_import
import codecs

import os

import sysconfig

from distutils.core import setup, Extension
# if you want to build wheels, use setuptools instead of distutils
# otherwise stick with distutils to avoid extra dependencies
#from setuptools import setup, Extension
from setuptools import setup, Extension

from commands import jep_build
from commands.clean import really_clean
Expand Down Expand Up @@ -143,5 +138,6 @@ def read_file(name):
'clean': really_clean,
'test': test,
},
zip_safe=False
)

13 changes: 10 additions & 3 deletions src/main/scripts/jep
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
{ld_library_path}
{ld_preload}

cp="{install_lib}jep/jep-{version}.jar"
jep_dir={install_lib}jep

if [ ! -d "$jep_dir" ]
then
jep_dir={install_lib}{egg_dir}/jep
fi

cp="$jep_dir/jep-{version}.jar"
if test "x$CLASSPATH" != "x"; then
cp="$cp":"$CLASSPATH"
fi

jni_path="{install_lib}jep"
jni_path=$jep_dir

args=$*
if test "x$args" = "x"; then
args="{install_lib}jep/console.py"
args="$jep_dir/console.py"
fi

exec java -classpath "$cp" -Djava.library.path="$jni_path" jep.Run $args

0 comments on commit 0b0e234

Please sign in to comment.