This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare_recipe.py
175 lines (127 loc) · 5.37 KB
/
prepare_recipe.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
import os
import sys
import json
import shutil
import subprocess
from datetime import datetime
import requests
from jinja2 import Template
SOURCE_DEV = """
source:
path: ../../{{package}}
"""
SOURCE_STABLE = """
source:
fn: {{filename}}
url: {{url}}
md5: {{md5_digest}}
"""
SOURCE_STABLE_GIT = """
source:
git_tag: {{tag}}
git_url: https://github.com/spacetelescope/{{package}}.git
"""
def prepare_recipe_dev(package):
with open(os.path.join('recipes', package, 'meta.yaml')) as f:
content = f.read()
if os.path.exists(package):
os.chdir(package)
package_dir = package
elif os.path.exists(package + '.git'):
os.chdir(package + '.git')
package_dir = package + '.git'
else:
print('Package {0} not cloned'.format(package))
sys.exit(1)
overall_version = subprocess.check_output('python setup.py --version', shell=True).decode('ascii').splitlines()[-1]
# If using a pre-release instead of a git repository, we shouldn't try and
# get the git hash.
if os.path.exists('.git'):
utime = subprocess.check_output('git log -1 --pretty=format:%ct', shell=True).decode('ascii')
chash = subprocess.check_output('git log -1 --pretty=format:%h', shell=True).decode('ascii')
os.chdir('..')
stamp = datetime.fromtimestamp(int(utime)).strftime('%Y%m%d%H%M%S')
overall_version = overall_version.strip().split('.dev')[0]
version = overall_version + '.dev' + stamp + '.' + chash
else:
os.chdir('..')
version = overall_version
source = Template(SOURCE_DEV).render(package=package_dir)
recipe = Template(content).render(version=version, source=source)
if not os.path.exists('generated'):
os.mkdir('generated')
if not os.path.exists(os.path.join('generated', package)):
os.mkdir(os.path.join('generated', package))
with open(os.path.join('generated', package, 'meta.yaml'), 'w') as f:
f.write(recipe)
for filename in os.listdir(os.path.join('recipes', package)):
if filename != 'meta.yaml':
shutil.copyfile(os.path.join('recipes', package, filename),
os.path.join('generated', package, filename))
def pre(version):
return 'a' in version or 'b' in version or 'rc' in version
def prepare_recipe_stable(package):
with open(os.path.join('recipes', package, 'meta.yaml')) as f:
content = f.read()
# Find latest stable version from PyPI
package_json = requests.get('https://pypi.python.org/pypi/{package}/json'.format(package=package)).json()
releases = package_json['releases']
releases = dict([(version, release) for version, release in releases.items() if not pre(version)])
version = sorted(releases, key=lambda s: tuple(map(int, s.split('.'))))[-1]
releases = releases[version]
for release in releases:
if release['python_version'] == 'source':
break
else:
raise Exception("Cannot find source package for {0}".format(package))
source = Template(SOURCE_STABLE).render(**release)
recipe = Template(content).render(version=version, source=source)
if not os.path.exists('generated'):
os.mkdir('generated')
if not os.path.exists(os.path.join('generated', package)):
os.mkdir(os.path.join('generated', package))
with open(os.path.join('generated', package, 'meta.yaml'), 'w') as f:
f.write(recipe)
for filename in os.listdir(os.path.join('recipes', package)):
if filename != 'meta.yaml':
shutil.copyfile(os.path.join('recipes', package, filename),
os.path.join('generated', package, filename))
def prepare_recipe_stable_git(package):
with open(os.path.join('recipes', package, 'meta.yaml')) as f:
content = f.read()
# Find latest stable version from GitHub - needs authentication on Travis
# package_json = requests.get('https://api.github.com/repos/spacetelescope/{package}/tags'.format(package=package)).json()
# version = package_json[0]['name']
with open('tags.json') as f:
tag = json.load(f)[package]
version = tag.replace('v', '')
source = Template(SOURCE_STABLE_GIT).render(tag=tag, version=version, package=package)
recipe = Template(content).render(version=version, source=source)
if not os.path.exists('generated'):
os.mkdir('generated')
if not os.path.exists(os.path.join('generated', package)):
os.mkdir(os.path.join('generated', package))
with open(os.path.join('generated', package, 'meta.yaml'), 'w') as f:
f.write(recipe)
for filename in os.listdir(os.path.join('recipes', package)):
if filename != 'meta.yaml':
shutil.copyfile(os.path.join('recipes', package, filename),
os.path.join('generated', package, filename))
def main_stable(*packages):
for package in packages:
prepare_recipe_stable(package)
def main_stable_git(*packages):
for package in packages:
prepare_recipe_stable_git(package)
def main_dev(*packages):
for package in packages:
prepare_recipe_dev(package)
if __name__ == "__main__":
if '--stable' in sys.argv:
sys.argv.remove('--stable')
main_stable(*sys.argv[1:])
elif '--stable-git' in sys.argv:
sys.argv.remove('--stable-git')
main_stable_git(*sys.argv[1:])
else:
main_dev(*sys.argv[1:])