You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
589 lines
22 KiB
589 lines
22 KiB
1 year ago
|
#!/usr/bin/env python
|
||
|
"""
|
||
|
Usage:
|
||
|
deploy-minetest-kit <project name> [--from <built dir>] [options]
|
||
|
|
||
|
The available project names are:
|
||
1 year ago
|
classic (or final or minetest) to install ~/minetest-rsync,
|
||
1 year ago
|
finetest (or fine) to install ~/finetest-rsync
|
||
|
(for the game that ripped off Multicraft.org's name), or
|
||
|
trolltest (or troll) to install ~/trolltest-rsync (based on MT5).
|
||
|
|
||
|
Options:
|
||
|
--from <built dir> Install from this directory. Defaults to
|
||
|
"$HOME/linux-minetest-kit-rsync/mtkit/minetest".
|
||
|
--server Require a binary ending with "server" to be
|
||
|
present in built dir. Defaults to False.
|
||
|
--client Require a binary ending with "server" to be
|
||
|
present in built dir. Defaults to True, but False
|
||
|
if --server is used without --client.
|
||
|
"""
|
||
|
from __future__ import print_function
|
||
|
import sys
|
||
|
import platform
|
||
|
import os
|
||
|
import shutil
|
||
|
import tempfile
|
||
|
import stat
|
||
1 year ago
|
import copy
|
||
|
from pprint import pformat
|
||
1 year ago
|
|
||
|
if platform.system() == "Windows":
|
||
|
HOME = os.environ['USERPROFILE']
|
||
|
SHORTCUTS_DIR = os.path.join(HOME, "Desktop")
|
||
|
elif platform.system() == "Darwin":
|
||
|
HOME = os.environ['HOME']
|
||
|
SHORTCUTS_DIR = os.path.join(HOME, "Desktop")
|
||
|
else:
|
||
|
HOME = os.environ['HOME']
|
||
|
SHORTCUTS_DIR = os.path.join(HOME, ".local", "share", "applications")
|
||
|
|
||
|
if sys.version_info.major < 3:
|
||
|
FileNotFoundError = IOError
|
||
|
ModuleNotFoundError = ImportError
|
||
|
|
||
|
|
||
|
INSTALL_SRC = os.path.join(HOME, "linux-minetest-kit-rsync", "mtkit", "minetest")
|
||
1 year ago
|
VARIANT = "rsync"
|
||
1 year ago
|
|
||
|
mode_of_path = {
|
||
|
os.path.join("bin", "finetest"): {
|
||
|
'name': "finetest",
|
||
|
'appender': "",
|
||
|
'executable': "finetest",
|
||
|
},
|
||
|
os.path.join("bin", "finetestserver"): {
|
||
|
'name': "finetest",
|
||
|
'appender': "server",
|
||
|
'executable': "finetestserver",
|
||
|
},
|
||
|
# indeterminate if bin/minetest: same exe for classic & trolltest
|
||
|
}
|
||
|
|
||
1 year ago
|
MINETEST_KEYWORDS = ("sandbox;world;mining;crafting;blocks;nodes;multiplayer;"
|
||
|
"roleplaying;")
|
||
1 year ago
|
|
||
|
project_metas = {
|
||
1 year ago
|
'minetest': { # minetest is the project name (in mtsrc/newline dir)
|
||
1 year ago
|
'shortcut': {
|
||
|
'GenericName': "Final Minetest",
|
||
|
'Keywords': MINETEST_KEYWORDS,
|
||
|
},
|
||
1 year ago
|
'dirname': "minetest",
|
||
1 year ago
|
'name_and_variant_fmt': "Final Minetest ({})",
|
||
1 year ago
|
'name': "Final Minetest",
|
||
1 year ago
|
'shortcut_exe_relpaths': [
|
||
|
os.path.join("bin", "minetest"),
|
||
|
],
|
||
1 year ago
|
'platform_icon_relpath': {
|
||
|
'Linux': os.path.join("misc", "minetest.svg"),
|
||
|
'Darwin': os.path.join("misc", "minetest-icon.icns"),
|
||
|
'Windows': os.path.join("misc", "minetest-icon-24x24.png"),
|
||
|
},
|
||
|
'shortcut_relpath': os.path.join("misc", "net.minetest.minetest.desktop"),
|
||
|
'shortcut_name_noext': "org.minetest.minetest",
|
||
|
},
|
||
|
'finetest': {
|
||
1 year ago
|
'shortcut': {
|
||
|
'GenericName': "Finetest",
|
||
|
'Keywords': MINETEST_KEYWORDS+"minetest;",
|
||
|
},
|
||
1 year ago
|
'dirname': "finetest",
|
||
1 year ago
|
'name_and_variant_fmt': "Finetest ({})",
|
||
1 year ago
|
'name': "Finetest",
|
||
1 year ago
|
'shortcut_exe_relpaths': [
|
||
|
# os.path.join("bin", "multicraft"),
|
||
|
os.path.join("bin", "finetest"),
|
||
|
],
|
||
1 year ago
|
'platform_icon_relpath': {
|
||
|
'Linux': os.path.join("misc", "multicraft-xorg-icon-128.png"),
|
||
|
'Darwin': os.path.join("misc", "minetest-icon.icns"),
|
||
|
'Windows': os.path.join("misc", "multicraft-xorg-icon-128.png"),
|
||
|
},
|
||
|
'shortcut_relpath': os.path.join("misc", "net.minetest.minetest.desktop"),
|
||
|
'shortcut_name_noext': "org.minetest.finetest",
|
||
|
},
|
||
|
'trolltest': {
|
||
1 year ago
|
'shortcut': {
|
||
|
'GenericName': "Trolltest",
|
||
|
'Keywords': MINETEST_KEYWORDS+"minetest;",
|
||
|
},
|
||
1 year ago
|
'dirname': "trolltest",
|
||
1 year ago
|
'name_and_variant_fmt': "Trolltest ({}) (minetest.org)",
|
||
1 year ago
|
'name': "Trolltest (minetest.org)",
|
||
1 year ago
|
'shortcut_exe_relpaths': [
|
||
|
os.path.join("bin", "minetest"),
|
||
|
],
|
||
1 year ago
|
'platform_icon_relpath': {
|
||
|
'Linux': os.path.join("misc", "minetest.svg"),
|
||
|
'Darwin': os.path.join("misc", "minetest-icon.icns"),
|
||
|
'Windows': os.path.join("misc", "minetest-icon-24x24.png"),
|
||
|
},
|
||
|
'shortcut_relpath': os.path.join("misc", "net.minetest.minetest.desktop"),
|
||
|
'shortcut_name_noext': "org.minetest.trolltest",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
|
||
|
def write0(*args):
|
||
|
sys.stderr.write(*args)
|
||
|
sys.stderr.flush()
|
||
|
|
||
|
def echo0(*args):
|
||
|
print(*args, file=sys.stderr)
|
||
|
|
||
|
|
||
|
def usage():
|
||
|
echo0(__doc__)
|
||
|
|
||
|
|
||
|
def main():
|
||
1 year ago
|
required_bin_suffixes = None
|
||
1 year ago
|
# project_info = detect_project_info(INSTALL_SRC)
|
||
|
key_arg = None
|
||
|
install_from = None
|
||
|
if len(sys.argv) < 2:
|
||
|
usage()
|
||
|
# if project_info is None:
|
||
|
echo0("Error: You must specify one of the names above.")
|
||
|
return 1
|
||
|
# TODO: maybe detect it:
|
||
|
# else:
|
||
|
# echo0("using detected project: {}".format(project_info))
|
||
|
elif len(sys.argv) == 2:
|
||
|
pass # 1st arg (arg [1]) is always handled further down
|
||
|
else:
|
||
|
for argi in range(2, len(sys.argv)):
|
||
|
arg = sys.argv[argi]
|
||
|
if key_arg is not None:
|
||
|
if arg.startswith("--"):
|
||
|
usage()
|
||
|
echo0("Error: {} must be followed by a value but got {}."
|
||
|
"".format(key_arg, arg))
|
||
|
return 1
|
||
|
if key_arg == "--from":
|
||
|
install_from = arg
|
||
|
else:
|
||
|
usage()
|
||
|
echo0("Error: unknown argument {}".format(key_arg))
|
||
|
return 1
|
||
|
elif arg == "--server":
|
||
1 year ago
|
if required_bin_suffixes is None:
|
||
|
required_bin_suffixes = ["server"]
|
||
1 year ago
|
else:
|
||
1 year ago
|
required_bin_suffixes.append("server")
|
||
1 year ago
|
elif arg == "--client":
|
||
1 year ago
|
if required_bin_suffixes is None:
|
||
|
required_bin_suffixes = [""]
|
||
1 year ago
|
else:
|
||
1 year ago
|
required_bin_suffixes.append("")
|
||
1 year ago
|
elif arg == "--from":
|
||
|
key_arg = arg
|
||
|
else:
|
||
|
usage()
|
||
|
echo0('Error: The 2nd argument must be "server" or left out')
|
||
|
return 1
|
||
|
if key_arg is not None:
|
||
|
usage()
|
||
|
echo0("Error: {} must be followed by a value."
|
||
|
"".format(key_arg))
|
||
|
return 1
|
||
1 year ago
|
if required_bin_suffixes is None:
|
||
|
required_bin_suffixes = [""] # Always require at least the plain exe name.
|
||
1 year ago
|
if install_from is None:
|
||
|
install_from = INSTALL_SRC
|
||
|
project_name = sys.argv[1]
|
||
|
results = install_minetest(
|
||
|
project_name,
|
||
|
install_from,
|
||
1 year ago
|
required_bin_suffixes=required_bin_suffixes,
|
||
1 year ago
|
)
|
||
1 year ago
|
error = results.get('error')
|
||
|
if error is not None:
|
||
|
echo0("Error: %s" % error)
|
||
1 year ago
|
return 0
|
||
|
|
||
|
|
||
1 year ago
|
def install_minetest(project_name, src, dst=None, variant_dirname=None,
|
||
|
required_bin_suffixes=[""], variant=VARIANT):
|
||
|
"""Install Minetest
|
||
1 year ago
|
|
||
1 year ago
|
Requires globals:
|
||
|
project_meta (dict[string]): The information necessary
|
||
|
to install the program. It must have the keys:
|
||
|
- 'dirname' (string): The directory under the
|
||
|
OS program files.
|
||
|
- There are more required keys for shortcut
|
||
|
generation (See install_shortcut).
|
||
1 year ago
|
|
||
|
Args:
|
||
1 year ago
|
project_name (string): Must be minetest, finetest, or trolltest.
|
||
1 year ago
|
src (string): The location of the minetest install source to
|
||
|
copy.
|
||
|
dst (Optional[string]): Install here. If None, it will become
|
||
1 year ago
|
the default. Defaults to variant_dirname under C:\games on
|
||
1 year ago
|
Windows, otherwise under HOME.
|
||
1 year ago
|
variant_dirname (Optional[string]): Set the install directory
|
||
1 year ago
|
name (ignored if dst is set). If None, it will become the
|
||
1 year ago
|
default. Defaults to project_name + "-" + VARIANT (such as
|
||
|
minetest-rsync). If VARIANT is blank or None, the
|
||
|
variant_dirname will become the same as the dirname
|
||
1 year ago
|
(such as minetest).
|
||
1 year ago
|
required_bin_suffixes (list): Install client if [""], or server if
|
||
|
["server"]. Include both to require that both are in
|
||
|
os.path.join(src, "bin").
|
||
|
variant (string): Append this to the dirname. It also
|
||
|
affects the shortcut--see "variant" under install_shortcut.
|
||
1 year ago
|
|
||
|
Returns:
|
||
|
dict: "destination" is where it was installed if at all. See
|
||
|
"warning" in case there was something incorrect about the
|
||
|
install.
|
||
|
"""
|
||
|
arg = project_name
|
||
1 year ago
|
project_name = arg_to_project_name(arg)
|
||
1 year ago
|
if project_name is None:
|
||
|
usage()
|
||
1 year ago
|
return {
|
||
|
'error': "{} is not a valid project name.".format(arg),
|
||
|
}
|
||
1 year ago
|
src_files = expected_src_files(src, project_name, required_bin_suffixes)
|
||
1 year ago
|
if src_files is None:
|
||
|
usage()
|
||
1 year ago
|
error = "There are no source files for {}".format(project_name)
|
||
|
raise NotImplementedError(error)
|
||
1 year ago
|
|
||
|
missing_files = []
|
||
|
for src_file in src_files:
|
||
|
if not os.path.isfile(src_file):
|
||
|
missing_files.append(src_file)
|
||
|
|
||
|
if len(missing_files) > 0:
|
||
1 year ago
|
error = ("Error: The following files are required to be compiled"
|
||
|
" {} before install but are not present: {}"
|
||
|
"".format(project_name, missing_files))
|
||
|
return {
|
||
|
'error': error,
|
||
|
}
|
||
|
|
||
|
project_meta = project_metas[project_name]
|
||
|
dirname = project_meta['dirname']
|
||
1 year ago
|
variant_dirname = dirname
|
||
|
if (variant is not None) and (len(variant.strip()) > 0):
|
||
|
variant_dirname += "-" + variant
|
||
|
else:
|
||
|
variant = None
|
||
1 year ago
|
|
||
1 year ago
|
|
||
|
if dst is None:
|
||
|
if platform.system() == "Windows":
|
||
|
GAMES = "C:\\games"
|
||
|
if not os.path.isdir(GAMES):
|
||
|
os.mkdir(GAMES)
|
||
1 year ago
|
dst = os.path.join(GAMES, variant_dirname)
|
||
1 year ago
|
else:
|
||
1 year ago
|
dst = os.path.join(HOME, variant_dirname)
|
||
1 year ago
|
warning = None
|
||
1 year ago
|
|
||
|
|
||
1 year ago
|
if not os.path.isdir(dst):
|
||
|
write0('Installing {} to "{}"...'.format(project_name, dst))
|
||
|
shutil.copytree(src, dst)
|
||
|
echo0("Done")
|
||
|
result_path = dst
|
||
|
else:
|
||
|
# Leave result_path as None
|
||
|
warning = 'Skipping installed "{}".'.format(dst)
|
||
|
echo0('WARNING: {}'.format(warning))
|
||
1 year ago
|
for Exec_relpath in project_meta['shortcut_exe_relpaths']:
|
||
|
Exec = os.path.join(dst, Exec_relpath)
|
||
|
sc_results = install_shortcut(Exec, dst, project_meta, variant)
|
||
|
sc_warning = sc_results.get('warning')
|
||
|
if sc_warning is not None:
|
||
|
if warning is not None:
|
||
|
warning += "; " + sc_warning
|
||
|
else:
|
||
|
warning = sc_warning
|
||
|
return {
|
||
|
'dst': dst,
|
||
|
'warning': warning,
|
||
|
}
|
||
1 year ago
|
|
||
|
|
||
1 year ago
|
def install_shortcut(Exec, dst, project_meta, variant):
|
||
|
"""Install a shortcut to any program on any understood platform.
|
||
1 year ago
|
|
||
1 year ago
|
- sc_template_path is determined based on dst and shortcut_relpath
|
||
|
- sc_installed_path (path) is determined from OS and shortcut_name_noext
|
||
|
(and variant if not None).
|
||
|
- sc_template_path is read, Exec string is filled based on dst
|
||
|
(the selected destination where the program is installed)
|
||
|
then the resulting shortcut is saved to sc_installed_path
|
||
|
(only after temp file is complete).
|
||
|
|
||
|
Args:
|
||
|
Exec (string): The executable path where the shortcut
|
||
|
should point.
|
||
|
dst (string): The directory path where the program is
|
||
|
installed.
|
||
|
project_meta (dict): All metadata describing the program.
|
||
|
For this method, it must have the keys:
|
||
|
- 'name': The entire name (except variant) that
|
||
|
should be displayed as the shortcut's caption.
|
||
|
- 'name_and_variant_fmt': Should either be not
|
||
|
present or contain the name and the placeholder
|
||
|
"{}" where the variant should go. If not present,
|
||
|
" " and variant will be added to the end of Name.
|
||
|
- 'shortcut' (dict): contains:
|
||
|
- 'GenericName' (Optional[string]): A simplified
|
||
|
name for the program. If None, the GenericName
|
||
|
line will be removed from the shortcut. This
|
||
|
option is only for GNU/Linux systems or other
|
||
|
systems using XDG.
|
||
|
- 'Keywords' (Optional[string]): If None, Keywords
|
||
|
line will be removed from the shortcut. This
|
||
|
option is only for GNU/Linux systems or other
|
||
|
systems using XDG.
|
||
|
- 'shortcut_relpath': The location of an existing
|
||
|
shortcut file to use and modify.
|
||
|
- 'platform_icon_relpath' (dict[string]): A dict
|
||
|
where the key is platform.system() (Must have
|
||
|
at least 'Linux', 'Windows', *AND* 'Darwin')
|
||
|
and the value is the relative path from
|
||
|
dst to the icon image file.
|
||
|
variant (string): The special string to put in parenthesis
|
||
|
after the name to denote what kind of package or source was
|
||
|
used to obtain the program, such as "rsync" if a local
|
||
|
custom build, or more commonly "git", "deb", etc. If it is
|
||
|
an official binary archive, set this to "release". However,
|
||
|
if the package type (such as deb) is native to your distro,
|
||
|
set this to None to indicate it is the package supported
|
||
|
for your distro.
|
||
|
- Name is constructed using
|
||
|
project_meta['name_and_variant_fmt'] if present, otherwise
|
||
|
Name will be project_meta['name] + " (" + 'variant' + ")".
|
||
|
If variant is None, name is project_meta['name'].
|
||
|
Raises:
|
||
|
FileNotFoundError: If src does not exist.
|
||
|
"""
|
||
|
warning = None
|
||
1 year ago
|
Name = project_meta['name']
|
||
1 year ago
|
if variant is not None:
|
||
|
name_and_variant_fmt = project_meta.get('name_and_variant_fmt')
|
||
|
if name_and_variant_fmt is not None:
|
||
|
Name = name_and_variant_fmt.format(variant)
|
||
|
else:
|
||
|
Name += " (" + project_meta['variant'] + ")" # raise if None
|
||
|
platform_icon_relpath = project_meta.get('platform_icon_relpath')
|
||
|
icon_relpath = None
|
||
|
if platform_icon_relpath is not None:
|
||
|
icon_relpath = platform_icon_relpath.get(platform.system())
|
||
1 year ago
|
if icon_relpath is None:
|
||
|
raise NotImplementedError(
|
||
|
"There is no platform icon for {}.".format(platform.system())
|
||
|
)
|
||
|
Icon = os.path.join(dst, icon_relpath)
|
||
1 year ago
|
shortcut_meta = copy.deepcopy(project_meta.get('shortcut'))
|
||
|
shortcut_meta['Name'] = Name
|
||
|
shortcut_meta['Exec'] = Exec
|
||
|
shortcut_meta['Icon'] = Icon
|
||
|
# ^ rewrite_conf *removes* any lines where value is None
|
||
1 year ago
|
|
||
|
if platform.system() == "Linux":
|
||
1 year ago
|
sc_template_path = os.path.join(dst, project_meta['shortcut_relpath'])
|
||
1 year ago
|
shortcut_name = project_meta['shortcut_name_noext'] + ".desktop"
|
||
1 year ago
|
sc_installed_path = os.path.join(SHORTCUTS_DIR, shortcut_name)
|
||
1 year ago
|
if not os.path.isdir(SHORTCUTS_DIR):
|
||
|
os.makedirs(SHORTCUTS_DIR) # default mode is 511
|
||
1 year ago
|
write0('Installing icon to "{}"...'.format(sc_installed_path))
|
||
1 year ago
|
rewrite_conf(
|
||
1 year ago
|
sc_template_path,
|
||
|
sc_installed_path,
|
||
|
changes=shortcut_meta,
|
||
1 year ago
|
)
|
||
|
echo0("OK")
|
||
|
elif platform.system() == "Darwin":
|
||
|
shortcut_name = Name + ".command"
|
||
1 year ago
|
sc_installed_path = os.path.join(SHORTCUTS_DIR, shortcut_name)
|
||
|
with open(sc_installed_path) as stream:
|
||
1 year ago
|
stream.write('"%s"\n' % Exec)
|
||
|
# ^ Run the game & close Command Prompt immediately.
|
||
|
# ^ First arg is Command Prompt title, so leave it blank.
|
||
1 year ago
|
st = os.stat(sc_installed_path)
|
||
|
os.chmod(sc_installed_path, st.st_mode | stat.S_IXUSR)
|
||
1 year ago
|
# ^ same as stat.S_IEXEC: "Unix V7 synonym for S_IXUSR."
|
||
|
elif platform.system() == "Windows":
|
||
|
shortcut_name = Name + ".bat"
|
||
1 year ago
|
sc_installed_path = os.path.join(SHORTCUTS_DIR, shortcut_name)
|
||
|
with open(sc_installed_path) as stream:
|
||
1 year ago
|
stream.write('start "" "%s"\n' % Exec)
|
||
|
# ^ Run the game & close Command Prompt immediately.
|
||
|
# ^ First arg is Command Prompt title, so leave it blank.
|
||
|
else:
|
||
1 year ago
|
warning = ("Icon install isn't implemented for {}."
|
||
|
"".format(platform.system()))
|
||
1 year ago
|
return {
|
||
|
"warning": warning, # may be None
|
||
|
"destination": dst,
|
||
|
}
|
||
|
|
||
|
|
||
|
# def get_missing_subs(mt_share_path, subs):
|
||
|
# """Get a list of any missing files for a source *or* destination.
|
||
|
# """
|
||
|
|
||
|
|
||
1 year ago
|
def arg_to_project_name(arg):
|
||
1 year ago
|
if arg == "final":
|
||
1 year ago
|
return "minetest"
|
||
1 year ago
|
elif arg == "classic":
|
||
1 year ago
|
return "minetest"
|
||
|
elif arg == "minetest":
|
||
|
return "minetest"
|
||
1 year ago
|
elif arg == "trolltest":
|
||
|
return "trolltest"
|
||
|
elif arg == "troll":
|
||
|
return "trolltest"
|
||
|
elif arg == "finetest":
|
||
|
return "finetest"
|
||
|
elif arg == "fine":
|
||
|
return "finetest"
|
||
|
return None
|
||
|
|
||
|
|
||
|
def detect_project_info(mt_share_path):
|
||
|
"""Detect the project name from a source *or* destination.
|
||
|
"""
|
||
|
for sub, project_info in mode_of_path.items():
|
||
|
sub_path = os.path.join(mt_share_path, sub)
|
||
|
if os.path.isfile(sub_path):
|
||
|
return project_info
|
||
|
return None
|
||
|
|
||
|
|
||
1 year ago
|
def expected_src_files(src, project_name, required_bin_suffixes=[""]):
|
||
1 year ago
|
"""Get full paths of required files.
|
||
|
|
||
|
Args:
|
||
1 year ago
|
required_bin_suffixes (list): Usually either [""] or ["server"], but if
|
||
1 year ago
|
the list contains both, both will be required
|
||
|
|
||
|
Returns:
|
||
|
list: Files that are required for install to continue.
|
||
|
"""
|
||
1 year ago
|
if ((required_bin_suffixes is None) or (len(required_bin_suffixes) < 1)
|
||
|
or (not isinstance(required_bin_suffixes, list))):
|
||
|
required_bin_suffixes = [""]
|
||
|
echo0('Warning: reverting to required_bin_suffixes=[""] since {}'
|
||
|
''.format(required_bin_suffixes))
|
||
1 year ago
|
|
||
|
src_files = None
|
||
|
|
||
1 year ago
|
for appender in required_bin_suffixes:
|
||
1 year ago
|
src_file = None
|
||
1 year ago
|
if project_name == "minetest":
|
||
1 year ago
|
src_file = os.path.join(src, "bin", "minetest"+appender)
|
||
|
elif project_name == "trolltest":
|
||
|
src_file = os.path.join(src, "bin", "minetest"+appender)
|
||
|
elif project_name == "finetest":
|
||
|
src_file = os.path.join(src, "bin", "finetest"+appender)
|
||
|
else:
|
||
|
break
|
||
|
if src_file is not None:
|
||
|
if src_files is None:
|
||
|
src_files = []
|
||
|
src_files.append(src_file)
|
||
|
# else caller must say project_name is not valid.
|
||
|
return src_files
|
||
|
|
||
|
|
||
|
def rewrite_conf(src, dst, changes={}):
|
||
|
"""Install a conf such as an XDG desktop shortcut with changes.
|
||
|
|
||
|
Args:
|
||
|
src (string): The conf file to read.
|
||
|
dst (string): The conf file to write or overwrite.
|
||
|
changes (dict): A set of values to change by name. For any value
|
||
|
that is None, the line will be removed!
|
||
|
"""
|
||
|
# This function is redefined further down in the case of Python 2.
|
||
|
fd, path = tempfile.mkstemp()
|
||
|
try:
|
||
|
with os.fdopen(fd, 'wb') as tmp:
|
||
|
# ^ ensure still exists when moving
|
||
|
write0("Generating temporary icon %s..." % path)
|
||
|
# NOTE: tmp.name is just some number (int)!
|
||
|
with open(src, "rb") as stream:
|
||
|
for rawL in stream:
|
||
|
signI = rawL.find(b'=')
|
||
|
# commentI = rawl.find(b'#')
|
||
|
if rawL.strip().startswith(b"#"):
|
||
|
tmp.write(rawL)
|
||
|
continue
|
||
|
if rawL.strip().startswith(b"["):
|
||
|
tmp.write(rawL)
|
||
|
continue
|
||
|
if signI < 0:
|
||
|
tmp.write(rawL)
|
||
|
continue
|
||
|
key_bytes = rawL[:signI].strip()
|
||
|
key = key_bytes.decode("utf-8")
|
||
|
value = changes.get(key)
|
||
|
if key not in changes:
|
||
|
# The value wasn't changed so write it as-is
|
||
|
# echo0("%s not in %s" % (key, changes))
|
||
|
tmp.write(rawL)
|
||
|
continue
|
||
|
if value is None:
|
||
|
echo0("%s was excluded from the icon" % key)
|
||
|
continue
|
||
|
line = "%s=%s\n" % (key, value)
|
||
|
tmp.write(line.encode("utf-8"))
|
||
|
shutil.copy(path, dst)
|
||
|
finally:
|
||
|
write0("removing tmp file...")
|
||
|
os.remove(path)
|
||
|
|
||
|
|
||
|
if sys.version_info.major < 3:
|
||
|
# Python 2 (strings are bytes)
|
||
|
def rewrite_conf(src, dst, changes={}):
|
||
|
"""Install a conf such as an XDG desktop shortcut with changes.
|
||
|
"""
|
||
|
fd, path = tempfile.mkstemp()
|
||
|
try:
|
||
|
with os.fdopen(fd, 'wb') as tmp:
|
||
|
write0("Generating temporary icon %s..." % path)
|
||
|
with open(src, "rb") as stream:
|
||
|
for rawL in stream:
|
||
|
signI = rawL.find('=')
|
||
|
# commentI = rawl.find('#')
|
||
|
if rawL.strip().startswith("#"):
|
||
|
tmp.write(rawL)
|
||
|
continue
|
||
|
if rawL.strip().startswith("["):
|
||
|
tmp.write(rawL)
|
||
|
continue
|
||
|
if signI < 0:
|
||
|
tmp.write(rawL)
|
||
|
continue
|
||
|
key_bytes = rawL[:signI].strip()
|
||
|
key = key_bytes
|
||
|
value = changes.get(key)
|
||
|
if key not in changes:
|
||
|
# The value wasn't changed so write it as-is
|
||
|
tmp.write(rawL)
|
||
|
continue
|
||
|
if value is None:
|
||
|
echo0("%s was excluded from the icon" % key)
|
||
|
continue
|
||
|
line = "%s=%s\n" % (key, value)
|
||
|
tmp.write(line)
|
||
|
shutil.copy(path, dst)
|
||
|
finally:
|
||
|
write0("removing tmp file...")
|
||
|
os.remove(path)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
sys.exit(main())
|