forked from Geonovum/technisch-register
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
146 lines (106 loc) · 5.04 KB
/
Copy pathbackend.py
File metadata and controls
146 lines (106 loc) · 5.04 KB
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
from fs.osfs import OSFS
from fs.errors import ResourceNotFoundError
from subprocess import call
from webpages import create_standard_webpage
from os import symlink
import codecs
import time
import logging
def build_folders(source, destination_temp, standard, root):
"""Transform the repos' folder structure to that of the register
and build HTML pages for each standard.
"""
source_fs = OSFS(source)
print "Processing %s ... " % standard['id']
standard_fs = source_fs.opendir(standard['id'])
# list all artifacts of a standard
artifacts = standard_fs.listdir(dirs_only=True)
if '.git' in artifacts: artifacts.remove(".git")
for artifact in artifacts:
# check whether artifact folder exists in destination_temp
if root.exists('%s/%s' % (destination_temp, artifact)) == False:
root.makedir('%s/%s' % (destination_temp, artifact))
# copy standard folders from source to destination_temp in desired structure
root.copydir('%s/%s/%s' % (source, standard['id'], artifact), '%s/%s/%s' % (destination_temp, artifact, standard['id']))
html = create_standard_webpage(standard, artifacts)
# check whether register/standard exists
if root.exists('%s/%s' % (destination_temp, standard['id'])) == False:
root.makedir('%s/%s' % (destination_temp, standard['id']))
# write standard HTML page to register/standard/index.html
with codecs.open('%s/%s/index.html' % (destination_temp, standard['id']), 'w', encoding='utf8') as f:
f.write(html)
# copy web assets
root.copydir('web/assets', '%s/r' % destination_temp, overwrite=True)
def fetch_repo(root, repo, url, destination_temp):
"""Clone repos from GitHub to source folder."""
print "Fetching %s from %s" % (repo, url)
if root.exists('repos/%s' % repo):
print "Repo %s exists, issuing a git pull..." % repo
call('cd repos/%s; git pull' % repo, shell=True)
else:
print "Repo %s does not exist, issuing a git clone..." % repo
# explicitely create dir as implicit creation fails on server
root.makedir('%s/%s' % (destination_temp, repo))
call('cd repos; git clone %s %s' % (url, repo), shell=True)
# call('git clone %s %s/%s > /dev/null 2>&1' % (repo['url'], source, repo['id']), shell=True)
def create_staging(staging_build):
"""Create a staging version of the register hosted at
register.geostandaarden.nl/staging
"""
logging.info("Building staging...")
# TODO invoke build_folders from here
# staging moet een dir hoger zitten om op
# register.geostandaarden.nl/staging
# beshickbaar te zijn
# TODO: user OSFS like in create_production
print "Removing current staging..."
call('rm -rf ../%s' % staging_build, shell=True)
print 'Moving new register to staging...'
# TODO rename destination_temp to staging_build
# call('mv %s staging' % staging_build, shell=True)
# call('mv %s ../' % staging_build, shell=True)
call('cp -r %s ../' % staging_build, shell=True)
call('chmod -R a+rx ../%s' % staging_build, shell=True)
logging.info("Staging built successfully!")
def create_production(build_dir, backups, script_dir):
"""Put the staging version to production hosted at
register.geostandaarden.nl
"""
print "Building production..."
logging.info("Building production...")
deploy = OSFS('..')
if deploy.exists(backups) == False:
deploy.makedir(backups)
deploy.copydir('%s/%s' % (script_dir, build_dir), 'register-new', overwrite=True)
if deploy.exists('register') == True:
# server refuses to recursively remove register/staging
# hence we excplicitly remove symbolic link to staging
try:
deploy.remove('register/staging/staging')
except ResourceNotFoundError:
print "Warning, register/staging/staging not found..."
try:
deploy.removedir('register/staging')
except ResourceNotFoundError:
print "Warning, register/staging not found..."
backup_dir = time.strftime('%Y-%m-%d-%H-%M-%S')
# if deploy.exists('backups/%s' % backup_dir):
# deploy.removedir('backups/%s' % backup_dir, force=True)
deploy.copydir('register', 'backups/%s' % backup_dir, overwrite=True)
try:
deploy.movedir('register', 'register-old', overwrite=True)
except ResourceNotFoundError:
pass
deploy.movedir('register-new', 'register', overwrite=True)
# create symbolic link to standalone staging directory
# fails if production is built first...
deploy.makedir('register/staging')
call('cd ../register/staging; ln -s ../../staging', shell=True)
call('cd ../register; ln -s ../%s/log.txt' % script_dir , shell=True)
try:
deploy.removedir('register-old', force=True)
except ResourceNotFoundError:
pass
call('chmod -R a+rx ../register', shell=True)
print "Done building production..."
logging.info("Production built successfully!")