-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.py
77 lines (63 loc) · 1.93 KB
/
repo.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
import json, os
import git, tarfile
class Repo():
def __init__(self, metapath='repo.json', pkg_prefix='packages', git_prefix='clones'):
self.packages = {}
self.metapath = metapath
self.pkg_prefix = pkg_prefix
self.git_prefix = git_prefix
with open(metapath, 'r') as f:
self.packages = json.load(f)
def write(self):
with open(self.metapath, 'w') as f:
json.dump(self.packages, f)
def search(self, term):
results = {}
for name in self.packages.keys():
pkg = self.packages[name]
if term in name or term in pkg['description']:
# Don't want to give back unneed information
results[name] = {
'source' : pkg['source'],
'description' : pkg['description'],
}
return results
def tarball_path(self, name):
return self.packages.get(name, {}).get('tarball', None)
def git_path(self, name):
return os.path.join(self.git_prefix, name)
def update_pkg(self, name):
pkg = self.packages[name]
try:
# Open the local git repository if we can
gitr = git.Repo(path=self.git_path(name))
# Do a git pull to see if there is anything to update
rem = git.remote.Remote(gitr, 'origin')
info = rem.pull()[0]
if info.commit == gitr.head.commit:
# No update
return False
except:
# Failed to open the local git repo, clone from source
gitr = git.Repo.clone_from(pkg['source'], self.git_path(name))
# Update the tarball
tf = tarfile.open(name=self.tarball_path(name), mode='w:gz')
def add_files(dirpath, prefix):
ls = os.listdir(dirpath)
for n in ls:
if n != '.git':
full = os.path.join(dirpath, n)
arcpath = os.path.join(prefix, n)
if os.path.isdir(full):
add_files(full, arcpath)
else:
tf.add(full, arcname=arcpath)
add_files(self.git_path(name), '')
tf.close()
print(name, 'updated')
return True
def update(self):
updated = False
for name in self.packages.keys():
updated = self.update_pkg(name) or updated
return updated