-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathprepare-binaries-for-github-release.py
executable file
·112 lines (95 loc) · 4.32 KB
/
prepare-binaries-for-github-release.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
#!/usr/bin/env python3
# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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.
"""Builds the bundle of binaries for attaching them to a new Release of the
project at GitHub."""
import os
import shutil
import sys
import tarfile
import tempfile
# Name of the directory where the created binaries will be placed. The directory
# will be created in the same directory where this script is located.
TARGET_DIRECTORY_NAME = 'binaries-for-github-release'
# Base name (not including the extension) of the binary archive files to be
# created in the target directory.
BINARY_ARCHIVE_BASE_FILE_NAME = 'binaries'
# Paths to the files and directories to be copied into the resulting binary
# archives under the same relative path.
BINARY_CONTENTS_RELATIVES_PATHS = [
'LICENSE',
'built_app_packages/',
'example_js_standalone_smart_card_client_library/README.rst',
'example_js_standalone_smart_card_client_library/js_build/',
]
# Paths to the files and directories to be copied into the resulting binary
# archives, accompanied with the relative target file paths.
PATHS_TO_COPY = [
['example_js_standalone_smart_card_client_library/js_build/'
'extension_emscripten_Debug/google-smart-card-client-library.js',
'google-smart-card-client-library.debug.js'],
['example_js_standalone_smart_card_client_library/js_build/'
'extension_emscripten_Release/google-smart-card-client-library.js',
'google-smart-card-client-library.js'],
['example_js_standalone_smart_card_client_library/js_build/'
'extension_emscripten_Debug/google-smart-card-client-library-es-module.js',
'google-smart-card-client-library-es-module.debug.js'],
['example_js_standalone_smart_card_client_library/js_build/'
'extension_emscripten_Release/google-smart-card-client-library-es-module.js',
'google-smart-card-client-library-es-module.js'],
]
def main():
repository_path = (os.path.relpath(os.path.dirname(__file__))
if os.path.dirname(__file__) else '.')
target_path = os.path.join(repository_path, TARGET_DIRECTORY_NAME)
shutil.rmtree(target_path, ignore_errors=True)
os.mkdir(target_path)
for source_relative_path, target_relative_path in PATHS_TO_COPY:
copy_file_or_dir(os.path.join(repository_path, source_relative_path),
os.path.join(target_path, target_relative_path))
temp_path = tempfile.mkdtemp()
try:
for relative_path in BINARY_CONTENTS_RELATIVES_PATHS:
copy_file_or_dir(os.path.join(repository_path, relative_path),
os.path.join(temp_path, relative_path))
binary_archive_base_file_path = os.path.join(target_path,
BINARY_ARCHIVE_BASE_FILE_NAME)
make_targz(temp_path, binary_archive_base_file_path)
make_zip(temp_path, binary_archive_base_file_path)
finally:
shutil.rmtree(temp_path)
sys.stderr.write('Successfully created resulting files at {0}\n'.format(
target_path))
def mkdir_recursive(directory_path):
if os.path.isdir(directory_path):
return
if os.path.exists(directory_path):
raise RuntimeError('Cannot create directory "{0}": file already '
'exists'.format(directory_path))
mkdir_recursive(os.path.dirname(directory_path))
os.mkdir(directory_path)
def copy_file_or_dir(source_path, target_path):
mkdir_recursive(os.path.dirname(os.path.normpath(target_path)))
if os.path.isdir(source_path):
shutil.copytree(source_path, target_path)
else:
shutil.copy2(source_path, target_path)
def make_targz(source_directory, target_base_file_path):
with tarfile.open(target_base_file_path + '.tar.gz', 'w:gz') as tar:
tar.add(source_directory, arcname='')
def make_zip(source_directory, target_base_file_path):
shutil.make_archive(target_base_file_path, format='zip',
root_dir=source_directory)
if __name__ == '__main__':
main()