-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpackage
More file actions
executable file
·160 lines (130 loc) · 4.58 KB
/
package
File metadata and controls
executable file
·160 lines (130 loc) · 4.58 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
#!/usr/bin/env python3
# Copyright 2019 Katteli Inc.
# TestFlows.com Open-Source Software Testing Framework (http://testflows.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import re
import sys
import shutil
import argparse
import subprocess
from urllib.parse import urlparse
from datetime import datetime, timezone
parser = argparse.ArgumentParser(description="TestFlows - Core build script")
parser.add_argument(
"--debug", help="enable debugging", action="store_true", default=False
)
current_dir = os.path.dirname(os.path.abspath(__file__))
package = os.path.join("testflows", "_core")
init_path = os.path.join(current_dir, package, "__init__.py")
setup_path = os.path.join(current_dir, "setup.py")
def clean_url(url, rchop=None):
if "git@github.com:" in url:
url = f"https://github.com/{url.split('git@github.com:',1)[-1]}"
url = url.rsplit(".git", 1)[0]
parsed = urlparse(url)
cleaned_url = parsed._replace(netloc="{}".format(parsed.hostname)).geturl()
if rchop and cleaned_url.endswith(rchop):
cleaned_url[: -len(rchop)]
return cleaned_url
def call(*command):
return subprocess.run(command, check=True, capture_output=True, encoding="utf-8")
repo_name = clean_url(call("git", "remote", "get-url", "origin").stdout.strip())
repo_commit = call("git", "rev-parse", "HEAD").stdout.strip()
repo_branch = call("git", "branch").stdout.lstrip("*").strip()
def get_base_version():
"""Return package base version."""
version = None
with open(init_path) as fd:
for line in fd.readlines():
if line.startswith("__version__ = "):
match = re.match(r'__version__\s=\s"(?P<version>.+).__VERSION__', line)
if match:
version = match.groupdict().get("version")
if version:
break
if not version:
raise ValueError("failed to get base version number")
return version
def get_revision():
"""Return build revision."""
now = datetime.now(timezone.utc)
major_revision = now.strftime("%y%m%d")
minor_revision = now.strftime("%H%M%S")
return major_revision + ".1" + minor_revision
def set_attributes(path, **attrs):
"""Set attributes in the file.
:param path: path
:param version: version
"""
with open(path, "a+") as fd:
fd.seek(0)
content = fd.read()
fd.seek(0)
fd.truncate()
new_content = content
for key, value in attrs.items():
new_content = new_content.replace(key, value)
fd.write(new_content)
return content
def unset_attributes(path, content):
"""Unset attributes in the file.
:param path: path
:param content: original file content
"""
with open(path, "a+") as fd:
fd.seek(0)
fd.truncate()
fd.write(content)
def build_package(args, options):
"""Build package.
:param args: arguments
:param options: extra options
"""
subprocess.run(
["/usr/bin/env", "python3", "setup.py"]
+ (["-q"] if not args.debug else [])
+ ["sdist"]
+ (options if options else [])
)
def build(args, options=None):
"""Build package.
:param args: arguments
:param options: build options, default: ``None``
"""
if options is None:
options = []
if os.path.exists("dist"):
shutil.rmtree("dist")
base_version = get_base_version()
revision = get_revision()
version = ".".join([base_version, revision])
init_content, setup_content = None, None
attrs = {
"__REPOSITORY__": repo_name,
"__COMMIT__": repo_commit,
"__BRANCH__": repo_branch,
}
try:
init_content = set_attributes(init_path, **{"__VERSION__": revision}, **attrs)
setup_content = set_attributes(setup_path, **{"__VERSION__": version})
build_package(args, options)
finally:
if init_content:
unset_attributes(init_path, init_content)
if setup_content:
unset_attributes(setup_path, setup_content)
if __name__ == "__main__":
args = parser.parse_args()
build(args)