-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_luigi.py
231 lines (174 loc) · 7.02 KB
/
run_luigi.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""run_luigi.py
Saisoku is a Python module that helps you build complex pipelines of batch file copying jobs.
See README.md or https://github.com/shirosaidev/saisoku
for more information.
Author: shirosai <[email protected]>
Copyright (C) Chris Park 2019-2020
saisoku is released under the Apache 2.0 license. See
LICENSE for the full license text.
"""
import luigi
from luigi.contrib.s3 import S3Target, S3Client
import os
import time
import logging
from saisoku import logger
class CopyFiles(luigi.Task):
src = luigi.Parameter()
dst = luigi.Parameter()
threads = luigi.IntParameter(default=16)
filelist = luigi.OptionalParameter(default=None)
symlinks = luigi.BoolParameter(default=False)
ignore = luigi.OptionalParameter(default=None)
copymeta = luigi.BoolParameter(default=True)
#def output(self):
# return []
#def requires(self):
# return []
def run(self):
from saisoku import ThreadedCopy
ThreadedCopy(src=self.src, dst=self.dst, threads=self.threads, filelist=self.filelist,
symlinks=self.symlinks, ignore=self.ignore, copymeta=self.copymeta)
class CopyFilesHTTP(luigi.Task):
src = luigi.Parameter()
dst = luigi.Parameter()
threads = luigi.IntParameter(default=1)
ports = luigi.ListParameter(default=[5005])
fetchmode = luigi.Parameter(default='urlretrieve')
chunksize = luigi.IntParameter(default=8192)
def run(self):
from saisoku import ThreadedHTTPCopy
ThreadedHTTPCopy(src=self.src, dst=self.dst, threads=self.threads, ports=self.ports,
fetchmode=self.fetchmode, chunksize=self.chunksize)
class PackageDirectory(luigi.Task):
src = luigi.Parameter()
filelist = luigi.Parameter(default='saisoku_filelist.txt')
def output(self):
return luigi.LocalTarget(self.filelist)
def run(self):
import tarfile
from scandir import scandir
import os
archives = []
src_path = os.path.abspath(self.src)
# create tar.gz of all files in directory
archive_name = '{}_saisoku_archive.tar.gz'.format(os.path.basename(src_path))
logger.info('Compressing files to %s...' % archive_name)
tar = tarfile.open(archive_name, "w:gz")
for item in scandir(self.src):
if item.is_file():
logger.debug(' Adding %s...' % item.name)
tar.add(item.path, item.name)
tar.close()
archives.append(archive_name)
with self.output().open('w') as f:
for archive in archives:
f.write('{archive}\n'.format(archive=archive))
class CopyFilesPackage(luigi.Task):
src = luigi.Parameter()
dst = luigi.Parameter()
threads = luigi.IntParameter(default=16)
filelist = luigi.Parameter(default='saisoku_filelist.txt')
symlinks = luigi.BoolParameter(default=False)
ignore = luigi.OptionalParameter(default=None)
copymeta = luigi.BoolParameter(default=True)
cleanup = luigi.BoolParameter(default=True)
def output(self):
return luigi.LocalTarget(self.filelist)
def requires(self):
return [PackageDirectory(src=self.src, filelist=self.filelist)]
def run(self):
from saisoku import ThreadedCopy
import os
ThreadedCopy(src=self.src, dst=self.dst, threads=self.threads, filelist=self.filelist,
symlinks=self.symlinks, ignore=self.ignore, copymeta=self.copymeta, package=True)
if self.cleanup:
logger.info('Cleaning up %s...' % self.src)
with self.input()[0].open() as f:
filename = f.read().rstrip('\n')
logger.debug(' Removing %s...' % filename)
os.remove(filename) # delete tar.gz file in src
logger.debug(' Removing %s...' % self.filelist)
os.remove(self.filelist) # delete file list
class S3File(luigi.ExternalTask):
src = luigi.Parameter()
def output(self):
return S3Target(self.src)
class CopyS3FileToLocal(luigi.Task):
src = luigi.Parameter()
dst = luigi.Parameter()
def output(self):
return luigi.LocalTarget(self.dst)
def requires(self):
return S3File(src=self.src)
def run(self):
"""
examples:
* s3://bucket/foo/bar.txt
* s3://bucket/foo/bar.txt?aws_access_key_id=xxx&aws_secret_access_key=yyy
"""
def copy():
with self.input().open('r') as infile:
for line in infile:
yield line
logger.info('Copying %s to %s ...' % (self.src, self.dst))
t = time.time()
with self.output().open('w') as outfile:
for line in copy():
outfile.write(line)
t = round(time.time() - t, 2)
size = round(os.path.getsize(self.dst) / 1024 * 1.0, 2) # in KB
kb_per_sec = round(size / t, 2)
logger.info('Done. Copied %s KB in %s seconds (%s KB/s)' % (size, t, kb_per_sec))
class LocalFile(luigi.ExternalTask):
src = luigi.Parameter()
def output(self):
return luigi.LocalTarget(self.src)
class CopyLocalFileToS3(luigi.Task):
src = luigi.Parameter()
dst = luigi.Parameter()
def output(self):
return S3Target(self.dst)
def requires(self):
return LocalFile(src=self.src)
def run(self):
def copy():
with self.input().open('r') as infile:
for line in infile:
yield line
logger.info('Copying %s to %s ...' % (self.src, self.dst))
t = time.time()
with self.output().open('w') as outfile:
for line in copy():
outfile.write(line)
t = round(time.time() - t, 2)
size = round(os.path.getsize(self.src) / 1024 * 1.0, 2) # in KB
kb_per_sec = round(size / t, 2)
logger.info('Done. Copied %s KB in %s seconds (%s KB/s)' % (size, t, kb_per_sec))
class SyncDirsRclone(luigi.Task):
src = luigi.Parameter()
dst = luigi.Parameter()
flags = luigi.ListParameter(default=[])
command = luigi.Parameter(default='sync')
cmdargs = luigi.ListParameter(default=['--dry-run', '-vv'])
def run(self):
from saisoku import Rclone
Rclone(src=self.src, dst=self.dst, flags=self.flags, command=self.command, cmdargs=self.cmdargs)
class SyncDirsWatchdog(luigi.Task):
src = luigi.Parameter()
dst = luigi.Parameter()
recursive = luigi.BoolParameter(default=True)
patterns = luigi.Parameter(default="*")
ignore_patterns = luigi.Parameter(default="")
ignore_directories = luigi.BoolParameter(default=False)
case_sensitive = luigi.BoolParameter(default=True)
def run(self):
from saisoku import WatchDog
WatchDog(
src=self.src, dst=self.dst, recursive=self.recursive, patterns=self.patterns,
ignore_patterns=self.ignore_patterns, ignore_directories=self.ignore_directories,
case_sensitive=self.case_sensitive)
if __name__ == '__main__':
luigi.run()