2 # -*- coding: utf-8 -*-
4 This app performs the synchonization with the system.
8 from __future__
import with_statement
19 print """Syncpy - synchronization tool by rlazo
21 This system was originally designed to work with data stored on a
22 versioning system, as git. If so _please_ update your copy before
28 -c Diff between files stored and those in use.
29 -u Overrides files in use using those stored. If some files
30 are missing they are created.""" % app_name
32 def diff_files(index
):
33 """Check for differences between the actual files and the
35 issues
= {'missing' : [], 'diff' : [], 'count' : 0}
37 path
= os
.path
.expanduser("~/%s" % value
)
38 if not os
.path
.exists(path
):
39 issues
['missing'].append(value
)
40 elif not filecmp
.cmp(path
, value
, shallow
=False):
41 issues
['diff'].append(value
)
42 issues
['count'] = len(issues
['missing']) + len(issues
['diff'])
45 def load_index(filename
="index.txt"):
46 """Load the index file. Lines starting with \# are considered
50 - `filename`: index filename (path optional)
53 with
open(filename
, 'r') as fd
:
54 ignore_regexp
= re
.compile(r
"^ *(#.*| *)$")
56 if ignore_regexp
.match(line
) is None:
57 index
.append(line
.replace("\n", ""))
60 if __name__
== "__main__":
62 opts
, args
= getopt
.getopt(sys
.argv
[1:], "cuh",[])
63 except getopt
.GetoptError
, err
:
68 file_index
= load_index()
71 res
= diff_files(file_index
)
73 print "Everything is ok"
75 for issue
in res
['missing']:
76 print "File %s missing." % issue
77 for issue
in res
['diff']:
78 print "File %s is different." % issue
80 ans
= str(raw_input("Are you sure you want to override your files?[y/n] "))
81 if ans
.find("y") != -1:
82 issues
= diff_files(file_index
)
83 for dotfile
in itertools
.chain(issues
['missing'], issues
['diff']):
84 print "Copying %s ..." % dotfile
85 shutil
.copy(dotfile
, os
.path
.expanduser("~/%s" % dotfile
))
88 print "Updating the repo..."
89 issues
= diff_files(file_index
)
90 for dotfile
in issues
['diff']:
91 print "Copying %s ..." % dotfile
92 shutil
.copy(os
.path
.expanduser("~/%s" % dotfile
), dotfile
)