|
1
|
+"""
|
|
2
|
+foo_transform - transform "file" from previous sources into "filetransform"
|
|
3
|
+===========================================================================
|
|
4
|
+
|
|
5
|
+This is a test source plugin that looks for a file named "file" staged by
|
|
6
|
+previous sources, and copies its contents to a file called "filetransform".
|
|
7
|
+
|
|
8
|
+"""
|
|
9
|
+
|
1
|
10
|
import os
|
2
|
11
|
import hashlib
|
3
|
12
|
|
... |
... |
@@ -5,23 +14,25 @@ from buildstream import Consistency, Source, SourceError, utils |
5
|
14
|
|
6
|
15
|
|
7
|
16
|
class FooTransformSource(Source):
|
8
|
|
- """Test plugin that copies "file" from previous source as "filetransform".
|
9
|
17
|
|
10
|
|
- """
|
11
|
|
- requires_previous_sources_fetch = True
|
|
18
|
+ # We need access to previous both at track time and fetch time
|
12
|
19
|
requires_previous_sources_track = True
|
13
|
|
-
|
14
|
|
- def configure(self, node):
|
15
|
|
- self.node_validate(node, ['ref'] + Source.COMMON_CONFIG_KEYS)
|
16
|
|
- self.ref = self.node_get_member(node, str, 'ref', None)
|
|
20
|
+ requires_previous_sources_fetch = True
|
17
|
21
|
|
18
|
22
|
@property
|
19
|
23
|
def mirror(self):
|
|
24
|
+ """Directory where this source should stage its files
|
|
25
|
+
|
|
26
|
+ """
|
20
|
27
|
path = os.path.join(self.get_mirror_directory(), self.name,
|
21
|
28
|
self.ref.strip())
|
22
|
29
|
os.makedirs(path, exist_ok=True)
|
23
|
30
|
return path
|
24
|
31
|
|
|
32
|
+ def configure(self, node):
|
|
33
|
+ self.node_validate(node, ['ref'] + Source.COMMON_CONFIG_KEYS)
|
|
34
|
+ self.ref = self.node_get_member(node, str, 'ref', None)
|
|
35
|
+
|
25
|
36
|
def preflight(self):
|
26
|
37
|
pass
|
27
|
38
|
|
... |
... |
@@ -31,6 +42,8 @@ class FooTransformSource(Source): |
31
|
42
|
def get_consistency(self):
|
32
|
43
|
if self.ref is None:
|
33
|
44
|
return Consistency.INCONSISTENT
|
|
45
|
+ # If we have a file called "filetransform", verify that its checksum
|
|
46
|
+ # matches our ref. Otherwise, it resolved but not cached.
|
34
|
47
|
fpath = os.path.join(self.mirror, 'filetransform')
|
35
|
48
|
try:
|
36
|
49
|
with open(fpath, 'rb') as f:
|
... |
... |
@@ -47,19 +60,25 @@ class FooTransformSource(Source): |
47
|
60
|
self.ref = node['ref'] = ref
|
48
|
61
|
|
49
|
62
|
def track(self, previous_sources_dir):
|
|
63
|
+ # Store the checksum of the file from previous source as our ref
|
50
|
64
|
fpath = os.path.join(previous_sources_dir, 'file')
|
51
|
65
|
with open(fpath, 'rb') as f:
|
52
|
66
|
return hashlib.sha256(f.read()).hexdigest()
|
53
|
67
|
|
54
|
68
|
def fetch(self, previous_sources_dir):
|
55
|
69
|
fpath = os.path.join(previous_sources_dir, 'file')
|
|
70
|
+ # Verify that the checksum of the file from previous source matches
|
|
71
|
+ # our ref
|
56
|
72
|
with open(fpath, 'rb') as f:
|
57
|
73
|
if hashlib.sha256(f.read()).hexdigest() != self.ref.strip():
|
58
|
74
|
raise SourceError("Element references do not match")
|
|
75
|
+
|
|
76
|
+ # Copy "file" as "filetransform"
|
59
|
77
|
newfpath = os.path.join(self.mirror, 'filetransform')
|
60
|
78
|
utils.safe_copy(fpath, newfpath)
|
61
|
79
|
|
62
|
80
|
def stage(self, directory):
|
|
81
|
+ # Simply stage the "filetransform" file
|
63
|
82
|
utils.safe_copy(os.path.join(self.mirror, 'filetransform'),
|
64
|
83
|
os.path.join(directory, 'filetransform'))
|
65
|
84
|
|