forked from aws/deep-learning-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.py
More file actions
82 lines (65 loc) · 2.5 KB
/
Copy pathcontext.py
File metadata and controls
82 lines (65 loc) · 2.5 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
"""
Copyright 2019-2020 Amazon.com, Inc. or its affiliates. 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. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file 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 tarfile
class Context:
"""
The context class encapsulates all required functions for
preparing, managing and removing the docker build context
"""
def __init__(self, artifacts=None, context_path="context.tar.gz", artifact_root="./"):
"""
The constructor for the Context class
Parameters:
artifacts: array of (source, destination) tuples
context_path: path for the resulting tar.gz file
artifact_root: root directory for all artifacts
Returns:
None
"""
self.artifacts = {}
self.context_path = context_path
self.artifact_root = artifact_root
# Check if the context path is just a filename,
# or includes a directory. If path includes a
# directory, create directory if it does not exist
directory = os.path.dirname(context_path)
if directory != "" and not os.path.isdir(directory):
os.mkdir(directory)
if artifacts is not None:
self.add(artifacts)
def add(self, artifacts):
"""
Adds artifacts to the build context
Parameters:
artifacts: array of (source, destination) tuples
"""
self.artifacts.update(artifacts)
# TODO: Use glob to expand
# TODO: Add logic to untar and retar
with tarfile.open(self.context_path, "w:gz") as tar:
for artifact_name in artifacts:
artifact = artifacts[artifact_name]
if "source" not in artifact or "target" not in artifact:
continue
source = os.path.join(self.artifact_root, artifact["source"])
target = artifact["target"]
tar.add(source, arcname=target)
def remove(self):
"""
Removes the context tar file
Parameters:
None
Returns:
None
"""
os.remove(self.context_path)