Skip to content

Commit ca735e0

Browse files
Create PackageManifest classes for windows manifests
Creates PackageManifest classes for various windows manifests and overrides the methods for detection and PackageManifest creation. Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent 1277580 commit ca735e0

21 files changed

Lines changed: 901 additions & 857 deletions

src/packagedcode/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@
9999
build.BuckPackage,
100100
build.AutotoolsPackage,
101101
conda.Condayml,
102-
win_pe.WindowsExecutable,
102+
win_pe.WindowsExecutableManifest,
103103
readme.ReadmeManifest,
104104
build.MetadataBzl,
105105
msi.MsiInstallerPackage,
106-
windows.MicrosoftUpdateManifestPackage,
106+
windows.MicrosoftUpdateManifest,
107107
pubspec.PubspecYAML,
108108
pubspec.PubspecLock
109109
]

src/packagedcode/win_pe.py

Lines changed: 95 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -222,26 +222,8 @@ def pe_info(location):
222222

223223

224224
@attr.s()
225-
class WindowsExecutable(models.Package, models.PackageManifest):
226-
file_patterns = ()
227-
extensions = (
228-
'.exe',
229-
'.dll',
230-
'.mui',
231-
'.mun',
232-
'.com',
233-
'.winmd',
234-
'.sys',
235-
'.tlb',
236-
'.exe_*',
237-
'.dll_*',
238-
'.mui_*',
239-
'.mun_*',
240-
'.com_*',
241-
'.winmd_*',
242-
'.sys_*',
243-
'.tlb_*',
244-
)
225+
class WindowsExecutable(models.Package):
226+
245227
filetypes = ('pe32', 'for ms windows',)
246228
mimetypes = ('application/x-dosexec',)
247229

@@ -251,10 +233,6 @@ class WindowsExecutable(models.Package, models.PackageManifest):
251233
default_download_baseurl = None
252234
default_api_baseurl = None
253235

254-
@classmethod
255-
def recognize(cls, location):
256-
yield parse(location)
257-
258236

