Debian Codegraph @

src/buildstream_plugins/sources/patch.py

raw
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 #
14 # Authors:
15 # Chandan Singh <[email protected]>
16 # Tiago Gomes <[email protected]>
17
18 #
19 # This plugin was originally developped in the https://github.com/apache/buildstream/
20 # repository and was copied from 1a3c707a6c46573ab159de64ac9cd92e7f6027e6
21 #
22
23 """
24 patch - apply locally stored patches
25 ====================================
26
27 **Host dependencies:**
28
29 * patch
30
31 **Usage:**
32
33 .. code:: yaml
34
35 # Specify the local source kind
36 kind: patch
37
38 # Specify the project relative path to a patch file
39 path: files/somefile.diff
40
41 # Optionally specify the strip level, defaults to 1
42 strip-level: 1
43
44
45 See `built-in functionality doumentation
46 <https://docs.buildstream.build/master/buildstream.source.html#core-source-builtins>`_ for
47 details on common configuration options for sources.
48
49
50 Reporting `SourceInfo <https://docs.buildstream.build/master/buildstream.source.html#buildstream.source.SourceInfo>`_
51 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52 The patch source reports the project relative path of the patch file as the *url*.
53
54 Further, the patch source reports the `SourceInfoMedium.LOCAL
55 <https://docs.buildstream.build/master/buildstream.source.html#buildstream.source.SourceInfoMedium.LOCAL>`_
56 *medium* and the `SourceVersionType.SHA256
57 <https://docs.buildstream.build/master/buildstream.source.html#buildstream.source.SourceVersionType.SHA256>`_
58 *version_type*, for which it reports the sha256 checksum of the patch file as the *version*.
59
60 The *guess_version* of a patch source is meaningless, as it is tied instead to
61 the BuildStream project in which it is contained.
62 """
63
64 import os
65 from buildstream import Source, SourceError
66 from buildstream import utils
67
68 #
69 # Soft import of buildstream symbols only available in newer versions
70 #
71 # The BST_MIN_VERSION will provide a better user experience.
72 #
73 try:
74 from buildstream import SourceInfoMedium, SourceVersionType
75 except ImportError:
76 pass
77
78
79 class PatchSource(Source):
80 # pylint: disable=attribute-defined-outside-init
81
82 BST_MIN_VERSION = "2.5"
83
84 BST_REQUIRES_PREVIOUS_SOURCES_STAGE = True
85
86 def configure(self, node):
87 node.validate_keys(["path", "strip-level", *Source.COMMON_CONFIG_KEYS])
88 self.path = self.node_get_project_path(node.get_scalar("path"), check_is_file=True)
89 self.strip_level = node.get_int("strip-level", default=1)
90 self.fullpath = os.path.join(self.get_project_directory(), self.path)
91 self.sha256 = None
92
93 def preflight(self):
94 # Check if patch is installed, get the binary at the same time
95 self.host_patch = utils.get_host_tool("patch")
96
97 def get_unique_key(self):
98 self.sha256 = utils.sha256sum(self.fullpath)
99 return [self.path, self.sha256, self.strip_level]
100
101 def is_resolved(self):
102 return True
103
104 def is_cached(self):
105 return True
106
107 def load_ref(self, node):
108 pass
109
110 def get_ref(self):
111 return None # pragma: nocover
112
113 def set_ref(self, ref, node):
114 pass # pragma: nocover
115
116 def fetch(self): # pylint: disable=arguments-differ
117 # Nothing to do here for a local source
118 pass # pragma: nocover
119
120 def stage(self, directory):
121 with self.timed_activity("Applying local patch: {}".format(self.path)):
122
123 # Bail out with a comprehensive message if the target directory is empty
124 if not os.listdir(directory):
125 raise SourceError(
126 "Nothing to patch in directory '{}'".format(directory),
127 reason="patch-no-files",
128 )
129
130 strip_level_option = "-p{}".format(self.strip_level)
131 self.call(
132 [
133 self.host_patch,
134 strip_level_option,
135 "-i",
136 self.fullpath,
137 "-d",
138 directory,
139 ],
140 fail="Failed to apply patch {}".format(self.path),
141 )
142
143 def collect_source_info(self):
144 return [self.create_source_info(self.path, SourceInfoMedium.LOCAL, SourceVersionType.SHA256, self.sha256)]
145
146
147 # Plugin entry point
148 def setup():
149 return PatchSource