Commit 070560d6 authored by rgaudin's avatar rgaudin
Browse files

Various fixes for windows/py3.4

using mkdtemp as NamedTemporaryFile can't be reopened on windows
saving files with UTF-8 encoding
removing commas after **kwargs
removing f-strings
using universal_newlines instead of text with subprocess
parent 3efef594
......@@ -6,8 +6,10 @@
* system locale to en_US (was en_GB*)
* Updated kiwix-serve to 3.0.1
* Updated to raspbian 2019-07-10 (buster)
* Base image 1GB lighter to download
* Added support for Pi4
* WPA password must be at least 8chars.
* Fixed bug for ZIMs with UTF-8 title/desc on windows
* Changed data partition label to HSPOT_DATA
* Internal scripts (captive_portal, time) to use python3
* Tested on the following revisions:
......
......@@ -26,7 +26,7 @@ def run(machine, tags, extra_vars={}, secret_keys=[]):
# save extra_vars to a file on guest
extra_vars_path = posixpath.join(ansiblecube_path, "extra_vars.json")
with tempfile.NamedTemporaryFile("w", delete=False) as fp:
with tempfile.NamedTemporaryFile("w", delete=False, encoding="utf-8") as fp:
json.dump(extra_vars, fp, indent=4)
fp.close()
machine.put_file(fp.name, extra_vars_path)
......@@ -194,7 +194,9 @@ def run_phase_one(
# save YAML catalogs into local files inside VM for use by ideascube
for index, catalog in enumerate(CATALOGS):
with tempfile.NamedTemporaryFile(suffix=".yml", delete=False) as fd:
with tempfile.NamedTemporaryFile(
suffix=".yml", delete=False, encoding="utf-8"
) as fd:
yaml.safe_dump(
get_catalogs(machine._logger)[index],
fd,
......
......@@ -3,6 +3,7 @@
import os
import random
import shutil
import tempfile
import yaml
......@@ -25,14 +26,15 @@ def fetch_catalogs(logger):
catalogs = []
logger.std("downloading catalogs...")
try:
tmp_dir = tempfile.mkdtemp()
for catalog in CATALOGS:
tmpfile = tempfile.NamedTemporaryFile(suffix=".yml", delete=False)
tmpfile.close()
dlf = download_file(catalog.get("url"), tmpfile.name, logger)
tmp_fpath = os.path.join(tmp_dir, "{}.catalog".format(catalog["name"]))
dlf = download_file(catalog.get("url"), tmp_fpath, logger)
if dlf.successful:
with open(tmpfile.name, "r") as fp:
with open(tmp_fpath, "r") as fp:
catalogs.append(yaml.load(fp.read()))
os.unlink(tmpfile.name)
os.unlink(tmp_fpath)
else:
raise ValueError("Unable to download {}".format(catalog.get("url")))
......@@ -53,6 +55,8 @@ def fetch_catalogs(logger):
logger.err("Catalog format is not valid")
catalogs.pop() # remove catalog from list
break
shutil.rmtree(tmp_dir, ignore_errors=True)
except Exception as exp:
logger.err("Exception while downloading/parsing catalogs: {}".format(exp))
return None
......
......@@ -130,7 +130,7 @@ def download_file(url, fpath, logger, checksum=None):
as it can be multiple files) """
output_dir, fname = os.path.split(fpath)
args = [aria2_exe, f"--dir={output_dir}", f"--out={fname}"]
args = [aria2_exe, "--dir={}".format(output_dir), "--out={}".format(fname)]
args += [
"--connect-timeout=60",
"--max-file-not-found=5",
......@@ -151,7 +151,9 @@ def download_file(url, fpath, logger, checksum=None):
args += ["--http-accept-gzip=true"] # only for catalog?
args += [url]
aria2c = subprocess.Popen(args, stdout=subprocess.PIPE, text=True)
aria2c = subprocess.Popen(
args, stdout=subprocess.PIPE, universal_newlines=True, **startup_info_args()
)
# logger.std(" ".join(args))
if not logger.on_tty:
......@@ -293,7 +295,7 @@ def win_unarchive_compressed_tar_pipe(archive_fpath, dest_folder, logger):
[szip_exe, "x", "-so", archive_fpath],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
**startup_info_args(),
**startup_info_args()
)
logger.std("Call: " + str(uncompress.args))
......@@ -303,7 +305,7 @@ def win_unarchive_compressed_tar_pipe(archive_fpath, dest_folder, logger):
stdin=uncompress.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
**startup_info_args(),
**startup_info_args()
)
logger.std("Call: " + str(untar.args))
......
......@@ -37,7 +37,9 @@ jinja_env.filters["language_bidi"] = language_is_bidirectional
def save_homepage(html):
with tempfile.NamedTemporaryFile("w", delete=False, suffix=".html") as fp:
with tempfile.NamedTemporaryFile(
"w", delete=False, suffix=".html", encoding="utf-8"
) as fp:
fp.write(html)
fp.close()
return fp.name
......
......@@ -361,7 +361,9 @@ def get_etcher_command(image_fpath, device_fpath, logger, from_cli):
# on macOS, GUI sudo captures stdout so we use a log file
log_to_file = not from_cli and sys.platform == "darwin"
if log_to_file:
log_file = tempfile.NamedTemporaryFile(suffix=".log", delete=False)
log_file = tempfile.NamedTemporaryFile(
suffix=".log", delete=False, encoding="utf-8"
)
else:
log_file = None
......
......@@ -1355,7 +1355,7 @@ class Application:
else "{}.json".format(dialog.get_filename())
)
try:
with open(filename, "w") as fd:
with open(filename, "w", encoding="utf-8") as fd:
json.dump(self.get_config(), fd, indent=4)
except Exception:
self.display_error_message(
......
......@@ -586,7 +586,7 @@ def save_prefs(prefs, auto_reload=True):
""" save a passed preferences dict to the preferences file on disk """
fpath = get_prefs_path()
try:
with open(fpath, "w") as fd:
with open(fpath, "w", encoding="utf-8") as fd:
json.dump(prefs, fd, indent=4)
except Exception as exp:
print("Failed to save prefs to {}: {}".format(fpath, exp))
......
......@@ -688,7 +688,7 @@ and digits '0' to '9', space and hyphen '-'</property>
<object class="GtkLabel" id="no_id_23">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Must contains only 0 to 31 ASCII letters 'A' to 'z',
<property name="label" translatable="yes">Must contains only 8 to 31 ASCII letters 'A' to 'z',
digits '0' to '9', hyphen '-' and underscore '_'</property>
<attributes>
<attribute name="foreground" value="#efef29292929"/>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment