-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcmapbuilder.py
106 lines (86 loc) · 3.26 KB
/
etcmapbuilder.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
# -*- coding: utf-8 -*-
"""
etcmapbuilder
~~~~~
This is a simple tool meant to help you create /etc/map rules for an AEM/Sling CMS.
"""
import sys
import os
import errno
import six
try:
from clint.textui import puts, indent, colored, prompt, validators
except ImportError as ex:
print(ex)
print("Unable to import the 'clint' library; you may be able to fix this with `pip install clint`")
sys.exit(1)
def mkdir_sane(dir):
try:
if (six.PY3):
os.makedirs(dir, 0o755, True)
else:
os.makedirs(dir, 0o755)
except OSError as ex:
if ex.errno == errno.EEXIST and os.path.isdir(dir):
pass
else:
raise ex
puts(colored.green("You will need the following pieces of information to proceed safely:"))
puts("FQDN")
puts("Content Root")
readyOptions = [
{'selector': '0', 'prompt': 'No {will exit immediately}', 'return': False},
{'selector': '1', 'prompt': 'Yes', 'return': True}
]
ready = prompt.options("Are you prepared?", readyOptions)
if (ready):
puts(colored.red("OK. Enter '.' at any non-option prompt to exit without saving."))
print("OK")
fqdn = prompt.query("What is the FQDN? :")
if (fqdn == '.'):
sys.exit(0)
proposedContentRoot = prompt.query("What is the content root for {} [e.g., /content/foo/en/]".format(fqdn))
if (proposedContentRoot == '.'):
sys.exit(0)
contentRoot = '/'.join([y for y in filter(None, proposedContentRoot.split('/')) if y not in ['content','en']])
etcRoot = prompt.query("In which directory should these rules be created?")
if (etcRoot == '.'):
sys.exit(0)
primaryXMLString = """
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:mixinTypes="[cq:ReplicationStatus]"
jcr:primaryType="sling:Mapping"
sling:internalRedirect="[/content/{0}/en/,/,/content/dam/{0}/,/dam/]"/>
""".format(contentRoot)
rootDomainXMLString = """
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:mixinTypes="[cq:ReplicationStatus]"
jcr:primaryType="sling:Mapping"
sling:internalRedirect="/content/{0}/en.html"
sling:match="{1}/$"/>
""".format(contentRoot, fqdn)
fqdnPrimary = "{}/{}".format(etcRoot, fqdn)
fqdnBare = "{}/{}".format(etcRoot, fqdn.replace(".", "_"))
puts("""
If you proceed, we will write this to {0}/.content.xml:
{1}
and this to {2}/.content.xml:
{3}
""".format(colored.red(fqdnPrimary), primaryXMLString, colored.red(fqdnBare), rootDomainXMLString))
createRules = prompt.options("Go ahead with creating entries in path {}?".format(etcRoot), readyOptions)
if (createRules):
mkdir_sane(etcRoot)
mkdir_sane(fqdnPrimary)
f = open("{}/.content.xml".format(fqdnPrimary), "w+")
f.write(primaryXMLString)
mkdir_sane(fqdnBare)
f = open("{}/.content.xml".format(fqdnBare), "w+")
f.write(rootDomainXMLString)
__version__ = '0.1.5'
__build__ = 0x000105
__copyright__ = 'Copyright 2015 Axis41 and Joey Smith'
__docformat__ = 'restructuredtext'
__author__ = 'Joey Smith'
__email__ = 'jsmith@axis41.com'