Example #1
0
    def test_tarfile_root_owner(self):
        tmpdir, tmpdir2, base_name = self._create_files()
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        group = grp.getgrgid(0)[0]
        owner = pwd.getpwuid(0)[0]
        try:
            archive_name = _make_tarball(base_name,
                                         'dist',
                                         compress=None,
                                         owner=owner,
                                         group=group)
        finally:
            os.chdir(old_dir)

        # check if the compressed tarball was created
        self.assertTrue(os.path.exists(archive_name))

        # now checks the rights
        archive = tarfile.open(archive_name)
        try:
            for member in archive.getmembers():
                self.assertEqual(member.uid, 0)
                self.assertEqual(member.gid, 0)
        finally:
            archive.close()
Example #2
0
    def test_make_tarball(self):
        # creating something to tar
        tmpdir = self.mkdtemp()
        self.write_file([tmpdir, 'file1'], 'xxx')
        self.write_file([tmpdir, 'file2'], 'xxx')
        os.mkdir(os.path.join(tmpdir, 'sub'))
        self.write_file([tmpdir, 'sub', 'file3'], 'xxx')

        tmpdir2 = self.mkdtemp()
        # force shutil to create the directory
        os.rmdir(tmpdir2)
        unittest.skipUnless(
            splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
            "source and target should be on same drive")

        base_name = os.path.join(tmpdir2, 'archive')

        # working with relative paths to avoid tar warnings
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(splitdrive(base_name)[1], '.')
        finally:
            os.chdir(old_dir)

        # check if the compressed tarball was created
        tarball = base_name + '.tar.gz'
        self.assertTrue(os.path.exists(tarball))

        # trying an uncompressed one
        base_name = os.path.join(tmpdir2, 'archive')
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(splitdrive(base_name)[1], '.', compress=None)
        finally:
            os.chdir(old_dir)
        tarball = base_name + '.tar'
        self.assertTrue(os.path.exists(tarball))
Example #3
0
    def test_make_tarball(self):
        # creating something to tar
        tmpdir = self.mkdtemp()
        self.write_file([tmpdir, "file1"], "xxx")
        self.write_file([tmpdir, "file2"], "xxx")
        os.mkdir(os.path.join(tmpdir, "sub"))
        self.write_file([tmpdir, "sub", "file3"], "xxx")

        tmpdir2 = self.mkdtemp()
        # force shutil to create the directory
        os.rmdir(tmpdir2)
        unittest.skipUnless(
            splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], "source and target should be on same drive"
        )

        base_name = os.path.join(tmpdir2, "archive")

        # working with relative paths to avoid tar warnings
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(splitdrive(base_name)[1], ".")
        finally:
            os.chdir(old_dir)

        # check if the compressed tarball was created
        tarball = base_name + ".tar.gz"
        self.assertTrue(os.path.exists(tarball))

        # trying an uncompressed one
        base_name = os.path.join(tmpdir2, "archive")
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(splitdrive(base_name)[1], ".", compress=None)
        finally:
            os.chdir(old_dir)
        tarball = base_name + ".tar"
        self.assertTrue(os.path.exists(tarball))
Example #4
0
    def test_make_tarball(self):
        # creating something to tar
        tmpdir = self.mkdtemp()
        write_file((tmpdir, 'file1'), 'xxx')
        write_file((tmpdir, 'file2'), 'xxx')
        os.mkdir(os.path.join(tmpdir, 'sub'))
        write_file((tmpdir, 'sub', 'file3'), 'xxx')

        tmpdir2 = self.mkdtemp()
        # force shutil to create the directory
        os.rmdir(tmpdir2)
        unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
                            "source and target should be on same drive")

        base_name = os.path.join(tmpdir2, 'archive')

        # working with relative paths to avoid tar warnings
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(splitdrive(base_name)[1], '.')
        finally:
            os.chdir(old_dir)

        # check if the compressed tarball was created
        tarball = base_name + '.tar.gz'
        self.assertTrue(os.path.exists(tarball))

        # trying an uncompressed one
        base_name = os.path.join(tmpdir2, 'archive')
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(splitdrive(base_name)[1], '.', compress=None)
        finally:
            os.chdir(old_dir)
        tarball = base_name + '.tar'
        self.assertTrue(os.path.exists(tarball))
