-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.py
107 lines (86 loc) · 3.48 KB
/
init.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
# -*- coding: utf-8 -*-
import os
import shutil
import datetime
import subprocess
from sys import argv
import zipfile
import StringIO
import requests
def prompt(message, validate):
res = None
while res is None:
res = raw_input(message)
if not validate(res):
print 'Invalid value!'
res = None
return res.strip()
def removeInvalidChars(s):
validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
s = ''.join(c for c in s if c in validChars)
return s
def className(s):
return removeInvalidChars(s.title())
def replaceInFiles(filename, toReplace):
with open(filename) as f :
text = f.read()
for before, after in toReplace:
text = text.replace(before, after)
with open(filename, 'w') as f:
f.write(text)
def main():
pluginName = prompt('Plugin name: ', lambda v : bool(v.strip()))
defaultShortName = removeInvalidChars(pluginName).lower()
pluginShortName = (prompt("Plugin short name (no blank spaces allowed) [Leave empty to use '%s']: "
% defaultShortName, lambda s: s == removeInvalidChars(s)) or defaultShortName)
defaultClassName = className(pluginName)
pluginClassName = (prompt("Plugin class name [Leave empty to use '%s']: " % defaultClassName,
lambda s: s == removeInvalidChars(s)) or defaultClassName)
addBoundlessCommons = prompt("Add Boundless commons library?[Y/n]:", lambda s: s.lower() in ["y", "n", ""]).lower() in ["y", ""]
authorName = prompt('Plugin author: ', lambda v : bool(v.strip()))
d = datetime.date.today()
year = str(d.year)
month = d.strftime('%B')
if addBoundlessCommons:
boundlessCommons = '''
tmpCommonsPath = path(__file__).dirname() / "boundlesscommons"
dst = extlibs / "boundlesscommons"
if dst.exists():
dst.rmtree()
r = requests.get("https://github.com/boundlessgeo/lib-qgis-boundless-commons/archive/master.zip", stream=True)
z = zipfile.ZipFile(StringIO.StringIO(r.content))
z.extractall(path=tmpCommonsPath.abspath())
src = tmpCommonsPath / "lib-qgis-boundless-commons-master" / "boundlesscommons"
src.copytree(dst.abspath())
tmpCommonsPath.rmtree()
'''
else:
boundlessCommons = ""
toReplace = [('[pluginname]', pluginName),
('[pluginshortname]', pluginShortName),
('[pluginclassname]', pluginClassName),
('[month]', month),
('[year]', year),
('[authorname]', authorName),
('[boundlessCommons]', boundlessCommons),
]
folder = os.path.dirname(os.path.realpath(__file__))
gitFolder = os.path.join(folder, '.git')
subprocess.call('git submodule init'.split(' '))
subprocess.call('git submodule update'.split(' '))
if os.path.exists(gitFolder):
try:
shutil.rmtree(gitFolder)
except:
pass
for root, dirs, files in os.walk(folder):
for f in files:
if '.git' not in root:
replaceInFiles(os.path.join(root, f), toReplace)
os.rename(os.path.join(folder, 'pluginname'), os.path.join(folder, pluginShortName))
os.rename(os.path.join(folder, '_readme.rst'), os.path.join(folder, "README.rst"))
os.remove(argv[0])
os.remove(os.path.join(folder, 'console.png'))
os.remove(os.path.join(folder, 'README.md'))
if __name__ == '__main__':
main()