-
Notifications
You must be signed in to change notification settings - Fork 36
/
setup.py
180 lines (149 loc) · 6.54 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
import os
import sys
import shutil
from pathlib import Path
from filecmp import dircmp
from glob import glob
from os.path import basename, splitext, join, dirname
__dir__ = Path(__file__).absolute().parent
# Remove current dir from sys.path, otherwise setuptools will peek up our
# module instead of system's.
sys.path.pop(0)
import setuptools
sys.path.append("..")
import sdist_upip
def read(file_relative):
file = __dir__ / file_relative
with open(str(file)) as f:
return f.read()
MINIFIED_DIR = Path('build_app')
class PythonMinifier(setuptools.Command):
"""A custom command to run Python Minifier"""
description = 'run Python Minifier on Python source files'
user_options = [
('minified-dir=', 'm', 'Path where the result is to be saved.'),
]
def initialize_options(self):
"""Set default values for options."""
self.minified_dir = str(MINIFIED_DIR)
def finalize_options(self):
"""Post-process options."""
if self.minified_dir == str(MINIFIED_DIR):
if not os.path.exists(str(MINIFIED_DIR)):
os.mkdir(str(MINIFIED_DIR))
if not os.path.isdir(self.minified_dir):
raise Exception("Directory does not exist: {0}".format(self.minified_dir))
if os.path.exists(str(Path(self.minified_dir) / 'umqtt')):
shutil.rmtree(str(Path(self.minified_dir) / 'umqtt'))
os.makedirs(str(Path(self.minified_dir) / 'umqtt'), exist_ok=True)
print('OUT directory: ', self.minified_dir)
def run(self):
"""Run command."""
import python_minifier
for file in os.listdir(str(__dir__ / 'src' / 'umqtt')):
with open(str(__dir__ / 'src' / 'umqtt' / file)) as f:
print('Minify: %s' % file)
source = f.read()
filename = file.split('/')[-1]
out = python_minifier.minify(
source,
filename,
remove_annotations=True,
remove_pass=True,
remove_literal_statements=True,
combine_imports=False,
hoist_literals=False,
rename_locals=True,
preserve_locals=None,
rename_globals=False,
preserve_globals=None,
remove_object_base=False,
convert_posargs_to_args=False
)
with open(str(Path(self.minified_dir) / 'umqtt' / file), 'w') as f:
f.write(out)
from distutils import dir_util, dep_util, file_util, archive_util
from distutils import log
class SDistCommand(sdist_upip.sdist):
"""Custom build command."""
def make_release_tree(self, base_dir, files):
"""Create the directory tree that will become the source
distribution archive. All directories implied by the filenames in
'files' are created under 'base_dir', and then we hard link or copy
(if hard linking is unavailable) those files into place.
Essentially, this duplicates the developer's source tree, but in a
directory named after the distribution, containing only the files
to be distributed.
"""
# Create all the directories under 'base_dir' necessary to
# put 'files' there; the 'mkpath()' is just so we don't die
# if the manifest happens to be empty.
self.mkpath(base_dir)
files_tree_fix = [f.replace(str(MINIFIED_DIR) + '/', '') for f in files]
dir_util.create_tree(base_dir, files_tree_fix, dry_run=self.dry_run)
# And walk over the list of files, either making a hard link (if
# os.link exists) to each one that doesn't already exist in its
# corresponding location under 'base_dir', or copying each file
# that's out-of-date in 'base_dir'. (Usually, all files will be
# out-of-date, because by default we blow away 'base_dir' when
# we're done making the distribution archives.)
if hasattr(os, 'link'): # can make hard links on this system
link = 'hard'
msg = "making hard links in %s..." % base_dir
else: # nope, have to copy
link = None
msg = "copying files to %s..." % base_dir
if not files:
log.warn("no files to distribute -- empty manifest?")
else:
log.info(msg)
for file in files:
if not os.path.isfile(file):
log.warn("'%s' not a regular file -- skipping" % file)
else:
file_fix = file.replace(str(MINIFIED_DIR) + '/', '')
dest = os.path.join(base_dir, file_fix)
self.copy_file(file, dest, link=link)
self.distribution.metadata.write_pkg_info(base_dir)
def run(self):
self.run_command('minify')
def print_diff_files(dcmp):
is_diff = False
for name in dcmp.diff_files:
print("diff_file %s found in %s and %s" % (name, dcmp.left, dcmp.right))
is_diff = True
for sub_dcmp in dcmp.subdirs.values():
is_diff = is_diff or print_diff_files(sub_dcmp)
return is_diff
dcmp = dircmp(str(MINIFIED_DIR), 'src_minimized')
if print_diff_files(dcmp):
raise Exception('There are differences in minimized files. '
'Compare %s and %s directories. They must be identical.' %
(str(MINIFIED_DIR), 'src_minimized'))
super(SDistCommand, self).run()
setuptools.setup(
name='micropython-umqtt.simple2',
version='2.2.0',
description='MQTT client for MicroPython.',
long_description=read('README.rst'),
long_description_content_type="text/x-rst",
url='https://github.com/fizista/micropython-umqtt.simple2',
author='Wojciech Banaś',
author_email='fizista@gmail.com',
maintainer='Wojciech Banaś',
maintainer_email='fizista+umqtt.simple2@gmail.com',
license='MIT',
classifiers=[
'Programming Language :: Python :: Implementation :: MicroPython',
],
keywords='mqtt micropython',
cmdclass={'sdist': SDistCommand, 'minify': PythonMinifier},
setup_requires=['python_minifier'],
packages=setuptools.find_packages('src'),
package_dir={'': str(MINIFIED_DIR)},
py_modules=[splitext(basename(path))[0] for path in glob('src/*.py')],
project_urls={
'Bug Reports': 'https://github.com/fizista/micropython-umqtt.simple2/issues',
'Source': 'https://github.com/fizista/micropython-umqtt.simple2',
},
)