]> git.0d.be Git - chloro.git/blob - setup.py
trivial: apply new pre-commit hooks
[chloro.git] / setup.py
1 #! /usr/bin/env python3
2
3 import os
4 import re
5 import subprocess
6 import sys
7 from distutils.cmd import Command
8 from distutils.command.build import build as _build
9 from distutils.command.sdist import sdist
10 from distutils.spawn import find_executable
11
12 from setuptools import find_packages, setup
13 from setuptools.command.install_lib import install_lib as _install_lib
14
15
16 class eo_sdist(sdist):
17     def run(self):
18         if os.path.exists('VERSION'):
19             os.remove('VERSION')
20         version = get_version()
21         with open('VERSION', 'w') as fd:
22             fd.write(version)
23         sdist.run(self)
24         if os.path.exists('VERSION'):
25             os.remove('VERSION')
26
27
28 def get_version():
29     """Use the VERSION, if absent generates a version with git describe, if not
30     tag exists, take 0.0- and add the length of the commit log.
31     """
32     if os.path.exists('VERSION'):
33         with open('VERSION') as v:
34             return v.read()
35     if os.path.exists('.git'):
36         p = subprocess.Popen(
37             ['git', 'describe', '--dirty=.dirty', '--match=v*'],
38             stdout=subprocess.PIPE,
39             stderr=subprocess.PIPE,
40         )
41         result = p.communicate()[0]
42         if p.returncode == 0:
43             result = result.decode('ascii').strip()[1:]  # strip spaces/newlines and initial v
44             if '-' in result:  # not a tagged version
45                 real_number, commit_count, commit_hash = result.split('-', 2)
46                 version = '%s.post%s+%s' % (real_number, commit_count, commit_hash)
47             else:
48                 version = result
49             return version
50         else:
51             return '0.0.post%s' % len(subprocess.check_output(['git', 'rev-list', 'HEAD']).splitlines())
52     return '0.0'
53
54
55 class compile_translations(Command):
56     description = 'compile message catalogs to MO files via django compilemessages'
57     user_options = []
58
59     def initialize_options(self):
60         pass
61
62     def finalize_options(self):
63         pass
64
65     def run(self):
66         orig_dir = os.getcwd()
67         try:
68             from django.core.management import call_command
69
70             for path, dirs, files in os.walk('chloro'):
71                 if 'locale' not in dirs:
72                     continue
73                 curdir = os.getcwd()
74                 os.chdir(os.path.realpath(path))
75                 call_command('compilemessages')
76                 os.chdir(curdir)
77         except ImportError:
78             sys.stderr.write('!!! Please install Django >= 1.4 to build translations\n')
79         os.chdir(orig_dir)
80
81
82 class compile_scss(Command):
83     description = 'compile scss files into css files'
84     user_options = []
85
86     def initialize_options(self):
87         pass
88
89     def finalize_options(self):
90         pass
91
92     def run(self):
93         sass_bin = None
94         for program in ('sassc', 'sass'):
95             sass_bin = find_executable(program)
96             if sass_bin:
97                 break
98         if not sass_bin:
99             raise CompileError(
100                 'A sass compiler is required but none was found.  See sass-lang.com for choices.'
101             )
102
103         for path, dirnames, filenames in os.walk('chloro'):
104             for filename in filenames:
105                 if not filename.endswith('.scss'):
106                     continue
107                 if filename.startswith('_'):
108                     continue
109                 subprocess.check_call(
110                     [
111                         sass_bin,
112                         '%s/%s' % (path, filename),
113                         '%s/%s' % (path, filename.replace('.scss', '.css')),
114                     ]
115                 )
116
117
118 class build(_build):
119     sub_commands = [('compile_translations', None), ('compile_scss', None)] + _build.sub_commands
120
121
122 class install_lib(_install_lib):
123     def run(self):
124         self.run_command('compile_translations')
125         _install_lib.run(self)
126
127
128 setup(
129     name='chloro',
130     version=get_version(),
131     description='Content Manager',
132     author='Frederic Peters',
133     author_email='fpeters@0d.be',
134     packages=find_packages(exclude=['tests']),
135     include_package_data=True,
136     scripts=('manage.py',),
137     classifiers=[
138         'Development Status :: 4 - Beta',
139         'Environment :: Web Environment',
140         'Framework :: Django',
141         'Intended Audience :: Developers',
142         'License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)',
143         'Operating System :: OS Independent',
144         'Programming Language :: Python',
145         'Programming Language :: Python :: 3',
146     ],
147     install_requires=['django>=1.11, <1.12', 'django-ckeditor<=4.5.3', 'django-taggit', 'gadjo'],
148     zip_safe=False,
149     cmdclass={
150         'build': build,
151         'compile_scss': compile_scss,
152         'compile_translations': compile_translations,
153         'install_lib': install_lib,
154         'sdist': eo_sdist,
155     },
156 )