forked from itsgrimetime/updot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
updot.py
executable file
·1044 lines (903 loc) · 39 KB
/
updot.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
Updot - Dotfile Updater
A script made to automatically grab all of the dotfiles a user desires to
keep track of, and keep them synced with their GitHub repository.
Files to be updated should be included in a 'dotfiles.manifest' file in the
'dotfiles' directory that this script will create in your home directory.
"""
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import absolute_import
from datetime import datetime
import os
import errno
import sys
import time
import socket
import getpass
import shutil
import argparse
import json
import base64
from subprocess import call, check_call, CalledProcessError, STDOUT
from typing import overload
try:
# Attempt importing check_output, this fails on Python older than 2.7
# so we need to define it ourselves
from subprocess import check_output
except ImportError:
# Source: https://gist.github.com/edufelipe/1027906
import subprocess
def check_output(*popenargs, **kwargs):
"""
Run command with arguments and return its output as a byte string.
Backported from Python 2.7 as it's implemented as pure python on stdlib.
"""
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
# Get proper urllib for Python version
try:
# Python 3
import urllib.request as urllib2
except ImportError:
# Python 2
import urllib2
# Setup input for use in Python 2 or 3
try:
input = raw_input
except NameError:
pass
# Define iter funcs based on Python version
try:
dict.iteritems
except AttributeError:
# Python 3
def itervalues(dictionary):
"""Python 3 alias for dictionary values iterator."""
return iter(dictionary.values())
def iteritems(dictionary):
"""Python 3 alias for dictionary items iterator."""
return iter(dictionary.items())
else:
# Python 2
def itervalues(dictionary):
"""Python 2 alias for dictionary values iterator."""
return dictionary.itervalues()
def iteritems(dictionary):
"""Python 2 alias for dictionary items iterator."""
return dictionary.iteritems()
try:
base64.encodebytes
except AttributeError:
# Python 2
def b64encode(*args, **kwargs):
"""Python 2 alias for bas64 string encoding."""
return base64.encodestring(*args, **kwargs)
else:
# Python 3
def b64encode(*args, **kwargs):
"""Python 3 alias for bas64 string encoding."""
return base64.encodebytes(*args, **kwargs)
# Define error for handling problems detected during dotfile status checks
class DotfileStatusError(Exception):
"""
Raised in the event of an error while checking dotfile status to prevent continued execution.
"""
pass
# Script version
UPDOT_VERSION = "2.30"
# When false, unnecessary output is suppresed
VERBOSE = False
# When false, debug output is suppressed
DEBUG = False
# When true, no output is generated
SILENT = False
# Open output streams
devnull = open(os.devnull, "w")
# Set active output streams
outstream = devnull
errstream = devnull
# Default message used if none is provided
DEFAULT_COMMIT_MESSAGE = "updot.py update"
# Setup directory variables
UPDOT_DIR = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
USER_HOME_DIR = os.path.expanduser("~")
DOTFILES_DIR = USER_HOME_DIR + "/.dotfiles"
BACKUP_DIR = USER_HOME_DIR + "/.dotfiles_backup"
SSH_PRIVATE_KEY_PATH = USER_HOME_DIR + "/.ssh/id_rsa"
SSH_PUBLIC_KEY_PATH = SSH_PRIVATE_KEY_PATH + ".pub"
MANIFEST_PATH = DOTFILES_DIR + "/dotfiles.manifest"
# Custom print functions
def dprint(*args, **kwargs):
"""Print function alias to only print when debug flag is set."""
if DEBUG:
print(*args, **kwargs)
def vprint(*args, **kwargs):
"""Print function alias to only print when verbose or debug flag is set."""
if VERBOSE or DEBUG:
print(*args, **kwargs)
def sprint(*args, **kwargs):
"""Print function alias to only print when silent flag is not set."""
if not SILENT:
print(*args, **kwargs)
def set_debug():
"""Enable debug mode"""
global DEBUG
global VERBOSE
global outstream
global errstream
DEBUG = True
VERBOSE = True
# Set debug options
if DEBUG:
outstream = sys.stdout
errstream = sys.stderr
def post_request(url, data, required_scopes = None):
"""
Issue a post request to a remote host.
Handles user authentication.
Also handles two-factor authentication with GitHub.
Keyword Args:
url -- the url to post to
data -- the payload to post to the url
required_scopes -- the minimum scopes required to perform the specified call, displayed to the user on authantication error
"""
headers = {'Content-Type' : 'application/json'}
request = urllib2.Request(url, data, headers)
sprint("A personal access token is required to interact with GitHub. This should only be required during the first run of this script.")
sprint("Navigate to the following page for the GitHub account where you want to host your dotfiles, and create a personal access token: ")
sprint("https://github.com/settings/tokens")
if (required_scopes):
sprint("NOTE: The token must be created with at least the following scopes: " + required_scopes)
github_token = getpass.getpass("GitHub Personal Access Token: ")
request.add_header('Authorization', 'token %s' % github_token)
retries = 0
max_attempts = 1
while retries < max_attempts:
try:
response = urllib2.urlopen(request)
dprint("Response:" + response.read().decode("UTF-8"))
success = True
except urllib2.HTTPError as error:
dprint("Request Failed: " + str(error))
if error.code == 401:
otp_header = error.info().get('X-Github-OTP')
dprint("X-Github-OTP: " + str(otp_header))
if otp_header and "required" in otp_header:
sprint("Two-Factor Authentication enabled for your account!")
sprint("Please enter 2FA code to continue.")
auth_code = input("GitHub 2FA Code: ")
request.add_header('X-Github-OTP', auth_code)
continue
else:
sprint("Failed to authenticate using provided personal access token. Check that the correct token was given and try again.")
if required_scopes:
sprint("Ensure the token has the following scopes: " + required_scopes)
github_token = getpass.getpass("GitHub Personal Access Token: ")
request.add_header('Authorization', 'token %s' % github_token)
continue
success = False
retries += 1
return success
def confirm(message):
"""
Prompts the user for confirmation with the provided message.
Keyword Args:
message -- the message to display to the user describing the reason for the confirmation prompt
"""
retries = 0
max_retries = 2
while retries < max_retries:
sprint(message)
confirm_response = input("Confirm (y/n):").lower()
if confirm_response == "yes" or confirm_response == "y":
return True
elif confirm_response == "no" or confirm_response == "n":
return False
else:
sprint("Invalid response.")
retries += 1
def check_dependencies():
"""
Verify script dependencies prior to execution.
Checks for a git installation and an active internet connection.
"""
# Check if git is installed
vprint("\nChecking for git...")
try:
check_call(["git", "--version"], stdout=outstream, stderr=errstream)
vprint("Git installation - Okay")
except (OSError, CalledProcessError):
sprint("Git not found!")
sprint("Install git, then rerun this script.")
sprint("Exiting...")
sys.exit()
# Ensure there is an internet connection
vprint("\nChecking internet connection...")
try:
# Try connecting to Google to see if there is an active internet connection
urllib2.urlopen('http://www.google.com/', timeout=5)
vprint("Internet connection - Okay")
except urllib2.URLError:
sprint("No internet connection detected!")
sprint("Check your connection, then rerun this script.")
sprint("Exiting...")
sys.exit()
def self_update():
"""
Checks if a newer version of updot exists in its repository, and updates
itself if so.
After update is complete, the script is restarted.
"""
sprint("\nChecking for new version of updot...")
# Check if local updot is a git repo
os.chdir(UPDOT_DIR)
if not os.path.exists(os.path.join(UPDOT_DIR, ".git")):
sprint("Unable to check for new versions of updot!")
sprint("Updot must be cloned as a git repository to be kept up to date!")
sprint("To get the latest updates, please reinstall updot by cloning its repository.")
return
# Check if an update is available
try:
# Get remote info
check_call(["git", "fetch"], stdout=outstream, stderr=errstream)
# Get hashes from git to determine if an update is needed
local = check_output(["git", "rev-parse", "@"])
remote = check_output(["git", "rev-parse", "@{u}"])
base = check_output(["git", "merge-base", "@", "@{u}"])
# Check the hashes to see if we need to update
if local != base:
sprint("Update failed! Local changes detected to updot!")
elif local != remote:
sprint("New version of updot found! Updating...")
# Update
check_call(["git", "pull", "origin", "master"], stdout=outstream, stderr=errstream)
sprint("Update successful. Restarting updot...\n\n")
# Restart script
os.execl(sys.executable, *([sys.executable]+sys.argv))
else:
sprint("Updot is already up to date!")
except CalledProcessError:
sprint("Failed to check for new version of Updot. Try again later.")
def call_git_config(key, config_path = None):
"""
Calls the git config command to get the value of the given key.
Assumes the global config unless a path is provided to a git config instead.
Keyword Args:
key -- key of the value stored in the git config
config_path -- optional path the git config to read (if omitted the global git config is assumed)
"""
if config_path:
dprint("Calling git config with file path: " + config_path)
value = check_output(["git", "config", "--file", config_path, key])[:-1]
else:
dprint("Calling git config with global flag")
value = check_output(["git", "config", "--global", key])[:-1]
# Decode the value string if needed
if value:
value = value.decode(encoding="utf-8", errors="ignore")
return value
def get_git_config_value(key, message):
"""
Gets the value of the specified key in the git config.
First checks the local config for the dotfiles directory, and falls back to the global git config if needed.
If the value is found in the global git config, the user is prompted to confirm that is the value they want to use.
When a new value is provided by the user, either due to it not being found or because the user chose to override the global value,
then the value is stored in the local git config for the dotfiles directory.
Keyword Args:
key -- key of the value stored in the git config
message -- the message to display when prompting for the requested value if it is not found in the git config
"""
value = ""
from_global_config = False
dotfiles_git_config = DOTFILES_DIR + "/.git/config"
if os.path.exists(DOTFILES_DIR):
try:
dprint("Checking local git config for value of '" + key + "' - (config: '" + dotfiles_git_config + "')")
value = call_git_config(key, dotfiles_git_config)
dprint("Found value in local config for '" + key + "': '" + value + "'")
except CalledProcessError:
dprint("Value not found in local git config for '" + key + "', falling back to global git config")
else:
dprint("No local git config found in dotfiles directory (" + dotfiles_git_config + ")")
# If we didn't find the value in the local config, check the global one
if not value:
try:
dprint("Checking global git config for value of '" + key + "'")
value = call_git_config(key)
sprint("Found value in global git config for '" + key + "': '" + value + "'")
# If we found a value in the global config, confirm this is the one the user wants to use
if not confirm("Accept global config value?"):
dprint("Global value rejected. Prompting for new value.")
value = ""
except CalledProcessError:
dprint("Value not found in global git config for '" + key + "'")
# If no value was found, or the user chose not to use the global value, then prompt for a new value
if not value:
if not from_global_config:
sprint("Value not found in git config for '" + key + "'.")
sprint(message)
value = input("Enter value for '" + key + "': ")
try:
# Attempt storing the new value in the local dotfiles git config
sprint("Saving value to local git config: '" + dotfiles_git_config + "'")
call(["git", "config", "--file", dotfiles_git_config, key, value])
sprint("Value stored in git config for '" + key + "': '" + value + "'")
except CalledProcessError:
sprint("Failed to update git config for '" + key + "'")
return value
def get_github_username():
"""
Gets the GitHub username set in the git config.
First checks the local config in the dotfiles directory, and falls back to the global git config if needed.
If the value is found in the global git config, the user is prompted to confirm that is the value they want to use.
When a new value is provided by the user, either due to it not being found or because the user chose to override the global value,
then the value is stored in the local git config for the dotfiles directory.
"""
return get_git_config_value("github.user", "Please provide the GitHub username for the account hosting your dotfiles repository.")
def get_git_email():
"""
Gets the email set in the git config.
If the value is found in the global git config, the user is prompted to confirm that is the value they want to use.
When a new value is provided by the user, either due to it not being found or because the user chose to override the global value,
then the value is stored in the local git config for the dotfiles directory.
"""
return get_git_config_value("user.email", "Please provide the email you would like to use for managing your dotfiles repository.")
def get_git_name():
"""
Gets the user name set in the git config.
If the value is found in the global git config, the user is prompted to confirm that is the value they want to use.
When a new value is provided by the user, either due to it not being found or because the user chose to override the global value,
then the value is stored in the local git config for the dotfiles directory.
"""
return get_git_config_value("user.name", "Please provide the name you would like to use for managing your dotfiles repository.")
def github_setup():
"""
Ensures that git config is setup and remote access to GitHub is successful.
"""
setup_okay = True
vprint("\nInspecting local git configuration...")
# Check for user name
git_name = get_git_name()
if git_name:
sprint("Git Config: user.name - '" + git_name + "'")
else:
setup_okay = False
sprint("Git Config: user.name - Not Found")
# Check for email
git_email = get_git_email()
if git_email:
sprint("Git Config: user.email - '" + git_email + "'")
else:
setup_okay = False
sprint("Git Config: user.email - Not Found")
# Check if GitHub username has been set
github_username = get_github_username()
if github_username:
sprint("Git Config: github.user - '" + github_username + "'")
else:
setup_okay = False
sprint("Git Config: github.user - Not Found")
vprint("\nTrying remote access to GitHub...")
try:
check_output(["ssh", "-T", "[email protected]"], stderr=STDOUT, shell=True)
except CalledProcessError as error:
vprint(error.output.decode()[:-1])
if "denied" in str(error.output):
setup_okay = False
sprint("Public key not setup with GitHub!")
ssh_setup()
else:
vprint("Connected to GitHub successfully!")
return setup_okay
def ssh_setup():
"""
Checks for a public SSH key and creates one if none is found.
Also attempts to add key to the ssh-agent.
"""
vprint("\nChecking for existing local public key...")
pub_key = None
try:
pub_key = open(SSH_PUBLIC_KEY_PATH, "r")
vprint("Public key found locally.")
except IOError:
sprint("Public key not found locally. Generating new SSH keys...")
git_email = get_git_email()
if not git_email:
sprint("No email defined in global git config. Unable to generate SSH key.")
sprint("Add email to git config and rerun this script.")
sprint("The following prompts will guide you through creating a new key pair.")
sprint("(Please leave directory options set to default values)\n")
call(["ssh-keygen", "-t", "rsa", "-C", git_email], shell=True)
vprint("\nAdding to SSH agent...")
try:
check_call(["ssh-add", SSH_PRIVATE_KEY_PATH], shell=True)
vprint("Key added to agent successfully.")
except (CalledProcessError, OSError):
vprint("Failed to add to agent. Is 'ssh-agent' running?")
pub_key = open(SSH_PUBLIC_KEY_PATH, "r")
sprint("\nAdding key to GitHub...")
hostname = socket.gethostname()
username = getpass.getuser()
data_dict = dict([('title', username + "@" + hostname), ('key', pub_key.read().strip())])
data = json.dumps(data_dict).encode("UTF-8")
url = "https://api.github.com/user/keys"
post_succeeded = post_request(url, data, 'write.public_key')
if post_succeeded:
vprint("Key added to GitHub successfully!")
else:
sprint("Failed to add key to GitHub account!")
sprint("Please follow the directions on the following page, then rerun this script:")
sprint("https://help.github.com/articles/generating-ssh-keys")
sprint("Exiting...")
sys.exit()
def directory_setup():
"""
Ensures that the dotfiles directory exists, and is a git repository.
Creates the direcotry an initializes an empty git repository if needed.
"""
# Check if dotfile directory exists, and create it if it doesn't
vprint("\nChecking for '~/.dotfiles' directory...")
if not os.path.exists(DOTFILES_DIR):
vprint("Dotfiles directory does not exist.")
vprint("Creating dotfiles directory...")
os.makedirs(DOTFILES_DIR)
else:
vprint("Dotfiles directory exists!")
# Check if dotfiles directory is a git repo
vprint("\nVerifying dotfiles directory is a git repository...")
if os.path.exists(DOTFILES_DIR + "/.git"):
vprint("Dotfiles directory is a git repo!")
else:
# Init as a local git repo
vprint("Dotfiles directory does not contain a git repository.")
vprint("Initializing local repository...")
os.chdir(DOTFILES_DIR)
call(["git", "init"], stdout=outstream, stderr=errstream)
def manifest_setup():
"""
Ensures a manifest file exists in the dotfiles directory.
If none is found one is created, and it is opened for editing by the user.
Attempts to use system default editor, otherwise defaults to vi.
"""
# Open manifest file, or create it if it doesn't exist
vprint("\nChecking for 'dotfiles.manifest'...")
try:
manifest = open(MANIFEST_PATH, "r")
vprint("Manifest file exists!")
except IOError:
sprint("Manifest file not found!")
if confirm("Create default manifest?"):
sprint("Creating empty 'dotfiles.manifest'...")
manifest = open(MANIFEST_PATH, "w+")
manifest.write("# updot.py Dotfile Manifest\n")
manifest.write("# This file is used to define which dotfiles you want\n")
manifest.write("# tracked with updot.py\n")
manifest.write("# Add the path to each dotfile (relative to your home\n")
manifest.write("# directory) you wish to track below this line\n\n")
manifest.close()
try:
vprint("Getting default text editor...")
editor = os.environ.get('EDITOR')
if editor is None:
vprint("Environment variable 'EDITOR' not defined, checking for 'VISUAL'...")
editor = os.environ.get('VISUAL')
if editor is None:
vprint("Default editor unknown. Defaulting to vi for editing.")
editor = "vi"
input("Press Enter to continue editing manifest...")
sprint("Opening manifest file in " + editor + " for editing...")
time.sleep(1)
check_call([editor, MANIFEST_PATH])
sprint("File contents updated by user. Attempting to continue...")
except OSError:
sprint("\n" + editor + " not found. Unable to open manifest for user editing.")
sprint("Add to the manifest file the path of each dotfile you wish to track.")
sprint("Then run this script again.")
sprint("Exiting...")
sys.exit()
def backup_file(file_name, src_path):
"""
Moves file to backup directory. This is used in place of deleting files.
Keyword Args:
file_name -- name of the file to backup
src_path -- path to the file to be backed up
"""
if os.path.exists(src_path):
if not os.path.exists(BACKUP_DIR):
os.makedirs(BACKUP_DIR)
# Prepend datetime to backup filename to prevent overwriting backup files
current_datetime = datetime.now().strftime("%Y-%m-%dT%H_%M_%S")
file_name = "[" + current_datetime + "]" + file_name
dst_path = os.path.join(BACKUP_DIR, file_name)
shutil.move(src_path, dst_path)
def update_links(files):
"""
Updates all symlinks to files in the manifest, ensuring they are all valid.
Keyword Args:
files -- paths to files to verify and/or update symlinks for
"""
longest_name = 0
sprint("\nChecking symlinks...\n")
for path in files:
name = path.split("/")[-1][:-1]
longest_name = len(name) if (len(name) > longest_name) else longest_name
if name and path:
path = path.strip("\n")
src_dir = path[:len(name) * -1]
dst_dir = src_dir
src_dir = os.path.join(USER_HOME_DIR, src_dir)
if dst_dir and dst_dir[0] == ".":
dst_dir = dst_dir[1:]
update_link(src_dir, dst_dir, name, longest_name)
def update_link(src_dir, dst_dir, name, output_indent=0):
"""
Updates the symlink between the provided source and destination paths.
Cases Handled:
1. A file exists in both the dotfile and target directories: It is removed
from the target directory and linked.
2. The file does not exist in the target directory, but does in the dotfile
directory: It is linked.
3. The file exists in the target directory, but not the dotfile directory:
It is moved to the dotfile directory and linked.
4. The file does not exist in target or dotfile directories: A warning is
displayed.
5. The file exists in the dotfile directory, and a link exists in the target
directory: Nothing is done.
6. The file does not exist in the dotfile directory, and a link exists in
the target directory: The dead link is removed.
Keyword Args:
src_dir -- source directory to link from
dst_dir -- destination directory to link to
name -- name of the file to link
output_indent -- optional amount of spacing to indent output from this function
"""
# Handle Possible Conditions:
# src = target dir (from manifest); dst = dotfile dir
# 1: src:exist && dst:exist => backup and link
# 2: src:!exist && dst:exist => link
# 3: src:exists && dst:!exist => move and link
# 4: src:!exist && dst:!exist => warning
# 5: src:link && dst:exist => okay
# 6: src:link && dst:!exist => delete link
indent_space = " " * (output_indent - len(name))
indent_name = name + indent_space
indent_name_space = " " * len(name) + indent_space
dst_name = name
if dst_name[0] == ".":
dst_name = dst_name[1:]
src_path = os.path.join(src_dir, name)
dst_path = os.path.join(DOTFILES_DIR, dst_dir, dst_name)
if os.path.exists(dst_path):
if os.path.lexists(src_path):
if not os.path.islink(src_path):
#1: src:exist dst:exist => backup and link
sprint(indent_name + " - Removing from target directory: " + src_dir)
backup_file(name, src_path)
sprint(indent_name_space + " - Linking into target directory: " + src_dir)
os.symlink(dst_path, src_path)
else:
#5: src:link dst:exit => okay
sprint(name + indent_space + " - Okay")
else:
#2: src:!exist dst:exist => link
sprint(indent_name + " - Linking into target directory: " + src_dir)
if not os.path.exists(src_dir):
os.makedirs(src_dir)
os.symlink(dst_path, src_path)
else:
if os.path.lexists(src_path):
if os.path.islink(src_path):
#6: src:link dst:!exist => delete link
sprint(indent_name + " - Removing dead link from target directory: " + src_dir)
os.remove(src_path)
else:
#3: src:exist dst:!exist => move and link
sprint(indent_name + " - Moving to dotfiles directory...")
try:
os.makedirs(os.path.dirname(dst_path))
except OSError as error:
if error.errno != errno.EEXIST:
raise
shutil.move(src_path, dst_path)
sprint(indent_name_space + " - Linking into target directory: " + src_dir)
os.symlink(dst_path, src_path)
else:
#4: src:!exist dst:!exist => warning
sprint(indent_name + " - Warning: present in manifest, but no remote or local copy exists!")
def repo_setup():
"""
Ensures local and remote git repositories are set up.
If no local repo is found, one is initialized.
If no remote repo is found on GitHub, one is created use the GitHub API.
"""
# Ensure the already directory exists and is a git repo
if not os.path.exists(DOTFILES_DIR):
directory_setup()
# Change to dotfiles repo directory
os.chdir(DOTFILES_DIR)
# Check if remote already added
vprint("\nChecking for remote repository...")
try:
check_call(["git", "fetch", "origin", "master"], stdout=outstream, stderr=errstream)
vprint("Repository has remote!")
except CalledProcessError:
vprint("No remote added to repository!")
vprint("Adding dotfiles remote...")
# Check if repo already exists
github_username = get_github_username()
remote_path = "[email protected]:" + github_username + "/dotfiles.git"
try:
urllib2.urlopen("http://www.github.com/" + github_username + "/dotfiles")
call(["git", "remote", "add", "origin", remote_path], stdout=outstream, stderr=errstream)
call(["git", "fetch", "origin", "master"], stdout=outstream, stderr=errstream)
call(["git", "checkout", "master"], stdout=outstream, stderr=errstream)
vprint("Remote added successfully.")
except urllib2.HTTPError:
sprint("Remote repository does not exist.")
if confirm("Create remote GitHub repository?"):
sprint("Creating GitHub repository...\n")
# Create repo on GitHub
url = "https://api.github.com/user/repos"
data_dict = {'name': 'dotfiles', 'description': 'My dotfiles repository'}
data = json.dumps(data_dict).encode("UTF-8")
post_request(url, data)
sprint("\nAdding dotfiles remote...")
call(["git", "remote", "add", "origin", remote_path], stdout=outstream, stderr=errstream)
sprint("\nCreating initial commit...")
call(["git", "add", ".", "-A"], stdout=outstream, stderr=errstream)
call(["git", "commit", "-m", "\"Initial commit.\""], stdout=outstream, stderr=errstream)
else:
sprint("Skipping GitHub repository creation.")
def get_repo_status(retry=True):
"""
Get the current status of tracked dotfiles.
Keyword Args:
retry -- optional flag to specify if the status check should be retried on failure
"""
try:
check_call(["git", "fetch", "origin", "master"], stdout=outstream, stderr=errstream)
return check_output(["git", "diff", "origin/master", "HEAD", "--name-status"], stderr=errstream)
except CalledProcessError:
if retry and confirm("Forcibly retry status check? (this will force checkout master)"):
check_call(["git", "remote", "update", "--prune"], stdout=outstream, stderr=errstream)
check_call(["git", "checkout", "master", "--force"], stdout=outstream, stderr=errstream)
return get_repo_status(False)
return None
def pull_changes():
"""Check for remote changes, and pull if any are found."""
sprint("\nChecking for remote changes...")
# Only pull if master branch exists
remote_branches = check_output(["git", "ls-remote", "--heads", "origin"], stderr=errstream)
if "master" in remote_branches.decode("UTF-8"):
try:
# Check if we need to pull
status = get_repo_status()
if status is None:
sprint("\nUnable to pull changes: Error reaching repository.")
elif status:
sprint("\nRemote Changes:")
parse_print_diff(status)
sprint("\nPulling most recent revisions from remote repository...")
check_call(["git", "pull", "origin", "master"], stdout=outstream, stderr=errstream)
else:
sprint("\nNo remote changes!")
except CalledProcessError:
sprint("\nFailed to pull changes.")
else:
sprint("\nNo remote master found! Not pulling.")
def push_changes(commit_message):
"""
Add, commit, and push all changes to the dotfiles.
Keyword Args:
commit_message -- message to use as the commit message for this update
"""
call(["git", "add", ".", "-A"], stdout=outstream, stderr=errstream)
status = check_output(["git", "diff", "--name-status", "--cached"], stderr=errstream)
if status:
sprint("\nLocal Changes:")
parse_print_diff(status)
if confirm("Commit and push changes?"):
sprint("\nPushing updates to remote repository...")
try:
check_call(["git", "commit", "-m", commit_message], stdout=outstream, stderr=errstream)
check_call(["git", "push", "origin", "master"], stdout=outstream, stderr=errstream)
except CalledProcessError:
sprint("Error: Failed to push changes!")
else:
sprint("\nNot pushing changes.")
else:
sprint("\nNo changes to push!")
def check_readme():
"""Check if a readme exists, and create a default one if not."""
# Check for a readme, and create one if one doesn't exist
if not os.path.isfile("README.md") and confirm("Create default readme?"):
#Create Readme file
vprint("\nReadme not found.")
vprint("Creating readme file...")
readme = open("README.md", "w+")
readme.write("dotfiles\n")
readme.write("========\n")
readme.write("My dotfiles repository.\n\n")
readme.write("Created and maintained by the awesome 'updot.py' script!\n\n")
readme.write("Get the script for yourself here: https://github.com/ntpeters/updot\n")
readme.close()
call(["git", "add", DOTFILES_DIR + "/README.md"], stdout=outstream, stderr=errstream)
def read_manifest():
"""Read in the file paths to track from the manifest file."""
files = []
vprint("\nReading manifest file...")
manifest = open(MANIFEST_PATH, "r")
for path in manifest:
# Don't process line if it is commented out
if path[0] != "#":
files.append(path)
return files
def parse_print_diff(diff_string):
"""
Parses the git diff file statuses and prints them out in a more readable
format.
Keyword Args:
diff_string -- git diff status string to process
"""
file_statuses = diff_string.decode('UTF-8').split("\n")
status_dict = {}
longest_status = 0
for file_status in file_statuses:
if file_status:
code = file_status[:1]
name = file_status[1:].strip()
status_dict[name] = code
longest_status = len(name) if len(name) > longest_status else longest_status
for name, code in iteritems(status_dict):
indent_space = (longest_status - len(name)) * " "
line = name + indent_space + " - "
if code == "M":
line += "Modified"
elif code == "A":
line += "Added"
elif code == "D":
line += "Deleted"
elif code == "R":
line += "Renamed"
elif code == "C":
line += "Copied"
elif code == "U":
line += "Updated (Unmerged)"
else:
line += "Unknown Status (" + code + ")"
sprint(line)
def get_status():
"""Display the status of local and remote dotfiles."""
# Track if any errors occur
error_detected = False
# Track if changes were detected
changes_found = False
# Ensure the dotfiles directory exist
if os.path.exists(DOTFILES_DIR):
os.chdir(DOTFILES_DIR)
# Get local status
try:
# Mark all untracked files with 'intent to add'
check_call(["git", "add", "-N", "."], stdout=outstream, stderr=errstream)
status = check_output(["git", "diff", "--name-status"])
status += check_output(["git", "diff", "--name-status", "--cached"])
if status:
sprint("\nLocal Dotfiles Status:")
parse_print_diff(status)
changes_found = True
else:
sprint("\nNo local changes!")
except CalledProcessError:
error_detected = True
sprint("\nError: Unable to get local status")
# Get remote status
try:
check_call(["git", "fetch", "origin"], stdout=outstream, stderr=errstream)
status = check_output(["git", "diff", "origin/master", "HEAD", "--name-status"], stderr=errstream)
if status:
sprint("\nRemote Dotfiles Status:")
parse_print_diff(status)
changes_found = True
else:
sprint("\nNo remote changes!")
except CalledProcessError:
error_detected = True
sprint("\nError: Unable to get remote status")
else:
sprint("\nWarning: Dotfiles directory does not exist. Skipping status check.")
changes_found = True
if error_detected:
raise DotfileStatusError
return changes_found
def main():
"""Script entry point."""
global SILENT
global VERBOSE
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", help="Print debug output during execution (implies verbose)", action="store_true")
parser.add_argument("-v", "--verbose", help="Print additional output during execution", action="store_true")
parser.add_argument("-s", "--silent", help="Print nothing during execution", action="store_true")
parser.add_argument("-m", "--message", help="Add a custom message to this commit")
parser.add_argument("--status", help="Print the current status of the dotfiles directory", action="store_true")
parser.add_argument("--selfupdate", help="Check if an update to Updot is available", action="store_true")
parser.add_argument("--doctor", help="Ensure all dependencies are met, and git and SSH are properly configured", action="store_true")
parser.add_argument("--relink", help="Re-link all dotfiles into place", action="store_true")
args = parser.parse_args()
# Set options based on args
if args.debug:
set_debug()
elif args.verbose:
VERBOSE = True
elif args.silent:
SILENT = True
# Set custom commit message if one was provided
commit_message = DEFAULT_COMMIT_MESSAGE
if args.message:
commit_message = args.message
sprint("updot v" + UPDOT_VERSION + " - Dotfile update script")
if DEBUG:
sprint("Debug Mode: Enabled")
if args.selfupdate:
check_dependencies()
self_update()
exit()
if args.doctor:
check_dependencies()
directory_setup()
setup_check = github_setup()
if setup_check:
repo_setup()
sprint("\nNo problems detected. All systems go!")
exit()
if args.relink:
files = read_manifest()
update_links(files)