Example #5
0
    def test_tarfile_vs_tar(self):
        tmpdir, tmpdir2, base_name = self._create_files()
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(base_name, 'dist')
        finally:
            os.chdir(old_dir)

        # check if the compressed tarball was created
        tarball = base_name + '.tar.gz'
        self.assertTrue(os.path.exists(tarball))

        # now create another tarball using `tar`
        tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
        tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
        gzip_cmd = ['gzip', '-f9', 'archive2.tar']
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            with captured_stdout() as s:
                spawn(tar_cmd)
                spawn(gzip_cmd)
        finally:
            os.chdir(old_dir)

        self.assertTrue(os.path.exists(tarball2))
        # let's compare both tarballs
        self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))

        # trying an uncompressed one
        base_name = os.path.join(tmpdir2, 'archive')
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(base_name, 'dist', compress=None)
        finally:
            os.chdir(old_dir)
        tarball = base_name + '.tar'
        self.assertTrue(os.path.exists(tarball))

        # now for a dry_run
        base_name = os.path.join(tmpdir2, 'archive')
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(base_name, 'dist', compress=None, dry_run=True)
        finally:
            os.chdir(old_dir)
        tarball = base_name + '.tar'
        self.assertTrue(os.path.exists(tarball))
Example #6
0
    def test_tarfile_vs_tar(self):
        tmpdir, tmpdir2, base_name =  self._create_files()
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(base_name, 'dist')
        finally:
            os.chdir(old_dir)

        # check if the compressed tarball was created
        tarball = base_name + '.tar.gz'
        self.assertTrue(os.path.exists(tarball))

        # now create another tarball using `tar`
        tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
        tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
        gzip_cmd = ['gzip', '-f9', 'archive2.tar']
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            with captured_stdout() as s:
                spawn(tar_cmd)
                spawn(gzip_cmd)
        finally:
            os.chdir(old_dir)

        self.assertTrue(os.path.exists(tarball2))
        # let's compare both tarballs
        self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))

        # trying an uncompressed one
        base_name = os.path.join(tmpdir2, 'archive')
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(base_name, 'dist', compress=None)
        finally:
            os.chdir(old_dir)
        tarball = base_name + '.tar'
        self.assertTrue(os.path.exists(tarball))

        # now for a dry_run
        base_name = os.path.join(tmpdir2, 'archive')
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(base_name, 'dist', compress=None, dry_run=True)
        finally:
            os.chdir(old_dir)
        tarball = base_name + '.tar'
        self.assertTrue(os.path.exists(tarball))
Example #7
0
    def test_tarfile_vs_tar(self):
        tmpdir, tmpdir2, base_name = self._create_files()
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(base_name, "dist")
        finally:
            os.chdir(old_dir)

        # check if the compressed tarball was created
        tarball = base_name + ".tar.gz"
        self.assertTrue(os.path.exists(tarball))

        # now create another tarball using `tar`
        tarball2 = os.path.join(tmpdir, "archive2.tar.gz")
        tar_cmd = ["tar", "-cf", "archive2.tar", "dist"]
        gzip_cmd = ["gzip", "-f9", "archive2.tar"]
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            with captured_stdout() as s:
                spawn(tar_cmd)
                spawn(gzip_cmd)
        finally:
            os.chdir(old_dir)

        self.assertTrue(os.path.exists(tarball2))
        # let's compare both tarballs
        self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))

        # trying an uncompressed one
        base_name = os.path.join(tmpdir2, "archive")
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(base_name, "dist", compress=None)
        finally:
            os.chdir(old_dir)
        tarball = base_name + ".tar"
        self.assertTrue(os.path.exists(tarball))

        # now for a dry_run
        base_name = os.path.join(tmpdir2, "archive")
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        try:
            _make_tarball(base_name, "dist", compress=None, dry_run=True)
        finally:
            os.chdir(old_dir)
        tarball = base_name + ".tar"
        self.assertTrue(os.path.exists(tarball))
