forked from kimchi-project/kimchi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
93 lines (80 loc) · 2.99 KB
/
scan.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
#
# Project Kimchi
#
# Copyright IBM Corp, 2015-2016
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
import glob
import hashlib
import os.path
import shutil
import tempfile
import time
from wok.plugins.kimchi.isoinfo import IsoImage
from wok.plugins.kimchi.isoinfo import probe_iso
from wok.utils import wok_log
SCAN_IGNORE = ['/tmp/kimchi-scan-*']
class Scanner(object):
SCAN_TTL = 300
def __init__(self, record_clean_cb):
self.clean_cb = record_clean_cb
def delete(self):
self.clean_stale(-1)
def clean_stale(self, window=SCAN_TTL):
"""
Clear scan pools generated before time window,
Clear all scan pools if window is -1.
"""
try:
now = time.time()
clean_list = glob.glob('/tmp/kimchi-scan-*')
for d in clean_list:
transient_pool = os.path.basename(
d).replace('kimchi-scan-', '')[0:-6]
if now - os.path.getmtime(d) > window:
shutil.rmtree(d)
self.clean_cb(transient_pool)
except OSError as e:
msg = f'Exception {e} occured when cleaning stale pool, ignore'
wok_log.debug(msg)
def scan_dir_prepare(self, name):
# clean stale scan storage pools
self.clean_stale()
return tempfile.mkdtemp(prefix='kimchi-scan-' + name, dir='/tmp')
def start_scan(self, cb, params):
def updater(iso_info):
iso_name = os.path.basename(iso_info['path'])[:-3]
duplicates = '%s/%s*' % (params['pool_path'], iso_name)
for f in glob.glob(duplicates):
iso_img = IsoImage(f)
if (iso_info['distro'], iso_info['version']) == iso_img.probe():
return
iso_path = (
iso_name +
hashlib.md5(iso_info['path'].encode('utf-8')).hexdigest() +
'.iso'
)
link_name = os.path.join(
params['pool_path'], os.path.basename(iso_path))
os.symlink(iso_info['path'], link_name)
ignore_paths = params.get('ignore_list', [])
scan_params = dict(
path=params['scan_path'],
updater=updater,
ignore_list=ignore_paths + SCAN_IGNORE,
)
probe_iso(None, scan_params)
cb('', True)