-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset-workspace.py
More file actions
executable file
·100 lines (78 loc) · 3.1 KB
/
Copy pathreset-workspace.py
File metadata and controls
executable file
·100 lines (78 loc) · 3.1 KB
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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from sys import exit
from os import path, mkdir, remove, listdir, unlink
from shutil import rmtree
from subprocess import call
from time import time
HOMEDIR = "/home/ide"
PROJECT = "{}/project".format(HOMEDIR)
HOST = "localhost"
DB = "drupal"
TIMESTAMP = int(time())
FILE = "/tmp/mysql_{}".format(TIMESTAMP)
def prepare_mysql_creds():
"""Store MySQL credentials in a file for improved security."""
with open(FILE, 'w') as f:
mysql_info = """[client]
host={}
user={}
password={}
""".format(HOST, DB, DB)
f.write(mysql_info)
f.close()
def reset_database():
"""Recreate the default MySQL database."""
if path.isdir("{}/mysql-data/{}".format(HOMEDIR, DB)):
call(["mysql", "--defaults-extra-file={}".format(FILE), "-e", "DROP DATABASE {}".format(DB)])
call(["mysql", "--defaults-extra-file={}".format(FILE), "-e", "CREATE DATABASE {}".format(DB)])
print("\n✓ The '{}' database is reset.".format(DB))
else:
call(["mysql", "--defaults-extra-file={}".format(FILE), "-e", "CREATE DATABASE {}".format(DB)])
print("\n✓ The '{}' database did not exist so we created it.\n".format(DB))
remove(FILE)
def reset_project_dir():
"""Reset the IDE workspace fully or create it as needed."""
default_dir = "{}/docroot/sites/default".format(PROJECT)
# Make sure we don't run into permission issues when wiping the project dir.
# See https://docs.acquia.com/ide/faq/#why-can-t-i-save-changes-made-to-settings-php
if path.isdir(PROJECT):
if path.isdir(default_dir):
call(['chmod', '-R', 'u+w', "{}".format(default_dir)])
success = True
for filename in listdir(PROJECT):
file_path = path.join(PROJECT, filename)
try:
if path.isfile(file_path) or path.islink(file_path):
unlink(file_path)
elif path.isdir(file_path):
rmtree(file_path)
except Exception as e:
success = False
print ("\nFailed to delete '{}'. Reason: '{}'".format(file_path, e.strerror))
if success:
print("✓ The '{}' workspace is reset.".format(PROJECT))
else:
try:
mkdir(PROJECT)
print("✓ The '{}' workspace did not exist so we created it.".format(PROJECT))
except OSError as e:
print ("\nCreation of the '{}' workspace failed".format(PROJECT), e.strerror)
def get_user_confirmation():
"""Ask for user confirmation before wiping anything."""
print("This will reset the '{}' workspace and '{}' database without an option to recover.\n".format(PROJECT, DB))
answer = input("Do you want to continue? [y/n] ")
if answer == "yes" or answer == "y":
prepare_mysql_creds()
reset_database()
reset_project_dir()
elif answer == "no" or answer == "n":
print("\nOk. We're not doing anything.")
exit()
else:
print("\nPlease either yes (y) or no (n).")
exit()
def main():
get_user_confirmation()
if __name__ == "__main__":
main()