-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepos.py
executable file
·189 lines (158 loc) · 5.31 KB
/
repos.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
#!/usr/bin/env python
# coding=utf8
"""
Script for managing all my local git repositories.
"""
import sys, os, json, tempfile, subprocess
import ConfigParser
import optfunc
DEFAULT_CONFIG_FILE = os.path.expanduser("~/.repos.json")
DEFAULT_BASE_DIR = os.path.expanduser("~/")
def run(args, cwd=None):
try:
output = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=cwd).communicate()[0]
except OSError, e:
print args
raise e
return output
class GitProject(object):
def __init__(self, local, remote, basedir):
self.local, self.remote, self.basedir = local, remote, basedir
self.provider = "git"
def getDict(self):
# This is not pretty.
return {"local":self.local,"remote":self.remote,"provider":self.provider}
def update(self):
path = os.path.join(self.basedir, self.local)
run(["git", "pull"], cwd=path)
def create(self):
path = os.path.abspath(os.path.join(self.basedir, os.path.normpath(self.local)))
remote = os.path.expanduser(self.remote)
print "Cloning", remote, "into", path
print run(["git", "clone", "--no-hardlinks", remote, path])
def status(self):
print self.local, "status:"
path = os.path.abspath(os.path.join(self.basedir, os.path.normpath(self.local)))
config = self.getDiskGitConfig(path)
actualRemote = None
for section in config.sections():
if "remote" in section and "origin" in section:
actualRemote = config.get(section, "url")
if os.path.expanduser(self.remote) != os.path.expanduser(actualRemote):
print "Remote origin does not match. Expected %s, got %s" % (self.remote, actualRemote)
print run(["git", "status"], cwd=path)
def getDiskGitConfig(self, path):
# hack to remove left hand whitespace so we can use ConfigParser to read the git .config, eww.
config = ConfigParser.ConfigParser()
f = open(os.path.join(path, '.git/config'))
tmp = tempfile.TemporaryFile()
for line in f:
tmp.write(line.lstrip())
f.close()
tmp.seek(0)
config.readfp(tmp)
tmp.close()
return config
class Workspace(object):
def __init__(self, basedir=DEFAULT_BASE_DIR, config=DEFAULT_CONFIG_FILE):
self.projects = []
self.basedir = basedir
self.config = config
if os.path.exists(self.config):
f = open(self.config)
conf = json.load(f)
f.close()
else:
conf = {}
if "projects" in conf:
for project in conf["projects"]:
provider = project["provider"]
local = project["local"]
remote = project["remote"]
if provider == "git":
self.projects.append(GitProject(local, remote, self.basedir))
elif provider == "svn":
self.projects.append(SvnProject(local, remote, self.basedir))
self.sort()
def save(self):
configDict = { "projects": list(project.getDict() for project in self.projects) }
f = None
try:
f = open(self.config, 'w')
f.write(json.dumps(configDict, sort_keys=True, indent=2))
finally:
if f:
f.close()
def update(self):
for project in self.projects:
print "Updating", project.local
project.update()
def add(self, local):
config = ConfigParser.ConfigParser()
# hack to remove left hand whitespace so we can use ConfigParser to read the git .config, eww.
f = open('%s/.git/config' % local)
tmp = tempfile.TemporaryFile()
for line in f:
tmp.write(line.lstrip())
f.close()
tmp.seek(0)
config.readfp(tmp)
tmp.close()
for section in config.sections():
if "remote" in section:
remote = config.get(section, "url")
break
local = os.path.normpath(os.path.relpath(local, self.basedir))
print local, remote
project = GitProject(local, remote, self.basedir)
for p in self.projects:
if p.local == project.local:
print p.__dict__
raise Exception("This project already being tracked")
self.projects.append(project)
self.sort()
def sort(self):
self.projects.sort(key=lambda project: project.local)
def drop(self, path):
path = os.path.abspath(os.path.normpath(path))
for project in self.projects:
if os.path.abspath(os.path.normpath(project.local)) == path:
print "Dropping project", project.local
self.projects.remove(project)
return
print "No project with that path is being tracked"
def create(self):
for project in self.projects:
projectPath = os.path.abspath(os.path.join(self.basedir, os.path.normpath(project.local)))
if not os.path.exists(projectPath):
print "Creating project ", project.local
project.create()
print "Project created"
def status(self):
for project in self.projects:
project.status()
def update():
workspace = Workspace()
workspace.update()
def add(path):
workspace = Workspace()
workspace.add(path)
workspace.save()
def drop(path):
workspace = Workspace()
workspace.drop(path)
workspace.save()
def ls():
workspace = Workspace()
print "Projects:"
for project in workspace.projects:
print "\t", project.local
def status():
workspace = Workspace()
workspace.status()
# create local repos which do not exist yet.
def create():
workspace = Workspace()
workspace.create()
if __name__ == '__main__':
optfunc.run([update, add, create, drop, ls, status])