259237
def get_first(mapping, *keys):
260238
"""
@@ -279,75 +257,100 @@ def concat(mapping, *keys):
279257
return '\n'.join(values)
280258

281259

282-
def parse(location):
283-
"""
284-
Return a WindowsExecutable package from the file at `location` or None.
285-
"""
286-
if not filetype.is_file(location):
287-
return
288-
289-
T = contenttype.get_type(location)
290-
if not T.is_winexe:
291-
return
292-
293-
infos = pe_info(location)
294-
295-
version = get_first(
296-
infos,
297-
'Full Version',
298-
'ProductVersion',
299-
'FileVersion',
300-
'Assembly Version',
301-
)
302-
release_date = get_first(infos, 'BuildDate')
303-
if release_date:
304-
if len(release_date) >= 10:
305-
release_date = release_date[:10]
306-
release_date = release_date.replace('/', '-')
307-
308-
name = get_first(
309-
infos,
310-
'ProductName',
311-
'OriginalFilename',
312-
'InternalName',
260+
@attr.s()
261+
class WindowsExecutableManifest(WindowsExecutable, models.PackageManifest):
262+
extensions = (
263+
'.exe',
264+
'.dll',
265+
'.mui',
266+
'.mun',
267+
'.com',
268+
'.winmd',
269+
'.sys',
270+
'.tlb',
271+
'.exe_*',
272+
'.dll_*',
273+
'.mui_*',
274+
'.mun_*',
275+
'.com_*',
276+
'.winmd_*',
277+
'.sys_*',
278+
'.tlb_*',
313279
)
314-
copyr = get_first(infos, 'LegalCopyright')
315-
316-
LegalCopyright = copyr,
317-
318-
LegalTrademarks = concat(
319-
infos,
320-
'LegalTrademarks',
321-
'LegalTrademarks1',
322-
'LegalTrademarks2',
323-
'LegalTrademarks3')
280+
manifest_type = 'winexe'
324281

325-
License = get_first(infos, 'License')
282+
@classmethod
283+
def is_manifest(cls, location):
284+
"""
285+
Return True if the file at ``location`` is likely a manifest of this type.
286+
"""
287+
T = contenttype.get_type(location)
288+
return (filetype.is_file(location) and T.is_winexe)
326289

327-
declared_license = {}
328-
if LegalCopyright or LegalTrademarks or License:
329-
declared_license = dict(
330-
LegalCopyright=copyr,
331-
LegalTrademarks=LegalTrademarks,
332-
License=License
290+
@classmethod
291+
def recognize(cls, location):
292+
"""
293+
Yield one or more Package manifest objects given a file ``location`` pointing to a
294+
package archive, manifest or similar.
295+
"""
296+
infos = pe_info(location)
297+
298+
version = get_first(
299+
infos,
300+
'Full Version',
301+
'ProductVersion',
302+
'FileVersion',
303+
'Assembly Version',
304+
)
305+
release_date = get_first(infos, 'BuildDate')
306+
if release_date:
307+
if len(release_date) >= 10:
308+
release_date = release_date[:10]
309+
release_date = release_date.replace('/', '-')
310+
311+
name = get_first(
312+
infos,
313+
'ProductName',
314+
'OriginalFilename',
315+
'InternalName',
316+
)
317+
copyr = get_first(infos, 'LegalCopyright')
318+
319+
LegalCopyright = copyr,
320+
321+
LegalTrademarks = concat(
322+
infos,
323+
'LegalTrademarks',
324+
'LegalTrademarks1',
325+
'LegalTrademarks2',
326+
'LegalTrademarks3')
327+
328+
License = get_first(infos, 'License')
329+
330+
declared_license = {}
331+
if LegalCopyright or LegalTrademarks or License:
332+
declared_license = dict(
333+
LegalCopyright=copyr,
334+
LegalTrademarks=LegalTrademarks,
335+
License=License
336+
)
337+
338+
description = concat(infos, 'FileDescription', 'Comments')
339+
340+
parties = []
341+
cname = get_first(infos, 'CompanyName', 'Company')
342+
343+
if cname:
344+
parties = [Party(type=party_org, role='author', name=cname)]
345+
homepage_url = get_first(infos, 'URL', 'WWW')
346+
347+
yield cls(
348+
name=name,
349+
version=version,
350+
release_date=release_date,
351+
copyright=copyr,
352+
declared_license=declared_license,
353+
description=description,
354+
parties=parties,
355+
homepage_url=homepage_url,
333356
)
334-
335-
description = concat(infos, 'FileDescription', 'Comments')
336-
337-
parties = []
338-
cname = get_first(infos, 'CompanyName', 'Company')
339-
340-
if cname:
341-
parties = [Party(type=party_org, role='author', name=cname)]
342-
homepage_url = get_first(infos, 'URL', 'WWW')
343-
344-
return WindowsExecutable(
345-
name=name,
346-
version=version,
347-
release_date=release_date,
348-
copyright=copyr,
349-
declared_license=declared_license,
350-
description=description,
351-
parties=parties,
352-
homepage_url=homepage_url,
353-
)

src/packagedcode/windows.py

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import xmltodict
1212

1313
from packagedcode import models
14+
from commoncode import filetype
1415

1516

1617
# Tracing flags
@@ -34,66 +35,65 @@ def logger_debug(*args):
3435

3536

3637
@attr.s()
37-
class MicrosoftUpdateManifestPackage(models.Package, models.PackageManifest):
38+
class MicrosoftUpdatePackage(models.Package, models.PackageManifest):
3839
extensions = ('.mum',)
3940
filetypes = ('xml 1.0 document',)
4041
mimetypes = ('text/xml',)
4142

4243
default_type = 'windows-update'
4344

45+
46+
@attr.s()
47+
class MicrosoftUpdateManifest(MicrosoftUpdatePackage, models.PackageManifest):
48+
49+
manifest_type = 'winupdatemanifest'
50+
51+
@classmethod
52+
def is_manifest(cls, location):
53+
"""
54+
Return True if the file at ``location`` is likely a manifest of this type.
55+
"""
56+
return filetype.is_file(location) and location.endswith('.mum')
57+
4458
@classmethod
4559
def recognize(cls, location):
46-
yield parse(location)
47-
48-
49-
def parse_mum(location):
50-
"""
51-
Return a dictionary of Microsoft Update Manifest (mum) metadata from a .mum
52-
file at `location`. Return None if this is not a parsable mum file. Raise
53-
Exceptions on errors.
54-
"""
55-
if not location.endswith('.mum'):
56-
return
57-
with open(location , 'rb') as loc:
58-
return xmltodict.parse(loc)
59-
60-
61-
def parse(location):
62-
"""
63-
Return a MicrosoftUpdateManifestPackage from a .mum XML file at `location`.
64-
Return None if this is not a parsable .mum file.
65-
"""
66-
parsed = parse_mum(location)
67-
if TRACE:
68-
logger_debug('parsed:', parsed)
69-
if not parsed:
70-
return
71-
72-
assembly = parsed.get('assembly', {})
73-
description = assembly.get('@description', '')
74-
company = assembly.get('@company', '')
75-
copyright = assembly.get('@copyright', '')
76-
support_url = assembly.get('@supportInformation', '')
77-
78-
assembly_identity = assembly.get('assemblyIdentity', {})
79-
name = assembly_identity.get('@name', '')
80-
version = assembly_identity.get('@version', '')
81-
82-
parties = []
83-
if company:
84-
parties.append(
85-
models.Party(
86-
name=company,
87-
type=models.party_org,
88-
role='owner',
60+
"""
61+
Yield one or more Package manifest objects given a file ``location`` pointing to a
62+
package archive, manifest or similar.
63+
"""
64+
with open(location , 'rb') as loc:
65+
parsed = xmltodict.parse(loc)
66+
67+
if TRACE:
68+
logger_debug('parsed:', parsed)
69+
if not parsed:
70+
return
71+
72+
assembly = parsed.get('assembly', {})
73+
description = assembly.get('@description', '')
74+
company = assembly.get('@company', '')
75+
copyright = assembly.get('@copyright', '')
76+
support_url = assembly.get('@supportInformation', '')
77+
78+
assembly_identity = assembly.get('assemblyIdentity', {})
79+
name = assembly_identity.get('@name', '')
80+
version = assembly_identity.get('@version', '')
81+
82+
parties = []
83+
if company:
84+
parties.append(
85+
models.Party(
86+
name=company,
87+
type=models.party_org,
88+
role='owner',
89+
)
8990
)
90-
)
9191

92-
return MicrosoftUpdateManifestPackage(
93-
name=name,
94-
version=version,
95-
description=description,
96-
homepage_url=support_url,
97-
parties=parties,
98-
copyright=copyright,
99-
)
92+
yield cls(
93+
name=name,
94+
version=version,
95+
description=description,
96+
homepage_url=support_url,
97+
parties=parties,
98+
copyright=copyright,
99+
)

tests/packagedcode/data/plugin/help.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,13 @@ Package: war
382382

383383
--------------------------------------------
384384
Package: windows-update
385-
class: packagedcode.windows:MicrosoftUpdateManifestPackage
385+
class: packagedcode.windows:MicrosoftUpdateManifest
386386
extensions: .mum
387387
filetypes: xml 1.0 document
388388

389389
--------------------------------------------
390390
Package: winexe
391-
class: packagedcode.win_pe:WindowsExecutable
391+
class: packagedcode.win_pe:WindowsExecutableManifest
392392
extensions: .exe, .dll, .mui, .mun, .com, .winmd, .sys, .tlb, .exe_*, .dll_*, .mui_*, .mun_*, .com_*, .winmd_*, .sys_*, .tlb_*
393393
filetypes: pe32, for ms windows
394394

0 commit comments

Comments
 (0)