根据你告诉它的合同,在你的文件系统上播下“植物”目录、文件和符号链接。
sower的Python项目详细描述
播种机
sower“plants”文件系统上的目录、文件和符号链接基于您告诉它的契约。
如何安装
$ pip install sower
你为什么需要播种机?
问题
你有没有遇到过这样的情况:你需要创建一个 文件,可能跨越多个子目录?你可能需要这样做 测试,特别是如果您正在测试的应用程序应该 测试它如何根据目录管理一组文件 结构。
解决方案-第1部分
播种是解决这个问题的办法!您只需在中定义一个contract yaml或json如下:
YAML (sower-contract.yml) | JSON (sower-contract.json) |
---|---|
^{pr 2}$ | ^{pr 3}$ |
将契约保存到磁盘(从上面选择yaml或json列表)。现在 我们只是告诉sower在哪里创建这些文件,以及这个合同的路径。
如果我们想基于这个合同在/home/davydany/foobar上创建文件,我们可以 以下内容:
如果选择了yaml,请运行以下命令: ……代码::bash
$ sower sow /home/davydany/foobar /tmp/sower-contract.yml
如果选择json,请运行以下命令: ……代码::bash
$ sower sow /home/davydany/foobar /tmp/sower-contract.json
这将创建以下结构:
/home/davydany/foobar ├── bin │ ├── start.sh │ └── stop.sh ├── data │ └── test-data.tar.gz └── src └── foobar ├── __init__.py ├── link_main.py -> /tmp/foobar/src/foobar/main.py └── main.py 4 directories, 6 files
解决方案-第2部分
现在,假设您需要在使用python的unittest的集成测试中这样做。你 仍然可以使用sower api来利用这一点。
在测试的设置方法中会有类似的内容。
import tempfile import unittest from sower.farm import perform_sow class TestMyApp(unittest.TestCase): def setUp(self): self.root = tempfile.mkdtemp('_farmer_test') self.contract = """ --- sower: plan: bin: start.sh: type: file content: "echo 'Starting foobar'" stop.sh: type: file content: "echo 'Stopping foobar'" data: test-data.tar.gz: type: file content: "!random!" size: 1Mb src: foobar: __init__.py: type: file content: "# just a comment" main.py: type: file content: > import os\n print('Foo Bar: %s' % os.path.abspath('.'))\n\n link_main.py: type: symlink target: ../foobar/main.py """ perform_sow(self.contract, self.root)