-
Notifications
You must be signed in to change notification settings - Fork 7
/
setup.py
479 lines (386 loc) · 12.6 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/bin/env python
# encoding: utf-8
"""
setup.py -- dotfiles
::
python setup.py --help
python setup.py --help-commands
"""
import errno
import glob
import logging
import os
import subprocess
import sys
from collections import deque, OrderedDict
from fnmatch import fnmatchcase
import setuptools
from setuptools import setup, find_packages
from distutils.command.build import build as DistutilsBuildCommand
from distutils.util import convert_path
from distutils.text_file import TextFile
try:
import z3c.recipe.tag
z3c.recipe.tag
except ImportError:
pass
SETUPPY_PATH = os.path.dirname(os.path.abspath(__file__)) or '.'
def read_version_txt():
with open(os.path.join(SETUPPY_PATH, 'VERSION.txt')) as f:
version = next(f).strip()
return version
VERSION = read_version_txt()
APPNAME = 'dotfiles'
CONFIG = {}
DEBUG = CONFIG.get('debug', True) # False # True
logging.basicConfig(
format='%(asctime)s %(name)s %(levelname)-5s %(message)s')
log = logging.getLogger()
if DEBUG:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
SETUPPY_PATH = os.path.dirname(os.path.abspath(__file__)) or '.'
# log.debug('SETUPPY_PATH: %s' % SETUPPY_PATH)
DATA_DIRS = CONFIG['data_dirs'] = ['bin', 'etc', 'docs', 'requirements']
CONFIG['excludes'] = (
'*.pyc',
'*.pyo',
'*.swp*',
'.hgignore',
'.hgsubs',
'.netrwhist',
'*~',
'*.un~',
'*.bak',
)
CONFIG['exclude_dirs'] = (
'.git',
'.hg',
'.bzr',
'.svn',
'CVS',
'_darcs',
'./build',
'./dist',
'EGG-INFO',
'*.egg-info',
'__pycache__',
)
def list_files(
where='.',
exclude=CONFIG['excludes'],
exclude_directories=CONFIG['exclude_dirs'],
show_ignored=False):
"""
Return an iterator of ``files`` below ``where`` that
don't match anything in ``exclude``.
Directories matching any pattern in ``exclude_directories`` will
be ignored; by default directories with leading ``.``, ``CVS``,
and ``_darcs`` will be ignored.
If ``show_ignored`` is true, then all the files that aren't
included in package data are logged at DEBUG level
Note patterns use wildcards, or can be exact paths (including
leading ``./``), and all searching is case-insensitive.
This function is derived from ``find_package_data`` by Ian Bicking.
"""
stack = deque([(convert_path(where), '')])
while stack:
where, prefix = stack.pop()
try:
for name in sorted(os.listdir(where), reverse=True):
fn = os.path.join(where, name)
bad_name = False
if os.path.isdir(fn):
for pattern in exclude_directories:
if (fnmatchcase(name, pattern)
or fn.lower() == pattern.lower()):
bad_name = True
if show_ignored:
log.debug(
"Skipping dir %s [[ pattern %s ]]",
fn, pattern)
if not bad_name:
stack.append((fn, prefix + name + '/'))
else:
for pattern in exclude:
if (fnmatchcase(name, pattern)
or fn.lower() == pattern.lower()):
bad_name = True
if show_ignored:
log.debug(
"Skipping file %s [[ pattern %s ]]",
fn, pattern)
continue
if not bad_name:
yield prefix+name
except OSError as e:
if e.errno == errno.EACCES:
log.error("Skipping %s", name)
return
def get_data_dirs(dirs=DATA_DIRS):
"""
mkdir ./a
mkdir ./a/b
mkdir ./a/b/c
touch ./a/b/c/1
touch ./a/file
touch ./a/b/c/2
>> get_data_dirs('.')
<< (
('./a/b/c', ('a/b/c/1',) ), # ..
('./a', ('a/file',) ),
('./a/b/c', ('2',) ),
#...,
)
"""
# from itertools import groupby
# groupby(func, lambda x: x[0])
for d in dirs:
for f in list_files(d):
p = os.path.join(d, f)
dirname = os.path.dirname(p)
yield './%s' % dirname, (p,)
data_files = list(get_data_dirs())
def get_long_description(readme='README.rst', changelog='CHANGELOG.rst'):
"""
Returns:
str: README.rst and CHANGELOG.rst read into a string
Expects README and CHANGELOG to include compatible headers
"""
with open(os.path.join(SETUPPY_PATH, readme)) as f:
README = f.read()
with open(os.path.join(SETUPPY_PATH, changelog)) as f:
CHANGELOG = f.read()
return '\n\n'.join((README, CHANGELOG,))
def read_requirements_txt(path):
"""
read only entries from requirements.txt in the form::
pkg
pkg==0.1.0
pkg<=0.1.0
pkg>=0.1.0
this parser reads any non-comment, non -* line
"""
requirements = []
_path = os.path.join(SETUPPY_PATH, path)
try:
tf = TextFile(_path)
requirements_txt = (x.lstrip() for x in tf.readlines())
for line in requirements_txt:
if not line.startswith('-'):
requirements.append(line)
finally:
tf and tf.close()
return requirements
def read_requirements_pip(path):
import pip.req
return list(
(req.name, req.editable)
for req in pip.req.parse_requirements(path))
# Extra requirement sets
dev_extras = read_requirements_txt('requirements/requirements-dev.txt')
testing_extras = read_requirements_txt('requirements/requirements-testing.txt')
docs_extras = read_requirements_txt('requirements/requirements-docs.txt')
extras_require = {
"dev": dev_extras,
"testing": testing_extras,
"docs": docs_extras,
}
class RunCommand(setuptools.Command):
user_options = []
description = "<TODO>"
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
print(self.__class__.__name__)
# log.debug(extras_require)
class PyTestCommand(RunCommand):
def run(self):
#cmd = [sys.executable, 'pytest']
cmd = ['pytest']
pths = []
fnglobs = (
'src/dotfiles/venv/*.py',
'src/dotfiles/*.py',
'scripts/*.py',
'bin/*',
)
exclude = [
'*/scripts/prompt6.py',# TODO: BLD,TST
'*/scripts/usrlog.py', # TODO: BUG: fix parser to handle '^#\s\s'
# TODO: add unix timestamp to the fromt
# TODO,IDEA: `^##\t` usrlog.log prefix
]
for fnglob in fnglobs:
pths.extend(
sorted(
pth for pth in glob.glob(
os.path.join(SETUPPY_PATH, fnglob))
if (not any(
fnmatchcase(pth, ptrn) for ptrn in exclude))
))
pthdict = OrderedDict()
for pth in pths:
realpath = os.path.realpath(pth)
pthdict.setdefault(realpath, [])
pthdict[realpath].append(pth)
uniq_pths = []
for key in pthdict:
values = pthdict[key]
uniq_pth = next(v for v in values if v.endswith('.py')
and os.path.exists(v))
uniq_pths.append(uniq_pth)
# log.info("%r", uniq_pth)
cmdlist = cmd + uniq_pths
errno = subprocess.call(cmdlist)
raise SystemExit(errno)
class DotfilesBuildCommand(DistutilsBuildCommand):
"""re-generate MANIFEST.in and build"""
description = (
"update MANIFEST.in AND " + DistutilsBuildCommand.description)
def run(self):
generate_manifest_in_from_vcs()
DistutilsBuildCommand.run(self)
class HgManifestCommand(RunCommand):
"""re-generate MANIFEST.in from $(hg manifest)"""
description = __doc__
def run(self):
generate_manifest_in_from_hg()
class GitManifestCommand(RunCommand):
"""Generate MANIFEST.in from $(git ls-files)"""
description = __doc__
def run(self):
generate_manifest_in_from_git()
def launch_hg_serve(path='.'):
cmd = ["hg", "-R", path, "serve"]
output = subprocess.call(cmd)
return output
class HgServeCommand(RunCommand):
"""run hg serve"""
description = __doc__
def run(self):
return launch_hg_serve(SETUPPY_PATH)
def launch_git_serve():
cmd = ["git", "instaweb", "--httpd=webrick"]
print("# Run the following command to stop the git instaweb server::")
print(" git instaweb --httpd=webrick --stop")
output = subprocess.call(cmd)
return output
class GitServeCommand(RunCommand):
"""run hg serve"""
description = __doc__
def run(self):
return launch_git_serve()
def find_vcs_repository():
"""
Find the nearest .hg and .git repository roots,
accoridn to the commandline tools
evaluated with ``shell=True``.
Returns:
(vcs_name, repo_path): generator of tuples
"""
try:
cmd = r'''hg root'''
hg_root = subprocess.check_output(cmd, shell=True)
yield ('hg', hg_root)
cmd = r'''git rev-parse --show-toplevel 2>/dev/null '''
git_root = subprocess.check_output(cmd, shell=True)
yield ('git', git_root)
except subprocess.CalledProcessError:
pass
def find_closest_repository(matches, relative_to):
"""
Args:
matches (iterable): iterable of (key, path) tuples
relative_to (str): path from which to measure filesystem 'distance'
Returns:
(key, path): closest path relative to the given
Raises:
Exception: no repositories found
"""
minimum_relpath = None
key, match = None, None
for _key, _match in matches:
relpath = os.path.relpath(_match, relative_to)
if (minimum_relpath is None or len(relpath) < len(minimum_relpath)):
minimum_relpath = relpath
key, match = _key, _match
# if None in (key, match):
# raise Exception("no repositories found")
return (key, match)
def generate_manifest_in_from_vcs():
"""
detect .hg or .git and run the manifest command
this .git repository may be nested within a .hg repository
this .hg repository my be nested within a .git repository
example::
# (mkdir outer && cd outer && hg init)
# (mkdir outer/inner && cd outer/inner && git init)
cd /outer/inner
here=/outer/inner
hg_root=$(hg root)
# /outer
git_root=$(git rev-parse --show-toplevel)
# /outer/inner
os.path.relpath(git_root, here)
os.path.relpath(hg_root, here)
"""
relative_to = SETUPPY_PATH
matches = find_vcs_repository()
(key, path) = find_closest_repository(matches, relative_to)
funcmap = {
'hg': generate_manifest_in_from_hg,
'git': generate_manifest_in_from_git,
}
generate_func = funcmap.get(key, NotImplementedError)
return generate_func()
def generate_manifest_in_from_hg():
"""Generate MANIFEST.in from 'hg manifest'"""
print("generating MANIFEST.in from 'hg manifest'")
cmd = r'''hg manifest | sed 's/\(.*\)/include \1/g' > MANIFEST.in'''
return subprocess.call(cmd, shell=True)
def generate_manifest_in_from_git():
"""Generate MANIFEST.in from 'git ls-files'"""
cmd = r'''git ls-files | sed 's/\(.*\)/include \1/g' > MANIFEST.in'''
return subprocess.call(cmd, shell=True)
setup(
name=APPNAME,
version=VERSION,
description=APPNAME,
long_description=get_long_description(),
classifiers=[],
keywords='dotfiles',
author='Wes Turner',
author_email='[email protected]',
url='https://github.com/westurner/dotfiles',
license='',
packages=find_packages('src'),
package_dir={'': 'src'},
include_package_data=True,
# package_data=package_data,
data_files=data_files,
zip_safe=False,
# test_suite='nose.collector',
# tests_require=testing_extras, # pip install -r requirements-testing.txt
# install_requires=(always_install + testing_extras),
install_requires=[],
extras_require=extras_require,
entry_points={
'console_scripts':
[
'dotfiles=dotfiles.cli.cli:main',
'venvpy=dotfiles.venv.ipython_config:main'
]
},
cmdclass={
'test': PyTestCommand,
'hg_manifest': HgManifestCommand,
'git_manifest': GitManifestCommand,
'hg_serve': HgServeCommand,
'git_serve': GitServeCommand,
'build': DotfilesBuildCommand,
}
)