forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceMetaPathImporter.cs
More file actions
435 lines (384 loc) · 18.7 KB
/
ResourceMetaPathImporter.cs
File metadata and controls
435 lines (384 loc) · 18.7 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using IronPython.Runtime;
using IronPython.Runtime.Exceptions;
using IronPython.Zlib;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
namespace IronPython.Modules {
/// <summary>
/// Implementes a resource-based meta_path importer as described in PEP 302.
/// </summary>
[PythonType]
public class ResourceMetaPathImporter {
private readonly PackedResourceLoader _loader;
private readonly IDictionary<string, PackedResourceInfo> _unpackedLibrary;
private readonly IDictionary<string, PackedResourceInfo[]> _unpackedModules;
private readonly string _unpackingError;
private static readonly Dictionary<string, ModuleCodeType> SearchOrder;
[Flags]
private enum ModuleCodeType {
Source = 0,
//ByteCode,
Package,
}
static ResourceMetaPathImporter() {
// we currently don't support bytecode, so just include the source versions.
SearchOrder = new Dictionary<string, ModuleCodeType> {
{
Path.DirectorySeparatorChar + "__init__.py",
ModuleCodeType.Package |
ModuleCodeType.Source
},
{".py", ModuleCodeType.Source},
};
}
/// <summary>
/// Instantiates a new meta_path importer using an embedded ZIP resource file.
/// </summary>
/// <param name="fromAssembly"></param>
/// <param name="resourceName"></param>
public ResourceMetaPathImporter(Assembly fromAssembly, string resourceName) {
_loader = new PackedResourceLoader(fromAssembly, resourceName);
if (_loader.LoadZipDirectory(out _unpackedLibrary, out _unpackedModules,
out _unpackingError))
return;
_unpackedLibrary = new Dictionary<string, PackedResourceInfo>();
_unpackedModules = new Dictionary<string, PackedResourceInfo[]>();
if (!String.IsNullOrEmpty(_unpackingError))
throw MakeError("meta_path importer initialization error: {0}", _unpackingError);
}
[Documentation(
@"find_module(fullname, path=None) -> self or None.
Search for a module specified by 'fullname'. 'fullname' must be the
fully qualified (dotted) module name. It returns the importer
instance itself if the module was found, or None if it wasn't.
The optional 'path' argument is ignored -- it's there for compatibility
with the importer protocol."
)]
public object find_module(CodeContext /*!*/ context, string fullname, params object[] args) {
var packedName = MakeFilename(fullname);
foreach (var entry in SearchOrder) {
var temp = packedName + entry.Key;
if (_unpackedLibrary.ContainsKey(temp))
return this;
}
return null;
}
[Documentation(
@"load_module(fullname) -> module.
Load the module specified by 'fullname'. 'fullname' must be the
fully qualified (dotted) module name. It returns the imported
module, or raises ResourceImportError if it wasn't found."
)]
public object load_module(CodeContext /*!*/ context, string fullname) {
var modules = context.LanguageContext.SystemStateModules;
if (modules.TryGetValue(fullname, out var module))
return module;
bool ispackage;
string modpath;
var code = GetModuleCode(context, fullname, out ispackage, out modpath);
if (code == null)
return null;
var pythonContext = context.LanguageContext;
ScriptCode script;
var mod = pythonContext.CompileModule(modpath, fullname,
new SourceUnit(pythonContext,
new MemoryStreamContentProvider(pythonContext, code, modpath),
modpath, SourceCodeKind.File),
ModuleOptions.None, out script);
var dict = mod.__dict__;
// we do these here because we don't want CompileModule to initialize the module until we've set
// up some additional stuff
dict.Add("__name__", fullname);
dict.Add("__loader__", this);
// ReSharper disable AssignNullToNotNullAttribute
dict.Add("__package__", null);
// ReSharper restore AssignNullToNotNullAttribute
dict.Add("__file__", "<resource>");
if (ispackage) {
//// add __path__ to the module *before* the code
//// gets executed
//var subname = GetSubName(fullname);
//var fullpath = string.Format("{0}{1}",
// Path.DirectorySeparatorChar,
// subname);
//var pkgpath = PythonOps.MakeList(fullpath);
var pkgpath = new PythonList();
dict.Add("__path__", pkgpath);
}
modules.Add(fullname, mod);
try {
script.Run(mod.Scope);
}
catch (Exception) {
modules.Remove(fullname);
throw;
}
return mod;
}
private byte[] GetModuleCode(CodeContext /*!*/ context, string fullname, out bool ispackage,
out string modpath) {
var path = MakeFilename(fullname);
ispackage = false;
modpath = string.Empty;
if (String.IsNullOrEmpty(path))
return null;
foreach (var entry in SearchOrder) {
var temp = path + entry.Key;
if (!_unpackedLibrary.ContainsKey(temp))
continue;
var tocEntry = _unpackedLibrary[temp];
ispackage = (entry.Value & ModuleCodeType.Package) == ModuleCodeType.Package;
// we currently don't support bytecode modules, so we don't check
// the time of the bytecode file vs. the time of the source file.
byte[] code = GetCodeFromData(context, false, tocEntry);
if (code == null) {
continue;
}
modpath = tocEntry.FullName;
return code;
}
throw MakeError("can't find module '{0}'", fullname);
}
private byte[] GetCodeFromData(CodeContext /*!*/ context, bool isbytecode, PackedResourceInfo tocEntry) {
byte[] data = GetData(tocEntry);
byte[] code = null;
if (data != null) {
if (isbytecode) {
// would put in code to unmarshal the bytecode here...
}
else {
code = data;
}
}
return code;
}
private byte[] GetData(PackedResourceInfo tocEntry) {
string unpackingError;
byte[] result;
if (!_loader.GetData(tocEntry, out result, out unpackingError))
throw MakeError(unpackingError);
return result;
}
private static Exception MakeError(params object[] args) {
return IronPython.Runtime.Operations.PythonOps.CreateThrowable(PythonExceptions.ImportError, args);
}
private static string MakeFilename(string name) {
return name.Replace('.', Path.DirectorySeparatorChar);
}
private struct PackedResourceInfo {
private int _fileSize;
public string FullName;
public int Compress;
public int DataSize;
public int FileOffset;
public static PackedResourceInfo Create(string fullName, int compress,
int dataSize, int fileSize, int fileOffset) {
PackedResourceInfo result;
result.FullName = fullName;
result.Compress = compress;
result.DataSize = dataSize;
result._fileSize = fileSize;
result.FileOffset = fileOffset;
return result;
}
#if DEBUG
public override string ToString() {
var sizeDesc = String.Format("{0} bytes", _fileSize);
if (Convert.ToDouble(_fileSize)/1024.0 > 1.0)
sizeDesc = String.Format("{0} KB", Math.Round(Convert.ToDouble(_fileSize)/1024.0, 1));
return String.Format("{0} ({1})", FullName, sizeDesc);
}
#endif
}
private class PackedResourceLoader {
private readonly Assembly _fromAssembly;
private readonly string _resourceNameBase;
private const int MaxPathLen = 256;
public PackedResourceLoader(Assembly fromAssembly, string resourceName) {
_fromAssembly = fromAssembly;
_resourceNameBase = resourceName;
}
public bool LoadZipDirectory(
out IDictionary<string, PackedResourceInfo> files,
out IDictionary<string, PackedResourceInfo[]> modules,
out string unpackingError) {
if (!ReadZipDirectory(out files, out unpackingError)) {
modules = null;
return false;
}
try {
var parsedSources =
from entry in files.Values
let isPyFile = entry.FullName.EndsWith(".py", StringComparison.OrdinalIgnoreCase)
where isPyFile
let name = entry.FullName.Substring(0, entry.FullName.Length - 3)
let dottedName = name.Replace('\\', '.').Replace('/', '.')
let lineage = dottedName.Split('.')
let fileName = lineage.Last()
let path = lineage.Take(lineage.Length - 1).ToArray()
orderby fileName
select new {
name = fileName,
path,
dottedPath = String.Join(".", path),
entry
};
var moduleContents =
from source in parsedSources
orderby source.dottedPath
group source by source.dottedPath
into moduleGroup
select new {
moduleGroup.Key,
Items = moduleGroup.Select(item => item.entry).ToArray()
};
modules = moduleContents.ToDictionary(
moduleGroup => moduleGroup.Key,
moduleGroup => moduleGroup.Items);
return true;
}
catch (Exception exception) {
files = null;
modules = null;
unpackingError = String.Format("{0}: {1}", exception.GetType().Name, exception.Message);
return false;
}
}
private Stream GetZipArchive() {
var fullResourceName = _fromAssembly.GetManifestResourceNames().Where(name => name.EndsWith(_resourceNameBase, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
return string.IsNullOrEmpty(fullResourceName) ? null : _fromAssembly.GetManifestResourceStream(fullResourceName);
}
private bool ReadZipDirectory(out IDictionary<string, PackedResourceInfo> result,
out string unpackingError) {
unpackingError = null;
result = null;
try {
var stream = GetZipArchive();
if (stream == null) {
unpackingError = "Resource not found.";
return false;
}
using (var reader = new BinaryReader(stream)) {
if (reader.BaseStream.Length < 2) {
unpackingError = "Can't read ZIP resource: Empty Resource.";
return false;
}
var endofCentralDir = new byte[22];
reader.BaseStream.Seek(-22, SeekOrigin.End);
var headerPosition = (int) reader.BaseStream.Position;
if (reader.Read(endofCentralDir, 0, 22) != 22) {
unpackingError = "Can't read ZIP resource: Invalid ZIP Directory.";
return false;
}
if (BitConverter.ToUInt32(endofCentralDir, 0) != 0x06054B50) {
// Bad: End of Central Dir signature
unpackingError = "Can't read ZIP resource: Not a ZIP file.";
return false;
}
var headerSize = BitConverter.ToInt32(endofCentralDir, 12);
var headerOffset = BitConverter.ToInt32(endofCentralDir, 16);
var arcOffset = headerPosition - headerOffset - headerSize;
headerOffset += arcOffset;
var directoryEntries = ReadZipDirectory(reader, headerOffset, arcOffset);
result = directoryEntries
.OrderBy(entry => entry.FullName)
.ToDictionary(entry => entry.FullName);
return true;
}
}
catch (Exception exception) {
unpackingError = String.Format("{0}: {1}", exception.GetType().Name, exception.Message);
return false;
}
}
private static IEnumerable<PackedResourceInfo> ReadZipDirectory(BinaryReader reader, int headerOffset,
int arcoffset) {
while (true) {
var name = string.Empty;
reader.BaseStream.Seek(headerOffset, SeekOrigin.Begin); // Start of file header
int l = reader.ReadInt32();
if (l != 0x02014B50) {
break; // Bad: Central Dir File Header
}
reader.BaseStream.Seek(headerOffset + 10, SeekOrigin.Begin);
var compress = reader.ReadInt16();
/*var time =*/
reader.ReadInt16();
/*var date =*/
reader.ReadInt16();
/*var crc =*/
reader.ReadInt32();
var dataSize = reader.ReadInt32();
var fileSize = reader.ReadInt32();
var nameSize = reader.ReadInt16();
var headerSize = 46 + nameSize +
reader.ReadInt16() +
reader.ReadInt16();
reader.BaseStream.Seek(headerOffset + 42, SeekOrigin.Begin);
var fileOffset = reader.ReadInt32() + arcoffset;
if (nameSize > MaxPathLen)
nameSize = MaxPathLen;
for (int i = 0; i < nameSize; i++) {
char c = reader.ReadChar();
if (c == '/')
c = Path.DirectorySeparatorChar;
name += c;
}
headerOffset += headerSize;
yield return
PackedResourceInfo.Create(name, compress, dataSize, fileSize, fileOffset);
}
}
public bool GetData(PackedResourceInfo tocEntry, out byte[] result, out string unpackingError) {
unpackingError = null;
result = null;
var fileOffset = tocEntry.FileOffset;
var dataSize = tocEntry.DataSize;
var compress = tocEntry.Compress;
try {
var stream = GetZipArchive();
if (stream == null) {
unpackingError = "Resource not found.";
return false;
}
using (var reader = new BinaryReader(stream)) {
// Check to make sure the local file header is correct
reader.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
var l = reader.ReadInt32();
if (l != 0x04034B50) {
// Bad: Local File Header
unpackingError = "Bad local file header in ZIP resource.";
return false;
}
reader.BaseStream.Seek(fileOffset + 26, SeekOrigin.Begin);
l = 30 + reader.ReadInt16() + reader.ReadInt16(); // local header size
fileOffset += l; // start of file data
reader.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
byte[] rawData;
try {
rawData = reader.ReadBytes(compress == 0 ? dataSize : dataSize + 1);
}
catch {
unpackingError = "Can't read data";
return false;
}
if (compress != 0) {
rawData[dataSize] = (byte) 'Z';
}
result = compress == 0 ? rawData : ZlibModule.Decompress(rawData, -15);
return true;
}
}
catch (Exception exception) {
unpackingError = String.Format("{0}: {1}", exception.GetType().Name, exception.Message);
return false;
}
}
}
}
}