forked from datawrapper/datawrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
themes
executable file
·132 lines (116 loc) · 5.16 KB
/
themes
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
131
132
#!/usr/bin/env python
# -----------------------------------------------------------------------------
# Themes manager for Datawrapper
# -----------------------------------------------------------------------------
# Author : Edouard Richard <edou4rd@gmail.com>
# -----------------------------------------------------------------------------
# License : MIT License
# -----------------------------------------------------------------------------
# Creation : 18-Feb-2013
# Last mod : 13-Mar-2013
# -----------------------------------------------------------------------------
"""
Theme manager for Datawrapper
-----------------------------
List, install, update, create and delete them
Installation may be done via git or symlink
"""
import sys, os, uuid, shutil, json, subprocess
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
THEME_DIR = os.path.join(BASE_DIR, "www/static/themes")
TEMPL_DIR = os.path.join(BASE_DIR, "templates/themes")
def create_symlink(source, destination, force=False):
if os.path.lexists(destination):
if force:
save = raw_input("the file %s already exists. Do you want a backup? [y/n]\n" % destination) == 'y'
if save:
moved_file = "%s.backup-%s" % (destination, uuid.uuid4())
os.rename(destination, moved_file)
print " We saved it to %s" % (moved_file)
else:
os.remove(destination)
else:
raise Exception("the file %s already exists. Do a './theme update'" % destination)
os.symlink(source, destination)
print "file created in %s" % destination
# -----------------------------------------------------------------------------
#
# MAIN
#
# -----------------------------------------------------------------------------
if __name__ == "__main__":
ADD_SYNTAX = "theme add <id> <git_url|local_path>"
LIST_SYNTAX = "theme list"
UPDATE_SYNTAX = "theme update <id>"
REMOVE_SYNTAX = "theme remove <id>"
CREATE_SYNTAX = "theme create <id>"
# we manage a specified theme
if len(sys.argv) > 2:
action, theme_id = sys.argv[1:3]
theme_dir = os.path.join(THEME_DIR, theme_id)
template_file = os.path.join(theme_dir, "%s.twig" % theme_id)
template_dest = os.path.join(TEMPL_DIR, "%s.twig" % theme_id)
if action == "add":
assert len(sys.argv) == 4, "Syntax: %s" % ADD_SYNTAX
assert not os.path.exists(theme_dir), "the folder %s already exists. You probably want to update it.\nRun: ./theme update %s" % (theme_dir, theme_id)
uri = sys.argv[3]
# it's a git repository
if uri.split('://')[0] in ('ssh', 'http', 'https', 'git') or uri.startswith('git@'):
# clone the theme repository
error = subprocess.call(['git', 'clone', uri, theme_dir], stdout=subprocess.PIPE)
assert int(error) == 0, "An error has occurred, aborted"
print "theme cloned in %s" % theme_dir
# create a symlink for template file
create_symlink(template_file, template_dest)
else: # it's a local path
uri = os.path.abspath(uri)
create_symlink(uri, theme_dir)
create_symlink(template_file, template_dest)
sys.exit()
elif action == "update":
assert len(sys.argv) == 3, "Syntax: %s" % UPDATE_SYNTAX
assert os.path.exists(theme_dir), "%s doesn't exist, please run '%s' before" % (theme_dir, ADD_SYNTAX)
# update the theme repository
error = subprocess.call(['git', 'pull', 'origin'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=theme_dir)
assert int(error) == 0, "An error has occurred, aborted"
create_symlink(template_file, template_dest, force=True)
sys.exit()
elif action == "remove":
assert len(sys.argv) == 3, "Syntax: %s" % REMOVE_SYNTAX
assert os.path.exists(theme_dir), "%s theme doesn't exist" % (theme_id)
if os.path.islink(theme_dir):
os.unlink(theme_dir)
else:
shutil.rmtree(theme_dir)
print "%s theme (%s) removed" % (theme_id, theme_dir)
if os.path.lexists(template_dest):
os.remove(template_dest)
print "symlink %s removed" % template_dest
sys.exit()
elif len(sys.argv) == 2 and sys.argv[1] == 'list':
assert len(sys.argv) == 2, "Syntax: %s" % LIST_SYNTAX
theme_names = os.listdir(THEME_DIR)
print "%s installed themes" % len(theme_names)
for theme_name in theme_names:
meta = json.load(open(os.path.join(THEME_DIR, theme_name, 'meta.json')))
print "* %(name)s v%(version)s" % dict(meta, **dict({'name': theme_name}))
sys.exit()
# TODO
# if action == "create":
# assert len(sys.argv) == 3, "Syntax: %s" % CREATE_SYNTAX
# assert not os.path.exists(theme_dir), "The folder '%s' already exists, aborted" % theme_dir
# os.makedirs(theme_dir)
# sys.exit()
print "\nTheme manager for Datawrapper\n-----------------------------"
print "USAGE:\n"
print '1. List installed themes'
print "\t$ ./%s" % LIST_SYNTAX
print '\n2. Add a theme to this datawrapper instance. Copy the template file (<id>.twig) in the template directory'
print "\t$ ./%s" % ADD_SYNTAX
print '\n3. Update a theme from git repository. Copy the template file (<id>.twig) in the template directory'
print "\t$ ./%s" % UPDATE_SYNTAX
# NOTE: uncomment when create action will be implemented
# print '\n3. Create a new theme directory prepared to be configured'
# print "\t$ ./%s" % CREATE_SYNTAX
sys.exit('wrong syntax')
# EOF