Skip to content

Commit dd35c85

Browse files
committed
Use isolated temporary directory instead of a system's (fixes "Permission denied" on Windows)
1 parent 6bde0ed commit dd35c85

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

pioinstaller/pack/packer.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,28 @@ def pack(target):
3636
if not os.path.isdir(os.path.dirname(target)):
3737
os.makedirs(os.path.dirname(target))
3838

39-
tmpdir = tempfile.mkdtemp()
40-
create_wheels(os.path.dirname(util.get_source_dir()), tmpdir)
39+
tmp_dir = tempfile.mkdtemp()
40+
create_wheels(os.path.dirname(util.get_source_dir()), tmp_dir)
4141

4242
new_data = io.BytesIO()
43-
for whl in os.listdir(tmpdir):
44-
with zipfile.ZipFile(os.path.join(tmpdir, whl)) as existing_zip:
43+
for whl in os.listdir(tmp_dir):
44+
with zipfile.ZipFile(os.path.join(tmp_dir, whl)) as existing_zip:
4545
with zipfile.ZipFile(new_data, mode="a") as new_zip:
4646
for zinfo in existing_zip.infolist():
4747
if re.search(r"\.dist-info/", zinfo.filename):
4848
continue
4949
new_zip.writestr(zinfo, existing_zip.read(zinfo))
5050
zipdata = base64.b64encode(new_data.getvalue()).decode("utf8")
5151
with open(target, "w") as fp:
52-
with open(
53-
os.path.join(util.get_source_dir(), "pack", "template.py"), "r"
54-
) as fp_template:
55-
fp.write(
56-
fp_template.read().format(
57-
installed_version="latest", zipfile_content=zipdata,
58-
),
59-
)
52+
with open(os.path.join(util.get_source_dir(), "pack", "template.py")) as fptlp:
53+
fp.write(fptlp.read().format(zipfile_content=zipdata))
6054

6155
# Ensure the permissions on the newly created file
6256
oldmode = os.stat(target).st_mode & 0o7777
6357
newmode = (oldmode | 0o555) & 0o7777
6458
os.chmod(target, newmode)
6559

6660
# Clearing up
67-
shutil.rmtree(tmpdir)
61+
shutil.rmtree(tmp_dir)
6862

6963
return target

pioinstaller/pack/template.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,43 @@
2525
"""
2626

2727

28+
def create_temp_dir():
29+
try:
30+
cur_dir = os.path.dirname(os.path.realpath(__file__))
31+
tmp_dir = tempfile.mkdtemp(dir=cur_dir, prefix=".piocore-installer-")
32+
testscript_path = os.path.join(tmp_dir, "test.py")
33+
with open(testscript_path, "w") as fp:
34+
fp.write("print(1)")
35+
assert os.path.isfile(testscript_path)
36+
os.remove(testscript_path)
37+
return tmp_dir
38+
except (AssertionError, NameError):
39+
pass
40+
return tempfile.mkdtemp()
41+
42+
2843
def bootstrap():
2944
import pioinstaller.__main__
3045

3146
pioinstaller.__main__.main()
3247

3348

3449
def main():
35-
tmpdir = None
50+
runtime_tmp_dir = create_temp_dir()
51+
os.environ["TMPDIR"] = runtime_tmp_dir
52+
tmp_dir = tempfile.mkdtemp(dir=runtime_tmp_dir)
3653
try:
37-
tmpdir = tempfile.mkdtemp()
38-
39-
pioinstaller_zip = os.path.join(tmpdir, "pioinstaller.zip")
54+
pioinstaller_zip = os.path.join(tmp_dir, "pioinstaller.zip")
4055
with open(pioinstaller_zip, "wb") as fp:
4156
fp.write(b64decode(DEPENDENCIES))
4257

4358
sys.path.insert(0, pioinstaller_zip)
4459

4560
bootstrap()
4661
finally:
47-
if tmpdir:
48-
shutil.rmtree(tmpdir, ignore_errors=True)
62+
for d in (runtime_tmp_dir, tmp_dir):
63+
if d and os.path.isdir(d):
64+
shutil.rmtree(d, ignore_errors=True)
4965

5066

5167
if __name__ == "__main__":

0 commit comments

Comments
 (0)