Example #8
0
    def test_tarfile_root_owner(self):
        tmpdir, tmpdir2, base_name = self._create_files()
        old_dir = os.getcwd()
        os.chdir(tmpdir)
        group = grp.getgrgid(0)[0]
        owner = pwd.getpwuid(0)[0]
        try:
            archive_name = _make_tarball(base_name, "dist", compress=None, owner=owner, group=group)
        finally:
            os.chdir(old_dir)

        # check if the compressed tarball was created
        self.assertTrue(os.path.exists(archive_name))

        # now checks the rights
        archive = tarfile.open(archive_name)
        try:
            for member in archive.getmembers():
                self.assertEqual(member.uid, 0)
                self.assertEqual(member.gid, 0)
        finally:
            archive.close()
def _compress_files(archive_temp, temp_archive_compress_file_no_ext):

    # If the compression is changed the file extension needs to be changed as well in the parent method
    shutil._make_tarball(temp_archive_compress_file_no_ext, archive_temp, compress="gzip")
Example #10
0
#shutil高级的文件,文件夹,压缩包处理模块
import shutil
shutil.copyfileobj(open('a.txt.py', 'r'), open('a.py1', 'w'))  #将文件内容拷贝到另一个文件中
shutil.copyfile('a.py1', 'a2.py')  #拷贝文件
shutil.copymode('a2.py', 'a.txt.py')  #仅拷贝权限。原文件内容,组,用户均不变
shutil.copystat('a3.py', 'a4.py')  #拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.copy('a3.py', 'a4.py')  #拷贝文件和权限
shutil.copy2('a3.py', 'a4.py')  #拷贝文件和状态信息
shutil.copytree('a1', 'a2')  #shutil.ignore_patterns(*patterns)递归去拷贝文件(拷贝目录)
shutil.rmtree('a2', 'a1')  #递归删除目录
shutil.move('D:/软件/pycharm/day5/a.py1', 'D:/软件/pycharm')  #递归的去移动文件
#shutil.make_archive(压缩保存路径,压缩包种类,压缩目标路径);压缩包种类,“zip”, “tar”, “bztar”,“gztar”
shutil.make_archive('D:/软件/shutil.make_ar', 'zip', 'D:/软件/pycharm/day5')
#def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,owner=None, group=None, logger=None):默认是tar.gz包压缩目录
shutil._make_tarball('D:/软件/shutil.make_ar', 'D:/软件/pycharm/day5')
#shutil._make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):默认zip包压缩目录
shutil._make_zipfile('D:/软件/shutil.make_ar', 'D:/软件/pycharm/day5')

#shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:
import zipfile
# zip包解压
z = zipfile.ZipFile('D:/软件/shutil.make_ar.zip')  #指定解压包
z.extractall()  #解压到指定路径,默认是解压到程序当前路径
z.close()

#压缩zip包
z = zipfile.ZipFile('D:/软件/shutil_shiyan.zip', 'w')  #打开并指定压缩包保存路径(一定得是zip后缀)
z.write('D:/a.txt')  #添加文件或目录到压缩包
z.write('C:/shutil.make_ar.zip')  #添加文件或目录到压缩包
z.write('C:/b')  #添加文件或目录到压缩包
Example #11
0
 def update_event(self, inp=-1):
     self.set_output_val(0, shutil._make_tarball(self.input(0), self.input(1), self.input(2), self.input(3), self.input(4), self.input(5), self.input(6), self.input(7)))