-
Notifications
You must be signed in to change notification settings - Fork 11
/
gil.py
424 lines (364 loc) · 14.7 KB
/
gil.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Gil (git links) tool allows to describe and manage complex git repositories
dependency with cycles and cross references
"""
import collections
import os
import re
import subprocess
import sys
import time
__author__ = "Ivan Shynkarenka"
__email__ = "[email protected]"
__license__ = "MIT License"
__url__ = "https://github.com/chronoxor/gil"
__version__ = "1.25.0.0"
class GilRecord(object):
def __init__(self, name, path, repo, branch, links):
self.name = name
self.path = path
self.repo = repo
self.branch = branch
self.links = links
self.active = False
def __eq__(self, other):
if not isinstance(self, other.__class__):
return NotImplemented
if not self.name == other.name:
return False
if not self.repo == other.repo:
return False
if not self.branch == other.branch:
return False
return True
def __lt__(self, other):
if not isinstance(self, other.__class__):
return NotImplemented
if not self.name < other.name:
return True
return False
@property
def __key__(self):
return self.name, self.repo, self.branch
def __hash__(self):
return hash(self.__key__)
def __str__(self):
return "%s %s %s %s" % (self.name, self.path, self.repo, self.branch)
class GilContext(object):
def __init__(self, path, depth):
self.records = collections.OrderedDict()
self.path = os.path.abspath(path)
self.depth = depth
print("Working path: %s" % self.path)
def show(self):
print("Gil context:")
for value in self.records.values():
print(value)
def discover(self, path):
current = os.path.abspath(path)
# Recursive discover the root path
root = current
parent = current
while True:
top = os.path.abspath(os.path.join(parent, os.pardir))
if top == parent:
break
parent = top
# Try to find .gitlinks file
filename = os.path.join(parent, ".gitlinks")
if os.path.exists(filename):
root = parent
self.discover_recursive(root, 0)
# Mark active records
for record in self.records:
if record.path.startswith(current):
record.active = True
def discover_recursive(self, path, level):
# Limit the discovering recursion depth
if level >= self.depth:
raise Exception("Maximum function recursion depth (%d) reached" % level)
current = os.path.abspath(path)
# Discover the current directory
records = self.discover_path(current)
# Insert discovered record into the records dictionary
for record in records:
if record not in self.records:
self.records[record] = record
# Discover all child directories
for record in records:
self.discover_recursive(record.path, level + 1)
def discover_path(self, path):
# Try to find .gitlinks file
filename = os.path.join(path, ".gitlinks")
if not os.path.exists(filename):
return []
print("Discover git links: %s" % filename)
# Process .gitlinks file
return self.process_links(path, filename)
def process_links(self, path, filename):
result = []
with open(filename, 'r') as file:
index = 0
for line in file:
# Skip empty lines and comments
line = line.strip()
if line == '' or line.startswith('#'):
continue
# Split line into tokens
tokens = self.split(line)
if (len(tokens) < 4) or ((len(tokens) % 2) != 0):
raise Exception("%s:%d: Invalid git link format! Must be in the form of 'name path repo branch [base_path target_path]'" % (filename, index))
# Create a new git link record
gil_name = tokens[0]
gil_path = os.path.abspath(os.path.join(path, tokens[1]))
gil_repo = tokens[2]
gil_branch = tokens[3]
gil_links = dict()
for i in range(4, len(tokens), 2):
gil_links[tokens[i + 1]] = tokens[i]
record = GilRecord(gil_name, gil_path, gil_repo, gil_branch, gil_links)
# Try to find git link record in the records dictionary
if record not in self.records:
result.append(record)
index += 1
return result
def clone(self, args=None):
args = [] if args is None else args
stack = list(self.records.keys())
while len(stack) > 0:
value = stack.pop(0)
path = value.path
if not os.path.exists(path) or not os.listdir(path):
# Perform git clone operation
self.git_clone(value.path, value.repo, value.branch, args)
# Discover new repository and append new records to the stack
if os.path.exists(path) and os.listdir(path):
inner = collections.OrderedDict.fromkeys(self.discover_path(path))
for key in inner:
if key not in self.records:
self.records[key] = key
stack.append(key)
def link(self, path=None):
current = os.path.abspath(self.path if path is None else path)
# Recursive discover the root path
root = current
parent = current
while True:
top = os.path.abspath(os.path.join(parent, os.pardir))
if top == parent:
break
parent = top
# Try to find .gitlinks file
filename = os.path.join(parent, ".gitlinks")
if os.path.exists(filename):
root = parent
self.link_records(root)
def link_records(self, path):
current = os.path.abspath(path)
# Link the current directory
dirs = self.link_path(current)
# Link all child directories
for d in dirs:
self.link_path(d)
# Link all records directories
for record in self.records:
self.link_path(record.path)
def link_path(self, path):
# Try to find .gitlinks file
filename = os.path.join(path, ".gitlinks")
if not os.path.exists(filename):
return []
# Process .gitlinks file
return self.update_links(path, filename)
def update_links(self, path, filename):
result = []
with open(filename, 'r') as file:
index = 0
for line in file:
# Skip empty lines and comments
line = line.strip()
if line == '' or line.startswith('#'):
continue
# Split line into tokens
tokens = self.split(line)
if (len(tokens) < 4) or ((len(tokens) % 2) != 0):
raise Exception("%s:%d: Invalid git link format! Must be in the form of 'name path repo branch [base_path target_path]'" % (filename, index))
# Create a new git link record
gil_name = tokens[0]
gil_path = os.path.abspath(os.path.join(path, tokens[1]))
gil_repo = tokens[2]
gil_branch = tokens[3]
gil_links = dict()
for i in range(4, len(tokens), 2):
gil_links[tokens[i + 1]] = tokens[i]
record = GilRecord(gil_name, gil_path, gil_repo, gil_branch, gil_links)
# Try to find git link record in the records dictionary
found = os.path.exists(gil_path) and os.listdir(gil_path)
if record in self.records:
found = True
base = self.records[record]
# Try to check or create link to the existing git link record
src_path = base.path
dst_path = gil_path
# Add destination path to the result list
result.append(dst_path)
# Update root link
self.update_link(src_path, dst_path)
# Update record links if some exists
for dst, src in record.links.items():
src_link_path = os.path.abspath(os.path.join(src_path, src))
dst_link_path = os.path.abspath(os.path.join(path, dst))
self.update_link(src_link_path, dst_link_path)
# Validate git link path
if not found or not os.path.exists(gil_path) or not os.listdir(gil_path):
raise Exception("%s:%d: Invalid git link path! Please check %s git repository in %s" % (filename, index, gil_name, gil_path))
index += 1
return result
def command(self, name, args=None):
args = [] if args is None else args
# Call the command for the current directory
current = os.path.abspath(self.path)
self.git_command(current, name, args)
# Call the command for all active records
for record in self.records:
if record.active:
# Checkout to the required branch
self.git_checkout(record.path, record.branch)
# Call the command callback
self.git_command(record.path, name, args)
# Filesystem methods
@staticmethod
def create_link(src_path, dst_path):
# Create all directories at link path
parent = os.path.abspath(os.path.join(dst_path, os.pardir))
os.makedirs(parent, exist_ok=True)
# Remove existing file, link or folder
if os.path.exists(dst_path):
if os.path.isdir(dst_path):
os.rmdir(dst_path)
else:
os.remove(dst_path)
# Create the link
if os.path.exists(src_path):
os.symlink(src_path, dst_path, target_is_directory=True)
print("Create git link: %s -> %s" % (src_path, dst_path))
else:
print("Cannot create git link! Path not found: %s" % src_path)
@staticmethod
def update_link(src_path, dst_path):
if src_path == dst_path:
# Do nothing here...
pass
elif os.path.exists(dst_path) and os.listdir(dst_path):
# Check the link
if os.path.islink(dst_path):
real_path = os.readlink(dst_path)
if real_path != src_path:
# Re-create the link
GilContext.create_link(src_path, dst_path)
else:
GilContext.create_link(src_path, dst_path)
# Git methods
@staticmethod
def git_clone(path, repo, branch, args):
# Call git clone command
print("Running git clone %s branch \"%s\" into %s" % (repo, branch, path))
params = ["git", "clone", *args, "-b", branch, repo, "--recurse-submodules", path]
attempt = 1
while attempt <= 10:
process = subprocess.run(params)
if process.returncode == 0:
return
print("Failed to run git clone %s branch \"%s\" into %s. Attempt %d..." % (repo, branch, path, attempt))
time.sleep(10)
attempt += 1
raise Exception("Failed to run git clone %s branch \"%s\" into %s" % (repo, branch, path))
@staticmethod
def git_checkout(path, branch):
# Save the current working directory
working = os.getcwd()
# Change working directory into the current git repository
os.chdir(path)
# Call git checkout command
print("Running: git checkout branch \"%s\" in %s" % (branch, path))
params = ["git", "checkout", branch]
process = subprocess.run(params)
if process.returncode != 0:
raise Exception("Failed to run git checkout branch \"%s\" in %s" % (branch, path))
# Restore the current working directory
os.chdir(working)
@staticmethod
def git_command(path, command, args):
# Save the current working directory
working = os.getcwd()
# Change working directory into the current git repository
os.chdir(path)
# Call git command
print("Running: git %s in %s" % (command, path))
params = ["git", command, *args]
process = subprocess.run(params)
if process.returncode is None:
raise Exception("Failed to run git %s in %s" % (command, path))
# Restore the current working directory
os.chdir(working)
# Utility methods
# Split a string by spaces preserving quoted substrings
# Author: Ton van den Heuvel
# https://stackoverflow.com/a/51560564
@staticmethod
def split(line):
def strip_quotes(s):
if s and (s[0] == '"' or s[0] == "'") and s[0] == s[-1]:
return s[1:-1]
return s
return [strip_quotes(p).replace('\\"', '"').replace("\\'", "'") for p in re.findall(r'"(?:\\.|[^"])*"|\'(?:\\.|[^\'])*\'|[^\s]+', line)]
def show_help():
print("usage: gil command arguments")
print("Supported commands:")
print("\thelp - show this help")
print("\tversion - show version")
print("\tcontext - show git links context")
print("\tclone [args] - clone git repositories")
print("\tlink - link git repositories")
print("\tupdate - update git repositories (clone & link)")
print("\tpull [args] - pull git repositories")
print("\tpush [args] - push git repositories")
print("\tcommit [args] - commit git repositories")
print("\tstatus [args] - show status of git repositories")
sys.exit(1)
def show_version():
print(__version__)
sys.exit(1)
def main():
# Show help message
if len(sys.argv) <= 1:
show_help()
if sys.argv[1] == "help":
show_help()
elif sys.argv[1] == "version":
show_version()
# Get the current working directory
path = os.getcwd()
# Create git links context
context = GilContext(path, 1000)
# Discover working path
context.discover(path)
if sys.argv[1] == "context":
context.show()
elif sys.argv[1] == "clone":
context.clone(sys.argv[2:])
elif sys.argv[1] == "link":
context.link()
elif sys.argv[1] == "update":
context.clone()
context.link()
elif (sys.argv[1] == "pull") or (sys.argv[1] == "push") or (sys.argv[1] == "commit") or (sys.argv[1] == "status"):
context.command(sys.argv[1], sys.argv[2:])
else:
print("Unknown command: %s" % sys.argv[1])
return -1
return 0
if __name__ == "__main__":
sys.exit(main())