奇怪的os.chmod公司行为

2024-10-01 22:41:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python3.2.3编写了一个共享的git存储库创建脚本,虽然该脚本的绝大多数都在工作,并且几乎可以投入生产(仍有一些安全保护需要做),但我遇到了一个尴尬的bug,在我的一生中,我都无法理解。在

在我的迭代更改模式部分:

try:
  if (args.debug):
    print('Recursively changing the access mode of target directory ' + full_path +
          ' to ' oct(stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH) + '.')
  if (args.debug):
    print('setting ' + full_path +
          ' to stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH ')
  os.chmod(full_path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH)
  for root, dirs, files in os.walk(full_path):
    for spam in dirs:
      if (args.debug):
        print('spam in dirs: ' + spam +
              '; stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH: ')
      os.chmod(os.path.join(root, spam), stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH)
    for eggs in files:
      if (args.debug):
        print('eggs in files: ' + eggs +
              '; stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH: ')
      os.chmod(os.path.join(root, eggs), stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH)
except OSError as e:
  if (args.debug):
    print('OSError raised during recursive chmod ' +
          oct(stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH) + ' setting. ' +
          'Removing the partially created repository.')
  shutil.rmtree(full_path)

我遇到了一个问题——没有正确设置我的目录权限。事实上,它的做法完全相反;它剥夺了我所有的目录权限。 运行脚本后,目录树类似于:

^{pr2}$

(setgid位在稍后的工作循环中设置) 显然,我遗漏了一些东西,但我不能确切地说是什么,因为我使用的是stat模块的常量。然后对它们进行位运算,得到我要查找的0o744值。在

在有人建议我使用八进制文字而不是stat模块的常量之前,我已经尝试过了,结果是一样的。在

任何帮助都会很好。 ~米


Tags: pathindebugifosargsspameggs

热门问题