将文件复制到另一台计算机以进行远程开发
项目描述
文件复制器
将文件单向复制到另一台计算机,例如用于远程开发。
一个主要用例是同步一个开发文件目录,该目录位于编辑文件的计算机上,与运行在远程docker主机上的docker容器中的文件副本保持同步。
已测试并在两个Linux机器之间工作。即将提供在macOS上开发的支持...
安装
依赖项包括
- Python 3和开发机上的某些Python包。
- 在远程机器上运行shell(bash或类似bash)并连接到
stdin
的能力。 - 两台机器上的tar实用程序(完整版本,而非busybox版本)。
请注意,在远程机器上没有安装任何东西,没有要打开的端口,远程用户只需要能够在指定位置创建文件和目录。
因此,要在包含要复制的源文件的机器上安装file-replicator
pip install file-replicator
只要目标机器有bash
(busybox bash也可以)和tar
(gnu),就不需要在目标机器上安装任何东西。注意,在alpine linux上,busybox tar不足以使用,因此请使用以下命令安装gnu tar
apk install tar
工作原理
该方法涉及在远程(目标)端运行一个小型bash程序,该程序能够向(可能)新的目录中添加/更新新文件。它使用tar
格式(二进制)通过stdin
接收这些文件。
控制(源)端随后简单地将文件发送到接收bash程序的stdin
,并通过tar
管道将它们解包。请注意,GNU tar
能够从非阻塞文件描述符中提取(以及阻塞),这意味着它会不断尝试,直到获取所有数据。注意,busybox tar
没有这种行为。
建立与远程端的连接不属于工具的范畴,但file-replicator
需要一个参数来指定建立此类连接的命令。以下是一些示例。
一旦建立连接,就会发生两个操作阶段
- 首先,递归遍历源树中的文件,并将它们“通过线”发送到目标
- 然后,在发送它们“通过线”到目标之前,监视更改或新文件和目录
因此,没有像rsync那样的“差异算法”,没有压缩尝试(尽管当然连接已经压缩,例如如果通过ssh),连接完全使用标准方式(如ssh和docker)建立,没有需要打开的端口,并且每次都发送远程端上的bash程序,因此不会在远程端安装任何内容。
这足以在本地计算机上编辑代码,并在文件创建或修改时自动将其复制到远程服务器或docker容器。
用法和示例
请使用file-replicate --help
查看帮助
Usage: file-replicator [OPTIONS] SRC_DIR DEST_PARENT_DIR \
[CONNECTION_COMMAND]...
Replicate files to another computer e.g. for remote development.
SRC_DIR is the source directory on this machine.
DEST_PARENT_DIR is the (absolute) destination parent directory on the
remote machine accessed using the CONNECTION_COMMAND.
The CONNECTION_COMMAND must result in a running instance of bash ready to
receive commands on stdin.
Example CONNECTION_COMMANDs include:
ssh some.host.com bash
docker exec -i my_container bash
docker-compose exec -T my_container bash
So a full use of the tool might look like:
file-replicator my_code_dir /home/code -- docker exec -i a_container bash
(the use of "--" prevents any further processing of command line arguments
by file-replicator, leaving them all for docker)
Initially, all files and required directories are recursively copied. Then
it waits for changes before copying each modified or new file. This can be
modified with the switches.
Note that empty directories are not replicated until they contain a file.
Options:
--clean-out-first Optionally start by cleaning out the
destination directory.
--with-initial-replication / --no-initial-replication
Perform (or not) an initial replication of
all files.
--replicate-on-change / --no-replicate-on-change
Perform (or not) a wait-for-change-and-
replicate cycle.
--gitignore / --no-gitignore Use .gitignore (or not) to filter files.
--debugging Print debugging information.
--version Show the version and exit.
--help Show this message and exit.
例如,要将本地目录my_project_dir
中的文件复制到远程主机my.server.com
上的目录/home/code/my_project_dir
file-replicator my_project_dir /home/code ssh my.server.com bash
作为另一个示例,要将本地目录my_project_dir
中的文件复制到运行在名为my_container
的docker容器中的目录/home/code/my_project_dir
,该容器可能在远程主机上(取决于DOCKER*
环境变量,例如由docker-machine eval
设置)
file-replicator my_project_dir /home/code -- docker exec -i my_container bash
或者使用docker-compose
来完成相同的操作
file-replicator my_project_dir /home/code -- docker-compose exec -T my_container bash
最后,作为一个退化示例,它根本不连接到远程机器,而是将内容复制到本地/tmp/my_project_dir
file-replicator my_project_dir /tmp bash
单元测试使用这种退化方法来测试工具。
限制
由于inotify的限制(监视新创建目录中的更改时的竞态条件),监视更改阶段可能会出错。在这种情况下,只需重新启动整个程序即可。工具包括一些自重启行为,但最终有时可能需要完全重启。
打印到stdout的信息表明何时发生这种情况。
测试
============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-3.10.1, py-1.8.0, pluggy-0.12.0 -- /home/tcorbettclark/.cache/pypoetry/virtualenvs/file-replicator-py3.6/bin/python
cachedir: .pytest_cache
rootdir: /home/tcorbettclark/code/file-replicator, inifile:
collecting ... collected 8 items
tests/test_lib.py::test_empty_directories_are_copied PASSED [ 12%]
tests/test_lib.py::test_copy_one_file PASSED [ 25%]
tests/test_lib.py::test_copy_file_with_unusual_characters_in_name PASSED [ 37%]
tests/test_lib.py::test_make_missing_parent_directories PASSED [ 50%]
tests/test_lib.py::test_replicate_all_files PASSED [ 62%]
tests/test_lib.py::test_detect_and_copy_new_file PASSED [ 75%]
tests/test_lib.py::test_detect_and_copy_modified_file PASSED [ 87%]
tests/test_lib.py::test_detect_and_copy_new_file_in_new_directories PASSED [100%]
=========================== 8 passed in 4.00 seconds ===========================
贡献
欢迎pull请求!请考虑同时包含测试和更新文档。
该软件包使用poetry (https://poetry.eustace.io) 和 pyenv (https://github.com/pyenv/pyenv) 进行维护。
代码使用black (https://black.pythonlang.cn/en/stable) 和 isort (https://github.com/timothycrosley/isort) 进行格式化。
它使用pytest (https://pytest.cn) 进行测试。
提交清单
isort -rc .
black .
pytest -v
- 在
pyproject.toml
中更新clock版本 - 在
file_replicator/__init__.py
中更新clock版本 git tag
- 使用测试的最新输出更新此README.md
- 使用--help选项的最新输出更新此README.md
项目详情
file-replicator-0.1.11.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d7e1bcf031a4fe29eb286ea65e604c9a6a9240d74e85a3337fd1d15cdcbd7c36 |
|
MD5 | 45d72a4ab64b65dcee5af5d9445fdda2 |
|
BLAKE2b-256 | 9d115a763a0cef8884b67e9f81812847d3dd90ec7195fe38352b209f5a66a006 |
file_replicator-0.1.11-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | acf2b79160b70dae9e24e50e0d0f429e91a8853c2de6351b1875e73750588a0a |
|
MD5 | c2ee513be0adc2e9591ee7840717fdbe |
|
BLAKE2b-256 | ad71a66973f50f9f682d11a3648de36173e41676ab2a99880df16dbd887e1327 |