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
|
#!/usr/bin/env python3
"""
Handy Debian nginx module management helper
"""
import argparse
import os
import re
import shutil
import sys
import tarfile
import tempfile
from subprocess import call
import debian.deb822 as deb822
DEBIAN_PATH = os.path.dirname(os.path.realpath(__file__))
MODULES_PATH = os.path.join(DEBIAN_PATH, 'modules')
CTRL_PATH = os.path.join(MODULES_PATH, 'control')
CTRL = [p for p in deb822.Deb822.iter_paragraphs(open(CTRL_PATH))]
def ctrl(name):
"Return deb822 paragraph for the selected module"
global CTRL
for m in CTRL:
if m['Module'] == name:
return m
def update_ctrl():
"Save current control to disk"
global CTRL
new_ctrl = CTRL_PATH + '.new'
with open(new_ctrl, 'w') as c:
for m in CTRL:
c.write(m.dump() + '\n')
os.rename(new_ctrl, CTRL_PATH)
def prompt(query):
"""
Ask the given query and wait for an y/N answer.
:query: The query
:returns: The yes (True) or no (False) answer
"""
sys.stdout.write('%s [y/N]: ' % query)
choice = input().lower()
return choice == 'y'
def commit(module, version):
"""
Git-commit the given module's upgraded source.
:param module: module name
:param version: The updated module version
"""
module_path = os.path.join(MODULES_PATH, module)
git_add_cmd = ['git', 'add', '--all', module_path, CTRL_PATH]
call(git_add_cmd)
commit_msg = '%s: Upgrade to %s' % (module, version)
git_commit_cmd = [
'git', 'commit', '-m', commit_msg,
'--', module_path, CTRL_PATH]
call(git_commit_cmd)
def unpack(module, version, exclude):
"Unpack upstream tar file into module path"
tar_gz = os.path.join(
"..", "libnginx-mod-%s-%s.tar.gz" % (module, version))
if not os.path.exists(tar_gz):
print("%s doesn't exist!" % tar_gz)
exit(1)
module_path = os.path.join(MODULES_PATH, module)
tmpdir = tempfile.mkdtemp(prefix="libnginx-mod-%s-%s." % (module, version))
with tarfile.open(tar_gz) as tar:
members = tar.getmembers()
# stip top-level directory from tar
topdir = members[0]
if not topdir or not topdir.isdir():
print("No top directory found!")
exit(1)
topdir = members[0].path
if any(topdir not in ti.path for ti in members):
print("Not all files are under the top directory '%s'" % topdir)
exit(1)
def remove_topdir_prefix(tarinfo):
"Strip top directory"
tarinfo.path = os.path.relpath(tarinfo.path, topdir)
return tarinfo
members = [remove_topdir_prefix(tarinfo) for tarinfo in members]
tar.extractall(path=tmpdir, members=members)
shutil.rmtree(module_path)
shutil.move(tmpdir, module_path)
for e in exclude:
print("Removing %s..." % e)
abspath = os.path.join(module_path, e)
if os.path.isdir(abspath):
shutil.rmtree(abspath)
else:
try:
os.remove(abspath)
except FileNotFoundError as e:
# if it not exists, ignore it
pass
def cmd_uupdate(args):
"Internal uupdate helper, upgrades & commits the given module"
c = ctrl(args.module)
exclude = []
if 'Files-Excluded' in c:
exclude = c['Files-Excluded'].split()
unpack(args.module, args.upstream_version, exclude)
# change ctrl version
c['Version'] = args.upstream_version
update_ctrl()
commit(args.module, args.upstream_version)
def cmd_uscan(args):
"""
Run uscan for each nginx module listed in debian/modules/control.
If a new version is found, it will be downloaded & unpacked to the proper
debian/modules/MODULE path.
After the upgrade, all files listed in the Files-Excluded: control field
will be removed, debian/modules/control version will be updated and all
changes will be commited to git.
"""
global CTRL
# For every module that has watchfile...
for mi in CTRL:
sys.stdout.write('Uscanning %s: ' % mi['Module'])
sys.stdout.flush()
watchfile = os.path.join(MODULES_PATH, 'watch', mi['Module'])
if not os.path.isfile(watchfile):
print('no watchfile available.')
continue
# Check
uscan_check_cmd = [
'uscan', '--upstream-version', mi['Version'],
'--watchfile', watchfile, '--package',
mi['Module']
]
if call(uscan_check_cmd) != 0:
print('up-to-date')
continue
# Ask
if not prompt('Do you want to upgrade %s?' % mi['Module']):
continue
# Upgrade
uscan_upgrade_cmd = [
'uscan', '--upstream-version', mi['Version'],
'--watchfile', watchfile, '--no-symlink'
]
if call(uscan_upgrade_cmd, stdout=open(os.devnull, 'w')) != 0:
print("Uscan for %s failed!" % mi['Module'])
exit(1)
def main():
"""ngxmod main"""
parser = argparse.ArgumentParser(
description=__doc__)
cmds = parser.add_subparsers(title="Commands")
import textwrap
uscan_cmd = cmds.add_parser(
"uscan",
help="scan/watch upstream nginx module sources for new releases",
description=textwrap.dedent(cmd_uscan.__doc__),
formatter_class=argparse.RawDescriptionHelpFormatter)
uscan_cmd.set_defaults(func=cmd_uscan)
uupdate_cmd = cmds.add_parser(
"uupdate",
help=cmd_uupdate.__doc__,
description=cmd_uupdate.__doc__)
uupdate_cmd.add_argument("module", metavar="MODULE", help="Module name")
uupdate_cmd.add_argument(
"--upstream-version",
required=True,
metavar="VERSION",
help="Upstream version")
uupdate_cmd.set_defaults(func=cmd_uupdate)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()
|