only include download file if file is really available
[panikweb-esperanzah.git] / setup.py
1 #! /usr/bin/env python3
2
3 import os
4 import subprocess
5 import sys
6 from distutils.cmd import Command
7 from distutils.command.build import build as _build
8 from distutils.command.sdist import sdist as _sdist
9 from distutils.errors import CompileError
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 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     if os.path.exists('VERSION'):
30         with open('VERSION', 'r') as v:
31             return v.read()
32     if os.path.exists('.git'):
33         p = subprocess.Popen(
34             ['git', 'describe', '--dirty=.dirty', '--match=v*'],
35             stdout=subprocess.PIPE,
36             stderr=subprocess.PIPE,
37         )
38         result = p.communicate()[0]
39         if p.returncode == 0:
40             result = result.decode('ascii').strip()[1:]  # strip spaces/newlines and initial v
41             if '-' in result:  # not a tagged version
42                 real_number, commit_count, commit_hash = result.split('-', 2)
43                 version = '%s.post%s+%s' % (real_number, commit_count, commit_hash)
44             else:
45                 version = result
46             return version
47         else:
48             return '0.0.post%s' % len(subprocess.check_output(['git', 'rev-list', 'HEAD']).splitlines())
49     return '0.0'
50
51
52 class compile_translations(Command):
53     description = 'compile message catalogs to MO files via django compilemessages'
54     user_options = []
55
56     def initialize_options(self):
57         pass
58
59     def finalize_options(self):
60         pass
61
62     def run(self):
63         try:
64             from django.core.management import call_command
65
66             for path, dirs, files in os.walk('panikweb_esperanzah'):
67                 if 'locale' not in dirs:
68                     continue
69                 curdir = os.getcwd()
70                 os.chdir(os.path.realpath(path))
71                 call_command('compilemessages')
72                 os.chdir(curdir)
73         except ImportError:
74             sys.stderr.write('!!! Please install Django to build translations\n')
75
76
77 class compile_scss(Command):
78     description = 'compile scss files into css files'
79     user_options = []
80
81     def initialize_options(self):
82         pass
83
84     def finalize_options(self):
85         pass
86
87     def run(self):
88         sass_bin = None
89         for program in ('sassc', 'sass'):
90             sass_bin = find_executable(program)
91             if sass_bin:
92                 break
93         if not sass_bin:
94             raise CompileError(
95                 'A sass compiler is required but none was found.  See sass-lang.com for choices.'
96             )
97
98         for path, dirnames, filenames in os.walk('panikweb_esperanzah'):
99             for filename in filenames:
100                 if not filename.endswith('.scss'):
101                     continue
102                 if filename.startswith('_'):
103                     continue
104                 subprocess.check_call(
105                     [
106                         sass_bin,
107                         '%s/%s' % (path, filename),
108                         '%s/%s' % (path, filename.replace('.scss', '.css')),
109                     ]
110                 )
111
112
113 class build(_build):
114     sub_commands = [('compile_translations', None), ('compile_scss', None)] + _build.sub_commands
115
116
117 class install_lib(_install_lib):
118     def run(self):
119         self.run_command('compile_translations')
120         _install_lib.run(self)
121
122
123 setup(
124     name='panikweb_esperanzah',
125     version=get_version(),
126     description='Panikweb extension/theme for Radio Esperanzah',
127     author='Frederic Peters',
128     author_email='fpeters@0d.be',
129     url='https://radio.esperanzah.be',
130     packages=find_packages(),
131     include_package_data=True,
132     classifiers=[
133         'Development Status :: 3 - Alpha',
134         'Environment :: Web Environment',
135         'Framework :: Django',
136         'Intended Audience :: Developers',
137         'License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)',
138         'Operating System :: OS Independent',
139         'Programming Language :: Python',
140         'Programming Language :: Python :: 3',
141         'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
142     ],
143     zip_safe=False,
144     cmdclass={
145         'build': build,
146         'compile_scss': compile_scss,
147         'compile_translations': compile_translations,
148         'install_lib': install_lib,
149         'sdist': sdist,
150     },
151 )