/
make
executable file
·142 lines (100 loc) · 3.77 KB
/
make
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import os
import shlex
import shutil
import subprocess
import sys
import tempfile
import typing
import urllib.request
logger = logging.getLogger("cli")
try:
from clinner.command import Type, command
from clinner.inputs import bool_input
from clinner.run import Main
except Exception:
logger.error("Package clinner is not installed, run 'pip install clinner' to install it")
sys.exit(-1)
try:
import toml
except Exception:
toml = None
POETRY_URL = "https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py"
def poetry(*args) -> typing.List[str]:
"""
Build a poetry command.
:param args: Poetry command args.
:return: Poetry command.
"""
try:
subprocess.run(
shlex.split("poetry --version"), check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
) # noqa
except ImportError:
if bool_input("Do you want to install Poetry?"):
with tempfile.NamedTemporaryFile() as tmp_file, urllib.request.urlopen(POETRY_URL) as response:
tmp_file.write(response.read())
subprocess.run(shlex.split(f"python {tmp_file.name}"))
else:
logger.error("Poetry is not installed.")
return shlex.split("poetry") + list(args)
@command(command_type=Type.SHELL, parser_opts={"help": "Install requirements"})
def install(*args, **kwargs):
return [poetry("install", "-E", "full", *args)]
@command(command_type=Type.PYTHON, parser_opts={"help": "Clean directory"})
def clean(*args, **kwargs):
for path in (".pytest_cache", "dist", "pip-wheel-metadata", "flama.egg-info", ".coverage", "test-results", "site"):
shutil.rmtree(path, ignore_errors=True)
@command(
command_type=Type.SHELL,
args=((("-c", "--clean"), {"help": "Clean before build"}),),
parser_opts={"help": "Build package"},
)
def build(*args, **kwargs):
if kwargs["clean"]:
clean()
return [poetry("build", *args)]
@command(command_type=Type.SHELL, parser_opts={"help": "Black code formatting"})
def black(*args, **kwargs):
return [poetry("run", "black", *args)]
@command(command_type=Type.SHELL, parser_opts={"help": "Flake8 code analysis"})
def flake8(*args, **kwargs):
return [poetry("run", "flake8", *args)]
@command(command_type=Type.SHELL, parser_opts={"help": "Isort imports formatting"})
def isort(*args, **kwargs):
return [poetry("run", "isort", *args)]
@command(command_type=Type.SHELL, parser_opts={"help": "Code lint using multiple tools"})
def lint(*args, **kwargs):
return black() + flake8() + isort()
@command(command_type=Type.SHELL, parser_opts={"help": "Run tests"})
def test(*args, **kwargs):
return [poetry("run", "pytest", *args)]
@command(command_type=Type.SHELL, parser_opts={"help": "Build docs"})
def docs(*args, **kwargs):
return [poetry("run", "mkdocs", *args)]
@command(command_type=Type.SHELL, parser_opts={"help": "Upgrade version"})
def version(*args, **kwargs):
return [poetry("version", *args)]
@command(
command_type=Type.SHELL,
args=((("-b", "--build"), {"help": "Build package", "action": "store_true"}),),
parser_opts={"help": "Publish package"},
)
def publish(*args, **kwargs):
cmds = []
username = os.environ.get("PYPI_USERNAME")
password = os.environ.get("PYPI_PASSWORD")
if username and password:
cmds.append(poetry("config", "http-basic.pypi", username, password))
if kwargs["build"]:
cmds += build(clean=True)
cmds.append(poetry("publish"))
return cmds
class Make(Main):
commands = ("install", "clean", "build", "publish", "black", "flake8", "isort", "lint", "test", "version", "docs")
def main():
return Make().run()
if __name__ == "__main__":
sys.exit(main())