121
122
self._cache_cls = apt.Cache
124
def save_state(self, sourcedir, target,
125
with_dpkg_repack=False, with_dpkg_status=False):
125
def save_state(self, sourcedir, target,
126
with_dpkg_repack=False, with_dpkg_status=False,
127
scrub_sources=False):
126
128
""" save the current system state (installed pacakges, enabled
127
129
repositories ...) into the apt-state.tar.gz file in targetdir
143
145
self._write_uname(tar)
144
146
self._write_state_installed_pkgs(sourcedir, tar)
145
147
self._write_state_auto_installed(tar)
146
self._write_state_sources_list(tar)
148
self._write_state_sources_list(tar, scrub_sources)
147
149
self._write_state_apt_preferences(tar)
148
150
self._write_state_apt_keyring(tar)
149
151
if with_dpkg_status:
218
220
if os.path.exists(p):
219
221
tar.add(p, arcname="./etc/apt/trusted.gpg.d")
221
def _write_state_sources_list(self, tar):
222
tar.add(apt_pkg.config.find_file("Dir::Etc::sourcelist"),
223
arcname="./etc/apt/sources.list")
223
def _write_state_sources_list(self, tar, scrub=False):
224
self._add_file_to_tar_with_password_check(tar,
225
apt_pkg.config.find_file("Dir::Etc::sourcelist"), scrub)
224
226
source_parts = apt_pkg.config.find_dir("Dir::Etc::sourceparts")
225
227
if os.path.exists(source_parts):
226
tar.add(source_parts, arcname="./etc/apt/sources.list.d")
228
for source in os.listdir(source_parts):
229
sources_file_name = '%s/%s' % (source_parts, source)
230
self._add_file_to_tar_with_password_check(tar,
231
sources_file_name, scrub)
233
def _add_file_to_tar_with_password_check(self, tar, sources, scrub=False):
235
with tempfile.NamedTemporaryFile(mode='w') as source_copy, open(sources, 'r') as f:
236
for line in f.readlines():
237
if re.search('/[^/@:]*:[^/@:]*@', line):
238
scrubbed_line = re.sub('/[^/@:]*:[^/@:]*@',
239
'/USERNAME:PASSWORD@', line)
240
source_copy.write(scrubbed_line)
242
source_copy.write(line)
244
tar.add(source_copy.name, arcname=".%s" % (sources))
246
tar.add(sources, arcname='.%s' % sources)
228
248
def _write_modified_files_from_etc(self, tar):
229
249
#etcdir = os.path.join(apt_pkg.config.get("Dir"), "etc")