This repository has been archived by the owner on Oct 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.py
executable file
·98 lines (88 loc) · 3.82 KB
/
sync.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
#!/usr/bin/env python3
import re
import sys
import os
import time
import shutil
from subprocess import call
from optparse import OptionParser
# Check if running with python 3 or newer
if sys.version_info[0] != 3:
print("This script requires Python version 3.x")
sys.exit(1)
def add_to_repository(directory, filenames):
os.chdir(directory)
print("Copying and staging files to repo")
for tempFile in filenames:
toBeAdded = tempFile.split('/')[-1]
if(toBeAdded[0] == '.'):
toBeAdded = toBeAdded[1:]
call(["cp", "-R", tempFile, directory + "/" + toBeAdded])
else:
call(["cp", "-R", tempFile, directory])
call(["git", "add", toBeAdded])
print("Committing files to the repo")
call(["git", "commit", "-am", "\'" + time.strftime("%c") + "\'"])
return 1
def clean_up_directory(directory, filenames):
os.chdir(directory)
for tempFile in filenames:
toBeDeleted = tempFile.split('/')[-1]
if(os.path.isfile(toBeDeleted) and os.path.exists(toBeDeleted) and os.path.exists(tempFile)):
os.remove(toBeDeleted)
elif(os.path.isdir(toBeDeleted) and os.path.exists(toBeDeleted) and os.path.exists(tempFile)): shutil.rmtree(toBeDeleted)
return 1
def expand_path(path):
return os.path.realpath(os.path.expandvars(path))
def push_to_remote():
call(["git", "push", "-u", "--quiet", "origin", "master"])
print("Synced with remote repository")
return 1
def setup_options():
parser = OptionParser()
parser.add_option("-f", "--file", action="store", dest="filename", help="Manifest file to use to sync files with the repo", default="./manifest.txt")
parser.add_option("-r", "--repo-directory", action="store", dest="repoDirectory", help="Git repo directory", default="$HOME/dotfiles")
parser.add_option("-p", "--push-to-remote", action="store_true", dest="pushToRemote", help="Push to remote directory, defaults to true", default=True)
parser.add_option("-n", "--no-push-to-remote", action="store_false", dest="pushToRemote", help="Do not push to remote directory")
return parser
def exit(code=1, show_message=True):
if show_message:
print("Exiting now!")
sys.exit(code)
def main():
(options, args) = setup_options().parse_args(sys.argv[1:])
manifest_file = expand_path(options.filename)
repo_dir= expand_path(options.repoDirectory)
push_to_remote_flag = options.pushToRemote
if not os.path.isfile(manifest_file):
print("Manifest file {} does not exist!".format(manifest_file))
exit()
# Here, we check if the line read from the manifest is a comment or an actual filename
# Comments start with an '#'
comment_regex = re.compile("^\#.*\s$")
# This section picks up the file names from the provided manifest file.
files_to_commit = []
with open(manifest_file) as manifest:
for line in manifest:
if not comment_regex.match(line):
files_to_commit.append(line.rstrip('\n'))
# All the environment variable in the file paths are expanded
files_to_commit = [expand_path(tempFile) for tempFile in files_to_commit]
# The git directory is completely cleaned up to avoid any conflicts while copying/committing.
if clean_up_directory(repo_dir, files_to_commit):
print("Cleaned up current directory.")
else:
print("Failed to clean up current directory.")
exit()
# The files are then added/staged in the git repo
if (add_to_repository(repo_dir, files_to_commit)):
print("Copied from source location to current directory, and added to git repo.")
else:
print("Failed to copy from source location, and add to git repo.")
exit()
if push_to_remote_flag:
print("Pushing to remote repo")
push_to_remote()
exit(0)
if __name__ == '__main__':
main()