-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.py
149 lines (140 loc) · 4.73 KB
/
files.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import repository
import model
import json
import csv
import os
import itertools
# Given two lists of strings a, b
# determine if a is a prefix of b,
# treating each string a consecutive (atomic) token
def isprefix(a, b):
return all(itertools.imap(lambda x,y: x==y, a, b)) and len(a) < len(b)
# Given a list of strings s and a list of lists (of strings)
# determine the longest string on the list being the strict prefix of the given one
def findlongest(s, strings, empty = [os.curdir]):
longest = []
for i in range(len(strings)):
l = strings[i]
if isprefix(l, s) and (len(l) > len(longest)):
longest = l
if longest == []:
longest = empty
return longest
# Convert a list of strings into a normalized os path
def topath(strings):
result = os.curdir + os.sep
for i in range(len(strings)):
p = strings[i]
result = result + os.sep + p
result = os.path.normpath(result)
return result
def emptypath(s):
if s == u'':
return os.curdir
else:
return os.path.normpath(s)
def recurse_setdefault(res, array):
if len(array) == 0:
return
elif len(array) == 1:
res.append(array[0])
else:
recurse_setdefault(res.setdefault(array[0], [] if len(array) == 2 else {}), array[1:])
def another_approach():
files = ["foo/bar/Bla", "foo/bar/Foo", "foo/foo/Bsdf", "xsd/sdafd/saasf"]
dict_add = lambda x, y={}: dict_add(x[:-1], y).setdefault(x[-1], {}) if(x) else y
base_dict = {}
map(lambda x: dict_add(x, base_dict), [path.split("/") for path in files])
def retrieve(repository):
fs = repository.retrieve_files()
files = [
f
for (f,s) in fs
]
files.sort()
paths = [
(f, os.path.basename(f), emptypath(os.path.dirname(f)))
for f in files
]
modules = list({
dirname
for (filename, basename, dirname) in paths
})
modules.sort()
splits = [
m.split(os.sep)
for m in modules
]
parents = []
while splits:
z = splits.pop()
s = findlongest(z, splits)
parents.append((topath(z), topath(s)))
children = [
c
for (c,p) in parents
]
idx = files+children
#
graph = model.Graph([],[])
#
for f in files:
graph.nodes.append(model.Node(".\\"+f, 'File', idx.index(f), repository.address_files(f), 2))
for m in children:
if m == ".":
graph.nodes.append(model.Node(m, 'Directory', idx.index(m), repository.address_files(m), 1))
else:
graph.nodes.append(model.Node(".\\"+m, 'Directory', idx.index(m), repository.address_files(m), 1))
for (f, b, m) in paths:
graph.links.append(model.Link( idx.index(f), idx.index(m), 1, 2))
for (c, p) in parents:
graph.links.append(model.Link( idx.index(c), idx.index(p), 2, 1))
return graph
def con(bd, path, ind):
res = []
for key, value in bd.items():
if value == {}:
#print({"name" : key, "size": ind.get(path + key, 0)})
res.append({"name" : key, "size": ind.get(path + key, 0)})
else:
res.append({"name" : key, "children" : con(value, path + key + '\\', ind)})
return res
def output(repository, file):
mag = model.Magnify()
#
mag.project = model.Project(repository.origin, repository.commit, repository.owner, repository.name)
#
mag.graph = retrieve(repository)
#
filesize = repository.retrieve_files()
files = []
for (path, size) in filesize:
files.append(path.split("\\"))
findex = {}
for (path, size) in filesize:
findex.setdefault(path, size)
#
# dict_add = lambda x, y={}: dict_add(x[:-1], y).setdefault(x[-1], {}) if(x) else y
# base_dict = {}
# map(lambda x: dict_add(x, base_dict), [path.split("\\") for (path, size) in files])
#
dict_add = lambda x, y={}: dict_add(x[:-1], y).setdefault(x[-1], {}) if(x) else y
base_dict = {}
map(lambda x: dict_add(x, base_dict), files)
#
# flare_dict = {"name" : repository.name, "children" : con(base_dict, "", findex)}
flare_dict = {"name" : ".", "children" : con(base_dict, "", findex)}
#
#flare_add = lambda x, y: for key, value in x.items() flare_add(x[:-1], y).append("name" : x[-1], "children": {}) if(x) else y
#
mag.tree = flare_dict
#
# write json
with open(file+'.json', 'wb+') as out_json:
json.dump(mag, out_json, default=model.default, indent=2)
#
# write csv
out_csv = csv.writer(open(file+'.csv', 'wb+'), delimiter=';')
out_csv.writerow(['name', 'group', 'id', 'url'])
for i in range(len(mag.graph.nodes)):
out_csv.writerow([mag.graph.nodes[i].name, mag.graph.nodes[i].group, mag.graph.nodes[i].id, mag.graph.nodes[i].url])