This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrush-scm
executable file
·237 lines (198 loc) · 10.3 KB
/
drush-scm
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python2
import os
import sys
import json
import subprocess
import argparse
import ConfigParser
import re
import logging
def get_drush_major_version(config):
drush = config.get("common", "DrushCommand")
with open(os.devnull) as DEVNULL:
cmd = drush + " version"
try:
version_output = subprocess.check_output(cmd, shell=True, stderr=DEVNULL)
matches = re.search(r"Drush [vV]ersion\s+:\s+(.+)\.(.+)\.(.+)", version_output)
return matches.group(1)
except subprocess.CalledProcessError:
logging.error("Cann't get drush version.")
sys.exit(1)
def ups(args, config):
major_version = get_drush_major_version(config)
if int(major_version) >= 9:
ups_with_composer(args, config)
else:
ups_legacy(args, config)
def ups_with_composer(args, config):
drush = config.get("common", "DrushCommand")
include_non_security_update = args.include_non_security_update
with open(os.devnull) as DEVNULL:
cmd = drush + " pm:security"
if include_non_security_update:
logging.warn("--include-non-security-update is not support by drush 9.x later, so it will be ignore.")
try:
subprocess.check_call(cmd.split(" "), stderr=DEVNULL)
except subprocess.CalledProcessError:
# if you have something outdated, drush pm:secrity will return non-zero status code..
sys.exit(0)
def ups_legacy(args, config):
drush = config.get("common", "DrushCommand")
include_non_security_update = args.include_non_security_update
with open(os.devnull) as DEVNULL:
cmd = drush + " ups"
if not include_non_security_update:
cmd = cmd + " --security-only"
subprocess.check_call(cmd.split(" "), stderr=DEVNULL)
def is_managed_by_git():
with open(os.devnull) as DEVNULL:
try:
cmd = "git status"
subprocess.check_call(cmd.split(" "), stderr=DEVNULL)
except subprocess.CalledProcessError:
return False
return True
def upc(args, config):
if not is_managed_by_git():
logging.error("Your site doesn't seem to manage with git")
sys.exit(1)
major_version = get_drush_major_version(config)
if int(major_version) >= 9:
upc_with_composer(args, config)
else:
upc_legacy(args, config)
def upc_with_composer(args, config):
drush = config.get("common", "DrushCommand")
owner = config.get("common", "Owner")
group = config.get("common", "Group")
exclude_files = map(lambda x:x.lstrip().strip(), config.get("common", "ExcludeFiles").split(','))
if args.apply_non_security_update:
logging.warn("--apply-non-security-update is not support by drush 9.x later, so it will be ignore.")
if args.version != False:
logging.warn("--version is not support by drush 9.x later, so it will be ignore.")
with open(os.devnull) as DEVNULL:
security_status = ''
try:
cmd = drush + " pm:security --format=json"
cmd_output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
logging.info("Your site is up to date")
sys.exit(0)
except subprocess.CalledProcessError, e:
# if you have something outdated, drush pm:secrity will return non-zero status code..
security_status = json.loads(e.output[e.output.index("{"):])
modules = security_status.keys()
for module in args.exclude_module.split(","):
if module in modules:
modules.remove(module)
for module in modules:
if args.module != 'all' and args.module != module:
continue
cmd = "composer require \"%(module)s\"" % locals()
if module == 'drupal/core':
cmd = "composer update drupal/core webflo/drupal-core-require-dev --with-dependencies"
subprocess.call(cmd, shell=True, stderr=DEVNULL)
if owner and group:
cmd = "sudo chown -R %(owner)s:%(group)s .git vendor composer.*"
subprocess.call(cmd, shell=True, stderr=DEVNULL)
for exclude_file in exclude_files:
cmd = "git checkout %(exclude_file)s" % locals()
subprocess.call(cmd, shell=True, stderr=DEVNULL)
cmd = "git add -A"
subprocess.call(cmd, shell=True, stdout=DEVNULL, stderr=DEVNULL)
message = []
message.append("%(module)s: update for security\n\n" % locals())
if module == 'drupal/core':
message.append("Drupal core updated, but following files assume unchanged.")
message.append(" - %s" % ", ".join(exclude_files))
message.append("")
message.append("Please check the following article and manually update the git repository if necessary.")
message.append(" - For Drupal 8: https://www.drupal.org/docs/8/update/update-procedure-in-drupal-8")
message = "\n".join(message)
logging.info(message)
cmd = 'git commit -m "%s"' % message
subprocess.call(cmd, shell=True, stdout=DEVNULL, stderr=DEVNULL)
def upc_legacy(args, config):
drush = config.get("common", "DrushCommand")
owner = config.get("common", "Owner")
group = config.get("common", "Group")
exclude_files = map(lambda x:x.lstrip().strip(), config.get("common", "ExcludeFiles").split(','))
apply_non_security_update = args.apply_non_security_update
with open(os.devnull) as DEVNULL:
cmd = drush + " ups --format=json"
if not apply_non_security_update:
cmd = cmd + " --security-only"
update_status = subprocess.check_output(cmd, shell=True, stderr=DEVNULL)
# workaround for "docker-compose run -T" inconsistently outputs stdout / stderr?
# see also: https://github.com/docker/compose/issues/3267
start = update_status.index("{")
json_status = json.loads(update_status[update_status.index("{"):])
modules = json_status.keys()
for module in args.exclude_module.split(","):
if module in modules:
modules.remove(module)
for module in modules:
name = json_status[module]["name"]
if args.module != 'all' and args.module != name:
continue
path = json_status[module]["path"] or '.'
matched_update = False
if args.version == False:
if apply_non_security_update:
matched_update = json_status[module]["releases"][json_status[module]["latest_version"]]
else:
matched_update = json_status[module]["security updates"][0]
else:
for k, v in json_status[module]["releases"].iteritems():
if args.version == k:
matched_update = v
if matched_update == False:
logging.error("%s %s cann't found in releases, please check project page in drupal.org" % (name, args.version))
sys.exit(1)
existing_version = json_status[module]["existing_version"]
version = matched_update["version"]
release_link = matched_update["release_link"]
cmd = drush + " upc -y %(name)s-%(version)s" % locals()
subprocess.call(cmd, shell=True, stderr=DEVNULL)
if owner and group:
cmd = "sudo chown -R %(owner)s:%(group)s .git %(path)s" % locals()
subprocess.call(cmd, shell=True, stderr=DEVNULL)
for exclude_file in exclude_files:
cmd = "git checkout %(exclude_file)s" % locals()
subprocess.call(cmd, shell=True, stderr=DEVNULL)
cmd = "git add -A %(path)s" % locals()
subprocess.call(cmd, shell=True, stdout=DEVNULL, stderr=DEVNULL)
message = []
message.append("%(name)s: update to %(version)s for security\n\n%(release_link)s" % locals())
if module == 'drupal':
message.append("Drupal core updated to %s, but following files assume unchanged." % (version))
message.append(" - %s" % ", ".join(exclude_files))
message.append("")
message.append( "You can see changes at this update in: https://github.com/drupal/drupal/compare/%s...%s" \
% (existing_version, version))
message.append("Please check the following article and manually update the git repository if necessary.")
message.append(" - For Drupal 8: https://www.drupal.org/docs/8/update/update-procedure-in-drupal-8")
message.append(" - For Drupal 7: https://www.drupal.org/docs/7/updating-your-drupal-site/how-to-update-drupal-core")
message = "\n".join(message)
logging.info(message)
cmd = 'git commit -m "%s"' % message
subprocess.call(cmd, shell=True, stdout=DEVNULL, stderr=DEVNULL)
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
parser_upc = subparsers.add_parser('upc', help='Execute drush pm-updatecode and update your git repositoy.')
parser_upc.set_defaults(func=upc)
parser_upc.add_argument('--module', action='store', default='all',
help='module name to update. If omitted, all modules expect core will be updated.')
parser_upc.add_argument('--exclude-module', action='store', default='',
help='module name that to be excluded from update. If multiple, it should be separated by a comma.')
parser_upc.add_argument('--version', action='store', default=False,
help='version of the module to update. If omitted, the latest version will be used.')
parser_upc.add_argument('--apply-non-security-update', action='store_true', default=False,
help="update code even if is hasn't security fixes.")
parser_ups = subparsers.add_parser('ups', help='Execute drush pm-updatestatus.')
parser_ups.set_defaults(func=ups)
parser_ups.add_argument('--include-non-security-update', action='store_true', default=False,
help="show updates even if is hasn't security fixes.")
args = parser.parse_args()
config = ConfigParser.ConfigParser()
config.read([os.path.join(os.path.dirname(os.path.realpath(__file__)), 'drush-scm.ini'), os.path.expanduser('~/.drush-scm/drush-scm.ini')])
args.func(args, config)