Commit d6d05d02 by plusvic

Allow passing custom build options to the “build” command.

After commit eba22f3e you couldn’t  pass options like —dynamic-linking and—enable-profiling to the build command anymore. Those options were accepted by the build_ext command instead. With this change you can pass the options to both build and build_ext.
parent eaba8c1a
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
# #
from distutils.command.build import build
from distutils.command.build_ext import build_ext from distutils.command.build_ext import build_ext
from setuptools import setup, Command, Extension from setuptools import setup, Command, Extension
from codecs import open from codecs import open
...@@ -29,6 +30,21 @@ import tempfile ...@@ -29,6 +30,21 @@ import tempfile
import shutil import shutil
import subprocess import subprocess
OPTIONS = [
('dynamic-linking', None, 'link dynamically against libyara'),
('enable-cuckoo', None, 'enable "cuckoo" module'),
('enable-magic', None, 'enable "magic" module'),
('enable-profiling', None, 'enable profiling features')]
BOOLEAN_OPTIONS = [
'dynamic-linking',
'enable-cuckoo',
'enable-magic',
'enable-profiling']
@contextlib.contextmanager @contextlib.contextmanager
def muted(*streams): def muted(*streams):
"""A context manager to redirect stdout and/or stderr to /dev/null. """A context manager to redirect stdout and/or stderr to /dev/null.
...@@ -66,16 +82,29 @@ def has_function(function_name, libraries=None): ...@@ -66,16 +82,29 @@ def has_function(function_name, libraries=None):
return result return result
class BuildCommand(build_ext): class BuildCommand(build):
user_options = build.user_options + OPTIONS
boolean_options = build.boolean_options + BOOLEAN_OPTIONS
def initialize_options(self):
build.initialize_options(self)
self.dynamic_linking = None
self.enable_magic = None
self.enable_cuckoo = None
self.enable_profiling = None
def finalize_options(self):
build.finalize_options(self)
user_options = build_ext.user_options + [ class BuildExtCommand(build_ext):
('dynamic-linking', None,'link dynamically against libyara'),
('enable-cuckoo', None,'enable "cuckoo" module'),
('enable-magic', None,'enable "magic" module'),
('enable-profiling', None,'enable profiling features')]
boolean_options = build_ext.boolean_options + [ user_options = build_ext.user_options + OPTIONS
'dynamic-linking', 'enable-cuckoo', 'enable-magic', 'enable-profiling'] boolean_options = build_ext.boolean_options + BOOLEAN_OPTIONS
def initialize_options(self): def initialize_options(self):
...@@ -88,6 +117,16 @@ class BuildCommand(build_ext): ...@@ -88,6 +117,16 @@ class BuildCommand(build_ext):
def finalize_options(self): def finalize_options(self):
build_ext.finalize_options(self) build_ext.finalize_options(self)
# If the build_ext command was invoked by the build command, take the
# values for these options from the build command.
self.set_undefined_options('build',
('dynamic_linking', 'dynamic_linking'),
('enable_magic', 'enable_magic'),
('enable_cuckoo', 'enable_cuckoo'),
('enable_profiling', 'enable_profiling'))
if self.enable_magic and self.dynamic_linking: if self.enable_magic and self.dynamic_linking:
raise distutils.errors.DistutilsOptionError( raise distutils.errors.DistutilsOptionError(
'--enable-magic can''t be used with --dynamic-linking') '--enable-magic can''t be used with --dynamic-linking')
...@@ -175,36 +214,36 @@ class BuildCommand(build_ext): ...@@ -175,36 +214,36 @@ class BuildCommand(build_ext):
class UpdateCommand(Command): class UpdateCommand(Command):
"""Update libyara source. """Update libyara source.
This is normally only run by packagers to make a new release.
"""
user_options = []
This is normally only run by packagers to make a new release. def initialize_options(self):
""" pass
user_options = []
def initialize_options(self): def finalize_options(self):
pass pass
def finalize_options(self): def run(self):
pass subprocess.check_call(['git', 'stash'], cwd='yara')
def run(self): subprocess.check_call(['git', 'submodule', 'init'])
subprocess.check_call(['git', 'stash'], cwd='yara') subprocess.check_call(['git', 'submodule', 'update'])
subprocess.check_call(['git', 'submodule', 'init']) subprocess.check_call(['git', 'reset', '--hard'], cwd='yara')
subprocess.check_call(['git', 'submodule', 'update']) subprocess.check_call(['git', 'clean', '-x', '-f', '-d'], cwd='yara')
subprocess.check_call(['git', 'reset', '--hard'], cwd='yara') subprocess.check_call(['git', 'checkout', 'master'], cwd='yara')
subprocess.check_call(['git', 'clean', '-x', '-f', '-d'], subprocess.check_call(['git', 'pull'], cwd='yara')
cwd='yara') subprocess.check_call(['git', 'fetch', '--tags'], cwd='yara')
subprocess.check_call(['git', 'checkout', 'master'], cwd='yara')
subprocess.check_call(['git', 'pull'], cwd='yara')
subprocess.check_call(['git', 'fetch', '--tags'], cwd='yara')
tag_name = 'tags/v%s' % self.distribution.metadata.version tag_name = 'tags/v%s' % self.distribution.metadata.version
subprocess.check_call(['git', 'checkout', tag_name], cwd='yara') subprocess.check_call(['git', 'checkout', tag_name], cwd='yara')
subprocess.check_call(['./bootstrap.sh'], cwd='yara') subprocess.check_call(['./bootstrap.sh'], cwd='yara')
subprocess.check_call(['./configure'], cwd='yara') subprocess.check_call(['./configure'], cwd='yara')
with open('README.rst', 'r', 'utf-8') as f: with open('README.rst', 'r', 'utf-8') as f:
...@@ -221,7 +260,8 @@ setup( ...@@ -221,7 +260,8 @@ setup(
url='https://github.com/VirusTotal/yara-python', url='https://github.com/VirusTotal/yara-python',
zip_safe=False, zip_safe=False,
cmdclass={ cmdclass={
'build_ext': BuildCommand, 'build': BuildCommand,
'build_ext': BuildExtCommand,
'update': UpdateCommand}, 'update': UpdateCommand},
ext_modules=[Extension( ext_modules=[Extension(
name='yara', name='yara',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment