forked from ssricardo/anki-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
100 lines (75 loc) · 3.45 KB
/
build.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
# Simple automatization for building an addon
import sys
import os
import shutil
class Const:
ANKI = 0
ZIP = 1
CLEAR = 3
if __name__ == '__main__':
addonIndex = None
mode = None
target = None
acceptedArgs = ('-source', '-dist', '-dev', '-clear')
existingAddons = ('schedule-priority', 'words-shuffler', 'anki-markdown', 'fill-the-blanks')
print('====================== Building RSS Addon =====================')
for index, value in enumerate(sys.argv):
if value not in acceptedArgs:
continue
if value == acceptedArgs[0]: # source
if (index + 1) > len(sys.argv) - 1:
raise IOError('Incorrect parameters. After "-source" there must be a value ')
addonIndex = int(sys.argv[index + 1])
elif value == acceptedArgs[1]:
mode = Const.ZIP # dist
target = '/dist'
elif value == acceptedArgs[2]:
if mode:
print('Already set to dist mode. Ignoring -dev')
else:
mode = Const.ANKI
target = '/home/ricardo/.local/share/Anki2/addons21' # os.environ['anki_addon']
elif value == acceptedArgs[3]: # clear
mode = Const.CLEAR
target = './dist' # os.environ['anki_addon']
# -----------------------------------------------------------
if not addonIndex:
print('Choose an addon to be processed ===============================')
for index, name in enumerate(existingAddons):
print('{} - {}'.format(index + 1, name))
print('-' * 60)
addonIndex = int(input('> '))
if not addonIndex or addonIndex > len(existingAddons):
raise IOError('It was not possible to determine a valid addon as input')
addon = existingAddons[addonIndex - 1]
if not target:
raise IOError('No mode was informed. Choose either -dev or -dist')
# -----------------------------------------------------------
currentDir = os.path.dirname(os.path.realpath(__file__))
if mode == Const.ZIP:
if os.path.exists('dist'):
print('Cleaning dist directory')
shutil.rmtree('dist/')
print('Copying files')
shutil.copytree(currentDir + '/' + addon.replace('_', '-'), './dist',
ignore=shutil.ignore_patterns('tests', 'doc', '*_test*', '__pycache__'))
print('Creating binary')
shutil.make_archive('dist/' + addon, format='zip',
root_dir='dist/src')
# copies to anki's addon folder - test integrated
elif mode == Const.ANKI:
if os.path.exists(target + '/' + addon.replace('-', '_')):
print('Removing old files: {}'.format(target + '/' + addon))
shutil.rmtree(target + '/' + addon.replace('-', '_'))
print('Copying files to anki directory')
addonRoot = currentDir + '/' + addon
shutil.copytree(addonRoot + '/src', target + '/' + addon.replace('-', '_'),
ignore=shutil.ignore_patterns('tests', 'doc', '*_test*', '__pycache__'))
# Deletes from anki addons
elif mode == Const.CLEAR:
if os.path.exists(target + '/' + addon):
print('Removing old files: {}'.format(target + '/' + addon))
shutil.rmtree(target + '/' + addon)
os.remove(target + '/' + addon + '.py')
else:
print('Addon was not found on the target path: {}'.format(target + '/' + addon + '.py'))