跳转到主要内容

基于模式的简单独立实用脚本,用于管理文件和目录树的*nix权限。

项目描述

permset
=======

基于模式的简单独立实用脚本,用于管理文件和目录树的*nix权限。


安装
------------
作为独立脚本 - 前往某个可见路径的目录并执行

wget https://raw.github.com/trebor74hr/permset/master/permset/base.py -O permset && chmod u+x permset

或者作为Python包使用[pip](http://www.pip-installer.org/en/latest/)

pip install permset

试试看

permset

有关安装选项的更多详细信息请参阅页面底部。

请自行承担风险使用 - 此软件仍处于alpha阶段*

许可证和免责声明在[LICENCE](https://github.com/trebor74hr/permset/blob/master/LICENCE)文件中。


需求
------------

* *nix (linux, osx, bsd, unix ...).
* Python 2.6+ (使用json模块)

用法
-----

用法

permset [选项] [<目录>]

### 参数

* 当未指定目录时,使用当前目录(.)
* 脚本使用dir/.permset来保存/检查权限设置。
* 当未设置选项时 - 脚本计算模式并打印出来。如果dir/.permset存在 - 计算模式和保存模式

进行比较。

### 选项

选项

-h, --help 显示此帮助信息并退出
-o FILE, --output=FILE 将输出写入 FILE。选项集
也会保存选项。
-s, --save 将 perm.setup 保存到新文件或覆盖现有文件
-t, --set 通过实际 perm.setup 设置权限
-v, --verbose 详细输出

### 变量替换

模式可以包含用于用户或组的变量。例如

,["F", "S", null, "$user|admin|rw-------", ...

变量从 json ~/.permset.config 文件中读取。例如

{ "$user": "jo" }

常见工作流程
---------------
常见工作流程

1. 计算权限模式(permset),保存到 root/.permset
(permset --save)
2. 将当前权限与保存的模式进行比较
(permset),如果发现任何差异,则应用模式(permset --set)
或用新模式覆盖现有模式(permset --save)


按列排列模式代码
------------------------
按列排列模式代码

1. D - 目录模式,F - 文件模式
2. P - 是模式,S - 单个文件/目录
3. 如果是模式:R - 递归,L - 仅当前目录
4. 标记 - 用户|组|权限
5. 应用该条目的文件/目录
6. (可选)文件/目录深度 - 0 是根目录

示例会话
---------------
某个 django 项目

用户 @ ~/env/proj/src/python/proj$ permset
- - - -------------------- ----------------------------------------
F P R user1|staff|rw-r--r-- .
F P R user1|admin|rw-r--r-- ./sites
F S - user1|admin|rw------- ./sites/person/local_settings.py
F S - user1|admin|rw------- ./sites/person/local_settings.pyc
F S - user1|admin|rw------- ./sites/company/local_settings.py
F S - user1|admin|rw------- ./sites/company/local_settings.pyc
- - - -------------------- ----------------------------------------
D P R user1|staff|rwxr-xr-x .
D P R user1|admin|rwxr-xr-x ./sites

统计:F => 6 / 101 = 5.9% | D => 2 / 14 = 14.3%

使用 --save 选项调用脚本以保存权限模式。

保存模式

用户 @ ~/env/proj/src/python/proj$ permset --save
已保存至 ./.permset

使用保存的模式检查当前权限

用户 @ ~/env/proj/src/python/proj$ permset
权限设置 './.permset' 匹配。

更改某些文件的权限

用户 @ ~/env/proj/src/python/proj$ chmod u+x r.log

再次检查 - 注意到差异

用户 @ ~/env/proj/src/python/proj$ permset
权限与 ./.permset 设置不同。
=== 模式数量不同 (9!=10)

使用以下选项调用脚本
- 选项 --set - 将一切重置为保存的设置,或使用
- 选项 --save - 使用当前权限模式覆盖设置
- 选项 --verbose - 查看详细信息

查看详细信息

用户 @ ~/env/proj/src/python/proj$ permset --verbose
权限与 ./.permset 设置不同。
已保存的设置在 ./.permset
- - - -------------------- ----------------------------------------
F P R user1|staff|rw-r--r-- .
F P R user1|admin|rw-r--r-- ./sites
F S - user1|admin|rw------- ./sites/person/local_settings.py
F S - user1|admin|rw------- ./sites/person/local_settings.pyc
F S - user1|admin|rw------- ./sites/company/local_settings.py
F S - user1|admin|rw------- ./sites/company/local_settings.pyc
- - - -------------------- ----------------------------------------
D P R user1|staff|rwxr-xr-x .
D P R user1|admin|rwxr-xr-x ./sites

统计:F => 6 / 101 = 5.9% | D => 2 / 14 = 14.3%

目录的当前权限模式
- - - -------------------- ----------------------------------------
F P R user1|staff|rw-r--r-- .
F S - user1|staff|rwxr--r-- ./r.log
F P R user1|admin|rw-r--r-- ./sites
F S - user1|admin|rw------- ./sites/person/local_settings.py
F S - user1|admin|rw------- ./sites/person/local_settings.pyc
F S - user1|admin|rw------- ./sites/company/local_settings.py
F S - user1|admin|rw------- ./sites/company/local_settings.pyc
- - - -------------------- ----------------------------------------
D P R user1|staff|rwxr-xr-x .
D P R user1|admin|rwxr-xr-x ./sites

统计:F => 7 / 101 = 6.9% | D => 2 / 14 = 14.3%

=== 模式数量不同 (9!=10)

使用以下选项调用脚本
- 选项 --set - 将一切重置为保存的设置,或使用
- 选项 --save - 使用当前权限模式覆盖设置
- 选项 --verbose - 查看详细信息

将所有文件权限设置为与模式匹配

用户 @ ~/env/proj/src/python/proj$ permset --set
权限与 ./.permset 设置不同。
=== 模式数量不同 (9!=10)
=== 需要执行以下命令以应用保存的模式
chown -h user1 $(find . -type f)
chgrp -h staff $(find . -type f)
chmod -h u+rw,u-x,g+r,g-wx,o+r,o-wx $(find . -type f)
...
chown -h user1 $(find . -type d)
chgrp -h staff $(find . -type d)
chmod -h u+rwx,g+rx,g-w,o+rx,o-w $(find . -type d)
...
chmod -h u+rwx,g+rx,g-w,o+rx,o-w $(find ./sites -type d)
=== 是否继续 (y/n)? y
chown -h user1 $(find . -type f)
...
chmod -h u+rwx,g+rx,g-w,o+rx,o-w $(find ./sites -type d)
=== 完成

再次检查

用户 @ ~/env/proj/src/python/proj$ permset
权限设置 './.permset' 匹配。


模式背后的逻辑
---------------------
简而言之

* 文件和目录权限分别处理 - 由于不同的 x 解释
* 搜索模式 - 递归 (R) 和本地 - 仅当前文件夹
(L)。对于不匹配当前模式的文件/目录 - 为该文件/目录添加一个特殊的模式条目 (S)。
* 模式公式:如果有超过 2/3 的文件或目录具有相同的
标记(用户/组/权限),则将成为模式

文件 .permset
------------------
文件 .permset 包含以 JSON 格式保存的模式列表

[
["F", "P", "R", "user1|staff|rw-r--r--", ".", 0, [6, 7], [74, 101]]
,["F", "S", null, "user1|staff|rwxr--r--", "./permset", 0, null, null]
...
,["D", "P", "R", "user1|staff|rwxr-xr-x", ".", 0, [4, 5], [11, 14]]
,["D", "P", "R", "user1|admin|rwxr-xr-x", "./sites", 1, [2, 2], [2, 2]]
]

只要它是有效的 JSON 格式并包含有效的 permset 选项,文件就可以轻松手动编辑。

内部结构和潜在的性能问题
---------------------------------------------------
该脚本基于简单的逻辑,使用 [python](https://pythonlang.cn/) 编程语言编写,内部使用 python 内置的 [json](http://json.org/) 解析器和 python 的内置 [sqlite](https://sqlite.ac.cn/) 数据库引擎(作为内存数据库)。

脚本的预期使用是小型到中型目录树。对于大型目录树,脚本可能表现不佳。

开发
-----------
测试可以在包文件 tests/tests.py 中找到。


问题报告
---------------
在 [github](https://github.com/trebor74hr/permset/issues) 上报告问题。

许可和免责声明
----------------------
许可和免责声明在 [LICENCE](https://github.com/trebor74hr/permset/blob/master/LICENCE) 文件中。

安装选项
--------------------------

### 选项 1 - 独立脚本
作为独立脚本 - 前往某个可见路径的目录并执行

wget https://raw.github.com/trebor74hr/permset/master/permset/base.py -O permset && chmod u+x permset

### 选项 2 - pip 方式
或者作为Python包使用[pip](http://www.pip-installer.org/en/latest/)

pip install permset

### 选项 3 - distutils 方式
* 从 http://pypi.python.org/pypi/permset 下载最新版本
* 解包并进入文件夹
* 运行:python setup.py install

联系
-------
如果您想联系作者,请参阅 [LICENCE](https://github.com/trebor74hr/permset/blob/master/LICENCE) 文件中的电子邮件。
[LICENCE](https://github.com/trebor74hr/permset/blob/master/LICENCE) 文件。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

permset-0.22.tar.gz (12.3 kB 查看哈希值)

上传时间

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面