-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup.py
194 lines (144 loc) · 5.8 KB
/
setup.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
This setup logic is highly inspired to the one used in `https://github.com/shellcheck-py/shellcheck-py`.
After `https://github.com/editorconfig-checker/editorconfig-checker.python/issues/15` was opened,
we decided to move the wrapper logic directly in the setup phase.
During setup, the tarball that contains the executable will be downloaded based on
the target machine and its content extracted in the proper output directory.
Once the setup is complete, the `ec` executable should be available on your machine.
"""
from distutils.command.build import build as orig_build
from distutils.core import Command
from io import BytesIO
from os import chmod, makedirs, path, stat
from platform import architecture, machine, system
from stat import S_IXGRP, S_IXOTH, S_IXUSR
from tarfile import open as tarfile_open
from setuptools import setup
from setuptools.command.install import install as orig_install
try:
# Python 3
from urllib.request import urlopen
except ImportError:
# Python 2.7
from urllib2 import urlopen
WRAPPER_VERSION = '3.0.3'
EDITORCONFIG_CHECKER_CORE_VERSION = 'v3.0.3'
EDITORCONFIG_CHECKER_EXE_NAME = 'ec'
def get_tarball_url():
def get_ec_name_by_system():
# Platform architecture() system() machine()
# ---------------------------------------------------------------
# Linux x86 64bit ('64bit', 'ELF') 'Linux' 'x86_64'
# Linux AWS Graviton2 ('64bit', 'ELF') 'Linux' 'aarch64'
# Mac x86 64bit ('64bit', '') 'Darwin' 'x86_64'
# Mac M1 64bit ('64bit', '') 'Darwin' 'arm64'
_system = system()
_machine = machine()
if _machine == 'x86_64':
_architecture = 'amd64'
elif _machine == 'aarch64' or _machine == 'arm64':
_architecture = 'arm64'
elif isinstance(architecture(), tuple) and len(architecture()) > 0:
_architecture = 'amd64' if architecture()[0] == '64bit' else '386'
else:
raise ValueError('Cannot determine architecture')
# The core, from `2.7.0`, introduces the extension in the tarball name
# (e.g. `ec-windows-386.exe.tar.gz`, `ec-windows-arm.exe.tar.gz`)
_ext = '.exe' if _system == 'Windows' else ''
return 'ec-{}-{}{}'.format(
_system.lower(),
_architecture,
_ext,
)
return 'https://github.com/editorconfig-checker/editorconfig-checker/releases/download/{}/{}.tar.gz'.format(
EDITORCONFIG_CHECKER_CORE_VERSION,
get_ec_name_by_system(),
)
def download_tarball(url):
sock = urlopen(url)
code = sock.getcode()
if code != 200:
sock.close()
raise ValueError('HTTP failure. Code: {}'.format(code))
data = sock.read()
sock.close()
return data
def extract_tarball(url, data):
with BytesIO(data) as bio:
if '.tar.' in url:
with tarfile_open(fileobj=bio) as fp:
for info in fp.getmembers():
if info.isfile() and info.name.startswith('bin/ec-'):
return fp.extractfile(info).read()
raise AssertionError('unreachable `extract` function')
def save_executables(data, base_dir):
exe = EDITORCONFIG_CHECKER_EXE_NAME
if system() == 'Windows':
exe += '.exe'
output_path = path.join(base_dir, exe)
try:
# Python 3
makedirs(base_dir, exist_ok=True)
except TypeError:
# Python 2.7
makedirs(base_dir)
with open(output_path, 'wb') as fp:
fp.write(data)
# Mark as executable ~ https://stackoverflow.com/a/14105527
mode = stat(output_path).st_mode
mode |= S_IXUSR | S_IXGRP | S_IXOTH
chmod(output_path, mode)
class build(orig_build):
sub_commands = orig_build.sub_commands + [('fetch_binaries', None)]
class install(orig_install):
sub_commands = orig_install.sub_commands + [('install_editorconfig_checker', None)]
class fetch_binaries(Command):
build_temp = None
def initialize_options(self):
pass
def finalize_options(self):
self.set_undefined_options('build', ('build_temp', 'build_temp'))
def run(self):
# save binary to self.build_temp
url = get_tarball_url()
archive = download_tarball(url)
data = extract_tarball(url, archive)
save_executables(data, self.build_temp)
class install_editorconfig_checker(Command):
description = 'install the editorconfig-checker executable'
outfiles = ()
build_dir = install_dir = None
def initialize_options(self):
pass
def finalize_options(self):
# this initializes attributes based on other commands' attributes
self.set_undefined_options('build', ('build_temp', 'build_dir'))
self.set_undefined_options('install', ('install_scripts', 'install_dir'))
def run(self):
self.outfiles = self.copy_tree(self.build_dir, self.install_dir)
def get_outputs(self):
return self.outfiles
command_overrides = {
'install': install,
'install_editorconfig_checker': install_editorconfig_checker,
'build': build,
'fetch_binaries': fetch_binaries,
}
try:
from wheel.bdist_wheel import bdist_wheel as orig_bdist_wheel
except ImportError:
pass
else:
class bdist_wheel(orig_bdist_wheel):
def finalize_options(self):
orig_bdist_wheel.finalize_options(self)
# Mark us as not a pure python package
self.root_is_pure = False
def get_tag(self):
_, _, plat = orig_bdist_wheel.get_tag(self)
# We don't contain any python source, nor any python extensions
return 'py2.py3', 'none', plat
command_overrides['bdist_wheel'] = bdist_wheel
setup(version=WRAPPER_VERSION, cmdclass=command_overrides)