setup.py 4.66 KB
Newer Older
wxchan's avatar
wxchan committed
1
# coding: utf-8
2
# pylint: disable=invalid-name, exec-used, C0111
wxchan's avatar
wxchan committed
3
4
"""Setup lightgbm package."""
from __future__ import absolute_import
5

Guolin Ke's avatar
Guolin Ke committed
6
import distutils
7
import os
Guolin Ke's avatar
Guolin Ke committed
8
import shutil
9
10
11
import struct
import sys

12
from setuptools import find_packages, setup
13
14
15
from setuptools.command.install import install
from setuptools.command.install_lib import install_lib
from setuptools.command.sdist import sdist
16

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

def find_lib():
    CURRENT_DIR = os.path.dirname(__file__)
    libpath_py = os.path.join(CURRENT_DIR, 'lightgbm/libpath.py')
    libpath = {'__file__': libpath_py}
    exec(compile(open(libpath_py, "rb").read(), libpath_py, 'exec'), libpath, libpath)

    LIB_PATH = [os.path.relpath(path, CURRENT_DIR) for path in libpath['find_lib_path']()]
    print("Install lib_lightgbm from: %s" % LIB_PATH)
    return LIB_PATH


def copy_files(use_gpu=False):

    def copy_files_helper(folder_name):
        src = os.path.join('..', folder_name)
        if os.path.exists(src):
            dst = os.path.join('./lightgbm', folder_name)
            shutil.rmtree(dst, ignore_errors=True)
            distutils.dir_util.copy_tree(src, dst)
        else:
            raise Exception('Cannot copy {} folder'.format(src))

    if not os.path.isfile('./_IS_SOURCE_PACKAGE.txt'):
        copy_files_helper('include')
        copy_files_helper('src')
Guolin Ke's avatar
Guolin Ke committed
43
        if use_gpu:
44
45
            copy_files_helper('compute')
        distutils.file_util.copy_file("../CMakeLists.txt", "./lightgbm/")
wxchan's avatar
wxchan committed
46
47


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
def compile_cpp(use_mingw=False, use_gpu=False):

    if not os.path.exists("build"):
        os.makedirs("build")
    os.chdir("build")

    cmake_cmd = "cmake "
    build_cmd = "make _lightgbm"

    if os.name == "nt":
        if use_mingw:
            cmake_cmd += " -G \"MinGW Makefiles\" "
            build_cmd = "mingw32-make.exe _lightgbm"
        else:
            cmake_cmd += " -DCMAKE_GENERATOR_PLATFORM=x64 "
            build_cmd = "cmake --build . --target _lightgbm  --config Release"
    if use_gpu:
        cmake_cmd += " -DUSE_GPU=ON "
    print("Start to compile libarary.")
    os.system(cmake_cmd + " ../lightgbm/")
    os.system(build_cmd)
    os.chdir("..")


class CustomInstallLib(install_lib):

    def install(self):
        outfiles = install_lib.install(self)
        src = find_lib()[0]
        dst = os.path.join(self.install_dir, 'lightgbm')
        dst, _ = self.copy_file(src, dst)
        outfiles.append(dst)
        return outfiles


class CustomInstall(install):

    user_options = install.user_options + [
        ('mingw', 'm', 'compile with mingw'),
        ('gpu', 'g', 'compile gpu version'),
        ('precompile', 'p', 'use precompile library')
    ]

    def initialize_options(self):
        install.initialize_options(self)
        self.mingw = 0
        self.gpu = 0
        self.precompile = 0

    def run(self):
        if not self.precompile:
            copy_files(use_gpu=self.gpu)
            compile_cpp(use_mingw=self.mingw, use_gpu=self.gpu)
        self.distribution.data_files = [('lightgbm', find_lib())]
        install.run(self)


class CustomSdist(sdist):

    def run(self):
Guolin Ke's avatar
Guolin Ke committed
108
        copy_files(use_gpu=True)
109
        open("./_IS_SOURCE_PACKAGE.txt", 'w').close()
Guolin Ke's avatar
Guolin Ke committed
110
111
112
113
        if os.path.exists("./lightgbm/Release/"):
            shutil.rmtree('./lightgbm/Release/')
        if os.path.isfile('./lightgbm/lib_lightgbm.so'):
            os.remove('./lightgbm/lib_lightgbm.so')
114
115
116
117
118
119
120
121
122
123
124
        sdist.run(self)
        if os.path.isfile('./_IS_SOURCE_PACKAGE.txt'):
            os.remove('./_IS_SOURCE_PACKAGE.txt')


if __name__ == "__main__":
    if (8 * struct.calcsize("P")) != 64:
        raise Exception('Cannot install LightGBM in 32-bit python, please use 64-bit python instead.')
    if os.path.isfile('../VERSION.txt'):
        distutils.file_util.copy_file("../VERSION.txt", "./lightgbm/")
    version = '2.0.3'
Guolin Ke's avatar
Guolin Ke committed
125
    if os.path.isfile('./lightgbm/VERSION.txt'):
126
127
128
129
        with open('./lightgbm/VERSION.txt') as file_version:
            version = file_version.readline().strip()
    sys.path.insert(0, '.')
    data_files = []
Guolin Ke's avatar
Guolin Ke committed
130
131
132
133
    setup(name='lightgbm',
          version=version,
          description='LightGBM Python Package',
          install_requires=[
Guolin Ke's avatar
Guolin Ke committed
134
              'wheel',
Guolin Ke's avatar
Guolin Ke committed
135
136
137
138
139
140
141
              'numpy',
              'scipy',
              'scikit-learn'
          ],
          maintainer='Guolin Ke',
          maintainer_email='guolin.ke@microsoft.com',
          zip_safe=False,
142
143
144
145
146
          cmdclass={
              'install': CustomInstall,
              'install_lib': CustomInstallLib,
              'sdist': CustomSdist,
          },
Guolin Ke's avatar
Guolin Ke committed
147
148
149
          packages=find_packages(),
          include_package_data=True,
          data_files=data_files,
150
          license='The MIT License (Microsoft)',
Guolin Ke's avatar
Guolin Ke committed
151
          url='https://github.com/Microsoft/LightGBM')