-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.py
174 lines (137 loc) · 3.98 KB
/
custom.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
import string
import venv
import os
import sys
import subprocess
import argparse
import datetime
parser = argparse.ArgumentParser(
description='Processing commands from vscode tasks.')
parser.add_argument(
'command',
help='Command to execute',
type=str,
choices=[
'prepare',
'configure',
'hello',
'build',
'serve',
'preview',
'publish',
]
)
args = parser.parse_args()
# Path to this file.
def getMyPath():
if getattr(sys, 'frozen', False):
return sys.executable
elif __file__:
return __file__
# Path to directory with this file.
def getMyDirectory():
return os.path.dirname(getMyPath())
def isVirtualEnvironment():
return sys.prefix != sys.base_prefix
def getEnvironmentDirectory():
# Get path to venv directory.
environmentDirectory = os.path.join(getMyDirectory(), '.venvs/pelican')
environmentDirectory = os.path.normpath(environmentDirectory)
return environmentDirectory
def passToVirtualEnvironment(command):
environmentDirectory = getEnvironmentDirectory()
# Run this file in venv.
pythonBin = os.path.join(environmentDirectory, "Scripts/python.exe")
pythonBin = os.path.normpath(pythonBin)
scriptPath = getMyPath()
# print(isVirtualEnvironment())
res = subprocess.Popen([pythonBin, scriptPath, command])
res.wait()
def hello():
print("Hello!")
# Paths.
contentPath = os.path.join(getMyDirectory(), 'content')
outputPath = os.path.join(getMyDirectory(), 'output')
localConfigPath = os.path.join(getMyDirectory(), 'pelicanconf.py')
publishConfigPath = os.path.join(getMyDirectory(), 'publishconf.py')
# Prepare environmemt.
def prepare():
environmentDirectory = getEnvironmentDirectory()
# Create new venv with pip.
builder = venv.EnvBuilder(clear=True, with_pip=True)
builder.create(environmentDirectory)
passToVirtualEnvironment('configure')
def configure():
print('Executing inside venv...')
subprocess.check_call([sys.executable, '-m', 'pip',
'install', '--upgrade', 'pip'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'wheel'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pelican'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'invoke'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'Markdown'])
subprocess.check_call(
[sys.executable, '-m', 'pip', 'install', 'typogrify'])
subprocess.check_call(
[sys.executable, '-m', 'pip', 'install', 'ghp-import'])
def build():
print('Build site...')
res = subprocess.check_call([
sys.executable,
'-m',
'pelican',
contentPath,
'-o', outputPath,
'-s', localConfigPath,
])
def serve():
print('Serve site...')
res = subprocess.check_call([
sys.executable,
'-m',
'pelican',
'-l',
contentPath,
'-o', outputPath,
'-s', localConfigPath,
])
def preview():
print('Build site for publising...')
res = subprocess.check_call([
sys.executable,
'-m',
'pelican',
contentPath,
'-o', outputPath,
'-s', publishConfigPath,
])
def publish():
preview()
print('Build site for publising...')
commit_message = "\"Publish site on {}\"".format(
datetime.date.today().isoformat())
from ghp_import import ghp_import
ghp_import(
outputPath,
push=True,
cname='www.riuson.com',
remote='github',
mesg=commit_message,
nojekill=True)
scriptCommand = args.command
handlers = {
'hello': hello,
'configure': configure,
'build': build,
'serve': serve,
'preview': preview,
'publish': publish,
}
if scriptCommand == 'prepare':
prepare()
else:
if isVirtualEnvironment():
handler = handlers[scriptCommand]
handler()
else:
passToVirtualEnvironment(scriptCommand)