11
11
construct_record_file ,
12
12
copyfileobj_with_hashing ,
13
13
fix_shebang ,
14
+ make_file_executable ,
14
15
)
15
16
16
17
if TYPE_CHECKING :
@@ -44,13 +45,18 @@ def write_script(
44
45
raise NotImplementedError
45
46
46
47
def write_file (
47
- self , scheme : Scheme , path : Union [str , "os.PathLike[str]" ], stream : BinaryIO
48
+ self ,
49
+ scheme : Scheme ,
50
+ path : Union [str , "os.PathLike[str]" ],
51
+ stream : BinaryIO ,
52
+ is_executable : bool ,
48
53
) -> RecordEntry :
49
54
"""Write a file to correct ``path`` within the ``scheme``.
50
55
51
56
:param scheme: scheme to write the file in (like "purelib", "platlib" etc).
52
57
:param path: path within that scheme
53
58
:param stream: contents of the file
59
+ :param is_executable: whether the file should be made executable
54
60
55
61
The stream would be closed by the caller, after this call.
56
62
@@ -108,12 +114,19 @@ def __init__(
108
114
self .script_kind = script_kind
109
115
self .hash_algorithm = hash_algorithm
110
116
111
- def write_to_fs (self , scheme : Scheme , path : str , stream : BinaryIO ) -> RecordEntry :
117
+ def write_to_fs (
118
+ self ,
119
+ scheme : Scheme ,
120
+ path : str ,
121
+ stream : BinaryIO ,
122
+ is_executable : bool ,
123
+ ) -> RecordEntry :
112
124
"""Write contents of ``stream`` to the correct location on the filesystem.
113
125
114
126
:param scheme: scheme to write the file in (like "purelib", "platlib" etc).
115
127
:param path: path within that scheme
116
128
:param stream: contents of the file
129
+ :param is_executable: whether the file should be made executable
117
130
118
131
- Ensures that an existing file is not being overwritten.
119
132
- Hashes the written content, to determine the entry in the ``RECORD`` file.
@@ -130,16 +143,24 @@ def write_to_fs(self, scheme: Scheme, path: str, stream: BinaryIO) -> RecordEntr
130
143
with open (target_path , "wb" ) as f :
131
144
hash_ , size = copyfileobj_with_hashing (stream , f , self .hash_algorithm )
132
145
146
+ if is_executable :
147
+ make_file_executable (target_path )
148
+
133
149
return RecordEntry (path , Hash (self .hash_algorithm , hash_ ), size )
134
150
135
151
def write_file (
136
- self , scheme : Scheme , path : Union [str , "os.PathLike[str]" ], stream : BinaryIO
152
+ self ,
153
+ scheme : Scheme ,
154
+ path : Union [str , "os.PathLike[str]" ],
155
+ stream : BinaryIO ,
156
+ is_executable : bool ,
137
157
) -> RecordEntry :
138
158
"""Write a file to correct ``path`` within the ``scheme``.
139
159
140
160
:param scheme: scheme to write the file in (like "purelib", "platlib" etc).
141
161
:param path: path within that scheme
142
162
:param stream: contents of the file
163
+ :param is_executable: whether the file should be made executable
143
164
144
165
- Changes the shebang for files in the "scripts" scheme.
145
166
- Uses :py:meth:`SchemeDictionaryDestination.write_to_fs` for the
@@ -149,9 +170,11 @@ def write_file(
149
170
150
171
if scheme == "scripts" :
151
172
with fix_shebang (stream , self .interpreter ) as stream_with_different_shebang :
152
- return self .write_to_fs (scheme , path_ , stream_with_different_shebang )
173
+ return self .write_to_fs (
174
+ scheme , path_ , stream_with_different_shebang , is_executable
175
+ )
153
176
154
- return self .write_to_fs (scheme , path_ , stream )
177
+ return self .write_to_fs (scheme , path_ , stream , is_executable )
155
178
156
179
def write_script (
157
180
self , name : str , module : str , attr : str , section : "ScriptSection"
@@ -174,7 +197,9 @@ def write_script(
174
197
script_name , data = script .generate (self .interpreter , self .script_kind )
175
198
176
199
with io .BytesIO (data ) as stream :
177
- entry = self .write_to_fs (Scheme ("scripts" ), script_name , stream )
200
+ entry = self .write_to_fs (
201
+ Scheme ("scripts" ), script_name , stream , is_executable = True
202
+ )
178
203
179
204
path = os .path .join (self .scheme_dict [Scheme ("scripts" )], script_name )
180
205
mode = os .stat (path ).st_mode
@@ -206,4 +231,6 @@ def prefix_for_scheme(file_scheme: str) -> Optional[str]:
206
231
return path + "/"
207
232
208
233
with construct_record_file (records , prefix_for_scheme ) as record_stream :
209
- self .write_to_fs (scheme , record_file_path , record_stream )
234
+ self .write_to_fs (
235
+ scheme , record_file_path , record_stream , is_executable = False
236
+ )
0 commit comments