pyreposync 0.2.0__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.
- pyreposync/__init__.py +511 -0
- pyreposync/downloader.py +143 -0
- pyreposync/exceptions.py +10 -0
- pyreposync/sync_deb.py +218 -0
- pyreposync/sync_generic.py +169 -0
- pyreposync/sync_rpm.py +256 -0
- pyreposync-0.2.0.dist-info/METADATA +31 -0
- pyreposync-0.2.0.dist-info/RECORD +11 -0
- pyreposync-0.2.0.dist-info/WHEEL +4 -0
- pyreposync-0.2.0.dist-info/entry_points.txt +2 -0
- pyreposync-0.2.0.dist-info/licenses/LICENSE.txt +21 -0
pyreposync/__init__.py
ADDED
@@ -0,0 +1,511 @@
|
|
1
|
+
import argparse
|
2
|
+
import collections
|
3
|
+
import configparser
|
4
|
+
import datetime
|
5
|
+
import logging
|
6
|
+
import sys
|
7
|
+
import threading
|
8
|
+
import time
|
9
|
+
|
10
|
+
from pyreposync.downloader import Downloader
|
11
|
+
from pyreposync.exceptions import OSRepoSyncException, OSRepoSyncHashError
|
12
|
+
from pyreposync.sync_rpm import SyncRPM
|
13
|
+
from pyreposync.sync_deb import SyncDeb
|
14
|
+
|
15
|
+
|
16
|
+
def main():
|
17
|
+
parser = argparse.ArgumentParser(description="OS Repo Sync Tool")
|
18
|
+
|
19
|
+
parser.add_argument(
|
20
|
+
"--cfg",
|
21
|
+
dest="cfg",
|
22
|
+
action="store",
|
23
|
+
default="/etc/pyreposync/reposync.ini",
|
24
|
+
help="Full path to configuration",
|
25
|
+
)
|
26
|
+
|
27
|
+
parser.add_argument(
|
28
|
+
"--repo",
|
29
|
+
dest="repo",
|
30
|
+
action="store",
|
31
|
+
default=None,
|
32
|
+
help="""
|
33
|
+
execute command on this repository, if not set,
|
34
|
+
command applies to all configured repositories.
|
35
|
+
|
36
|
+
This command is mutually exclusive with --tags
|
37
|
+
""",
|
38
|
+
)
|
39
|
+
|
40
|
+
parser.add_argument(
|
41
|
+
"--tags",
|
42
|
+
dest="tags",
|
43
|
+
action="store",
|
44
|
+
default=None,
|
45
|
+
help="""
|
46
|
+
Comma separated list of repo tags the command applies to.
|
47
|
+
putting a '!' in front of a tag negates it.
|
48
|
+
At least one not negated tag has to be match.
|
49
|
+
|
50
|
+
This command is mutually exclusive with --repo
|
51
|
+
""",
|
52
|
+
)
|
53
|
+
|
54
|
+
subparsers = parser.add_subparsers(
|
55
|
+
help="commands",
|
56
|
+
dest="method",
|
57
|
+
)
|
58
|
+
subparsers.required = True
|
59
|
+
|
60
|
+
migrate_parser = subparsers.add_parser(
|
61
|
+
"migrate",
|
62
|
+
help="migrate to new sync format",
|
63
|
+
)
|
64
|
+
migrate_parser.set_defaults(
|
65
|
+
method="migrate",
|
66
|
+
)
|
67
|
+
|
68
|
+
snap_cleanup_parser = subparsers.add_parser(
|
69
|
+
"snap_cleanup",
|
70
|
+
help="remove all unnamed snapshots and unreferenced rpms.",
|
71
|
+
)
|
72
|
+
snap_cleanup_parser.set_defaults(
|
73
|
+
method="snap_cleanup",
|
74
|
+
)
|
75
|
+
|
76
|
+
snap_list_parser = subparsers.add_parser(
|
77
|
+
"snap_list",
|
78
|
+
help="list snapshots",
|
79
|
+
)
|
80
|
+
snap_list_parser.set_defaults(
|
81
|
+
method="snap_list",
|
82
|
+
)
|
83
|
+
|
84
|
+
snap_name_parser = subparsers.add_parser(
|
85
|
+
"snap_name",
|
86
|
+
help="give timed snapshot a name",
|
87
|
+
)
|
88
|
+
snap_name_parser.set_defaults(
|
89
|
+
method="snap_name",
|
90
|
+
)
|
91
|
+
snap_name_parser.add_argument(
|
92
|
+
"--timestamp",
|
93
|
+
dest="timestamp",
|
94
|
+
action="store",
|
95
|
+
required=True,
|
96
|
+
default=None,
|
97
|
+
help="source timestampm might also be a named snapshot or latest",
|
98
|
+
)
|
99
|
+
snap_name_parser.add_argument(
|
100
|
+
"--name",
|
101
|
+
dest="snapname",
|
102
|
+
action="store",
|
103
|
+
required=True,
|
104
|
+
default=None,
|
105
|
+
help="name to be created",
|
106
|
+
)
|
107
|
+
|
108
|
+
snap_unname_parser = subparsers.add_parser(
|
109
|
+
"snap_unname",
|
110
|
+
help="remove name from timed snapshot",
|
111
|
+
)
|
112
|
+
snap_unname_parser.set_defaults(
|
113
|
+
method="snap_unname",
|
114
|
+
)
|
115
|
+
snap_unname_parser.add_argument(
|
116
|
+
"--name",
|
117
|
+
dest="snapname",
|
118
|
+
action="store",
|
119
|
+
required=True,
|
120
|
+
help="name to be removed",
|
121
|
+
)
|
122
|
+
|
123
|
+
snap_parser = subparsers.add_parser(
|
124
|
+
"snap",
|
125
|
+
help="create new snapshots",
|
126
|
+
)
|
127
|
+
snap_parser.set_defaults(
|
128
|
+
method="snap",
|
129
|
+
)
|
130
|
+
|
131
|
+
sync_parser = subparsers.add_parser(
|
132
|
+
"sync",
|
133
|
+
help="sync all repos",
|
134
|
+
)
|
135
|
+
sync_parser.set_defaults(
|
136
|
+
method="sync",
|
137
|
+
)
|
138
|
+
|
139
|
+
validate_parser = subparsers.add_parser(
|
140
|
+
"validate",
|
141
|
+
help="re validate package downloads",
|
142
|
+
)
|
143
|
+
validate_parser.set_defaults(
|
144
|
+
method="validate",
|
145
|
+
)
|
146
|
+
|
147
|
+
parsed_args = parser.parse_args()
|
148
|
+
try:
|
149
|
+
snapname = parsed_args.snapname
|
150
|
+
except AttributeError:
|
151
|
+
snapname = None
|
152
|
+
try:
|
153
|
+
timestamp = parsed_args.timestamp
|
154
|
+
except AttributeError:
|
155
|
+
timestamp = None
|
156
|
+
|
157
|
+
osreposync = PyRepoSync(
|
158
|
+
cfg=parsed_args.cfg,
|
159
|
+
method=parsed_args.method,
|
160
|
+
snapname=snapname,
|
161
|
+
repo=parsed_args.repo,
|
162
|
+
tags=parsed_args.tags,
|
163
|
+
timestamp=timestamp,
|
164
|
+
)
|
165
|
+
osreposync.work()
|
166
|
+
|
167
|
+
|
168
|
+
class PyRepoSync:
|
169
|
+
def __init__(self, cfg, snapname, method, repo, tags, timestamp):
|
170
|
+
self._config_file = cfg
|
171
|
+
self._config = configparser.ConfigParser()
|
172
|
+
self._config_dict = None
|
173
|
+
self._method = method
|
174
|
+
self._snapname = snapname
|
175
|
+
self._repo = repo
|
176
|
+
self._tags = None
|
177
|
+
self._timestamp = timestamp
|
178
|
+
self.tags = tags
|
179
|
+
self.log = logging.getLogger("application")
|
180
|
+
self.config.read_file(open(self._config_file))
|
181
|
+
self._config_dict = self._cfg_to_dict(self.config)
|
182
|
+
self._logging()
|
183
|
+
if self._tags and self._repo:
|
184
|
+
self.log.fatal("both tags & repo have been specified, choose one")
|
185
|
+
|
186
|
+
@property
|
187
|
+
def method(self):
|
188
|
+
return self._method
|
189
|
+
|
190
|
+
@property
|
191
|
+
def snapname(self):
|
192
|
+
return self._snapname
|
193
|
+
|
194
|
+
@property
|
195
|
+
def repo(self):
|
196
|
+
return self._repo
|
197
|
+
|
198
|
+
@property
|
199
|
+
def tags(self):
|
200
|
+
return self._tags
|
201
|
+
|
202
|
+
@tags.setter
|
203
|
+
def tags(self, tags):
|
204
|
+
if tags:
|
205
|
+
self._tags = tags.split(",")
|
206
|
+
|
207
|
+
@property
|
208
|
+
def timestamp(self):
|
209
|
+
return self._timestamp
|
210
|
+
|
211
|
+
def _logging(self):
|
212
|
+
logfmt = logging.Formatter(
|
213
|
+
"%(asctime)sUTC - %(levelname)s - %(threadName)s - %(message)s"
|
214
|
+
)
|
215
|
+
logfmt.converter = time.gmtime
|
216
|
+
aap_level = self.config.get("main", "loglevel")
|
217
|
+
handler = logging.StreamHandler()
|
218
|
+
|
219
|
+
handler.setFormatter(logfmt)
|
220
|
+
self.log.addHandler(handler)
|
221
|
+
self.log.setLevel(aap_level)
|
222
|
+
self.log.debug("logger is up")
|
223
|
+
|
224
|
+
@staticmethod
|
225
|
+
def _cfg_to_dict(config):
|
226
|
+
result = {}
|
227
|
+
for section in config.sections():
|
228
|
+
result[section] = {}
|
229
|
+
for option in config.options(section):
|
230
|
+
try:
|
231
|
+
result[section][option] = config.getint(section, option)
|
232
|
+
continue
|
233
|
+
except ValueError:
|
234
|
+
pass
|
235
|
+
try:
|
236
|
+
result[section][option] = config.getfloat(section, option)
|
237
|
+
continue
|
238
|
+
except ValueError:
|
239
|
+
pass
|
240
|
+
try:
|
241
|
+
result[section][option] = config.getboolean(section, option)
|
242
|
+
continue
|
243
|
+
except ValueError:
|
244
|
+
pass
|
245
|
+
try:
|
246
|
+
result[section][option] = config.get(section, option)
|
247
|
+
continue
|
248
|
+
except ValueError:
|
249
|
+
pass
|
250
|
+
return result
|
251
|
+
|
252
|
+
@property
|
253
|
+
def config(self):
|
254
|
+
return self._config
|
255
|
+
|
256
|
+
@property
|
257
|
+
def config_dict(self):
|
258
|
+
return self._config_dict
|
259
|
+
|
260
|
+
def get_job(self, date, section):
|
261
|
+
self.log.info(f"section name: {section}")
|
262
|
+
if section.endswith(":rpm"):
|
263
|
+
return SyncRPM(
|
264
|
+
base_url=self.config.get(section, "baseurl"),
|
265
|
+
destination=self.config.get("main", "destination"),
|
266
|
+
reponame=section[:-4],
|
267
|
+
date=date,
|
268
|
+
treeinfo=self.config.get(section, "treeinfo", fallback=".treeinfo"),
|
269
|
+
proxy=self.config.get("main", "proxy", fallback=None),
|
270
|
+
client_cert=self.config.get(section, "sslclientcert", fallback=None),
|
271
|
+
client_key=self.config.get(section, "sslclientkey", fallback=None),
|
272
|
+
ca_cert=self.config.get(section, "sslcacert", fallback=None),
|
273
|
+
)
|
274
|
+
elif section.endswith(":deb"):
|
275
|
+
return SyncDeb(
|
276
|
+
base_url=self.config.get(section, "baseurl"),
|
277
|
+
destination=self.config.get("main", "destination"),
|
278
|
+
reponame=section[:-4],
|
279
|
+
date=date,
|
280
|
+
proxy=self.config.get(section, "proxy", fallback=None),
|
281
|
+
client_cert=self.config.get(section, "sslclientcert", fallback=None),
|
282
|
+
client_key=self.config.get(section, "sslclientkey", fallback=None),
|
283
|
+
ca_cert=self.config.get(section, "sslcacert", fallback=None),
|
284
|
+
suites=self.config.get(section, "suites").split(),
|
285
|
+
components=self.config.get(section, "components").split(),
|
286
|
+
binary_archs=self.config.get(section, "binary_archs").split(),
|
287
|
+
)
|
288
|
+
|
289
|
+
def get_sections(self):
|
290
|
+
sections = set()
|
291
|
+
for section in self.config:
|
292
|
+
if section.endswith(":rpm") or section.endswith(":deb"):
|
293
|
+
if self.repo and section != self.repo:
|
294
|
+
continue
|
295
|
+
if self._tags:
|
296
|
+
if not self.validate_tags(section):
|
297
|
+
continue
|
298
|
+
sections.add(section)
|
299
|
+
|
300
|
+
return sections
|
301
|
+
|
302
|
+
def work(self):
|
303
|
+
self.log.info("starting up")
|
304
|
+
date = datetime.datetime.utcnow().strftime("%Y%m%d%H%M%S")
|
305
|
+
queue = collections.deque()
|
306
|
+
for section in self.get_sections():
|
307
|
+
queue.append(self.get_job(date=date, section=section))
|
308
|
+
workers = set()
|
309
|
+
if self.method == "sync":
|
310
|
+
num_worker = self.config.getint("main", "downloaders", fallback=1)
|
311
|
+
else:
|
312
|
+
num_worker = 1
|
313
|
+
for _ in range(num_worker):
|
314
|
+
workers.add(
|
315
|
+
RepoSyncThread(
|
316
|
+
queue=queue,
|
317
|
+
action=self.method,
|
318
|
+
snapname=self.snapname,
|
319
|
+
timestamp=self.timestamp,
|
320
|
+
)
|
321
|
+
)
|
322
|
+
|
323
|
+
for worker in workers:
|
324
|
+
worker.start()
|
325
|
+
return_code = 0
|
326
|
+
for worker in workers:
|
327
|
+
worker.join()
|
328
|
+
if worker.status != 0:
|
329
|
+
return_code = 1
|
330
|
+
sys.exit(return_code)
|
331
|
+
|
332
|
+
def validate_tags(self, section):
|
333
|
+
try:
|
334
|
+
section_tags = self.config.get(section, "tags").split(",")
|
335
|
+
except Exception as err:
|
336
|
+
return False
|
337
|
+
for tag in self.tags:
|
338
|
+
if tag.startswith("!"):
|
339
|
+
if tag[1:] in section_tags:
|
340
|
+
return False
|
341
|
+
else:
|
342
|
+
if tag not in section_tags:
|
343
|
+
return False
|
344
|
+
self.log.info(f"section {section} has matching tags")
|
345
|
+
return True
|
346
|
+
|
347
|
+
|
348
|
+
class RepoSyncThread(threading.Thread):
|
349
|
+
def __init__(self, queue, action, snapname, timestamp):
|
350
|
+
super().__init__()
|
351
|
+
self._action = action
|
352
|
+
self._snapname = snapname
|
353
|
+
self._queue = queue
|
354
|
+
self._status = 0
|
355
|
+
self._timestamp = timestamp
|
356
|
+
self.daemon = True
|
357
|
+
self.log = logging.getLogger("application")
|
358
|
+
|
359
|
+
@property
|
360
|
+
def action(self):
|
361
|
+
return self._action
|
362
|
+
|
363
|
+
@property
|
364
|
+
def snapname(self):
|
365
|
+
return self._snapname
|
366
|
+
|
367
|
+
@property
|
368
|
+
def queue(self):
|
369
|
+
return self._queue
|
370
|
+
|
371
|
+
@property
|
372
|
+
def status(self):
|
373
|
+
return self._status
|
374
|
+
|
375
|
+
@status.setter
|
376
|
+
def status(self, value):
|
377
|
+
self._status = value
|
378
|
+
|
379
|
+
@property
|
380
|
+
def timestamp(self):
|
381
|
+
return self._timestamp
|
382
|
+
|
383
|
+
def do_migrate(self, job):
|
384
|
+
try:
|
385
|
+
self.name = job.reponame
|
386
|
+
self.log.info(f"{self.action} start repo {job.reponame}")
|
387
|
+
job.migrate()
|
388
|
+
self.log.info(f"{self.action} done repo {job.reponame}")
|
389
|
+
except OSRepoSyncException:
|
390
|
+
self.log.fatal(f"could not {self.action} repo {job.reponame}")
|
391
|
+
self.status = 1
|
392
|
+
|
393
|
+
def do_sync(self, job):
|
394
|
+
try:
|
395
|
+
self.name = job.reponame
|
396
|
+
self.log.info(f"{self.action} start repo {job.reponame}")
|
397
|
+
job.sync()
|
398
|
+
self.log.info(f"{self.action} done repo {job.reponame}")
|
399
|
+
except OSRepoSyncException:
|
400
|
+
self.log.fatal(f"could not {self.action} repo {job.reponame}")
|
401
|
+
self.status = 1
|
402
|
+
|
403
|
+
def do_snap(self, job):
|
404
|
+
try:
|
405
|
+
self.name = job.reponame
|
406
|
+
self.log.info(f"{self.action} start repo {job.reponame}")
|
407
|
+
job.snap()
|
408
|
+
self.log.info(f"{self.action} done repo {job.reponame}")
|
409
|
+
except OSRepoSyncException:
|
410
|
+
self.log.fatal(f"could not {self.action} repo {job.reponame}")
|
411
|
+
self.status = 1
|
412
|
+
|
413
|
+
def do_snap_cleanup(self, job):
|
414
|
+
try:
|
415
|
+
self.name = job.reponame
|
416
|
+
self.log.info(f"{self.action} start repo {job.reponame}")
|
417
|
+
job.snap_cleanup()
|
418
|
+
self.log.info(f"{self.action} done repo {job.reponame}")
|
419
|
+
except OSRepoSyncException:
|
420
|
+
self.log.fatal(f"could not {self.action} repo {job.reponame}")
|
421
|
+
self.status = 1
|
422
|
+
|
423
|
+
def do_snap_list(self, job):
|
424
|
+
try:
|
425
|
+
self.name = job.reponame
|
426
|
+
referenced_timestamps = job.snap_list_get_referenced_timestamps()
|
427
|
+
self.log.info(f"Repository: {job.reponame}")
|
428
|
+
self.log.info("The following timestamp snapshots exist:")
|
429
|
+
for timestamp in job.snap_list_timestamp_snapshots():
|
430
|
+
self.log.info(
|
431
|
+
f"{timestamp} -> {referenced_timestamps.get(timestamp, [])}"
|
432
|
+
)
|
433
|
+
self.log.info("The following named snapshots exist:")
|
434
|
+
base = f"{job.destination}/snap/{job.reponame}/"
|
435
|
+
for named in job.snap_list_named_snapshots():
|
436
|
+
timestamp = job.snap_list_named_snapshot_target(f"{base}/named/{named}")
|
437
|
+
self.log.info(f"named/{named} -> {timestamp}")
|
438
|
+
latest = f"{base}/latest"
|
439
|
+
self.log.info(f"latest -> {job.snap_list_named_snapshot_target(latest)}")
|
440
|
+
|
441
|
+
except OSRepoSyncException:
|
442
|
+
self.status = 1
|
443
|
+
|
444
|
+
def do_snap_name(self, job):
|
445
|
+
try:
|
446
|
+
self.name = job.reponame
|
447
|
+
self.log.info(f"{self.action} start repo {job.reponame}")
|
448
|
+
job.snap_name(self.timestamp, self.snapname)
|
449
|
+
self.log.info(f"{self.action} done repo {job.reponame}")
|
450
|
+
except OSRepoSyncException:
|
451
|
+
self.log.fatal(f"could not {self.action} repo {job.reponame}")
|
452
|
+
self.status = 1
|
453
|
+
|
454
|
+
def do_snap_unname(self, job):
|
455
|
+
try:
|
456
|
+
self.name = job.reponame
|
457
|
+
self.log.info(f"{self.action} start repo {job.reponame}")
|
458
|
+
job.snap_unname(self.snapname)
|
459
|
+
self.log.info(f"{self.action} done repo {job.reponame}")
|
460
|
+
except OSRepoSyncException:
|
461
|
+
self.log.fatal(f"could not {self.action} repo {job.reponame}")
|
462
|
+
self.status = 1
|
463
|
+
|
464
|
+
def do_validate(self, job):
|
465
|
+
_downloader = Downloader()
|
466
|
+
packages = dict()
|
467
|
+
try:
|
468
|
+
self.log.info(f"{self.action} start repo {job.reponame}")
|
469
|
+
packages.update(job.revalidate())
|
470
|
+
except OSRepoSyncException:
|
471
|
+
self.log.fatal(f"could not {self.action} repo {job.reponame}")
|
472
|
+
self.status = 1
|
473
|
+
for destination, hash_info in packages.items():
|
474
|
+
try:
|
475
|
+
self.log.info(f"validating: {destination}")
|
476
|
+
_downloader.check_hash(
|
477
|
+
destination=destination,
|
478
|
+
checksum=hash_info["hash_sum"],
|
479
|
+
hash_type=hash_info["hash_algo"],
|
480
|
+
)
|
481
|
+
except OSRepoSyncHashError:
|
482
|
+
self.log.error(f"hash mismatch for: {destination}")
|
483
|
+
except FileNotFoundError:
|
484
|
+
self.log.error(f"file not found: {destination}")
|
485
|
+
|
486
|
+
def run(self):
|
487
|
+
while True:
|
488
|
+
try:
|
489
|
+
job = self.queue.pop()
|
490
|
+
if self.action == "migrate":
|
491
|
+
self.do_migrate(job)
|
492
|
+
elif self.action == "sync":
|
493
|
+
self.do_sync(job)
|
494
|
+
elif self.action == "snap_cleanup":
|
495
|
+
self.do_snap_cleanup(job)
|
496
|
+
elif self.action == "snap_list":
|
497
|
+
self.do_snap_list(job)
|
498
|
+
elif self.action == "snap_name":
|
499
|
+
self.do_snap_name(job)
|
500
|
+
elif self.action == "snap_unname":
|
501
|
+
self.do_snap_unname(job)
|
502
|
+
elif self.action == "snap":
|
503
|
+
self.do_snap(job)
|
504
|
+
elif self.action == "validate":
|
505
|
+
self.do_validate(job)
|
506
|
+
except IndexError:
|
507
|
+
break
|
508
|
+
|
509
|
+
|
510
|
+
if __name__ == "__main__":
|
511
|
+
main()
|
pyreposync/downloader.py
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
import hashlib
|
2
|
+
import logging
|
3
|
+
import os
|
4
|
+
import requests
|
5
|
+
import requests.exceptions
|
6
|
+
import shutil
|
7
|
+
import tempfile
|
8
|
+
import time
|
9
|
+
|
10
|
+
from pyreposync.exceptions import OSRepoSyncDownLoadError, OSRepoSyncHashError
|
11
|
+
|
12
|
+
|
13
|
+
class Downloader(object):
|
14
|
+
def __init__(self, proxy=None, client_cert=None, client_key=None, ca_cert=None):
|
15
|
+
self.log = logging.getLogger("application")
|
16
|
+
if proxy:
|
17
|
+
self._proxy = {"http": proxy, "https": proxy}
|
18
|
+
else:
|
19
|
+
self._proxy = None
|
20
|
+
if client_cert and client_key:
|
21
|
+
self._cert = (client_cert, client_key)
|
22
|
+
else:
|
23
|
+
self._cert = None
|
24
|
+
if ca_cert:
|
25
|
+
self._ca_cert = ca_cert
|
26
|
+
else:
|
27
|
+
self._ca_cert = True
|
28
|
+
|
29
|
+
@property
|
30
|
+
def ca_cert(self):
|
31
|
+
return self._ca_cert
|
32
|
+
|
33
|
+
@property
|
34
|
+
def cert(self):
|
35
|
+
return self._cert
|
36
|
+
|
37
|
+
@property
|
38
|
+
def proxy(self):
|
39
|
+
return self._proxy
|
40
|
+
|
41
|
+
def check_hash(self, destination, checksum, hash_type):
|
42
|
+
self.log.debug("validating hash")
|
43
|
+
hasher = None
|
44
|
+
if hash_type == "md5":
|
45
|
+
hasher = hashlib.md5()
|
46
|
+
elif hash_type == "sha":
|
47
|
+
hasher = hashlib.sha1()
|
48
|
+
elif hash_type == "sha1":
|
49
|
+
hasher = hashlib.sha1()
|
50
|
+
elif hash_type == "sha256":
|
51
|
+
hasher = hashlib.sha256()
|
52
|
+
elif hash_type == "sha512":
|
53
|
+
hasher = hashlib.sha512()
|
54
|
+
|
55
|
+
with open(destination, "rb") as dest:
|
56
|
+
hasher.update(dest.read())
|
57
|
+
self.log.debug(f"expected hash: {hasher.hexdigest()}")
|
58
|
+
self.log.debug(f"actual hash: {checksum}")
|
59
|
+
if hasher.hexdigest() == checksum:
|
60
|
+
self.log.debug(f"download valid: {destination}")
|
61
|
+
else:
|
62
|
+
self.log.error(f"download invalid: {destination}")
|
63
|
+
raise OSRepoSyncHashError(f"download invalid: {destination}")
|
64
|
+
|
65
|
+
def get(
|
66
|
+
self,
|
67
|
+
url,
|
68
|
+
destination,
|
69
|
+
checksum=None,
|
70
|
+
hash_type=None,
|
71
|
+
replace=False,
|
72
|
+
not_found_ok=False,
|
73
|
+
):
|
74
|
+
self.log.info(f"downloading: {url}")
|
75
|
+
if not replace:
|
76
|
+
if os.path.isfile(destination):
|
77
|
+
self.log.info("already there, not downloading")
|
78
|
+
return
|
79
|
+
retries = 10
|
80
|
+
while retries >= 0:
|
81
|
+
try:
|
82
|
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
83
|
+
tmp_file = os.path.join(tmp_dir, os.path.basename(destination))
|
84
|
+
self._get(url, tmp_file, checksum, hash_type, not_found_ok)
|
85
|
+
self.create_dir(destination)
|
86
|
+
try:
|
87
|
+
shutil.move(tmp_file, destination)
|
88
|
+
except OSError:
|
89
|
+
if not_found_ok:
|
90
|
+
pass
|
91
|
+
else:
|
92
|
+
raise
|
93
|
+
self.log.info(f"done downloading: {url}")
|
94
|
+
return
|
95
|
+
except requests.exceptions.ConnectionError:
|
96
|
+
self.log.error("could not fetch resource, retry in 10 seconds")
|
97
|
+
retries -= 1
|
98
|
+
time.sleep(10)
|
99
|
+
except OSRepoSyncHashError:
|
100
|
+
self.log.error("download invalid, retry in 10 seconds")
|
101
|
+
retries -= 1
|
102
|
+
time.sleep(10)
|
103
|
+
except OSRepoSyncDownLoadError:
|
104
|
+
break
|
105
|
+
self.log.error(f"could not download: {url}")
|
106
|
+
raise OSRepoSyncDownLoadError(f"could not download: {url}")
|
107
|
+
|
108
|
+
def create_dir(self, destination):
|
109
|
+
if not os.path.isdir(os.path.dirname(destination)):
|
110
|
+
try:
|
111
|
+
os.makedirs(os.path.dirname(destination))
|
112
|
+
except OSError as err:
|
113
|
+
self.log.error(f"could not create directory: {err}")
|
114
|
+
raise OSRepoSyncDownLoadError(f"could not create directory: {err}")
|
115
|
+
|
116
|
+
def _get(
|
117
|
+
self,
|
118
|
+
url,
|
119
|
+
destination,
|
120
|
+
checksum=None,
|
121
|
+
hash_type=None,
|
122
|
+
not_found_ok=False,
|
123
|
+
):
|
124
|
+
self.create_dir(destination)
|
125
|
+
r = requests.get(
|
126
|
+
url, stream=True, proxies=self.proxy, cert=self.cert, verify=self.ca_cert
|
127
|
+
)
|
128
|
+
if r.status_code == 200:
|
129
|
+
with open(destination, "wb", 0) as dst:
|
130
|
+
r.raw.decode_content = True
|
131
|
+
shutil.copyfileobj(r.raw, dst)
|
132
|
+
dst.flush()
|
133
|
+
else:
|
134
|
+
if not_found_ok:
|
135
|
+
if r.status_code == 404:
|
136
|
+
self.log.info("not found, skipping")
|
137
|
+
return
|
138
|
+
raise OSRepoSyncDownLoadError()
|
139
|
+
if checksum:
|
140
|
+
self.check_hash(
|
141
|
+
destination=destination, checksum=checksum, hash_type=hash_type
|
142
|
+
)
|
143
|
+
self.log.info(f"successfully fetched: {url}")
|
pyreposync/exceptions.py
ADDED
pyreposync/sync_deb.py
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
from shutil import copyfile
|
2
|
+
|
3
|
+
import gzip
|
4
|
+
import os
|
5
|
+
|
6
|
+
from pyreposync.sync_generic import SyncGeneric
|
7
|
+
|
8
|
+
|
9
|
+
class SyncDeb(SyncGeneric):
|
10
|
+
def __init__(
|
11
|
+
self,
|
12
|
+
base_url,
|
13
|
+
destination,
|
14
|
+
reponame,
|
15
|
+
date,
|
16
|
+
suites,
|
17
|
+
components,
|
18
|
+
binary_archs,
|
19
|
+
proxy=None,
|
20
|
+
client_cert=None,
|
21
|
+
client_key=None,
|
22
|
+
ca_cert=None,
|
23
|
+
):
|
24
|
+
super().__init__(
|
25
|
+
base_url,
|
26
|
+
destination,
|
27
|
+
reponame,
|
28
|
+
date,
|
29
|
+
proxy,
|
30
|
+
client_cert,
|
31
|
+
client_key,
|
32
|
+
ca_cert,
|
33
|
+
)
|
34
|
+
self._binary_archs = binary_archs
|
35
|
+
self._components = components
|
36
|
+
self._destination = destination
|
37
|
+
self._suites = suites
|
38
|
+
|
39
|
+
@property
|
40
|
+
def binary_archs(self) -> list[str]:
|
41
|
+
return self._binary_archs
|
42
|
+
|
43
|
+
@property
|
44
|
+
def suites(self) -> list[str]:
|
45
|
+
return self._suites
|
46
|
+
|
47
|
+
@property
|
48
|
+
def components(self) -> list[str]:
|
49
|
+
return self._components
|
50
|
+
|
51
|
+
def _snap(self):
|
52
|
+
for suite in self.suites:
|
53
|
+
self.snap_suites(suite=suite)
|
54
|
+
|
55
|
+
def snap_suites(self, suite):
|
56
|
+
self.log.info(f"creating snapshot for suite {suite}")
|
57
|
+
self.snap_release(suite=suite)
|
58
|
+
self.snap_release_files(suite=suite)
|
59
|
+
for arch in self.binary_archs:
|
60
|
+
self.snap_package_binary_files(suite=suite, arch=arch)
|
61
|
+
self.log.info(f"creating snapshot for suite {suite}, done")
|
62
|
+
|
63
|
+
def snap_release(self, suite):
|
64
|
+
self.log.info(f"creating snapshot for suite {suite} release files")
|
65
|
+
release_files = ["InRelease", "Release", "Release.gpg"]
|
66
|
+
src_path = f"{self.destination}/sync/{self.reponame}/dists/{suite}"
|
67
|
+
dst_path = f"{self.destination}/snap/{self.reponame}/{self.date}/dists/{suite}"
|
68
|
+
try:
|
69
|
+
os.makedirs(dst_path)
|
70
|
+
except OSError:
|
71
|
+
pass
|
72
|
+
for release_file in release_files:
|
73
|
+
src = f"{src_path}/{release_file}"
|
74
|
+
dst = f"{dst_path}/{release_file}"
|
75
|
+
copyfile(src, dst)
|
76
|
+
self.log.info(f"creating snapshot for suite {suite} release files, done")
|
77
|
+
|
78
|
+
def snap_release_files(self, suite):
|
79
|
+
self.log.info(f"creating snapshot for suite {suite} release files")
|
80
|
+
release_files = self.release_files_sha256(suite=suite)
|
81
|
+
src_path = f"{self.destination}/sync/{self.reponame}/dists/{suite}"
|
82
|
+
dst_path = f"{self.destination}/snap/{self.reponame}/{self.date}/dists/{suite}"
|
83
|
+
for filename, sha256_dict in release_files.items():
|
84
|
+
src = f"{src_path}/{filename}"
|
85
|
+
dst = f"{dst_path}/{filename}"
|
86
|
+
try:
|
87
|
+
os.makedirs(os.path.dirname(dst))
|
88
|
+
except OSError:
|
89
|
+
pass
|
90
|
+
try:
|
91
|
+
copyfile(src, dst)
|
92
|
+
except FileNotFoundError:
|
93
|
+
pass
|
94
|
+
self.log.info(f"creating snapshot for suite {suite} release files, done")
|
95
|
+
|
96
|
+
def snap_package_binary_files(self, suite, arch):
|
97
|
+
self.log.info(
|
98
|
+
f"creating snapshot for suite {suite} arch {arch} package binary files"
|
99
|
+
)
|
100
|
+
packages = self.binary_files_sha256(suite=suite, component="main", arch=arch)
|
101
|
+
src_path = f"{self.destination}/sync/{self.reponame}"
|
102
|
+
dst_path = f"{self.destination}/snap/{self.reponame}/{self.date}"
|
103
|
+
for filename, sha256_dict in packages.items():
|
104
|
+
src = f"{src_path}/{filename}.sha256.{sha256_dict['sha256']}"
|
105
|
+
dst = f"{dst_path}/{filename}"
|
106
|
+
try:
|
107
|
+
os.makedirs(os.path.dirname(dst))
|
108
|
+
except OSError:
|
109
|
+
pass
|
110
|
+
try:
|
111
|
+
os.symlink(src, dst)
|
112
|
+
except FileExistsError:
|
113
|
+
pass
|
114
|
+
|
115
|
+
def sync(self):
|
116
|
+
self.log.info("starting thread")
|
117
|
+
for suite in self.suites:
|
118
|
+
self.sync_suites(suite=suite)
|
119
|
+
self.log.info("shutdown thread complete")
|
120
|
+
|
121
|
+
def sync_suites(self, suite):
|
122
|
+
self.log.info(f"syncing suite {suite}")
|
123
|
+
self.sync_release(suite=suite)
|
124
|
+
self.sync_release_files(suite=suite)
|
125
|
+
for arch in self.binary_archs:
|
126
|
+
self.sync_package_binary_files(suite=suite, arch=arch)
|
127
|
+
self.log.info(f"syncing suite {suite}, done")
|
128
|
+
|
129
|
+
def sync_release(self, suite):
|
130
|
+
self.log.info(f"syncing suite {suite} release files")
|
131
|
+
release_files = ["InRelease", "Release", "Release.gpg"]
|
132
|
+
base_path = f"{self.destination}/sync/{self.reponame}/dists/{suite}"
|
133
|
+
base_url = f"{self.base_url}/dists/{suite}"
|
134
|
+
self.log.info(base_url)
|
135
|
+
for release_file in release_files:
|
136
|
+
self.downloader.get(
|
137
|
+
url=f"{base_url}/{release_file}",
|
138
|
+
destination=f"{base_path}/{release_file}",
|
139
|
+
replace=True,
|
140
|
+
)
|
141
|
+
self.log.info(f"syncing suite {suite} release files, done")
|
142
|
+
|
143
|
+
def sync_package_binary_files(self, suite, arch):
|
144
|
+
self.log.info(f"syncing suite {suite} arch {arch} package binary files")
|
145
|
+
packages = self.binary_files_sha256(suite=suite, component="main", arch=arch)
|
146
|
+
base_path = f"{self.destination}/sync/{self.reponame}"
|
147
|
+
base_url = f"{self.base_url}"
|
148
|
+
for filename, sha256_dict in packages.items():
|
149
|
+
self.downloader.get(
|
150
|
+
url=f"{base_url}/{filename}",
|
151
|
+
destination=f"{base_path}/{filename}.sha256.{sha256_dict['sha256']}",
|
152
|
+
checksum=sha256_dict["sha256"],
|
153
|
+
hash_type="sha256",
|
154
|
+
)
|
155
|
+
|
156
|
+
self.log.info(f"syncing suite {suite} arch {arch} package binary files, done")
|
157
|
+
|
158
|
+
def binary_files_sha256(self, suite, component, arch):
|
159
|
+
packages_gz_file = f"{self.destination}/sync/{self.reponame}/dists/{suite}/{component}/binary-{arch}/Packages.gz"
|
160
|
+
packages = dict()
|
161
|
+
sha256 = None
|
162
|
+
filename = None
|
163
|
+
size = None
|
164
|
+
with gzip.open(packages_gz_file, "rb") as source:
|
165
|
+
for line in source:
|
166
|
+
line = line.decode("utf-8")
|
167
|
+
if line.startswith("SHA256: "):
|
168
|
+
sha256 = line.split("SHA256: ")[1].strip()
|
169
|
+
elif line.startswith("Filename: "):
|
170
|
+
filename = line.split("Filename: ")[1].strip()
|
171
|
+
elif line.startswith("Size: "):
|
172
|
+
size = int(line.split("Size: ")[1].strip())
|
173
|
+
if filename and sha256 and size:
|
174
|
+
packages[filename] = {
|
175
|
+
"sha256": sha256,
|
176
|
+
"size": size,
|
177
|
+
}
|
178
|
+
sha256 = None
|
179
|
+
filename = None
|
180
|
+
size = None
|
181
|
+
return packages
|
182
|
+
|
183
|
+
def sync_release_files(self, suite):
|
184
|
+
self.log.info(f"syncing suite {suite} release files")
|
185
|
+
release_files = self.release_files_sha256(suite=suite)
|
186
|
+
base_path = f"{self.destination}/sync/{self.reponame}/dists/{suite}"
|
187
|
+
base_url = f"{self.base_url}/dists/{suite}"
|
188
|
+
for filename, sha256_dict in release_files.items():
|
189
|
+
self.downloader.get(
|
190
|
+
url=f"{base_url}/{filename}",
|
191
|
+
destination=f"{base_path}/{filename}",
|
192
|
+
checksum=sha256_dict["sha256"],
|
193
|
+
hash_type="sha256",
|
194
|
+
replace=True,
|
195
|
+
not_found_ok=True,
|
196
|
+
)
|
197
|
+
self.log.info(f"syncing suite {suite} release files, done")
|
198
|
+
|
199
|
+
def release_files_sha256(self, suite):
|
200
|
+
release = f"{self.destination}/sync/{self.reponame}/dists/{suite}/Release"
|
201
|
+
with open(release, "r") as release_file:
|
202
|
+
release_file_content = release_file.read()
|
203
|
+
sha256_dict = {}
|
204
|
+
in_sha256_section = False
|
205
|
+
for line in release_file_content.splitlines():
|
206
|
+
if line.startswith("SHA256:"):
|
207
|
+
in_sha256_section = True
|
208
|
+
continue
|
209
|
+
if in_sha256_section:
|
210
|
+
if line.startswith(" "):
|
211
|
+
sha256, size, filename = line.split()
|
212
|
+
sha256_dict[filename] = {
|
213
|
+
"sha256": sha256,
|
214
|
+
"size": int(size),
|
215
|
+
}
|
216
|
+
else:
|
217
|
+
break
|
218
|
+
return sha256_dict
|
@@ -0,0 +1,169 @@
|
|
1
|
+
import logging
|
2
|
+
import os
|
3
|
+
import shutil
|
4
|
+
|
5
|
+
from pyreposync.downloader import Downloader
|
6
|
+
from pyreposync.exceptions import OSRepoSyncException
|
7
|
+
|
8
|
+
|
9
|
+
class SyncGeneric:
|
10
|
+
def __init__(
|
11
|
+
self,
|
12
|
+
base_url,
|
13
|
+
destination,
|
14
|
+
reponame,
|
15
|
+
date,
|
16
|
+
proxy=None,
|
17
|
+
client_cert=None,
|
18
|
+
client_key=None,
|
19
|
+
ca_cert=None,
|
20
|
+
):
|
21
|
+
self._base_url = base_url
|
22
|
+
self._date = date
|
23
|
+
self._destination = destination
|
24
|
+
self._reponame = reponame
|
25
|
+
self.downloader = Downloader(
|
26
|
+
proxy=proxy, client_cert=client_cert, client_key=client_key, ca_cert=ca_cert
|
27
|
+
)
|
28
|
+
self.log = logging.getLogger("application")
|
29
|
+
|
30
|
+
@property
|
31
|
+
def base_url(self):
|
32
|
+
return self._base_url
|
33
|
+
|
34
|
+
@property
|
35
|
+
def date(self):
|
36
|
+
return self._date
|
37
|
+
|
38
|
+
@property
|
39
|
+
def destination(self):
|
40
|
+
return self._destination
|
41
|
+
|
42
|
+
@property
|
43
|
+
def reponame(self):
|
44
|
+
return self._reponame
|
45
|
+
|
46
|
+
def migrate(self):
|
47
|
+
pass
|
48
|
+
|
49
|
+
def revalidate(self):
|
50
|
+
pass
|
51
|
+
|
52
|
+
def _snap(self):
|
53
|
+
raise NotImplementedError("this method must be implemented by subclasses")
|
54
|
+
|
55
|
+
def snap(self):
|
56
|
+
self.log.info("creating snapshot")
|
57
|
+
self._snap()
|
58
|
+
current = f"{self.destination}/snap/{self.reponame}/{self.date}"
|
59
|
+
latest = f"{self.destination}/snap/{self.reponame}/latest"
|
60
|
+
timestamp = f"{self.destination}/snap/{self.reponame}/{self.date}/timestamp"
|
61
|
+
self.log.info("setting latest to current release")
|
62
|
+
try:
|
63
|
+
os.unlink(latest)
|
64
|
+
except FileNotFoundError:
|
65
|
+
pass
|
66
|
+
os.symlink(current, latest)
|
67
|
+
with open(timestamp, "w") as _timestamp:
|
68
|
+
_timestamp.write(f"{self.destination}\n")
|
69
|
+
self.log.info("done creating snapshot")
|
70
|
+
|
71
|
+
def snap_cleanup(self):
|
72
|
+
referenced_timestamps = self.snap_list_get_referenced_timestamps()
|
73
|
+
for snap in self.snap_list_timestamp_snapshots():
|
74
|
+
if snap not in referenced_timestamps:
|
75
|
+
snap = f"{self.destination}/snap/{self.reponame}/{snap}"
|
76
|
+
shutil.rmtree(snap)
|
77
|
+
|
78
|
+
def snap_list_get_referenced_timestamps(self):
|
79
|
+
result = dict()
|
80
|
+
base = f"{self.destination}/snap/{self.reponame}/"
|
81
|
+
for candidate in self.snap_list_named_snapshots():
|
82
|
+
candidate = f"named/{candidate}"
|
83
|
+
timestamp = self.snap_list_named_snapshot_target(f"{base}/{candidate}")
|
84
|
+
if timestamp not in result:
|
85
|
+
result[timestamp] = [candidate]
|
86
|
+
else:
|
87
|
+
result[timestamp].append(candidate)
|
88
|
+
timestamp = self.snap_list_named_snapshot_target(f"{base}/latest")
|
89
|
+
if timestamp not in result:
|
90
|
+
result[timestamp] = ["latest"]
|
91
|
+
else:
|
92
|
+
result[timestamp].append("latest")
|
93
|
+
return result
|
94
|
+
|
95
|
+
def snap_list_named_snapshots(self):
|
96
|
+
try:
|
97
|
+
return os.listdir(f"{self.destination}/snap/{self.reponame}/named")
|
98
|
+
except FileNotFoundError:
|
99
|
+
return []
|
100
|
+
|
101
|
+
@staticmethod
|
102
|
+
def snap_list_named_snapshot_target(path):
|
103
|
+
try:
|
104
|
+
return os.readlink(path).split("/")[-1]
|
105
|
+
except FileNotFoundError:
|
106
|
+
return None
|
107
|
+
|
108
|
+
def snap_list_timestamp_snapshots(self):
|
109
|
+
try:
|
110
|
+
result = os.listdir(f"{self.destination}/snap/{self.reponame}/")
|
111
|
+
try:
|
112
|
+
result.remove("latest")
|
113
|
+
except ValueError:
|
114
|
+
pass
|
115
|
+
try:
|
116
|
+
result.remove("named")
|
117
|
+
except ValueError:
|
118
|
+
pass
|
119
|
+
return result
|
120
|
+
except FileNotFoundError:
|
121
|
+
return []
|
122
|
+
|
123
|
+
def snap_name(self, timestamp, snapname):
|
124
|
+
self.log.info("creating named snapshot")
|
125
|
+
try:
|
126
|
+
int(timestamp)
|
127
|
+
if not len(timestamp) == 14:
|
128
|
+
raise ValueError
|
129
|
+
except ValueError:
|
130
|
+
self.log.error(
|
131
|
+
f"{timestamp} is not a valid timestamp, checking if its a named snapshot"
|
132
|
+
)
|
133
|
+
source = f"{self.destination}/snap/{self.reponame}/{timestamp}"
|
134
|
+
_timestamp = self.snap_list_named_snapshot_target(source)
|
135
|
+
if _timestamp:
|
136
|
+
self.log.info(f"setting timestamp to {_timestamp}")
|
137
|
+
timestamp = _timestamp
|
138
|
+
else:
|
139
|
+
raise OSRepoSyncException(f"{snapname} is not a valid named snapshot")
|
140
|
+
source = f"{self.destination}/snap/{self.reponame}/{timestamp}"
|
141
|
+
target = f"{self.destination}/snap/{self.reponame}/named/{snapname}"
|
142
|
+
target_dir = f"{self.destination}/snap/{self.reponame}/named/"
|
143
|
+
if os.path.isdir(source):
|
144
|
+
self.log.debug(f"source directory exists: {source}")
|
145
|
+
else:
|
146
|
+
self.log.debug(f"source directory missing: {source}")
|
147
|
+
raise OSRepoSyncException(f"Source directory missing: {source}")
|
148
|
+
try:
|
149
|
+
os.makedirs(os.path.dirname(target_dir))
|
150
|
+
except OSError:
|
151
|
+
pass
|
152
|
+
try:
|
153
|
+
os.unlink(target)
|
154
|
+
except OSError:
|
155
|
+
pass
|
156
|
+
os.symlink(source, target)
|
157
|
+
self.log.info("done creating named snapshot")
|
158
|
+
|
159
|
+
def snap_unname(self, snapname):
|
160
|
+
self.log.info("removing named snapshot")
|
161
|
+
target = f"{self.destination}/snap/{self.reponame}/named/{snapname}"
|
162
|
+
try:
|
163
|
+
os.unlink(target)
|
164
|
+
except FileNotFoundError:
|
165
|
+
pass
|
166
|
+
self.log.info("done removing named snapshot")
|
167
|
+
|
168
|
+
def sync(self):
|
169
|
+
pass
|
pyreposync/sync_rpm.py
ADDED
@@ -0,0 +1,256 @@
|
|
1
|
+
from shutil import copyfile
|
2
|
+
|
3
|
+
import bz2
|
4
|
+
import gzip
|
5
|
+
import configparser
|
6
|
+
import os
|
7
|
+
import shutil
|
8
|
+
|
9
|
+
import xml.etree.ElementTree
|
10
|
+
|
11
|
+
from pyreposync.sync_generic import SyncGeneric
|
12
|
+
|
13
|
+
from pyreposync.exceptions import OSRepoSyncException
|
14
|
+
|
15
|
+
|
16
|
+
class SyncRPM(SyncGeneric):
|
17
|
+
def __init__(
|
18
|
+
self,
|
19
|
+
base_url,
|
20
|
+
destination,
|
21
|
+
reponame,
|
22
|
+
date,
|
23
|
+
treeinfo,
|
24
|
+
proxy=None,
|
25
|
+
client_cert=None,
|
26
|
+
client_key=None,
|
27
|
+
ca_cert=None,
|
28
|
+
):
|
29
|
+
super().__init__(
|
30
|
+
base_url,
|
31
|
+
destination,
|
32
|
+
reponame,
|
33
|
+
date,
|
34
|
+
proxy,
|
35
|
+
client_cert,
|
36
|
+
client_key,
|
37
|
+
ca_cert,
|
38
|
+
)
|
39
|
+
self._treeinfo = treeinfo
|
40
|
+
|
41
|
+
@property
|
42
|
+
def treeinfo(self):
|
43
|
+
return self._treeinfo
|
44
|
+
|
45
|
+
def packages(self, base_path=None):
|
46
|
+
if not base_path:
|
47
|
+
base_path = f"{self.destination}/sync/{self.reponame}"
|
48
|
+
primary = None
|
49
|
+
for location, hash_algo, hash_sum in self.repomd_files():
|
50
|
+
destination = f"{base_path}/{location}"
|
51
|
+
if "primary.xml" in destination.lower():
|
52
|
+
primary = destination
|
53
|
+
if not primary:
|
54
|
+
self.log.fatal("no primary.xml found in repomd.xml")
|
55
|
+
raise OSRepoSyncException("no primary.xml found in repomd.xml")
|
56
|
+
|
57
|
+
if primary.endswith(".gz"):
|
58
|
+
with gzip.open(primary, "rb") as source:
|
59
|
+
root = xml.etree.ElementTree.parse(source).getroot()
|
60
|
+
elif primary.endswith("bz2"):
|
61
|
+
with bz2.open(primary, "rb") as source:
|
62
|
+
root = xml.etree.ElementTree.parse(source).getroot()
|
63
|
+
else:
|
64
|
+
with open(primary, "rb") as source:
|
65
|
+
root = xml.etree.ElementTree.parse(source).getroot()
|
66
|
+
packages = root.findall("{http://linux.duke.edu/metadata/common}package")
|
67
|
+
for package in packages:
|
68
|
+
checksum = package.find("{http://linux.duke.edu/metadata/common}checksum")
|
69
|
+
hash_algo = checksum.get("type")
|
70
|
+
hash_sum = checksum.text
|
71
|
+
location = package.find("{http://linux.duke.edu/metadata/common}location")
|
72
|
+
yield location.get("href"), hash_algo, hash_sum
|
73
|
+
|
74
|
+
def migrate(self):
|
75
|
+
migrated_file = f"{self.destination}/sync/{self.reponame}/migrated"
|
76
|
+
if os.path.isfile(migrated_file):
|
77
|
+
self.log.info("migration already done")
|
78
|
+
return
|
79
|
+
|
80
|
+
for location, hash_algo, hash_sum in self.packages():
|
81
|
+
destination_old = f"{self.destination}/sync/{self.reponame}/{location}"
|
82
|
+
destination_new = f"{self.destination}/sync/{self.reponame}/{location}.{hash_algo}.{hash_sum}"
|
83
|
+
try:
|
84
|
+
os.rename(destination_old, destination_new)
|
85
|
+
except FileNotFoundError:
|
86
|
+
self.log.error(
|
87
|
+
f"could not migrate {location}: {destination_old} not found"
|
88
|
+
)
|
89
|
+
continue
|
90
|
+
except OSError as err:
|
91
|
+
self.log.error(f"could not migrate {location}: {err}")
|
92
|
+
continue
|
93
|
+
|
94
|
+
for snap in self.snap_list_timestamp_snapshots():
|
95
|
+
self.log.info(f"migrating {snap}")
|
96
|
+
base_path = f"{self.destination}/snap/{self.reponame}/{snap}"
|
97
|
+
for location, hash_algo, hash_sum in self.packages(base_path=base_path):
|
98
|
+
dst = f"{base_path}/{location}"
|
99
|
+
src = f"{self.destination}/sync/{self.reponame}/{location}.{hash_algo}.{hash_sum}"
|
100
|
+
try:
|
101
|
+
os.unlink(dst)
|
102
|
+
os.symlink(src, dst)
|
103
|
+
except OSError:
|
104
|
+
pass
|
105
|
+
try:
|
106
|
+
os.symlink(src, dst)
|
107
|
+
except OSError:
|
108
|
+
pass
|
109
|
+
|
110
|
+
with open(migrated_file, "w") as _migrated:
|
111
|
+
_migrated.write("migrated\n")
|
112
|
+
|
113
|
+
def sync_packages(self):
|
114
|
+
for location, hash_algo, hash_sum in self.packages():
|
115
|
+
url = f"{self.base_url}{location}"
|
116
|
+
destination = f"{self.destination}/sync/{self.reponame}/{location}.{hash_algo}.{hash_sum}"
|
117
|
+
self.downloader.get(url, destination, hash_sum, hash_algo, replace=False)
|
118
|
+
|
119
|
+
def treeinfo_files(self):
|
120
|
+
treeinfo_file = f"{self.destination}/sync/{self.reponame}/{self.treeinfo}"
|
121
|
+
treeinfo = configparser.ConfigParser()
|
122
|
+
treeinfo.optionxform = str
|
123
|
+
try:
|
124
|
+
treeinfo.read_file(open(treeinfo_file))
|
125
|
+
except FileNotFoundError:
|
126
|
+
return
|
127
|
+
try:
|
128
|
+
for file in treeinfo.options("checksums"):
|
129
|
+
if file == "repodata/repomd.xml":
|
130
|
+
continue
|
131
|
+
hash_algo, hash_sum = treeinfo.get("checksums", file).split(":", 1)
|
132
|
+
yield file, hash_algo, hash_sum
|
133
|
+
except configparser.NoSectionError:
|
134
|
+
files = set()
|
135
|
+
for section in treeinfo.sections():
|
136
|
+
if section.startswith("images-") or section.startswith("stage2"):
|
137
|
+
for option in treeinfo.options(section):
|
138
|
+
files.add(treeinfo.get(section, option))
|
139
|
+
for file in files:
|
140
|
+
yield file, None, None
|
141
|
+
|
142
|
+
def sync_treeinfo(self):
|
143
|
+
url = f"{self.base_url}{self.treeinfo}"
|
144
|
+
destination = f"{self.destination}/sync/{self.reponame}/{self.treeinfo}"
|
145
|
+
try:
|
146
|
+
self.downloader.get(url, destination, replace=True)
|
147
|
+
except OSRepoSyncException:
|
148
|
+
return
|
149
|
+
for file, hash_algo, hash_sum in self.treeinfo_files():
|
150
|
+
if file == "repodata/repomd.xml":
|
151
|
+
continue
|
152
|
+
url = f"{self.base_url}{file}"
|
153
|
+
destination = f"{self.destination}/sync/{self.reponame}/{file}"
|
154
|
+
self.downloader.get(url, destination, hash_sum, hash_algo, replace=True)
|
155
|
+
|
156
|
+
def repomd_files(self):
|
157
|
+
base_path = f"{self.destination}/sync/{self.reponame}/repodata/repomd.xml"
|
158
|
+
repomd = xml.etree.ElementTree.parse(base_path).getroot()
|
159
|
+
datas = repomd.findall("{http://linux.duke.edu/metadata/repo}data")
|
160
|
+
for data in datas:
|
161
|
+
checksum = data.find("{http://linux.duke.edu/metadata/repo}checksum")
|
162
|
+
hash_algo = checksum.get("type")
|
163
|
+
hash_sum = checksum.text
|
164
|
+
location = data.find("{http://linux.duke.edu/metadata/repo}location")
|
165
|
+
yield location.get("href"), hash_algo, hash_sum
|
166
|
+
|
167
|
+
def revalidate(self):
|
168
|
+
packages = dict()
|
169
|
+
try:
|
170
|
+
for location, hash_algo, hash_sum in self.packages():
|
171
|
+
destination = f"{self.destination}/sync/{self.reponame}/{location}.{hash_algo}.{hash_sum}"
|
172
|
+
packages[destination] = {"hash_algo": hash_algo, "hash_sum": hash_sum}
|
173
|
+
except FileNotFoundError:
|
174
|
+
self.log.error("no repodata found")
|
175
|
+
return packages
|
176
|
+
|
177
|
+
def sync_repomd(self):
|
178
|
+
url = f"{self.base_url}repodata/repomd.xml"
|
179
|
+
destination = f"{self.destination}/sync/{self.reponame}/repodata/repomd.xml"
|
180
|
+
try:
|
181
|
+
shutil.rmtree(f"{self.destination}/sync/{self.reponame}/repodata/")
|
182
|
+
except FileNotFoundError:
|
183
|
+
pass
|
184
|
+
self.downloader.get(url, destination, replace=True)
|
185
|
+
for location, hash_algo, hash_sum in self.repomd_files():
|
186
|
+
url = f"{self.base_url}{location}"
|
187
|
+
destination = f"{self.destination}/sync/{self.reponame}/{location}"
|
188
|
+
self.downloader.get(url, destination, hash_sum, hash_algo, replace=True)
|
189
|
+
self.sync_packages()
|
190
|
+
self.sync_treeinfo()
|
191
|
+
|
192
|
+
def _snap(self):
|
193
|
+
self.snap_repodata()
|
194
|
+
self.snap_treeinfo()
|
195
|
+
self.snap_packages()
|
196
|
+
|
197
|
+
def snap_repodata(self):
|
198
|
+
self.log.info("copy repodata")
|
199
|
+
repomd_dst = (
|
200
|
+
f"{self.destination}/snap/{self.reponame}/{self.date}/repodata/repomd.xml"
|
201
|
+
)
|
202
|
+
repomd_src = f"{self.destination}/sync/{self.reponame}/repodata/repomd.xml"
|
203
|
+
try:
|
204
|
+
os.makedirs(os.path.dirname(repomd_dst))
|
205
|
+
except OSError:
|
206
|
+
pass
|
207
|
+
copyfile(repomd_src, repomd_dst)
|
208
|
+
for location, hash_algo, hash_sum in self.repomd_files():
|
209
|
+
dst = f"{self.destination}/snap/{self.reponame}/{self.date}/{location}"
|
210
|
+
src = f"{self.destination}/sync/{self.reponame}/{location}"
|
211
|
+
try:
|
212
|
+
os.makedirs(os.path.dirname(dst))
|
213
|
+
except OSError:
|
214
|
+
pass
|
215
|
+
copyfile(src, dst)
|
216
|
+
self.log.info("done copy repodata")
|
217
|
+
|
218
|
+
def snap_treeinfo(self):
|
219
|
+
self.log.info("copy treeinfo")
|
220
|
+
try:
|
221
|
+
dst = f"{self.destination}/snap/{self.reponame}/{self.destination}/{self.treeinfo}"
|
222
|
+
src = f"{self.destination}/sync/{self.reponame}/{self.treeinfo}"
|
223
|
+
copyfile(src, dst)
|
224
|
+
except (OSError, FileNotFoundError) as err:
|
225
|
+
self.log.error(f"could not copy {self.treeinfo}: {err}")
|
226
|
+
for location, hash_algo, hash_sum in self.treeinfo_files():
|
227
|
+
dst = (
|
228
|
+
f"{self.destination}/snap/{self.reponame}/{self.destination}/{location}"
|
229
|
+
)
|
230
|
+
src = f"{self.destination}/sync/{self.reponame}/{location}"
|
231
|
+
try:
|
232
|
+
os.makedirs(os.path.dirname(dst))
|
233
|
+
except OSError:
|
234
|
+
pass
|
235
|
+
copyfile(src, dst)
|
236
|
+
self.log.info("done copy treeinfo")
|
237
|
+
|
238
|
+
def snap_packages(self):
|
239
|
+
self.log.info("copy packages")
|
240
|
+
for location, hash_algo, hash_sum in self.packages():
|
241
|
+
dst = f"{self.destination}/snap/{self.reponame}/{self.date}/{location}"
|
242
|
+
src = f"{self.destination}/sync/{self.reponame}/{location}.{hash_algo}.{hash_sum}"
|
243
|
+
try:
|
244
|
+
os.makedirs(os.path.dirname(dst))
|
245
|
+
except OSError:
|
246
|
+
pass
|
247
|
+
try:
|
248
|
+
os.symlink(src, dst)
|
249
|
+
except FileExistsError as err:
|
250
|
+
self.log.error(f"could not copy {location}: {err}")
|
251
|
+
self.log.info("done copy packages")
|
252
|
+
|
253
|
+
def sync(self):
|
254
|
+
self.log.info("starting thread")
|
255
|
+
self.sync_repomd()
|
256
|
+
self.log.info("shutdown thread complete")
|
@@ -0,0 +1,31 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: pyreposync
|
3
|
+
Version: 0.2.0
|
4
|
+
Summary: Orbit Api
|
5
|
+
Project-URL: Source, https://github.com/schlitzered/pyreposync
|
6
|
+
Author-email: "Stephan.Schultchen" <sschultchen@gmail.com>
|
7
|
+
License: The MIT License (MIT)
|
8
|
+
|
9
|
+
Copyright (c) 2015 Stephan Schultchen
|
10
|
+
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
13
|
+
in the Software without restriction, including without limitation the rights
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
16
|
+
furnished to do so, subject to the following conditions:
|
17
|
+
|
18
|
+
The above copyright notice and this permission notice shall be included in
|
19
|
+
all copies or substantial portions of the Software.
|
20
|
+
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
27
|
+
THE SOFTWARE.
|
28
|
+
License-File: LICENSE.txt
|
29
|
+
Classifier: Programming Language :: Python
|
30
|
+
Requires-Python: >=3.9
|
31
|
+
Requires-Dist: requests
|
@@ -0,0 +1,11 @@
|
|
1
|
+
pyreposync/__init__.py,sha256=GdNdSibEs53cwekuUQI9SqM0gXSgO7kDQgWU0o-vWNU,16258
|
2
|
+
pyreposync/downloader.py,sha256=828bIMCctd1X6I1w91-XHytQ1M5fmxkEw0YDKd5nvAY,4764
|
3
|
+
pyreposync/exceptions.py,sha256=IlnvhNaffQQ6geOgrjCciNFVbFpNcycH4ijSuMTbrGA,169
|
4
|
+
pyreposync/sync_deb.py,sha256=taH4Tf0i6alSR0Z_iJ21LfiAWZelcU74DnYCQ6EXwhs,8201
|
5
|
+
pyreposync/sync_generic.py,sha256=vNNuh-hJp3Qj_AqrPc7S6OibHhsFKSFRaTv_2Aj9H9Y,5502
|
6
|
+
pyreposync/sync_rpm.py,sha256=3aBKwO_wdyrOXR_3MZlup26WoMTG8p1qdiN165YigrA,10105
|
7
|
+
pyreposync-0.2.0.dist-info/METADATA,sha256=0sRFsPPiLmjuLvljPnmCxVJXdwmVbUZwKuKHlSOkBXw,1565
|
8
|
+
pyreposync-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
9
|
+
pyreposync-0.2.0.dist-info/entry_points.txt,sha256=9LsBDWOF3O6_3ONP3Lc-4v1MTt5ay0Xv-TMcFbOIt2s,47
|
10
|
+
pyreposync-0.2.0.dist-info/licenses/LICENSE.txt,sha256=lwnJoIo7uwc0h6y6gC_RYqJkvjplViV3Ad6u7pQM4Bw,1084
|
11
|
+
pyreposync-0.2.0.dist-info/RECORD,,
|
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Stephan Schultchen
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|