【问题标题】:Python chmod for windows用于 Windows 的 Python chmod
【发布时间】:2020-05-25 03:07:21
【问题描述】:

我正在尝试为 Windows 创建带有替换字体的 Skype 修补程序(股票错误地将日文汉字显示为中文等效)。也许 Python 不是最好的选择,但当前的问题与 Python 本身无关:asar 实用程序产生具有只读权限的输出,而更改权限尝试无能为力。但是,也许 os.chmod 不能在 Windows 上运行?

import tkinter as tk from tkinter import filedialog import subprocess import os import shutil def change_permissions_recursive(path, mode): for root, dirs, files in os.walk(path, topdown=False): for dir in [os.path.join(root,d) for d in dirs]: os.chmod(dir, mode) for file in [os.path.join(root, f) for f in files]: os.chmod(file, mode) root = tk.Tk() root.withdraw() dir_path = filedialog.askdirectory(initialdir="C:\Program Files (x86)\Microsoft\Skype for Desktop") cmd = "asar.cmd extract \"" + dir_path + "/resources/app.asar\" tmp/app" process = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=0x08000000, cwd="utils") process.wait() change_permissions_recursive("utils/tmp/app/fonts", 0o777) shutil.copyfile("utils/meiryo.ttf", "utils/tmp/app/fonts")

【问题讨论】:

  • 你能显示minimal reproducible example吗?我不确定这个问题是否需要 tkinter
  • 而且 PowerShell 或批处理会比 python“更好”,是的
  • codepicky.com/hacking-electron-restyle-skype 我正在关注本指南,但 asar 实用程序输出似乎是用错误的权限编写的。但是,也许这就是shutil.copy本身的错误。会用powershell重写
  • 是的,这与 Python 本身有关:使用 powershell 时一切正常

标签: python file-permissions


【解决方案1】:

shutil.copy 本身有问题:用 powershell 重写相同的逻辑时,一切都按预期工作

【讨论】:

    【解决方案2】:

    是的,chmod 通常不适用于 Windows。

    最好从命令行使用适当的工具:cacls 用于旧版本的 Windows(Windows XP 或更低版本),或icacls 用于大多数版本的 Windows。

    以下代码可以允许更改权限(如果需要,可以使用额外的上下文管理器临时更改):

    """
    Prototype of script to modify access rights with "icacls" in Windows
    """
    from contextlib import contextmanager
    from enum import Enum
    from subprocess import check_output
    from pathlib import Path
    from typing import Generator, List
    
    
    class AccessRight(Enum):
        """Access Rights for files/folders"""
    
        DELETE = "D"
        FULL = "F"  # Edit Permissions + Create + Delete + Read + Write
        NO_ACCESS = "N"
        MODIFY = "M"  # Create + Delete + Read + Write
        READ_EXECUTE = "RX"
        READ_ONLY = "R"
        WRITE_ONLY = "W"
    
    
    @contextmanager
    def set_access_right(file: Path, access: AccessRight) -> Generator[Path, None, None]:
        """Context Manager to temporarily set a given access rights to a file and reset"""
    
        def cmd(access_right: AccessRight) -> List[str]:
            return [
                "icacls",
                str(file),
                "/inheritance:r",
                "/grant:r",
                f"Everyone:{access_right.value}",
            ]
    
        try:
            check_output(cmd(access))
            yield file
    
        finally:
            check_output(cmd(AccessRight.FULL))
    
    
    # We create a file (if it does not exist) with empty content
    toto = Path("toto.txt")
    toto.touch()
    
    # We temporarily set access rights of the
    with set_access_right(toto, AccessRight.WRITE_ONLY) as path:
        path.write_text("My name is Toto")
        try:
            content = path.read_text()
            print(f":( Should not be able to read content of file but read: {content}")
        except PermissionError:
            print("Cannot read toto: YEAH!")
    
    # We check that access rights have been restored (and delete the file to stay clean)
    print(path.read_text())
    toto.unlink()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-20
      • 2020-08-10
      • 2010-10-18
      • 1970-01-01
      • 2017-06-18
      • 2020-12-15
      • 2017-11-16
      • 2021-12-29
      相关资源
      最近更新 更多