-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdodo.py
123 lines (91 loc) · 2.96 KB
/
dodo.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
# coding: utf-8
'''
doit script for building the tools.
Run `doit` to create runnable scripts for all platforms.
See http://pydoit.org/ for details.
'''
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
import datetime
import os
import shutil
import stat
import subprocess
import zipfile
DOIT_CONFIG = dict(default_tasks=['build'])
def task_git_clean():
return dict(actions=[git_clean])
def task_test():
return dict(
actions=['''in-virtualenv -r test_requirements.txt python -m unittest discover -p '*test*.py' '''])
def task_zip():
return dict(
file_dep=subprocess.check_output(['git', 'ls-files']).splitlines(),
actions=[make_zip],
targets=['sources.zip'],
clean=True)
def task_build():
def windows(name):
return '{}.cmd'.format(name)
# windows = '{}.cmd'.format
def unix(name):
return name
name_to_taxids = 'name-to-taxids-{}'.format(timestamp())
return dict(
file_dep=['sources.zip'],
actions=[
(make_zipapp, [unix( name_to_taxids), UNIX_PREFIX, 'firm_name_search.main']),
(make_zipapp, [windows( name_to_taxids), WINDOWS_PREFIX, 'firm_name_search.main'])],
targets=[
unix(name_to_taxids),
windows(name_to_taxids)],
clean=True)
###
# helpers, implementation
def run(cmd, *args, **kwargs):
print(cmd, args, kwargs)
subprocess.check_call(cmd.split(), *args, **kwargs)
def git_clean():
run('git clean -X -f')
def make_zip():
run('pip install . -t _build --no-compile')
# print(get_sources('_build').readlines())
sources = subprocess.Popen(
['find', '-name' , '*.py'], cwd='_build', stdout=subprocess.PIPE
).stdout
run('zip -@q ../sources.zip', cwd='_build', stdin=sources)
shutil.rmtree('_build', ignore_errors=True)
def timestamp():
return datetime.date.today().strftime('%Y%m%d')
def make_executable(file):
st = os.stat(file)
os.chmod(file, st.st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)
UNIX_PREFIX = b'#!/usr/bin/env python\n'
WINDOWS_PREFIX = b'\r\n'.join((
b'@echo off',
b'python.exe "%~f0" %*',
b'exit /b %errorlevel%',
b''))
MAIN_PY = '''\
from {module_path} import {main_function}
{main_function}('{version}')
'''
def make_zipapp(app_file, os_prefix, module_path, main_function='main'):
shutil.copy('sources.zip', app_file)
with zipfile.ZipFile(app_file, mode='a') as z:
z.writestr(
'__main__.py',
MAIN_PY.format(
module_path=module_path,
main_function=main_function,
version='Version {}'.format(datetime.datetime.now())))
with open(app_file, 'r+b') as f:
app_zip = f.read()
# rewrite with os_prefix
f.seek(0)
f.truncate()
f.write(os_prefix)
f.write(app_zip)
make_executable(app_file)