dotfilesmanager 1.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dfm.py ADDED
@@ -0,0 +1,289 @@
1
+ #! /usr/bin/env python
2
+
3
+
4
+ '''
5
+ dotfile管理工具(dotfiles manager),dotfile指保存配置信息的文件或包含配置文件的文件夹
6
+
7
+ Usage:
8
+ dfm add <install_path> [--system]
9
+ dfm rm <path>
10
+ dfm install [<save_path>]
11
+ dfm share <save_path> <install_path>
12
+
13
+ Commands:
14
+ add 添加一个dotfile,并将其移动至配置项目录并创建相应软链接
15
+ rm 删除一个dotfile,若该dotfile对应的软连接存在,则删除该软连接并恢复原始文件,若dotfile不再被任何平台使用,则将其从dotfiles目录移除
16
+ install 安装dotfiles,若<path>为空则安装所有dotfile,否则安装指定dotfile
17
+ share 共享一个dotfile,共享一个其他平台已添加的dotfile并安装
18
+
19
+ Arguments:
20
+ path save_path或install_path
21
+ save_path dotfile位于dotfiles中的保存路径
22
+ install_path dotfile的安装路径
23
+
24
+ Options:
25
+ -h --help 显示帮助
26
+ --system 该dotfile和操作系统相关
27
+ '''
28
+
29
+ from docopt import docopt
30
+ import yaml
31
+ import os
32
+ import platform
33
+ import shutil
34
+ import hashlib
35
+ import posixpath
36
+ import ctypes
37
+
38
+
39
+ dotfiles_root = os.path.join(*[os.path.expanduser("~"), "dotfiles"])
40
+
41
+
42
+ def __load_config():
43
+ config_path = os.path.join(*[dotfiles_root, "dfm.yaml"])
44
+ if not os.path.isfile(config_path):
45
+ return {"dotfiles": {}}
46
+ with open(config_path) as fin:
47
+ config = yaml.load(fin, Loader=yaml.SafeLoader)
48
+ if "dotfiles" not in config:
49
+ config["dotfiles"] = {}
50
+ return config
51
+
52
+
53
+ def __save_config(config):
54
+ config_path = os.path.join(*[dotfiles_root, "dfm.yaml"])
55
+ with open(config_path, 'w', newline='\n') as fout:
56
+ fout.write(yaml.dump(config, Dumper=yaml.SafeDumper))
57
+
58
+
59
+ def __get_os_name():
60
+ return platform.system().lower()
61
+
62
+
63
+ def __normalize_path(path):
64
+ if path is None:
65
+ return None
66
+ return os.path.abspath(os.path.normpath(__expanduser(path)))
67
+
68
+
69
+ def __expanduser(path):
70
+ if path is None:
71
+ return None
72
+
73
+ if __get_os_name() != "windows":
74
+ return os.path.expanduser(path)
75
+
76
+ home = str(os.path.expanduser("~"))
77
+ if path.startswith('~'):
78
+ path = path.replace('~', home, 1)
79
+ return path
80
+
81
+
82
+ # opposite of os.path.expanduser
83
+ def __shrinkuser(path):
84
+ if path is None:
85
+ return None
86
+
87
+ home = str(os.path.expanduser("~"))
88
+ if path.startswith(home):
89
+ path = path.replace(home, '~', 1)
90
+ return path
91
+
92
+
93
+ def __get_save_path(install_path, system):
94
+ install_path = __shrinkuser(install_path)
95
+ md5 = hashlib.md5()
96
+ md5.update(str(os.path.dirname(install_path)).encode("utf8"))
97
+ save_dir = md5.hexdigest()
98
+
99
+ filename = os.path.basename(install_path)
100
+ system_sep = __get_os_name() if system else ""
101
+ save_path = os.path.join(*[dotfiles_root, save_dir, system_sep, filename])
102
+ return save_path
103
+
104
+
105
+ def __mklink(target, link):
106
+ if not os.path.isdir(os.path.dirname(link)):
107
+ os.makedirs(os.path.dirname(link), exist_ok=True)
108
+ if os.path.lexists(link):
109
+ is_replace = input("文件 %s 已存在,是否替换?(y/N)" % link)
110
+ if is_replace.lower() != 'y':
111
+ return False
112
+
113
+ if os.path.islink(link) or os.path.isfile(link):
114
+ os.remove(link)
115
+ else:
116
+ shutil.rmtree(link)
117
+ os.symlink(target, link)
118
+ return True
119
+
120
+
121
+ def __set_path(config, rel_save_path, install_path):
122
+ os_name = __get_os_name()
123
+ if rel_save_path not in config["dotfiles"]:
124
+ config["dotfiles"][rel_save_path] = {}
125
+ if os_name not in config["dotfiles"][rel_save_path]:
126
+ config["dotfiles"][rel_save_path][os_name] = {}
127
+ config["dotfiles"][rel_save_path][os_name]["path"] = __shrinkuser(
128
+ install_path)
129
+ return config
130
+
131
+
132
+ def __get_path(config, rel_save_path):
133
+ os_name = __get_os_name()
134
+ if rel_save_path not in config["dotfiles"]:
135
+ return None
136
+ if os_name not in config["dotfiles"][rel_save_path]:
137
+ return None
138
+ install_path = config["dotfiles"][rel_save_path][os_name]["path"]
139
+ return __expanduser(install_path)
140
+
141
+
142
+ def __add(install_path, system, config):
143
+ abs_save_path = __get_save_path(install_path, system)
144
+ os.makedirs(os.path.dirname(abs_save_path), exist_ok=True)
145
+ shutil.move(install_path, abs_save_path)
146
+ os.symlink(abs_save_path, install_path)
147
+
148
+ rel_save_path = os.path.relpath(abs_save_path, dotfiles_root)
149
+ rel_save_path = rel_save_path.replace(os.sep, posixpath.sep)
150
+ print("Add %s to %s" % (install_path, rel_save_path))
151
+ return __set_path(config, rel_save_path, install_path)
152
+
153
+
154
+ def __rm(path, config):
155
+ abs_save_path = path
156
+ if os.path.islink(path):
157
+ abs_save_path = os.readlink(path)
158
+ rel_save_path = os.path.relpath(abs_save_path, dotfiles_root)
159
+ rel_save_path = rel_save_path.replace(os.sep, posixpath.sep)
160
+
161
+ install_path = __get_path(config, rel_save_path)
162
+ if install_path is None:
163
+ return config
164
+
165
+ if os.path.islink(install_path):
166
+ os.unlink(install_path)
167
+ del config["dotfiles"][rel_save_path][__get_os_name()]
168
+
169
+ if config["dotfiles"][rel_save_path]:
170
+ if os.path.isfile(abs_save_path):
171
+ shutil.copy(abs_save_path, install_path)
172
+ else:
173
+ shutil.copytree(abs_save_path, install_path)
174
+ else:
175
+ shutil.move(abs_save_path, install_path)
176
+ del config["dotfiles"][rel_save_path]
177
+ abs_save_dir = os.path.dirname(abs_save_path)
178
+ if len(os.listdir(abs_save_dir)) == 0:
179
+ os.rmdir(abs_save_dir)
180
+
181
+ print("Remove %s" % rel_save_path)
182
+ return config
183
+
184
+
185
+ def __install(abs_save_path, config):
186
+ rel_save_path = None
187
+ if abs_save_path is not None:
188
+ rel_save_path = os.path.relpath(abs_save_path, dotfiles_root)
189
+ rel_save_path = rel_save_path.replace(os.sep, posixpath.sep)
190
+ if __get_path(config, rel_save_path) is None:
191
+ print("%s is not kept in dotfiles" % rel_save_path)
192
+ return config
193
+
194
+ for item_rel_save_path in config["dotfiles"]:
195
+ if rel_save_path is not None and item_rel_save_path != rel_save_path:
196
+ continue
197
+
198
+ item_install_path = __get_path(config, item_rel_save_path)
199
+ if item_install_path is None:
200
+ continue
201
+ item_abs_save_path = os.path.join(dotfiles_root, item_rel_save_path)
202
+ item_abs_save_path = item_abs_save_path.replace(posixpath.sep, os.sep)
203
+
204
+ if __mklink(item_abs_save_path, item_install_path):
205
+ print("Install %s -> %s" % (item_rel_save_path, item_install_path))
206
+ return config
207
+
208
+
209
+ def __share(abs_save_path, install_path, config):
210
+ rel_save_path = os.path.relpath(abs_save_path, dotfiles_root)
211
+ rel_save_path = rel_save_path.replace(os.sep, posixpath.sep)
212
+ if rel_save_path not in config["dotfiles"]:
213
+ print("%s is not kept in dotfiles" % rel_save_path)
214
+ return config
215
+
216
+ config = __set_path(config, rel_save_path, install_path)
217
+
218
+ if __mklink(abs_save_path, install_path):
219
+ print("share %s -> %s" % (rel_save_path, install_path))
220
+ return config
221
+
222
+
223
+ def __dispatch(args):
224
+ def check_add_args():
225
+ path = __normalize_path((args["<install_path>"]))
226
+ system = args["--system"]
227
+ if not os.path.isfile(path) and not os.path.isdir(path):
228
+ print("%s is not valid file or directory" % path)
229
+ exit(-1)
230
+ if path.startswith(dotfiles_root):
231
+ print("%s cannot be in dotfiles" % path)
232
+ exit(-1)
233
+ if not path.startswith(str(os.path.expanduser("~"))):
234
+ print("%s must be in home" % path)
235
+ exit(-1)
236
+ if os.path.exists(__get_save_path(path, system)):
237
+ print("%s has been kept in dotfiles" % path)
238
+ exit(-1)
239
+
240
+ def check_rm_args():
241
+ raw_path = __normalize_path(args["<path>"])
242
+ path = raw_path
243
+ if os.path.islink(raw_path):
244
+ path = os.readlink(raw_path)
245
+ if not path.startswith(dotfiles_root):
246
+ print("%s is not in dotfiles" % raw_path)
247
+ exit(-1)
248
+
249
+ if args["add"]:
250
+ check_add_args()
251
+
252
+ def add(config):
253
+ return __add(__normalize_path(args["<install_path>"]),
254
+ args["--system"], config)
255
+ return add
256
+ elif args["rm"]:
257
+ check_rm_args()
258
+
259
+ def rm(config):
260
+ return __rm(__normalize_path(args["<path>"]), config)
261
+ return rm
262
+ elif args["install"]:
263
+ def install(config):
264
+ return __install(__normalize_path(args["<save_path>"]), config)
265
+ return install
266
+ elif args["share"]:
267
+ def share(config):
268
+ return __share(__normalize_path(args["<save_path>"]),
269
+ __normalize_path(args["<install_path>"]), config)
270
+ return share
271
+ else:
272
+ return None
273
+
274
+
275
+ def main():
276
+ if __get_os_name() == "windows":
277
+ if ctypes.windll.shell32.IsUserAnAdmin() == 0:
278
+ print("dfm must run with Administrator priviledges under Windows")
279
+ return
280
+
281
+ args = docopt(__doc__)
282
+
283
+ config = __load_config()
284
+ config = __dispatch(args)(config)
285
+ __save_config(config)
286
+
287
+
288
+ if __name__ == "__main__":
289
+ main()
@@ -0,0 +1,25 @@
1
+ Metadata-Version: 2.4
2
+ Name: dotfilesmanager
3
+ Version: 1.1.5
4
+ Summary: dotfiles管理工具
5
+ Home-page: https://github.com/xyz1001/dotfilesmanager
6
+ Author: xyz1001
7
+ Author-email: zgzf1001@gmail.com
8
+ License: MIT
9
+ Keywords: python dotfiles
10
+ Classifier: Programming Language :: Python :: 3
11
+ License-File: LICENSE
12
+ Requires-Dist: docopt
13
+ Requires-Dist: pyyaml
14
+ Dynamic: author
15
+ Dynamic: author-email
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: home-page
19
+ Dynamic: keywords
20
+ Dynamic: license
21
+ Dynamic: license-file
22
+ Dynamic: requires-dist
23
+ Dynamic: summary
24
+
25
+ dotfile管理工具,支持多平台
@@ -0,0 +1,7 @@
1
+ dfm.py,sha256=Vr74AVHX9dcu2RFQClLVTv7pq8rKdixep6W6QM-rrZQ,9103
2
+ dotfilesmanager-1.1.5.dist-info/licenses/LICENSE,sha256=UyQuq4yn0iAT0POkQkjiJ18cOimW3FTqifJMA3-uUuI,1064
3
+ dotfilesmanager-1.1.5.dist-info/METADATA,sha256=P6n8FZF9209iKs2g7-bvH0VVM3JFs9_6MbGzEE1-BV4,579
4
+ dotfilesmanager-1.1.5.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
5
+ dotfilesmanager-1.1.5.dist-info/entry_points.txt,sha256=3RCoYOAneTvwuXPcjCSFPkBEJTjfwqCfH149fegwHig,33
6
+ dotfilesmanager-1.1.5.dist-info/top_level.txt,sha256=rIexePLFYJwTsspufa7t5LV3yWyq51QSdGXDMO5JxKw,4
7
+ dotfilesmanager-1.1.5.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (83.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ dfm = dfm:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 xyz1001
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ dfm