github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/setup.py (about)

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2016 Google Inc. All Rights Reserved.
     5  #
     6  # Licensed under the Apache License, Version 2.0 (the "License");
     7  # you may not use this file except in compliance with the License.
     8  # You may obtain a copy of the License at
     9  #
    10  #     http://www.apache.org/licenses/LICENSE-2.0
    11  #
    12  # Unless required by applicable law or agreed to in writing, software
    13  # distributed under the License is distributed on an "AS IS" BASIS,
    14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  # See the License for the specific language governing permissions and
    16  # limitations under the License.
    17  
    18  """The setup script."""
    19  
    20  import os
    21  import fnmatch
    22  import shutil
    23  import sys
    24  from setuptools import setup, find_packages
    25  from distutils.command.build_py import build_py as BuildPyCommand
    26  from distutils.command.build_ext import build_ext as BuildExtCommand
    27  import subprocess
    28  
    29  try:
    30      with open('README.md') as readme_file:
    31          readme = readme_file.read()
    32  except:
    33      readme = ''
    34  
    35  setup_requirements = [
    36      'setuptools>=28.8.0',
    37  ]
    38  
    39  test_requirements = [
    40      'pytest',
    41      # TODO: Put package test requirements here
    42  ]
    43  
    44  needs_pytest = {'pytest', 'test', 'ptr'}.intersection(sys.argv)
    45  if needs_pytest:
    46      setup_requirements += ['pytest-runner']
    47  
    48  COMMON_OPTIONS = dict(
    49      version='0.3.0',
    50      description="Grumpy (Python to Go Transpiler) Runtime",
    51      long_description=readme,
    52      author="Dylan Trotter et al.",
    53      maintainer="Alan Justino et al.",
    54      maintainer_email="alan.justino@yahoo.com.br",
    55      url='https://github.com/grumpyhome/grumpy',
    56      license="Apache Software License 2.0",
    57      zip_safe=False,
    58      keywords='grumpy_runtime',
    59      python_requires='~=2.7.0',
    60      classifiers=[
    61          'Development Status :: 2 - Pre-Alpha',
    62          'Intended Audience :: Developers',
    63          'License :: OSI Approved :: Apache Software License',
    64          'Natural Language :: English',
    65          "Programming Language :: Python :: 2",
    66          'Programming Language :: Python :: 2.7',
    67      ],
    68      test_suite='tests',
    69      tests_require=test_requirements,
    70      setup_requires=setup_requirements,
    71  )
    72  
    73  
    74  def _run_make(self, *args, **kwargs):
    75      subprocess.check_call(["""echo "print 'Make Runtime Success'" | make run --debug=bjm -r"""], shell=True)
    76      subprocess.check_call(["""make clean-pycache --debug=bjm -r"""], shell=True)
    77  
    78  
    79  def _glob_deep(directory, pattern, rmtree=None):
    80      rmtree = rmtree or []
    81  
    82      # From: https://stackoverflow.com/a/2186673/798575
    83      for root, dirs, files in os.walk(directory):
    84          for basename in files:
    85              if fnmatch.fnmatch(basename, pattern):
    86                  filename = os.path.join(root, basename)
    87                  for filtered_name in blacklisted:
    88                      if filtered_name in filename:
    89                          print('Trying to rmtree', filename)
    90                          shutil.rmtree(filename, ignore_errors=True)
    91                          continue  # Skip this blacklisted one.
    92                  yield filename
    93  
    94  
    95  class BuildMakeCommandInstall(BuildPyCommand):  # Ran on setup.py install
    96      def run(self, *args, **kwargs):
    97          # Thanks http://www.digip.org/blog/2011/01/generating-data-files-in-setup.py.html for the tip
    98  
    99          _run_make(self, *args, **kwargs)
   100  
   101          # Makefile creates a "gopath" folder named "build" on root folder. Change it!
   102          shutil.move('build', 'gopath')
   103  
   104          if not self.dry_run:
   105              target_dir = os.path.join(self.build_lib, 'grumpy_runtime/data')
   106  
   107              # Remove the symlink used on develop
   108              shutil.rmtree(target_dir, ignore_errors=True)
   109              self.mkpath(target_dir)
   110              shutil.move('gopath', target_dir)
   111  
   112              build_dir = os.path.join(self.build_lib, 'grumpy_runtime')
   113              built_files = _glob_deep(os.path.join(build_dir, 'gopath'), '*', rmtree=['__pycache__'])
   114  
   115              # Strip directory from globbed filenames
   116              build_dir_len = len(build_dir) + 1 # One more for leading "/"
   117              built_files = [fn[build_dir_len:] for fn in built_files]
   118  
   119              self.data_files = [
   120                  # (package, src_dir, build_dir, filenames[])
   121                  ('grumpy_runtime', 'grumpy_runtime', build_dir, built_files)
   122              ]
   123  
   124          super_result = BuildPyCommand.run(self, *args, **kwargs)
   125          return super_result
   126  
   127  
   128  class BuildMakeCommandDevelop(BuildExtCommand):  # Ran on setup.py develop
   129      def run(self, *args, **kwargs):
   130          # Thanks http://www.digip.org/blog/2011/01/generating-data-files-in-setup.py.html for the tip
   131  
   132          _run_make(self, *args, **kwargs)
   133  
   134          # Makefile creates a "gopath" folder named "build" on root folder. Change it!
   135          shutil.move('build', 'gopath')
   136  
   137          if not self.dry_run:
   138              target_dir = os.path.join(self.build_lib, 'grumpy_runtime/data')
   139              self.mkpath(target_dir)
   140              shutil.move('gopath', target_dir)
   141  
   142              build_dir = os.path.join(self.build_lib, 'grumpy_runtime')
   143              built_files = _glob_deep(os.path.join(build_dir, 'gopath'), '*', rmtree=['__pycache__'])
   144  
   145              # Strip directory from globbed filenames
   146              build_dir_len = len(build_dir) + 1 # One more for leading "/"
   147              built_files = [fn[build_dir_len:] for fn in built_files]
   148  
   149              self.data_files = [
   150                  # (package, src_dir, build_dir, filenames[])
   151                  ('grumpy_runtime', 'grumpy_runtime', build_dir, built_files),
   152              ]
   153  
   154          super_result = BuildExtCommand.run(self, *args, **kwargs)
   155          return super_result
   156  
   157  
   158  GRUMPY_RUNTIME_OPTIONS = dict(
   159      name='grumpy-runtime',
   160      requires=['grumpy_tools'],
   161      install_requires=['grumpy-tools>=0.3.0'],
   162      packages=find_packages(
   163          exclude=["*.tests", "*.tests.*", "tests.*", "tests"],
   164      ),
   165      include_package_data=True,
   166      cmdclass={
   167          'build_py': BuildMakeCommandInstall,   # Ran on setup.py install
   168          'build_ext': BuildMakeCommandDevelop,  # Ran on setup.py develop
   169      },
   170      zip_safe=False,
   171  )
   172  GRUMPY_RUNTIME_OPTIONS.update(COMMON_OPTIONS)
   173  
   174  setup(**GRUMPY_RUNTIME_OPTIONS)