-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymlink.py
executable file
·130 lines (99 loc) · 3.43 KB
/
symlink.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
#! /usr/bin/python
__title__ = "SYMLINK"
__description__ = "Symlink script for python2x: Create and remove symlink quickly in the current directory."
__copyrigth__ = "Copyright (c) 2017 Massimiliano Ranauro (huckbit@gmail.com)"
__author__ = "Massimiliano RANAURO"
__license__ = "MIT"
__version__ = "1.0.1"
__maintainer__ = "Massimiliano RANAURO"
__email__ = "huckbit@huckbit.com"
__status__ = "Production"
import os
# Settings: change this variable name if needs to be different from public_html
# ------------------------------------------------------------------------------
folderName = 'public_html'
# global variables
currentPath = os.getcwd()
dest = currentPath + "/" + folderName
# Print header and current status
def header():
print """
====================================
LINK/UNLINK DOCROOT for Python 2.x
====================================
[1] - create link for the folder
[2] - unlink folder
[l] - list current folders
[q] - quit
"""
if os.path.exists(dest):
print "Currents Status: >>> LINK EXISTS \n"
else:
print "Current Status: >>> LINK DOESN'T EXISTS \n"
# function for removing the link to public_html
def removeLink(dest):
os.system("clear")
if os.path.exists(dest):
# unlink public_html
os.unlink(dest)
if not os.path.exists(dest):
os.system("clear")
print ">>> Link removed"
print ""
raw_input("Press Enter to continue...")
else:
print("Error: I can't unlink! Link doesn't exists \n")
raw_input("Press Enter to continue...")
# function for creating the link to public_html from the source path
def createLink(path, dest):
os.system("clear")
# get only the dir inside the path
directories = [d for d in os.listdir('.') if os.path.isdir(d)]
# list with option and value for the directories
for i, directory in enumerate(directories):
print(i, directory)
if directories:
option = int(input("insert the number of the folder you want to link: "))
# create the source path with the folder name
src = currentPath + "/" + (directories[option])
# create the symlink
os.symlink(src, dest)
# if the symlink is created
if os.path.islink(dest):
print ">>> Link created \n"
raw_input("Press Enter to continue...")
else:
print ">>> Error creating link \n"
raw_input("Press Enter to continue...")
else:
print("Error: Import your project folder before to create a link to it. \n")
raw_input("Press Enter to continue...")
def menu():
exit = True
while exit:
os.system("clear")
header()
option = raw_input("what would you like to do? ")
# create the link
if option == '1':
createLink(currentPath, dest)
# remove the link
elif option == '2':
removeLink(dest)
# list current directory
elif option == 'l':
os.system("clear")
os.system("ls -altr")
raw_input("Press Enter to continue...")
# quit
elif option == 'q':
os.system("clear")
print "Thanks for using link/unlik. Bye!"
exit = False
# exceptions
else:
os.system("clear")
print "ERROR: This option is not available! \n"
raw_input("Press Enter to continue...")
# start the script
menu()