forked from philips/ghar
/
ghar
executable file
·299 lines (255 loc) · 9.02 KB
/
ghar
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
#!/usr/bin/env python
#
# Copyright (C) 2010 Brandon Philips <brandon@ifup.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
""" ghar: manage dotfiles for your $HOME
"""
__author__ = 'Brandon Philips <brandon@ifup.org>'
__version__= '1'
import sys
import os
import re
from urlparse import urlparse
import subprocess
from stat import *
# work around environments missing argparse
try:
import argparse
except ImportError:
argparse = __import__('argparse_ghar')
repos = []
argp = argparse.ArgumentParser(description='manage your dotfiles.')
argp_sub = argp.add_subparsers(title='ghar commands')
class NoGitException(Exception):
pass
class Link:
def install(self):
status = self._target_check()
if self.installed:
return (True, "ok")
if self.target_clear:
os.symlink(self.path, self.target)
return (True, "installed")
else:
return (False, "%s exists" % self.target)
def uninstall(self):
status = self._target_check()
if self.installed:
os.unlink(self.target)
return (True, "ok")
elif self.target_clear:
return (False, "%s no such file" % self.target)
else:
return (False, "%s is not a ghar link" % self.target)
def status(self):
status = self._target_check()
if status == "ok":
return (True, status)
else:
return (False, status)
def _target_check(self):
self.target_clear = False
self.installed = False
if self.target_mode == None:
self.target_clear = True
return "no such file"
elif S_ISDIR(self.target_mode):
return "directory"
elif S_ISREG(self.target_mode):
return "file"
elif S_ISLNK(self.target_mode):
link = os.readlink(self.target)
if link != self.path:
return "link to %s" % link
else:
self.installed = True
return "ok"
else:
return "unknown"
def __str__(self):
return self.target
def __init__(self, path, collection = True):
self.path = path
self.basename = os.path.basename(path)
format = "%s"
if not collection: format = ".%s"
self.target = os.path.join(os.environ['HOME'], format % self.basename)
try:
self.target_mode = os.lstat(self.target)[ST_MODE]
except OSError as e:
self.target_mode = None
class Repo:
"""A ghar repo is a git repo either containing dotfiles or
will itself be used as a dotfile directory.
"""
# Ghar repos can either be a "collection" or a directly linked folder
# e.g.
# oh-my-zsh would be linked to $HOME/.oh-my-zsh
# while
# mail-config/.mutt
# mail-config/.msmtprc
# would be linked to $HOME/.mutt $HOME/.msmtprc respectively
def _is_collection(self):
# dotfiles in repo means a collection
l = self.ls[:]
found_one = False
for f in l:
# global .gitconfig as a config is OK but not .git/ or .gitignore
if re.match("^\.git(ignore|modules)?$", f) or re.match("^\README.*$", f):
self.ls.remove(f)
elif re.match("^\..+$", f):
found_one = True
if found_one: return True
return False
def has_git(self):
try:
os.listdir(os.path.join(self.path, '.git'))
return True
except:
return False
def _git_cmd(self, cmd):
if not self.has_git():
raise NoGitException("%s is not a git repo" % self.path)
pipe = subprocess.Popen(cmd, shell=True, cwd=self.path,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
ret = pipe.communicate()[0]
if pipe.returncode != 0:
return (True, ret)
return (False, ret)
def pull(self):
cmd = 'git pull'
return self._git_cmd(cmd)
def clean(self):
cmd = 'git status -s 2> /dev/null'
ok, ret = self._git_cmd(cmd)
if len(ret) > 0: return (False, "dirty")
return (True, "clean")
def __str__(self):
return self.basename
def __init__(self, path):
self.path = path
self.basename = os.path.basename(path)
self.ls = os.listdir(path)
self.is_collection = self._is_collection()
self.links = []
if self.is_collection:
for i in self.ls:
self.links.append(Link(os.path.join(path, i)))
else:
self.links.append(Link(path, collection = False))
#
# ghar sub-commands
#
argp_list = argp_sub.add_parser('list', help="list all repos")
def list(args):
for repo in repos:
print repo
argp_list.set_defaults(func=list)
argp_add = argp_sub.add_parser('add',
help="add a git repo to ghar with git clone")
argp_add.add_argument('clone_args', metavar='args', nargs='+',
help='git clone arguments')
def add(args):
cmd = 'git clone %s' % ' '.join(args.clone_args)
print cmd
pipe = subprocess.Popen(cmd, shell=True, cwd=args.ghar_dir)
pipe.wait()
argp_add.set_defaults(func=add)
def _git_subcommand(args, action):
pull_repos = repos
if args.repos:
pull_repos = filter(lambda x: str(x) in args.repos, repos)
for r in pull_repos:
try:
status, string = getattr(r, action)()
print "%s: %s" % (str(r), string)
except NoGitException as e:
print "%s is not a git repo" % str(r)
return 0
argp_pull = argp_sub.add_parser('pull', help='pull all or a few repos')
argp_pull.add_argument('repos', metavar='repos', nargs='*',
help='repos to pull [defaults to pulling all repos]')
def pull(args):
_git_subcommand(args, "pull")
argp_pull.set_defaults(func=pull)
argp_status = argp_sub.add_parser('status', help='clean/dirty status of repos')
argp_status.add_argument('repos', metavar='repos', nargs='*',
help='repos to check [defaults to checking all repos]')
def status(args):
_git_subcommand(args, "clean")
argp_status.set_defaults(func=status)
def _link_subcommand(args, action, error_msg):
pull_repos = repos
if args.repos:
pull_repos = filter(lambda x: str(x) in args.repos, repos)
for repo in repos:
if not repo.has_git():
print "%s: not a git repo" % str(repo)
elif len(repo.links) <= 0:
print "%s: has no dotfiles" % str(repo)
else:
success = True
ret = []
print repo.basename
for l in repo.links:
stat, string = getattr(l, action)()
if stat == False: success = False
print " %s\t%s" % (string, str(l))
if not success:
print error_msg % str(repo)
argp_install = argp_sub.add_parser('install', help="install symlinks")
argp_install.add_argument('repos',
help='repos to install [defaults to installing all repos]',
metavar='repos', nargs='*')
argp_install.add_argument('--status', help='get status of repo links',
action='store_true')
def install(args):
func = "install"
if args.status: func = "status"
error = " error: %s is not fully installed"
_link_subcommand(args, func, error)
argp_install.set_defaults(func=install)
argp_uninstall = argp_sub.add_parser('uninstall', help="uninstall symlinks")
argp_uninstall.add_argument('repos',
help='repos to uninstall [defaults to uninstalling all repos]',
metavar='repos', nargs='*')
def uninstall(args):
func = "uninstall"
error = " error: %s is not installed"
_link_subcommand(args, func, error)
argp_uninstall.set_defaults(func=uninstall)
def _init_repos():
for root, dirs, files in os.walk(args.ghar_dir):
if root == args.ghar_dir:
if 'bin' in dirs:
dirs.remove('bin')
if '.git' in dirs:
dirs.remove('.git')
for dir in dirs:
repos.append(Repo(os.path.join(root, dir)))
break
def main(args):
# the ghar executable lives in <repo directory>/bin/ghar
args.ghar_dir = os.path.dirname(os.path.dirname(sys.argv[0]))
_init_repos()
if len(repos) <= 0:
sys.stderr.write("No repos found in %s\n" % args.ghar_dir)
sys.stderr.write("Please read %s/README\n" % args.ghar_dir)
args.func(args)
if __name__ == "__main__":
args = argp.parse_args()
main(args)