make install.py py3 compatible

This commit is contained in:
Andrey Grozin 2020-02-08 22:32:18 +07:00
parent c32c314d44
commit a9ed7ec430
1 changed files with 39 additions and 37 deletions

View File

@ -29,7 +29,7 @@ app_path = sys.argv[0]
# print a message to stderr # print a message to stderr
def logError(s): def logError(s):
sys.stderr.write('%s: %s\n' % (app_path, s)) sys.stderr.write(f'{app_path}: {s}\n')
# this install script should not be used on Windows # this install script should not be used on Windows
if os.name == 'nt': if os.name == 'nt':
@ -51,7 +51,7 @@ files_only = False
# process --help option # process --help option
if len(sys.argv) == 2 and sys.argv[1] == '--help': if len(sys.argv) == 2 and sys.argv[1] == '--help':
print """Usage: %s [OPTION...] print(f"""Usage: {app_path} [OPTION...]
Install or remove Diffuse. Install or remove Diffuse.
@ -64,30 +64,30 @@ Options:
--destdir=PATH --destdir=PATH
path to the installation's root directory path to the installation's root directory
default: %s default: {options['destdir']}
--prefix=PATH --prefix=PATH
common installation prefix for files common installation prefix for files
default: %s default: {options['prefix']}
--sysconfdir=PATH --sysconfdir=PATH
directory for installing read-only single-machine data directory for installing read-only single-machine data
default: %s default: {options['sysconfdir']}
--examplesdir=PATH --examplesdir=PATH
directory for example configuration files directory for example configuration files
default: %s default: {options['examplesdir']}
--mandir=PATH --mandir=PATH
directory for man pages directory for man pages
default: %s default: {options['mandir']}
--pythonbin=PATH --pythonbin=PATH
command for python interpreter command for python interpreter
default: %s default: {options['pythonbin']}
--files-only --files-only
only install/remove files; skip the post install/removal tasks""" % (app_path, options['destdir'], options['prefix'], options['sysconfdir'], options['examplesdir'], options['mandir'], options['pythonbin']) only install/remove files; skip the post install/removal tasks""")
sys.exit(0) sys.exit(0)
# returns the list of components used in a path # returns the list of components used in a path
@ -126,7 +126,7 @@ def removeFile(f):
try: try:
os.unlink(f) os.unlink(f)
except OSError: except OSError:
logError('Error removing "%s".' % (f, )) logError(f'Error removing "{f}".')
# install/remove sets of files # install/remove sets of files
def processFiles(install, dst, src, template): def processFiles(install, dst, src, template):
@ -141,13 +141,13 @@ def processFiles(install, dst, src, template):
f.close() f.close()
if v is not None: if v is not None:
c = replace(c, v) c = replace(c, v)
print 'Installing %s' % (d, ) print(f'Installing {d}')
f = open(d, 'wb') f = open(d, 'wb')
f.write(c) f.write(c)
f.close() f.close()
if k == 'bin/diffuse': if k == 'bin/diffuse':
# turn on the execute bits # turn on the execute bits
os.chmod(d, 0755) os.chmod(d, 0o755)
else: else:
# remove file # remove file
removeFile(d) removeFile(d)
@ -156,16 +156,16 @@ def processFiles(install, dst, src, template):
def processTranslations(install, dst): def processTranslations(install, dst):
for s in glob.glob('translations/*.po'): for s in glob.glob('translations/*.po'):
lang = s[13:-3] lang = s[13:-3]
d = os.path.join(dst, 'share/locale/%s/LC_MESSAGES/diffuse.mo' % (lang, )) d = os.path.join(dst, f'share/locale/{lang}/LC_MESSAGES/diffuse.mo')
if install: if install:
# install file # install file
try: try:
print 'Installing %s' % (d, ) print(f'Installing {d}')
createDirs(os.path.dirname(d)) createDirs(os.path.dirname(d))
if subprocess.Popen(['msgfmt', '-o', d, s]).wait() != 0: if subprocess.Popen(['msgfmt', '-o', d, s]).wait() != 0:
raise OSError() raise OSError()
except OSError: except OSError:
logError('WARNING: Failed to compile "%s" localisation.' % (lang, )) logError(f'WARNING: Failed to compile "{lang}" localisation.')
else: else:
# remove file # remove file
removeFile(d) removeFile(d)
@ -178,19 +178,19 @@ for arg in sys.argv[1:]:
files_only = True files_only = True
else: else:
for opt in options.keys(): for opt in options.keys():
key = '--%s=' % (opt, ) key = f'--{opt}='
if arg.startswith(key): if arg.startswith(key):
options[opt] = arg[len(key):] options[opt] = arg[len(key):]
break break
else: else:
logError('Unknown option "%s".' % (arg, )) logError(f'Unknown option "{arg}".')
sys.exit(1) sys.exit(1)
# expand variables # expand variables
for s in 'sysconfdir', 'examplesdir', 'mandir': for s in 'sysconfdir', 'examplesdir', 'mandir':
for k in 'prefix', 'sysconfdir': for k in 'prefix', 'sysconfdir':
if s != k: if s != k:
options[s] = options[s].replace('${%s}' % (k, ), options[k]) options[s] = options[s].replace(f'${{{k}}}', options[k])
# validate inputs # validate inputs
if options['destdir'] == '': if options['destdir'] == '':
@ -199,7 +199,7 @@ for opt in 'prefix', 'sysconfdir', 'examplesdir', 'mandir':
p = options[opt] p = options[opt]
c = components(p) c = components(p)
if os.pardir in c or os.curdir in c: if os.pardir in c or os.curdir in c:
logError('Bad value for option "%s".' % (opt, )) logError(f'Bad value for option "{opt}".')
sys.exit(1) sys.exit(1)
c.insert(0, '') c.insert(0, '')
c.append('') c.append('')
@ -217,32 +217,34 @@ if install:
stage = 'install' stage = 'install'
else: else:
stage = 'removal' stage = 'removal'
print '''Performing %s with: print(f'''Performing {stage} with:
destdir=%s destdir={destdir}
prefix=%s prefix={prefix}
sysconfdir=%s sysconfdir={sysconfdir}
examplesdir=%s examplesdir={examplesdir}
mandir=%s mandir={mandir}
pythonbin=%s''' % (stage, destdir, prefix, sysconfdir, examplesdir, mandir, pythonbin) pythonbin={pythonbin}''')
# install files to prefix # install files to prefix
processFiles(install, os.path.join(destdir, prefix[1:]), 'src/usr/', { processFiles(install, os.path.join(destdir, prefix[1:]), 'src/usr/', {
'bin/diffuse': [ ("'../../etc/diffuserc'", repr(relpath(os.path.join(prefix, 'bin'), os.path.join(sysconfdir, 'diffuserc')))), ('/usr/bin/env python', pythonbin) ], 'bin/diffuse': [ (b"'../../etc/diffuserc'", repr(relpath(os.path.join(prefix, 'bin'), os.path.join(sysconfdir, 'diffuserc'))).encode()),
(b'/usr/bin/env python', pythonbin.encode()) ],
'share/applications/diffuse.desktop': None, 'share/applications/diffuse.desktop': None,
'share/diffuse/syntax/*.syntax': None, 'share/diffuse/syntax/*.syntax': None,
'share/gnome/help/diffuse/*/diffuse.xml': [ ('/usr/', prefix), ('/etc/', sysconfdir) ], 'share/gnome/help/diffuse/*/diffuse.xml': [ (b'/usr/', prefix.encode()), (b'/etc/', sysconfdir.encode()) ],
'share/omf/diffuse/diffuse-*.omf': [ ('/usr/', prefix) ], 'share/omf/diffuse/diffuse-*.omf': [ (b'/usr/', prefix.encode()) ],
'share/icons/hicolor/*/apps/diffuse.png': None 'share/icons/hicolor/*/apps/diffuse.png': None
}) })
# install manual # install manual
processFiles(install, os.path.join(destdir, mandir[1:]), 'src/usr/share/man/', { processFiles(install, os.path.join(destdir, mandir[1:]), 'src/usr/share/man/', {
'man1/diffuse.1': [ ('/usr/', prefix), ('/etc/', sysconfdir) ], 'man1/diffuse.1': [ (b'/usr/', prefix.encode()), (b'/etc/', sysconfdir.encode()) ],
'*/man1/diffuse.1': [ ('/usr/', prefix), ('/etc/', sysconfdir) ] '*/man1/diffuse.1': [ (b'/usr/', prefix.encode()), (b'/etc/', sysconfdir.encode()) ]
}) })
# install files to sysconfdir # install files to sysconfdir
processFiles(install, os.path.join(destdir, examplesdir[1:]), 'src/etc/', { 'diffuserc': [ ('/etc/', sysconfdir), ('../usr', relpath(sysconfdir, prefix)) ] }) processFiles(install, os.path.join(destdir, examplesdir[1:]), 'src/etc/', { 'diffuserc': [ (b'/etc/', sysconfdir.encode()),
(b'../usr', relpath(sysconfdir, prefix).encode()) ] })
# install translations # install translations
processTranslations(install, os.path.join(destdir, prefix[1:])) processTranslations(install, os.path.join(destdir, prefix[1:]))
@ -254,11 +256,11 @@ if not install:
try: try:
os.rmdir(d) os.rmdir(d)
except OSError: except OSError:
logError('Error removing "%s".' % (d, )) logError(f'Error removing "{d}".')
# do post install/removal tasks # do post install/removal tasks
if not files_only: if not files_only:
print 'Performing post %s tasks.' % (stage, ) print(f'Performing post {stage} tasks.')
cmds = [ [ 'update-desktop-database' ], cmds = [ [ 'update-desktop-database' ],
[ 'gtk-update-icon-cache', os.path.join(destdir, os.path.join(prefix, 'icons/hicolor')[1:]) ] ] [ 'gtk-update-icon-cache', os.path.join(destdir, os.path.join(prefix, 'icons/hicolor')[1:]) ] ]
@ -269,12 +271,12 @@ if not files_only:
for c in cmds: for c in cmds:
for p in os.environ['PATH'].split(os.pathsep): for p in os.environ['PATH'].split(os.pathsep):
if os.path.exists(os.path.join(p, c[0])): if os.path.exists(os.path.join(p, c[0])):
print ' '.join(c) print(' '.join(c))
try: try:
if subprocess.Popen(c).wait() != 0: if subprocess.Popen(c).wait() != 0:
raise OSError() raise OSError()
except OSError: except OSError:
logError('WARNING: Failed to update documentation database with %s.' % (c[0], )) logError(f'WARNING: Failed to update documentation database with {c[0]}.')
break break
else: else:
print 'WARNING: %s is not installed' % (c[0], ) print(f'WARNING: {c[0]} is not installed')