summaryrefslogtreecommitdiff
path: root/support/gladtex/setup.py
blob: 9f17c165ec363d405914d07365c6914535a8508d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
45
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
import distutils.command.install_scripts, distutils.command.build, distutils.command.clean
from distutils.core import setup
import os
import shutil
import sys
#pylint: disable=unused-import
try:
    import py2exe # only works on windows
except ImportError:
    pass
from gleetex import VERSION

class ScriptInstaller(distutils.command.install_scripts.install_scripts):
    """Custom script installer. Stript .py extension if not on Windows."""
    def run(self):
        distutils.command.install_scripts.install_scripts.run(self)
        for script in self.get_outputs():
            if script.endswith(".py") and not ('wind' in sys.platform or 'win32'
                    in sys.platform):
                # strip file ending (if not on windows) to make it executable as
                # a command
                shutil.move(script, script[:-3])

#pylint: disable=attribute-defined-outside-init
class CleanCommand(distutils.command.clean.clean):
    description = "clean all build files, including __pycache__ and others"
    user_options = []
    def initialize_options(self):
        self.cwd = None

    def finalize_options(self):
        self.cwd = os.getcwd()

    def run(self):
        assert os.getcwd() == self.cwd, 'Must be in package root: %s' % self.cwd
        for directory in ['build', '__pycache__', 'dist',
                os.path.join('gleetex', '__pycache')]:
            if os.path.exists(directory):
                shutil.rmtree(directory)
        if os.path.exists('gladtex.1'):
            os.remove('gladtex.1')

class CustomBuild(distutils.command.build.build):
    """Also build manpage to build/gladtex.1; it is not installed
    automatically."""
    def initialize_options(self):
        self.cwd = None
        super().initialize_options()

    def finalize_options(self):
        self.cwd = os.getcwd()
        super().finalize_options()

    def run(self):
        if not self.cwd:
            if not 'manpage.md' in os.listdir('.'):
                print("setup.py must be run from the source root")
                sys.exit(91)
        else:
            assert os.getcwd() == self.cwd, 'Must be in package root: %s' % self.cwd
        super().run()
        if shutil.which('pandoc'): # only build man page, if pandoc present
            self.build_manpage('manpage.md', os.path.join('build', 'gladtex.1'))
        else:
            print("w: pandoc not found, skipping man page conversion.")

    def build_manpage(self, input_fn, output_fn):
        """Convert `input_fn` from markdown into manpage format and save it to
        `output_fn`. Pandoc is required."""
        import subprocess
        try:
            cmd = ['pandoc', input_fn, '-s', '-t', 'man', '-o', output_fn]
            proc = subprocess.Popen(cmd)
            ret = proc.wait()
            if ret:
                raise subprocess.SubprocessError("Exit status %d when running '%s'" % (ret,
                    ' '.join(cmd)))
        except FileNotFoundError:
            sys.stderr.write("Pandoc was not found on the system, skipping man " +
                "page creation.")


setup(name='GladTeX',
      version=VERSION,
      description='generate html with LaTeX equations embedded as images',
      author='Sebastian Humenda',
      author_email='shumenda |at| gmx |dot| de',
      url='https://humenda.github.io/GladTeX',
      packages=['gleetex'],
      console=['gladtex.py'], # Windows-only option, use python instead of pythonw
      setup_requires=['py2app'], # require py2app on Mac OS
      scripts=['gladtex.py'],
      license = "LGPL3.0",
      cmdclass = {"install_scripts": ScriptInstaller,
          'clean': CleanCommand,
          'build': CustomBuild}
     )