Skip to content
Snippets Groups Projects
setup.py 2.5 KiB
Newer Older
Daniel Ecer's avatar
Daniel Ecer committed
from __future__ import print_function

import os
Daniel Ecer's avatar
Daniel Ecer committed
import subprocess
import shlex
from getpass import getuser
Daniel Ecer's avatar
Daniel Ecer committed

from distutils.command.build import build  # pylint: disable=import-error, no-name-in-module

Daniel Ecer's avatar
Daniel Ecer committed
from setuptools import (
    find_packages,
    setup,
Daniel Ecer's avatar
Daniel Ecer committed
)

import numpy as np


CUSTOM_COMMANDS = [
    shlex.split(command_line) for command_line in [
        'apt-get update',
        'apt-get --assume-yes install libxml2',
        'apt-get --assume-yes install poppler-utils'
    ]
Daniel Ecer's avatar
Daniel Ecer committed
]
Daniel Ecer's avatar
Daniel Ecer committed

with open(os.path.join('requirements.txt'), 'r') as f:
    REQUIRED_PACKAGES = f.readlines()
Daniel Ecer's avatar
Daniel Ecer committed

packages = find_packages()

Daniel Ecer's avatar
Daniel Ecer committed
# This class handles the pip install mechanism.
Daniel Ecer's avatar
Daniel Ecer committed
class CustomBuild(build):
    """A build command class that will be invoked during package install.
    The package built using the current setup.py will be staged and later
    installed in the worker using `pip install package'. This class will be
    instantiated during install for this specific scenario and will trigger
    running the custom commands specified.
    """
    sub_commands = build.sub_commands + [('CustomCommands', None)]

Daniel Ecer's avatar
Daniel Ecer committed

class CustomCommands(Command):
    """A setuptools Command class able to run arbitrary commands."""

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def _run_custom_command(self, command_list):
        if getuser() != 'root' or os.environ.get('SCIENCEBEAM_GYM_NO_APT'):
            print('Skipping setup command (not root): %s' % command_list)
            return
        print('Running command: %s' % command_list)
        p = subprocess.Popen(
            command_list,
            stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
        )
        # Can use communicate(input='y\n'.encode()) if the command run requires
        # some confirmation.
        stdout_data, _ = p.communicate()
        print('Command output: %s' % stdout_data)
        if p.returncode != 0:
            raise RuntimeError(
                'Command %s failed: exit code: %s (output: %s)' %
                (command_list, p.returncode, stdout_data)
            )

    def run(self):
        for command in CUSTOM_COMMANDS:
            self._run_custom_command(command)

Daniel Ecer's avatar
Daniel Ecer committed
setup(
    name='sciencebeam_gym',
    version='0.0.1',
    install_requires=REQUIRED_PACKAGES,
    packages=packages,
    include_package_data=True,
    description='ScienceBeam Gym',
    include_dirs=[np.get_include()],
    cmdclass={
        'build': CustomBuild,
        'CustomCommands': CustomCommands
    }
Daniel Ecer's avatar
Daniel Ecer committed
)