-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
executable file
·114 lines (92 loc) · 3.07 KB
/
build.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
#!/usr/bin/env python
from argparse import ArgumentParser
from datetime import datetime
from functools import partial
from pathlib import Path
from subprocess import check_output
from subprocess import run
import os
import yaml
root = Path(__file__).parent.resolve()
def main():
parser = ArgumentParser()
parser.add_argument('recipe_dir', type=Path)
parser.add_argument('py_ver')
args = parser.parse_args()
recipe_dir = args.recipe_dir.resolve()
# Donwload source code
src_dir = git_clone(recipe_dir)
# Set environment variables to be queried in recipe's meta.yaml
os.environ['RECIPE_PKG_VER'] = get_pkg_ver(src_dir)
os.environ['RECIPE_SRC_DIR'] = os.path.relpath(src_dir, recipe_dir)
# Build package
pkg_file = conda_build(recipe_dir, args.py_ver)
# Upload package
anaconda_upload(pkg_file, 'yt-project')
def git_clone(recipe_dir):
with (recipe_dir / 'source.yaml').open() as f:
source = yaml.load(f)
repo_dir = root / 'repo'
repo_dir.mkdir(exist_ok=True)
src_dir = repo_dir / recipe_dir.name
if not src_dir.exists():
print(f'\nDownloading {recipe_dir.name} source code')
run([
'git', 'clone',
'-b', source['git_rev'],
'--depth', '1',
source['git_url'], str(src_dir)
], check=True)
return src_dir
def get_pkg_ver(src_dir):
run_cmd = partial(check_output, shell=True, cwd=src_dir, text=True)
# The output could be multiple lines (on Windows)
# We just need the last one
pkg_ver = run_cmd('python setup.py --version').split()[-1]
pkg_ver = pkg_ver.strip()
utime = run_cmd('git log -1 --pretty=format:%ct')
chash = run_cmd('git log -1 --pretty=format:%h')
stamp = datetime.fromtimestamp(int(utime)).strftime('%Y%m%d%H%M%S')
pkg_ver += f'_{stamp}_{chash}'
print(f'Package version: {pkg_ver}')
return pkg_ver
def conda_build(recipe_dir, py_ver):
print(f'\nBuilding {recipe_dir.name} in Python {py_ver}')
build_cmd = [
'conda', 'build',
'--python', py_ver,
'--old-build-string',
str(recipe_dir)
]
run(build_cmd, check=True)
# Be careful with the trailing newline
pkg_file = check_output(build_cmd + ['--output'], text=True).strip()
return pkg_file
def check_env(name, value):
if (name in os.environ) and (os.environ[name] == value):
return True
else:
return False
def anaconda_upload(pkg_file, user):
# Don't upload when not using CI
# This check works for most CIs including Travis CI and AppVeyor
if not 'CI' in os.environ:
return
# Don't upload from pull requests
if check_env('TRAVIS_EVENT_TYPE', 'pull_request'):
return
if 'APPVEYOR_PULL_REQUEST_NUMBER' in os.environ:
return
# Now we actually want to upload
print(f'\nUploading {pkg_file}')
run([
'anaconda',
'-t', os.environ['CONDA_UPLOAD_TOKEN'],
'upload',
'-u', user,
'-l', 'dev',
'--skip',
str(pkg_file)
], check=True)
if __name__ == '__main__':
main()