forked from holatuwol/liferay-faster-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsourcetrie.py
171 lines (120 loc) · 4.5 KB
/
sourcetrie.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
from __future__ import print_function
from git import git_root
import os.path
try:
import cPickle as pickle
except ImportError:
import pickle
import sys
class SourceTrie:
def __init__(self, parent_node=None, node_id=None):
self.parent_node = parent_node
self.node_id = node_id
self.children = {}
self.value = None
def add(self, path, group, name, version):
result = self
for path_element in path.split('/'):
if path_element not in result.children:
result.children[path_element] = SourceTrie(result, path_element)
result = result.children[path_element]
result.value = (group, name, version)
def add_ant(self, path):
# portal-web is an exception
if path == 'portal-web':
self.add(path, None, None, None)
return
# Load information from bnd.bnd
artifact_group, artifact_name, artifact_version = self.extract_version(
path, 'modules/.releng/%s.properties' % path)
self.add(path, artifact_group, artifact_name, artifact_version)
def add_gradle(self, path):
artifact_group, artifact_name, artifact_version = self.extract_version(
path, 'modules/.releng/%s/artifact.properties' % path[8:])
self.add(path, artifact_group, artifact_name, artifact_version)
def extract_version(self, module_path, releng_path):
bnd_bnd = '%s/bnd.bnd' % module_path
if not os.path.exists(bnd_bnd):
return None, None, None
with open(bnd_bnd, 'r') as f:
lines = [line for line in f.readlines()]
name_lines = [line for line in lines if line.startswith('Bundle-SymbolicName:')]
version_lines = [line for line in lines if line.startswith('Bundle-Version:')]
if os.path.exists(releng_path):
with open(releng_path, 'r') as f:
lines = [line for line in f.readlines()]
artifact_url_lines = [line for line in lines if line.startswith('artifact.url')]
else:
artifact_url_lines = []
if len(name_lines) != 1 or len(version_lines) != 1:
return None, None, None
artifact_name = name_lines[0][20:].strip()
if len(artifact_url_lines) > 0:
artifact_url = artifact_url_lines[0][13:]
if artifact_name == '${manifest.bundle.symbolic.name}':
version_pos = artifact_url.rfind('/', 0, artifact_url.rfind('/') - 1) + 1
name_pos = artifact_url.rfind('/', 0, version_pos - 1) + 1
artifact_name = artifact_url[name_pos:version_pos - 1]
name_pos = artifact_url.find('/' + artifact_name + '/')
group_pos = artifact_url.rfind('/com/', 0, name_pos) + 1
artifact_group = artifact_url[group_pos:name_pos].replace('/', '.')
version_pos = name_pos + len(artifact_name) + 2
artifact_version = artifact_url[version_pos:artifact_url.find('/', version_pos)]
else:
artifact_group = 'com.liferay'
artifact_version = version_lines[0][15:].strip() + '-SNAPSHOT'
return artifact_group, artifact_name, artifact_version
def find_leaf(self, path):
result = self
for path_element in path.split('/'):
if path_element not in result.children:
break
result = result.children[path_element]
while result is not None and result.value is None:
result = result.parent_node
return result
def get_path(self):
if self.parent_node is None:
return None
parent_path = self.parent_node.get_path()
if parent_path is None:
return self.node_id
return '%s/%s' % (parent_path, self.node_id)
def load(parent_folder):
# Use the pickled source trie from a previous run (if present)
pickle_file = '%s/sourcetrie.pickle' % parent_folder
if os.path.exists(pickle_file):
with open(pickle_file, 'rb') as f:
try:
return pickle.load(f)
except:
print('Corrupt sourcetrie.pickle, regenerating', file=sys.stderr)
# Generate the pickled data structure if the source file is present
source_file = '%s/sourcetrie.txt' % parent_folder
if os.path.exists(source_file):
root = SourceTrie()
with open(source_file, 'r') as f:
for file_name in [line.strip() for line in f.readlines()]:
pos = file_name.rfind('/')
if pos == -1:
continue
module_path = file_name[0:pos]
if module_path.find('modules') == 0:
root.add_gradle(module_path)
else:
root.add_ant(module_path)
with open(pickle_file, 'wb') as f:
pickle.dump(root, f)
return root
# Otherwise, we have no idea what to do
print('Unable to find %s' % source_file, file=sys.stderr)
sys.exit(1)
load=staticmethod(load)
def get_rd_file(name=None):
if name is None:
return os.path.join(git_root, '.redeploy')
else:
return os.path.join(git_root, '.redeploy', name)
if __name__ == '__main__':
SourceTrie.load(get_rd_file())