File: util.py

package info (click to toggle)
seafile 9.0.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,072 kB
  • sloc: ansic: 45,052; python: 6,601; sh: 272; makefile: 268; cpp: 93
file content (246 lines) | stat: -rw-r--r-- 8,845 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#coding: utf-8

import os
import shutil
import time
import subprocess
import glob
from setting import Setting
import seaf_op

def call_process(params):
    with open(os.devnull, 'w') as fd:
        ret = subprocess.check_output(params, stderr=fd)
    return ret

class TestUtil():
    def __init__(self):
        self.setting = Setting()
        self.cli1_dir = os.path.join(os.getcwd(), 'cli1')
        self.cli2_dir = os.path.join(os.getcwd(), 'cli2')
        self.worktree1 = os.path.join(os.getcwd(), 'worktree1')
        self.worktree2 = os.path.join(os.getcwd(), 'worktree2')
        self.enc_repo = False
        try:
            self.enc_repo = bool(os.environ['ENCRYPTED_REPO'])
        except Exception:
            pass
        self.repo_id = None
        self.test_root = ''

    def set_test_root(self, root):
        self.test_root = root

    @staticmethod
    def clean_sync_data(conf):
        try:
            os.remove(os.path.join(conf, 'ccnet.sock'))
            os.remove(os.path.join(conf, 'seafile.ini'))
            shutil.rmtree(os.path.join(conf, 'logs'))
            shutil.rmtree(os.path.join(conf, 'misc'))
            shutil.rmtree(os.path.join(conf, 'seafile'))
            shutil.rmtree(os.path.join(conf, 'seafile-data'))
        except Exception:
            pass

    def init_conf(self):
        self.setting.parse_config()

        if not os.path.exists(self.cli1_dir) or not os.path.exists(self.cli2_dir):
            raise Exception('ccnet conf dir is missing')

        if os.name != 'nt':
            TestUtil.clean_sync_data(self.cli1_dir)
            TestUtil.clean_sync_data(self.cli2_dir)

        if os.path.exists(self.worktree1):
            shutil.rmtree(self.worktree1)
        if os.path.exists(self.worktree2):
            shutil.rmtree(self.worktree2)

        os.mkdir(self.worktree1)
        os.mkdir(self.worktree2)

        seaf_op.seaf_init(self.cli1_dir)
        seaf_op.seaf_init(self.cli2_dir)

    def start_daemon(self):
        seaf_op.seaf_start_all(self.cli1_dir)
        seaf_op.seaf_start_all(self.cli2_dir)

    def create_repo(self):
        self.repo_id = seaf_op.seaf_create(self.cli1_dir, self.setting.server_url,
                                           self.setting.user, self.setting.password,
                                           self.enc_repo)

    def sync_cli1(self):
        seaf_op.seaf_sync(self.cli1_dir, self.setting.server_url, self.repo_id,
                          self.worktree1, self.setting.user, self.setting.password)

    def sync_cli2(self):
        seaf_op.seaf_sync(self.cli2_dir, self.setting.server_url, self.repo_id,
                          self.worktree2, self.setting.user, self.setting.password)

    def sync_repo(self):
        self.sync_cli1()
        self.sync_cli2()

    def desync_cli1(self):
        seaf_op.seaf_desync(self.cli1_dir, self.worktree1)

    def desync_cli2(self):
        seaf_op.seaf_desync(self.cli2_dir, self.worktree2)

    def desync_repo(self):
        self.desync_cli1()
        self.desync_cli2()
        # delete test repo
        seaf_op.seaf_delete(self.cli1_dir, self.setting.server_url,
                            self.setting.user, self.setting.password,
                            self.repo_id)

    def stop_daemon(self):
        seaf_op.seaf_stop(self.cli1_dir)
        seaf_op.seaf_stop(self.cli2_dir)

    def clean(self):
        try:
            if os.name != 'nt':
                TestUtil.clean_sync_data(self.cli1_dir)
                TestUtil.clean_sync_data(self.cli2_dir)
            shutil.rmtree(self.worktree1)
            shutil.rmtree(self.worktree2)
        except Exception:
            pass

    def wait_sync(self):
        while True:
            time.sleep(5)
            repo1 = seaf_op.seaf_get_repo(self.cli1_dir, self.repo_id)
            if repo1 is None:
                continue
            repo2 = seaf_op.seaf_get_repo(self.cli2_dir, self.repo_id)
            if repo2 is None:
                continue
            if repo1.head_cmmt_id == repo2.head_cmmt_id:
                break

    @staticmethod
    def verify_by_rsync(dir1, dir2):
        ret = call_process(['rsync', '-acrin', dir1, dir2])
        if ret:
            for d in ret.split('\n'):
                # omit empty str
                if not d:
                    continue
                # omit directory has almost same result except st_mod
                items = d.split(' ')
                dattr = items[0]
                name = items[1]

                # Output format difference between rsync versions:
                # rsync 3.1.1 : '.d..t.......'
                # rsync 3.0.9 : '.d..t......'

                # On Windows, file timestamp may have 1 second difference
                # between two clients after sync. That's caused by the
                # precision lose when converting Windows timestamp to
                # Unix timestamp. So we don't check timestamp difference
                # for files either.
                if not all([c in ('f', 'd', 't', '.') for c in dattr]):
                    assert False, 'Sync with two client have different result: %s %s' % (dattr, name)

    def verify_result(self, callable=None):
        self.wait_sync()
        if callable:
            callable(self.worktree1, self.worktree2)
        else:
            dir1 = './worktree1/'
            dir2 = './worktree2/'
            TestUtil.verify_by_rsync(dir1, dir2)
            TestUtil.verify_by_rsync(dir2, dir1)

    # worktree: 1(worktree1), 2(worktree2)
    def mkdir(self, worktree, path):
        if worktree == 1:
            os.makedirs(os.path.join(self.worktree1, self.test_root, path))
        elif worktree == 2:
            os.makedirs(os.path.join(self.worktree2, self.test_root, path))

    def rmdir(self, worktree, path):
        if worktree == 1:
            shutil.rmtree(os.path.join(self.worktree1, self.test_root, path))
        elif worktree == 2:
            shutil.rmtree(os.path.join(self.worktree2, self.test_root, path))

    def mkfile(self, worktree, fpath, con=''):
        if worktree == 1:
            pdir = self.worktree1
        elif worktree == 2:
            pdir = self.worktree2
        else:
            return
        abs_path = os.path.join(pdir, self.test_root, fpath)
        dirname = os.path.dirname(abs_path)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        with open(abs_path, 'w') as fd:
            fd.write(con)

    def rmfile(self, worktree, fpath):
        if worktree == 1:
            os.remove(os.path.join(self.worktree1, self.test_root, fpath))
        elif worktree == 2:
            os.remove(os.path.join(self.worktree2, self.test_root, fpath))

    def modfile(self, worktree, fpath, con=''):
        if worktree == 1:
            pdir = self.worktree1
        elif worktree == 2:
            pdir = self.worktree2
        else:
            return
        abs_path = os.path.join(pdir, self.test_root, fpath)
        with open(abs_path, 'a') as fd:
            fd.write(con)

    def move(self, worktree, org_path, dest_path):
        if worktree == 1:
            shutil.move(os.path.join(self.worktree1, self.test_root, org_path),
                        os.path.join(self.worktree1, self.test_root, dest_path))
        elif worktree == 2:
            shutil.move(os.path.join(self.worktree2, self.test_root, org_path),
                        os.path.join(self.worktree2, self.test_root, dest_path))

    def batchmove(self, worktree, regex, dest_path):
        if worktree == 1:
            pdir = self.worktree1
        elif worktree == 2:
            pdir = self.worktree2
        else:
            return
        files = glob.glob(os.path.join(pdir, self.test_root, regex))
        dest = os.path.join(pdir, self.test_root, dest_path)
        for f in files:
            shutil.move(f, dest)

    def copy(self, worktree, org_path, dest_path):
        if worktree == 1:
            shutil.copytree(os.path.join(self.worktree1, self.test_root, org_path),
                            os.path.join(self.worktree1, self.test_root, dest_path))
        elif worktree == 2:
            shutil.copytree(os.path.join(self.worktree2, self.test_root, org_path),
                            os.path.join(self.worktree2, self.test_root, dest_path))

    def touch(self, worktree, path, time=None):
        if worktree == 1:
            os.utime(os.path.join(self.worktree1, self.test_root, path), time)
        if worktree == 2:
            os.utime(os.path.join(self.worktree2, self.test_root, path), time)

    def getpath(self, worktree, path):
        if worktree == 1:
            return os.path.join(self.worktree1, self.test_root, path)
        elif worktree == 2:
            return os.path.join(self.worktree2, self.test_root, path)
        raise Exception('Invalid worktree')