~ubuntu-branches/ubuntu/raring/apt-clone/raring

« back to all changes in this revision

Viewing changes to apt_clone.py

  • Committer: Brian Murray
  • Date: 2012-07-02 21:23:52 UTC
  • Revision ID: brian@canonical.com-20120702212352-lw3vbh7v3ii1luom
Tags: 0.2.3~ubuntu2
* apt_clone.py:
  - if specified remove usernames and passwords from sources files

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import glob
26
26
import hashlib
27
27
import os
 
28
import re
28
29
import shutil
29
30
import subprocess
30
31
import tarfile
121
122
            self._cache_cls = apt.Cache
122
123
 
123
124
    # save
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
128
130
        """
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")
220
222
 
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)
 
232
 
 
233
    def _add_file_to_tar_with_password_check(self, tar, sources, scrub=False):
 
234
        if scrub:
 
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)
 
241
                    else:
 
242
                        source_copy.write(line)
 
243
                    source_copy.flush()
 
244
                tar.add(source_copy.name, arcname=".%s" % (sources))
 
245
        else:
 
246
            tar.add(sources, arcname='.%s' % sources)
227
247
 
228
248
    def _write_modified_files_from_etc(self, tar):
229
249
        #etcdir = os.path.join(apt_pkg.config.get("Dir"), "etc")