-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmacmodelshelf.py
executable file
·133 lines (108 loc) · 3.64 KB
/
macmodelshelf.py
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
#!/usr/bin/env python
import sys
import os
import argparse
import shelve
import urllib2
from xml.etree import ElementTree
import re
DBPATH = "macmodelshelf"
def print8(*args):
print " ".join(unicode(x).encode(u"utf-8") for x in args)
def printerr8(*args):
print >>sys.stderr, " ".join(unicode(x).encode(u"utf-8") for x in args)
try:
macmodelshelf = shelve.open(DBPATH)
except BaseException, e:
printerr8(u"Couldn't open macmodelshelf.db: %s" % unicode(e))
sys.exit(1)
def model_code(serial):
if "serial" in serial.lower(): # Workaround for machines with dummy serial numbers.
return None
if len(serial) in (12, 13) and serial.startswith("S"): # Remove S prefix from scanned codes.
serial = serial[1:]
if len(serial) in (11, 12):
return serial[8:].decode("ascii")
return None
def lookup_mac_model_code_from_apple(model_code):
try:
earl = "http://support-sp.apple.com/sp/product?cc=%s&lang=en_US" % model_code
call = urllib2.Request(earl, headers={'Accept' : '*/*'})
f = urllib2.urlopen(call)
et = ElementTree.parse(f)
return et.findtext("configCode").decode("utf-8")
except:
return None
CLEANUP_RES = [
(re.compile(ur"inch ? "), u"inch, "),
(re.compile(ur" "), u" "),
]
def cleanup_model(model):
for pattern, replacement in CLEANUP_RES:
model = pattern.sub(replacement, model)
return model
def model(code, cleanup=True):
global macmodelshelf
if code == None:
return None
code = code.upper()
try:
model = macmodelshelf[code]
except KeyError:
printerr8(u"Looking up %s from Apple" % code)
model = lookup_mac_model_code_from_apple(code)
if model:
macmodelshelf[code] = model
if cleanup and model:
return cleanup_model(model)
else:
return model
def _dump(cleanup=True, format=u"json"):
assert format in (u"python", u"json", u"markdown")
def clean(model):
if cleanup:
return cleanup_model(model)
else:
return model
items = macmodelshelf.keys()
items.sort()
items.sort(key=len)
if format == u"python":
print8(u"macmodelshelfdump = {")
print8(u",\n".join([u' "%s": "%s"' % (code, clean(macmodelshelf[code])) for code in items]))
print8(u"}")
elif format == u"json":
print8(u"{")
print8(u",\n".join([u' "%s": "%s"' % (code, clean(macmodelshelf[code])) for code in items]))
print8(u"}")
elif format == u"markdown":
print8(u"Code | Model")
print8(u":--- | :---")
print8(u"\n".join(u'`%s` | %s' % (code, clean(macmodelshelf[code])) for code in items))
def main(argv):
p = argparse.ArgumentParser()
p.add_argument(u"-n", u"--no-cleanup", action=u"store_false",
dest=u"cleanup", help=u"Don't clean up model strings.")
p.add_argument(u"code", help=u"Serial number or model code")
args = p.parse_args([x.decode(u"utf-8") for x in argv[1:]])
dump_format = {
u"dump": u"python",
u"dump-python": u"python",
u"dump-json": u"json",
u"dump-markdown": u"markdown",
}
if args.code in dump_format.keys():
_dump(args.cleanup, dump_format[args.code])
return 0
if len(args.code) in (11, 12, 13):
m = model(model_code(args.code), cleanup=args.cleanup)
else:
m = model(args.code, cleanup=args.cleanup)
if m:
print m
return 0
else:
printerr8(u"Unknown model %s" % repr(args.code))
return os.EX_UNAVAILABLE
if __name__ == '__main__':
sys.exit(main(sys.argv))