Browse Source

init_homepath improved

84582af
Dirk Alders 1 year ago
parent
commit
c266beac68
1 changed files with 66 additions and 39 deletions
  1. 66
    39
      init_homepath

+ 66
- 39
init_homepath View File

@@ -1,12 +1,25 @@
1 1
 #!/usr/bin/python3
2 2
 #
3 3
 
4
+import os
5
+import shutil
4 6
 import subprocess
5 7
 import sys
6 8
 
7
-def proceed():
9
+class bcolors:
10
+    HEADER = '\033[95m'
11
+    OKBLUE = '\033[94m'
12
+    OKCYAN = '\033[96m'
13
+    OKGREEN = '\033[92m'
14
+    WARNING = '\033[93m'
15
+    FAIL = '\033[91m'
16
+    ENDC = '\033[0m'
17
+    BOLD = '\033[1m'
18
+    UNDERLINE = '\033[4m'
19
+
20
+def proceed(ask="Proceed?"):
8 21
     while True:
9
-        uf = input("\nProceed? [Y/n/q]")
22
+        uf = input("\n%s [Y/n/q]" % ask)
10 23
         if len(uf) == 0:
11 24
             uf = "y"
12 25
         if uf.lower() in ["y", "n", "q"]:
@@ -14,7 +27,17 @@ def proceed():
14 27
     if uf == "q":
15 28
         sys.exit(1)
16 29
     else:
17
-        return uf == "y"
30
+        return uf.lower() == "y"
31
+
32
+def delete_destination():
33
+    return proceed("/!\\ Delete Destination including ALL files? /!\\")
34
+
35
+def failed(error_msg):
36
+    print("    " + bcolors.WARNING + error_msg.rstrip("\n") + bcolors.ENDC)
37
+    print("    [" + bcolors.FAIL + " FAILED  " + bcolors.ENDC + "]")
38
+
39
+def success():
40
+    print("    [" + bcolors.OKGREEN + " SUCCESS " + bcolors.ENDC + "]")
18 41
 
19 42
 
20 43
 BASE_GIT_URL = "https://git.mount-mockery.de/dirk"
@@ -29,14 +52,13 @@ for data in REPOS:
29 52
 if proceed():
30 53
     for url, target in REPOS:
31 54
         command = "git clone %s %s" % (url, target)
32
-        sys.stdout.write("Cloning %s" % url)
55
+        print("Cloning %s" % url)
33 56
         process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
34 57
         process.wait()
35 58
         if process.returncode == 0:
36
-            print("\t\t[ SUCCESS ]")
59
+            success()
37 60
         else:
38
-            print("\t\t[ FAILED  ]")
39
-            print("   ", process.stderr.read().decode("utf-8"))
61
+            failed(process.stderr.read().decode("utf-8"))
40 62
 
41 63
 CONFIG_COMMANDS = (
42 64
     "GS=`grep .bash/enabled ~/.bashrc`; [ ${#GS} -ne 0 ] || cat ~/.bash/BASHRC_ADDON >> ~/.bashrc",
@@ -50,41 +72,46 @@ for command in CONFIG_COMMANDS:
50 72
     print("   ", command)
51 73
 if proceed():
52 74
     for command in CONFIG_COMMANDS:
53
-        sys.stdout.write("Executing %s" % command)
75
+        print("Executing %s" % command)
54 76
         process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
55 77
         process.wait()
56 78
         if process.returncode == 0:
57
-            print("\t\t[ SUCCESS ]")
79
+            success()
58 80
         else:
59
-            print("\t\t[ FAILED  ]")
60
-            print("   ", process.stderr.read().decode("utf-8"))
61
-
62
-#BASE_FOLDERS="Downloads media_images Videos C64"
63
-#VIP_FOLDERS="prj prj/Arduino Schreibtisch"
64
-
65
-
66
-#echo The following folders will be deleted:
67
-#for folder in $BASE_FOLDERS; do
68
-#	echo -e "* $HOME/\033[1m$folder\033[0m"
69
-#done
70
-#for folder in $VIP_FOLDERS; do
71
-#	echo -e "* $HOME/\033[1m$folder\033[0m"
72
-#done
73
-#echo
74
-#read -r -p "Are you sure? [y/N] " response
75
-#case "$response" in
76
-#    [yY][eE][sS]|[yY]) 
77
-#        for folder in $BASE_FOLDERS; do
78
-#            rm -rf $HOME/`basename $folder`; ln -s /usr/data/$USER/local/$folder $HOME
79
-#        done
80
-#        rm -rf $HOME/data; ln -s /usr/data/$USER/data data
81
-#        for folder in $VIP_FOLDERS; do
82
-#            rm -rf $HOME/`basename $folder`; ln -s /usr/data/$USER/data/$folder $HOME
83
-#        done
84
-#        ;;
85
-#    *)
86
-#        echo No folder initialisation!
87
-#        ;;
88
-#esac
81
+            failed(process.stderr.read().decode("utf-8"))
89 82
 
83
+DATA_LINKS = (
84
+    ('~/data', 'prj'),
85
+    ('~/data', 'prj/Arduino'),
86
+    ('~/data', 'Schreibtisch'),
87
+    ('/usr/data/dirk/local', 'C64'),
88
+    ('/usr/data/dirk/local', 'Downloads'),
89
+    ('/usr/data/dirk/local', 'media_images'),
90
+    ('/usr/data/dirk/local', 'Videos'),
91
+)
92
+print("\n\n\nAdding the ~/data softlinks to your home directory")
93
+for src_path, path in DATA_LINKS:
94
+    print("    %s -> %s" % (os.path.join(src_path, path), os.path.join('~', os.path.basename(path))))
95
+if proceed():
96
+    delete_dest = delete_destination()
97
+    for src_path, path in DATA_LINKS:
98
+        src = os.path.expanduser(os.path.join(src_path, path))
99
+        dest = os.path.expanduser(os.path.join("~", os.path.basename(path)))
100
+        print("    Creating symlink: %s -> %s" % (src, dest))
101
+        # Check existance of dest
102
+        if not os.path.exists(src):
103
+            failed("Source %s does not exists." % src)
104
+            continue
105
+        if os.path.exists(dest):
106
+            if delete_dest:
107
+                try:
108
+                    os.remove(dest)
109
+                except IsADirectoryError:
110
+                    shutil.rmtree(dest)
111
+            else:
112
+                failed("Destination %s already exists." % dest)
113
+                continue
114
+        # Create Symbolic Link
115
+        os.symlink(src, dest)
116
+        success()
90 117
 

Loading…
Cancel
Save