forked from awslabs/sagemaker-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_protobuf.py
executable file
·91 lines (74 loc) · 2.69 KB
/
compile_protobuf.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Standard Library
import glob
import logging
import os
import platform
import shutil
import sys
import tempfile
import time
import urllib
import urllib.request
from subprocess import check_call
from zipfile import ZipFile
def script_name() -> str:
""":returns: script name with leading paths removed"""
return os.path.split(sys.argv[0])[1]
def configure_logging():
logging.getLogger().setLevel(logging.INFO)
logging.basicConfig(format="{}: %(asctime)sZ %(levelname)s %(message)s".format(script_name()))
logging.Formatter.converter = time.gmtime
def _get_system_details():
return platform.system()
def _get_machine_details():
return platform.machine()
def get_protoc_download_url():
"""
Returns an archive with the binary protoc distro for the platform
"""
system = _get_system_details()
machine = _get_machine_details()
if system == "Darwin":
archive_url = "https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protoc-3.11.4-osx-x86_64.zip"
logging.info("Downloading protoc for Darwin: %s", archive_url)
elif system == "Linux":
archive_url = f"https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protoc-3.11.4-linux-{machine}.zip"
logging.info("Downloading protoc for Linux: %s", archive_url)
else:
system = platform.system()
raise RuntimeError(
f"Could not find protoc for System: {system} Machine: {machine}.\
Please install it manually by running sh protoc_downloader.sh"
)
return archive_url
def get_protoc():
"""make sure protoc is available, otherwise download it and return a tuple with the protoc
binary and a temporary dir if it needed to be downloaded"""
if shutil.which("protoc"):
return shutil.which("protoc"), None
archive_url = get_protoc_download_url()
(fname, headers) = urllib.request.urlretrieve(archive_url)
tmpdir = tempfile.mkdtemp(prefix="protoc_smdebug")
with ZipFile(fname, "r") as zipf:
zipf.extractall(tmpdir)
protoc_bin = os.path.join(tmpdir, "bin", "protoc")
# Make the binary executable
os.chmod(protoc_bin, 0o755)
return protoc_bin, tmpdir
def compile_protobuf():
"""
Compile protobuf files for smdebug
"""
logging.info("Compile protobuf")
logging.info("================")
(protoc_bin, tmpdir) = get_protoc()
cmd = [protoc_bin]
proto_files = glob.glob("smdebug/core/tfevent/proto/*.proto")
cmd.extend(proto_files)
cmd.append("--python_out=.")
logging.info("Call to protoc: %s", " ".join(cmd))
check_call(cmd)
if tmpdir:
shutil.rmtree(tmpdir, ignore_errors=True)