-
Notifications
You must be signed in to change notification settings - Fork 2
/
inject-default-gui.py
executable file
·252 lines (223 loc) · 9.66 KB
/
inject-default-gui.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
# coding=utf-8
"""Author: Lukasz Opiola
Copyright (C) 2016 ACK CYFRONET AGH
This software is released under the MIT license cited in 'LICENSE.txt'
This script is used to automate maintenance of dependency between
oz-gui-default and oz-gui-homepage, which works as follows.
oz-gui-default is included in this project as a subtree. In src dir, there are
copies of all the files of oz-gui-default. This ensures that the same codebase
is used. All such files are read-only and are automatically added to .gitignore.
Programmer may decide to override a file, and when he places a non-readonly
file in src dir, it will NOT be automatically replaced by a copy from
oz-gui-default. He may also create new files that do not exist in oz-gui-default
and they will stay untouched.
"""
import os
import filecmp
import shutil
from stat import *
script_dir = os.path.dirname(os.path.realpath(__file__))
def_gui_src_path = os.path.join(script_dir, 'oz-gui-default', 'src')
src_path = os.path.join(script_dir, 'src')
results = {
'checked_files': {},
'count_files_ok': 0,
'count_files_created': 0,
'count_files_updated': 0,
'count_links_ok': 0,
'count_links_created': 0,
'count_links_broken': 0,
'links_broken': [],
'count_overrides': 0,
'overrides': [],
'count_homepage_files': 0,
'homepage_files': [],
'to_ignore': []
}
# Creating symlinks with required dir structure and overwrite
def symlink(src, dest):
dest_dir = os.path.dirname(dest)
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
if os.path.islink(dest):
os.remove(dest)
os.symlink(src, dest)
# Copying files with required dir structure and overwrite
def copy(src, dest, perms):
dest_dir = os.path.dirname(dest)
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
if os.path.isfile(dest):
os.remove(dest)
shutil.copyfile(src, dest)
os.chmod(dest, perms)
def resolve_symlink(_original_path, _path_to_check, _rel_path, _results):
# Check if same path exists in src dir
if os.path.islink(_path_to_check):
orig_link_target = os.readlink(_original_path)
curr_link_target = os.readlink(_path_to_check)
if orig_link_target == curr_link_target:
_results['count_links_ok'] += 1
_results['to_ignore'].append(_rel_path)
else:
abs_curr_link_target = os.path.abspath(
os.path.join(os.path.dirname(_path_to_check),
curr_link_target))
# Check if this link's target is the file in oz-gui-default
if os.path.exists(abs_curr_link_target):
_results['count_homepage_files'] += 1
_results['homepage_files'].append(_rel_path)
else:
_results['count_links_broken'] += 1
_results['links_broken'].append(_rel_path)
elif os.path.isdir(_path_to_check):
_results['count_homepage_files'] += 1
_results['homepage_files'].append(_rel_path)
elif os.path.isfile(_path_to_check):
_results['count_homepage_files'] += 1
_results['homepage_files'].append(_rel_path)
else:
link_target = os.readlink(_original_path)
symlink(link_target, _path_to_check)
_results['to_ignore'].append(_rel_path)
_results['count_links_created'] += 1
# Excluded directories
exclude = set(['node_modules', 'bower_components', 'tmp', 'dist'])
# Walk the oz-gui-default dir and resolve links
for root, dirs, files in os.walk(def_gui_src_path, topdown=True):
# Skip excluded dirs
dirs[:] = [d for d in dirs if d not in exclude]
# Look through dirs for symlinks to dirs
for dir in dirs:
abs_dir_path = os.path.join(root, dir)
rel_dir_path = os.path.relpath(abs_dir_path, def_gui_src_path)
path_to_check = os.path.join(src_path, rel_dir_path)
results['checked_files'][path_to_check] = True
# Check only symlinks to directories
if os.path.islink(abs_dir_path):
resolve_symlink(abs_dir_path, path_to_check, rel_dir_path, results)
for file in files:
# Absolute path to file in oz-gui-default
abs_file_path = os.path.join(root, file)
# Relative path to file in oz-gui-default
# (relative to oz-gui-default/src)
rel_file_path = os.path.relpath(abs_file_path, def_gui_src_path)
# Corresponding path in src dir
path_to_check = os.path.join(src_path, rel_file_path)
results['checked_files'][path_to_check] = True
# Check the type of original file (file or symlink)
if os.path.islink(abs_file_path):
# This file is a link
resolve_symlink(
abs_file_path, path_to_check, rel_file_path, results)
else:
# This file is a file
# Check if such file exists in src dir and what type is it
if os.path.exists(path_to_check):
if os.path.isfile(path_to_check):
# File exists. Check if it is read only.
if oct(os.stat(path_to_check)[ST_MODE])[-3:] == '444':
# Read only - it was copied here by the script
# Should be later added to git ignore
results['to_ignore'].append(rel_file_path)
# Check if it should be updated
if filecmp.cmp(abs_file_path, path_to_check):
# No need to update
results['count_files_ok'] += 1
else:
# Update to newer version
copy(abs_file_path, path_to_check, 0444)
results['count_files_updated'] += 1
else:
# Not read only - an overriden file
results['count_overrides'] += 1
results['overrides'].append(rel_file_path)
else:
# Some other type
results['count_overrides'] += 1
results['overrides'].append(rel_file_path)
else:
# File does not exist, copy it
copy(abs_file_path, path_to_check, 0444)
# Should be later added to git ignore
results['count_files_created'] += 1
results['to_ignore'].append(rel_file_path)
# Walk the src dir and look for paths that were not checked
for root, dirs, files in os.walk(src_path, topdown=True):
# Skip excluded dirs
dirs[:] = [d for d in dirs if d not in exclude]
for file in files:
# Absolute path to file in src
abs_file_path = os.path.join(root, file)
# Relative path to file in src
rel_file_path = os.path.relpath(abs_file_path, src_path)
if not abs_file_path in results['checked_files']:
results['count_homepage_files'] += 1
results['homepage_files'].append(rel_file_path)
if os.path.islink(abs_file_path):
abs_curr_link_target = os.path.abspath(
os.path.join(os.path.dirname(abs_file_path),
os.readlink(abs_file_path)))
if not os.path.exists(abs_curr_link_target):
results['count_links_broken'] += 1
results['links_broken'].append(rel_file_path)
# Modify gitignore - add all files that were copied from default gui
# Sort to ignore list - diffs of gitignore are clearer
results['to_ignore'].sort()
gitignore_path = os.path.join(script_dir, 'src', '.gitignore')
with open(gitignore_path) as gitignore_read:
content = gitignore_read.readlines()
new_content = []
# Read the beginning of gitignore
for line in content:
new_content.append(line)
if line == '# inject-default-gui.py\n':
break
# Add a blank line
new_content.append('\n')
# Add files to ignore
for file_to_ignore in results['to_ignore']:
new_content.append('{0}\n'.format(file_to_ignore))
with open(gitignore_path, 'w') as gitignore_write:
for line in new_content:
gitignore_write.write(line)
# Format result message
overrides_str = ''
for override in results['overrides']:
overrides_str += ' \033[93m{0}\033[0m\n'.format(override)
homepage_str = ''
for homepage_file in results['homepage_files']:
homepage_str += ' \033[92m{0}\033[0m\n'.format(homepage_file)
broken_links_str = ''
for broken_link in results['links_broken']:
broken_links_str += ' \033[91m{0}\033[0m\n'.format(broken_link)
result_message = '''\
Finished!
-----------------------------------------------
Links: (injected from default GUI)
up-to-date: {count_links_ok}
created: {count_links_created}
Files: (injected from default GUI)
up-to-date: {count_files_ok}
created: {count_files_created}
updated: {count_files_updated}
Overrides: {count_overrides} (files from default GUI that are overriden)
{overrides_str}
Homepage files: {count_homepage_files} (files/links specific only for homepage)
{homepage_str}
Broken links: {count_links_broken} (should be fixed manually)
{broken_links_str}'''.format(
count_links_ok=results['count_links_ok'],
count_links_created=results['count_links_created'],
count_files_ok=results['count_files_ok'],
count_files_created=results['count_files_created'],
count_files_updated=results['count_files_updated'],
count_overrides=results['count_overrides'],
overrides_str=overrides_str,
count_homepage_files=results['count_homepage_files'],
homepage_str=homepage_str,
count_links_broken=results['count_links_broken'],
broken_links_str=broken_links_str,
)
print(result_message)