src/setup.py
changeset 175 6fcda59daff8
parent 142 56850f5c73f6
child 179 e7c7e6e0a8bc
equal deleted inserted replaced
174:ac1a026edd58 175:6fcda59daff8
     1 import os
     1 import os
       
     2 import re
       
     3 import sys
       
     4 from distutils.command.install import INSTALL_SCHEMES
       
     5 from distutils.command.install_data import install_data
       
     6 from shutil import move
       
     7 from tempfile import mkstemp
       
     8 
       
     9 from setuptools import Command
       
    10 
     2 try:
    11 try:
     3     from setuptools import setup
    12     from setuptools import setup
     4 except ImportError:
    13 except ImportError:
     5     from distutils.core import setup
    14     from distutils.core import setup
     6 from distutils.command.install_data import install_data
    15 
     7 from distutils.command.install import INSTALL_SCHEMES
    16 
     8 import sys
    17 """
       
    18 See: https://dankeder.com/posts/adding-custom-commands-to-setup-py/
       
    19 """
       
    20 class SetVersion(Command):
       
    21     """
       
    22     Set the version for the project
       
    23     """
       
    24     description = "Set the version for the project"
       
    25     # command_consumes_arguments = True
       
    26     user_options = [
       
    27         ('version=', 'v', 'version str')
       
    28     ]
       
    29 
       
    30     def initialize_options(self):
       
    31         """Set default values for options."""
       
    32         # Each user option must be listed here with their default value.
       
    33         self.version = None
       
    34 
       
    35     def finalize_options(self):
       
    36         """Post-process options."""
       
    37         if self.version is None:
       
    38             raise Exception("Parameter --version is missing")
       
    39         version_matches = re.match(r"(\d+).(\d+)(?:\.(\d+))?(?:(?:.(dev))|(?:(a|b|rc)(.*)))?", self.version)
       
    40         if version_matches is None:
       
    41             raise Exception("Parameter --version is not a version string (see )")
       
    42         version_list = version_matches.groups()
       
    43         version_tag = {'a': 'alpha', 'b': 'beta', 'rc': 'rc'}.get(version_list[4],'final')
       
    44         version_ext = int(version_list[5] or 0)
       
    45         if version_list[3] and version_list[3] == 'dev':
       
    46             version_tag = 'alpha'
       
    47             version_ext = 0
       
    48 
       
    49         self.version_tuple = (
       
    50             int(version_list[0]),
       
    51             int(version_list[1]),
       
    52             int(version_list[2] or 0),
       
    53             version_tag,
       
    54             version_ext,
       
    55         )
       
    56 
       
    57     def run(self):
       
    58         filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),"irinotes","__init__.py")
       
    59         print("Changing VERSION in %s to %r" % (filepath, self.version_tuple))
       
    60         fh, abs_path = mkstemp()
       
    61         with os.fdopen(fh,'w') as temp_file:
       
    62             with open(filepath, 'r') as initfiles:
       
    63                 for l in initfiles:
       
    64                     if re.match(r"\s*VERSION\s*=\s*\(\d+\s*,\s*\d+\s*,\s*\d+,\s*\".+\",\s*.+\s*\)", l):
       
    65                         temp_file.write("VERSION = %r" % (self.version_tuple,))
       
    66                     else:
       
    67                         temp_file.write(l)
       
    68         os.remove(filepath)
       
    69         move(abs_path, filepath)
       
    70 
     9 
    71 
    10 
    72 
    11 class osx_install_data(install_data):
    73 class osx_install_data(install_data):
    12     """
    74     """
    13     On MacOS, the platform-specific lib dir is /System/Library/Framework/Python/.../
    75     On MacOS, the platform-specific lib dir is /System/Library/Framework/Python/.../
   106                 m.write("include %s\n" % (filename))
   168                 m.write("include %s\n" % (filename))
   107 
   169 
   108     long_description = ''
   170     long_description = ''
   109     with open('README', 'r') as f:
   171     with open('README', 'r') as f:
   110         long_description = f.read()
   172         long_description = f.read()
       
   173 
       
   174     cmdclasses = {**cmdclasses, **{ 'set_version': SetVersion }}
   111 
   175 
   112     setup(
   176     setup(
   113         script_name=setup_script_name,
   177         script_name=setup_script_name,
   114         script_args=setup_script_args,
   178         script_args=setup_script_args,
   115         name='irinotes',
   179         name='irinotes',
   138             "Django >= 1.11",
   202             "Django >= 1.11",
   139             "python-decouple",
   203             "python-decouple",
   140             "Unipath",
   204             "Unipath",
   141             "dj-database-url",
   205             "dj-database-url",
   142             "six",
   206             "six",
   143             "django-auditlog == 0.4.5dev",
   207             "django-auditlog @ https://github.com/IRI-Research/django-auditlog/tarball/master#egg=django-auditlog-0.4.5+IRI",
   144             "django-extensions",
   208             "django-extensions",
   145             "djangorestframework >= 3.6",
   209             "djangorestframework >= 3.6",
   146             "django-rest-auth[with_social]",
   210             "django-rest-auth[with_social]",
   147             "djangorestframework-jwt",
   211             "djangorestframework-jwt",
   148             "django-guardian >= 1.4",
   212             "django-guardian >= 1.4",
   151             "django-concurrency",
   215             "django-concurrency",
   152             "django-filter",
   216             "django-filter",
   153             "drf-nested-routers",
   217             "drf-nested-routers",
   154             "markdown"
   218             "markdown"
   155         ],
   219         ],
   156         dependency_links=[
   220         # dependency_links=[
   157             "https://github.com/IRI-Research/django-auditlog/tarball/master#egg=django-auditlog-0.4.5dev"
   221         #     "https://github.com/IRI-Research/django-auditlog/tarball/master#egg=django-auditlog-0.4.5dev"
   158         ]
   222         # ]
   159     )
   223     )
   160 
   224 
   161 
   225 
   162 if __name__ == "__main__":
   226 if __name__ == "__main__":
   163 
   227