This repository was archived by the owner on Jun 9, 2021. It is now read-only.
forked from segmentio/analytics-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_analytics_namespace.py
More file actions
executable file
·246 lines (193 loc) · 7.36 KB
/
fix_analytics_namespace.py
File metadata and controls
executable file
·246 lines (193 loc) · 7.36 KB
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
#!/usr/bin/env python
import argparse
from glob import glob
import logging
import os
import re
import shutil
from subprocess import check_output, check_call, call, PIPE
import sys
description = """ This was created to take the analytics-python package
which segment.io provides and rename it to segmentio to avoid namespace
conflicts.
Works this way:
1) based on a `segment-release` branch
2) update the master based on segmentio/analytics-python
3) confirm an new release needs to happen
4) merge the new release into `segment-release`
5) generate 'segmentio' package from 'analytics' & update 'setup.py'
6) commit new 'segmentio' changes
TODO: re-write. Should do tagged releases similar. Basically analytics-python
has release tag=1.0.1 then this segmentio-release branch should have a
similar tag=1.0.1-seg1 where the first part is the same tag number then the
'-' separates a versioning for this particular code. Then dev can choose
particular tags to pull from.
"""
logging.basicConfig(
format='%(levelname)s (L%(lineno)s): %(message)s',
stream=sys.stdout,
level=logging.DEBUG,
)
def get_segmentio_analytics_python_remote():
remotes = check_output(["git", "remote", "-v"])
pattern = (
'(?P<name>[a-zA-Z_]*)'
'\t(?P<url>git@github\.com:segmentio/analytics-python)'
' \(fetch\)'
)
m = re.search(pattern, remotes)
if m is None:
raise ValueError('could not find segment remote')
name, url = m.groups()
return name, url
def fetch_master_from_segmentio_analytics_python():
name, url = get_segmentio_analytics_python_remote()
check_call(['git', 'fetch', name, 'master'])
check_call(['git', 'fetch', '--tags', name])
def checkout_segmentio_branded_branch():
fixed_branch = 'segmentio-release'
current_branch = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
if fixed_branch != current_branch:
exists = call(['git', 'rev-parse', '--verify', fixed_branch])
if exists != 0:
tag = check_output(['git', 'describe', 'master']).rstrip()
raise StandardError(
"\n\nwhy don't you have branch {0}?!"
"\n run `git checkout -b {0} tags/{1}` to create"
"\n then make sure you have this file committed"
.format(fixed_branch, tag)
)
check_call(['git', 'checkout', fixed_branch])
def get_most_recent_tag():
most_recent_tag = check_output(['git', 'describe', 'master']).rstrip()
cmd = ['git', 'describe', '--abbrev=0', '--tags']
current_tag = check_output(cmd).rstrip()
logging.debug('current_local_tag: {}'.format(current_tag))
logging.debug('most_recent_tag: {}'.format(most_recent_tag))
if current_tag == most_recent_tag:
logging.info('version up-to-date with analytics-python Release')
return current_tag, most_recent_tag
def merge_tagged_version(tag):
most_recent_tag_branch = 'tags/{}'.format(tag)
check_call([
'git', 'merge', '--no-commit',
'-X', 'theirs', most_recent_tag_branch,
])
msg = "merged Release {}".format(tag)
check_call(['git', 'commit', '-am', msg])
def create_segmentio_branded_package(rel_path=None):
if rel_path is None:
rel_path = ['']
p = ['analytics'] + rel_path + ['*']
search = os.path.join(*p)
filelist = glob(search)
for filepath in filelist:
filename = os.path.basename(filepath)
p = ['segmentio'] + rel_path + [filename]
filepath_out = os.path.join(*p)
if os.path.isdir(filepath):
os.mkdir(filepath_out)
create_segmentio_branded_package(rel_path + [filename])
elif filepath.endswith('.py'):
with open(filepath) as f:
c = f.read()
c = c.replace('import analytics', 'import segmentio')
c = c.replace('from analytics.', 'from segmentio.')
with open(filepath_out, 'w') as f:
f.write(c)
cmd = ['diff', '-q', filepath, filepath_out]
differ = call(cmd, stdout=PIPE, stderr=PIPE)
if differ:
msg = (
'{:>12} file `diff {} {}`'
.format('refactored', filepath, filepath_out)
)
else:
msg = (
'{:>12} file src:{} dest:{}`'
.format('copied', filepath, filepath_out)
)
logging.debug(msg)
else:
msg = (
'{:>12} file src:{} dest:{}`'
.format('copied', filepath, filepath_out)
)
logging.debug(msg)
shutil.copy2(filepath, filepath_out)
def refactor_setup_with_segmentio_branding():
fn = 'setup.py'
with open(fn) as f:
c = f.read()
c = c.replace(
"from version import VERSION",
"from segmentio.version import VERSION",
)
c = c.replace(
"name='analytics-python'",
"name='segmentio'",
)
c = c.replace(
"test_suite='analytics.test.all'",
"test_suite='segmentio.test.all'",
)
c = c.replace(
"packages=['analytics', 'analytics.test']",
"packages=['segmentio', 'segmentio.test']"
)
with open(fn, 'w') as f:
f.write(c)
parser = argparse.ArgumentParser(
description=description,
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
'--skip-release-merge',
action='store_true',
)
if __name__ == '__main__':
pargs = parser.parse_args()
cmd = ['git', 'rev-parse', '--verify', 'HEAD']
starting_commit = check_output(cmd).rstrip()
logging.info(
'\nif anything goes wrong revert to current state with:'
'\n `git reset --hard {}`'
.format(starting_commit)
)
logging.info('fetching from segmentio/analytics-python')
fetch_master_from_segmentio_analytics_python()
logging.info('checkout segmentio branded branch')
checkout_segmentio_branded_branch()
logging.info('extracting most recent tag tags')
current_tag, most_recent_tag = get_most_recent_tag()
if not pargs.skip_release_merge:
if current_tag == most_recent_tag:
sys.exit(0)
logging.info('merge most recent tagged version')
merge_tagged_version(most_recent_tag)
try:
logging.info('clobber segmentio generated packaged')
if os.path.isdir('segmentio'):
shutil.rmtree('segmentio')
os.mkdir('segmentio')
logging.info('generate segmentio package')
create_segmentio_branded_package()
refactor_setup_with_segmentio_branding()
logging.info('commit segmentio package creation')
msg = "created segmentio release"
check_call(['git', 'add', 'segmentio'])
check_call(['git', 'commit', '-am', msg])
except Exception:
logging.error('whoops, resetting to initial commit for you')
# reset to pior to merging release
check_call(['git', 'reset', '--hard', starting_commit])
raise
shutil.rmtree('analytics')
check_call(['git', 'add', 'analytics'])
msg = "removed analytics package"
check_call(['git', 'commit', '-am', msg])
logging.info('now you may run: `git push origin segmentio-release`')
logging.info(
'successfully created segmentio Release {}'
.format(most_recent_tag)
)