forked from rdawg-pidinst/white-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
77 lines (60 loc) · 1.99 KB
/
setup.py
File metadata and controls
77 lines (60 loc) · 1.99 KB
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
# This setup.py file serves the only purpose to dynamically create a
# _meta.py file which then is imported from conf.py.
import datetime
import distutils.core
import os
from pathlib import Path
import subprocess
from setuptools import setup
import setuptools.command.install
class GitProps:
"""Determine properties of the git repository.
"""
def __init__(self, root="."):
self.root = Path(root).resolve()
def _exec(self, cmd):
proc = subprocess.run(cmd.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self.root,
check=True,
env=dict(os.environ, LC_ALL='C'),
universal_newlines=True)
return proc.stdout.strip()
def is_dirty(self):
return bool(self._exec("git status --porcelain --untracked-files=no"))
def get_date(self):
if self.is_dirty():
return datetime.date.today()
else:
ts = int(self._exec("git log -1 --format=%cd --date=unix"))
return datetime.date.fromtimestamp(ts)
class meta(distutils.core.Command):
description = "generate a _meta.py file"
user_options = []
meta_template = '''
__version__ = "%(version)s"
__date__ = "%(date)s"
'''
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
git = GitProps()
values = {
'version': self.distribution.get_version(),
'date': git.get_date().strftime("%e %B %Y").strip(),
}
with open("_meta.py", "wt") as f:
print(self.meta_template % values, file=f)
class install(setuptools.command.install.install):
def run(self):
self.run_command('meta')
super().run()
setup(
use_scm_version=True,
python_requires='>= 3.6',
setup_requires=['setuptools_scm'],
cmdclass = {'meta': meta, 'install': install},
)