-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort.py
135 lines (114 loc) · 4.16 KB
/
sort.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
134
135
# ______________________________________________________________________________
#
# Sort content (apps, games, themes) into another folder based on metadata
# in the JSON index file
# ______________________________________________________________________________
#
import os, sys, re
import json, shutil
list = {}
rootdir = ''
outdir = ''
def sort(infile):
with open(infile, 'r') as in_f:
index = json.loads(in_f.read())
with open("resolutions.json", 'r') as res_f:
resolutions = json.loads(res_f.read())
for (hash, item) in index.items():
# /Applications/
if 'MIDlet-Vendor' in item and item['MIDlet-Vendor'] == 'Nokia':
if 'Nokia-MIDlet-Category' in item:
type = re.sub(r"Nokia.*?$", "", item['Nokia-MIDlet-Category']) + 's'
else:
type = 'Applications'
elif 'MIDlet-1' not in item:
if item['type'] == 'theme':
type = 'Themes'
elif item['type'] == 'swf':
type = 'Flash Lite'
else:
type = 'Unknown'
else:
type = 'Third-party'
# Some third-party apps have Nokia as the vendor but the info URL field is one way to spot them
if 'MIDlet-Info-URL' in item:
type = 'Third-party'
if type not in list: list[type] = {'type': 'folder'}
# /Applications/Snake EX2
if 'MIDlet-Name' in item:
title = item['MIDlet-Name']
else:
# For non-java content, simply remove the file extension of the file path
title = re.sub(r"\.\w+$", "", item['paths'][0].split('/')[-1])
if title not in list[type]: list[type][title] = {'type': 'folder'}
# /Applications/Snake EX2/128x160_6070/
modeltype = re.search(r"content/([a-zA-Z]+[_-]?[\da-zA-Z]*?)(_|dp)", item['paths'][0]).group(1).replace('-', '').replace('_', '').lower()
if modeltype in resolutions:
model = resolutions[modeltype]
else:
raise KeyError(f"'{modeltype}' is not in resolutions.json")
if model not in list[type][title]: list[type][title][model] = {'type': 'folder'}
# Use a version subfolder for jars, no version for themes or SWFs
if 'MIDlet-Version' in item:
# /Applications/Snake EX2/128x160_6070/1.1/
version = item['MIDlet-Version']
if version not in list[type][title][model]:
list[type][title][model][version] = {'type': 'folder'}
# /Applications/Snake EX2/128x160_6070/1.1/snake_en_fr_de_hu-HU_pl-PL_ro-RO.jar
name = item['paths'][0].split('/')[-1]
list[type][title][model][version][name] = {'type': 'file', 'path': item['paths'][0]}
else:
name = item['paths'][0].split('/')[-1]
# list[type][title][model]['rename_dupes'] = True
if name not in list[type][title][model]:
list[type][title][model][name] = {'type': 'filelist', 'paths': []}
list[type][title][model][name]['paths'].append(item['paths'][0])
def traverse(name, node):
if 'path' in node: node['path'] = node['path'].replace('\\x', '')
name = name.replace('\\x', '')
if node['type'] == 'folder':
# if name in list[type][title][model]:
# name = item['paths'][0].split('/')[2].split('.image_')[1] + '_' + name
newdir = os.path.join(os.curdir, re.sub(r'[\\/:\*\?"<>\|\u0000]', '', name))
try:
os.stat(newdir)
except FileNotFoundError:
os.mkdir(newdir)
os.chdir(newdir)
for (child_name, child) in node.items():
if child_name in ['type', 'rename_dupes']: continue
traverse(child_name, child)
os.chdir(os.path.pardir)
elif node['type'] == 'file':
shutil.copy(os.path.join(rootdir, node['path']), name)
if node['path'].endswith('.jar'):
try:
shutil.copy(os.path.join(rootdir, node['path']).replace('.jar', '.jad'), name.replace('.jar', '.jad'))
except:
pass
elif node['type'] == 'filelist':
for path in node['paths']:
if len(node['paths']) > 1:
destname = path.split('/')[2].split('.image_')[1] + '_' + name
else:
destname = name
try:
shutil.copy(os.path.join(rootdir, path), destname)
except:
pass
else:
raise TypeError(f"Invalid node type '{node['type']}'")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: sort.py <index>")
sys.exit()
sort(sys.argv[1])
rootdir = os.path.abspath(os.curdir)
outdir = os.path.join(os.path.abspath(os.curdir), 'sorted')
try:
os.stat(outdir)
except FileNotFoundError:
os.mkdir(outdir)
os.chdir(outdir)
for (name, node) in list.items():
traverse(name, node)