Beispiel #1
0
 def __enter__(self):
   self.tarfile = archive.TarFileWriter(
       self.output,
       self.compression,
       self.root_directory,
       default_mtime=self.default_mtime)
   return self
Beispiel #2
0
 def testPreserveTarMtimesFalse(self):
     with archive.TarFileWriter(self.tempfile,
                                preserve_tar_mtimes=False) as f:
         input_tar_path = self.data_files.Rlocation(
             os.path.join("rules_pkg", "tests", "testdata", "tar_test.tar"))
         f.add_tar(input_tar_path)
         for output_file in f.tar:
             self.assertEqual(output_file.mtime, 0)
Beispiel #3
0
 def __enter__(self):
     self.tarfile = archive.TarFileWriter(
         self.output,
         self.compression,
         self.root_directory,
         self.default_mtime,
         self.enable_mtime_preservation,
     )
     return self
Beispiel #4
0
 def testPreserveTarMtimesTrueByDefault(self):
     with archive.TarFileWriter(self.tempfile) as f:
         input_tar_path = self.data_files.Rlocation(
             os.path.join("rules_pkg", "tests", "testdata", "tar_test.tar"))
         f.add_tar(input_tar_path)
         input_tar = tarfile.open(input_tar_path, "r")
         for file_name in f.members:
             input_file = input_tar.getmember(file_name)
             output_file = f.tar.getmember(file_name)
             self.assertEqual(input_file.mtime, output_file.mtime)
Beispiel #5
0
    def testChangingRootDirectory(self):
        with archive.TarFileWriter(self.tempfile, root_directory="root") as f:
            f.add_file("d", tarfile.DIRTYPE)
            f.add_file("d/f")

            f.add_file("a", tarfile.DIRTYPE)
            f.add_file("a/b", tarfile.DIRTYPE)
            f.add_file("a/b", tarfile.DIRTYPE)
            f.add_file("a/b/", tarfile.DIRTYPE)
            f.add_file("a/b/c/f")

            f.add_file("x/y/f")
            f.add_file("x", tarfile.DIRTYPE)
        content = [
            {
                "name": "root",
                "mode": 0o755
            },
            {
                "name": "root/d",
                "mode": 0o755
            },
            {
                "name": "root/d/f"
            },
            {
                "name": "root/a",
                "mode": 0o755
            },
            {
                "name": "root/a/b",
                "mode": 0o755
            },
            {
                "name": "root/a/b/c",
                "mode": 0o755
            },
            {
                "name": "root/a/b/c/f"
            },
            {
                "name": "root/x",
                "mode": 0o755
            },
            {
                "name": "root/x/y",
                "mode": 0o755
            },
            {
                "name": "root/x/y/f"
            },
        ]
        self.assertTarFileContent(self.tempfile, content)
Beispiel #6
0
    def testAddingDirectoriesForFileManually(self):
        with archive.TarFileWriter(self.tempfile) as f:
            f.add_file("d", tarfile.DIRTYPE)
            f.add_file("d/f")

            f.add_file("a", tarfile.DIRTYPE)
            f.add_file("a/b", tarfile.DIRTYPE)
            f.add_file("a/b", tarfile.DIRTYPE)
            f.add_file("a/b/", tarfile.DIRTYPE)
            f.add_file("a/b/c/f")

            f.add_file("x/y/f")
            f.add_file("x", tarfile.DIRTYPE)
        content = [
            {
                "name": ".",
                "mode": 0o755
            },
            {
                "name": "./d",
                "mode": 0o755
            },
            {
                "name": "./d/f"
            },
            {
                "name": "./a",
                "mode": 0o755
            },
            {
                "name": "./a/b",
                "mode": 0o755
            },
            {
                "name": "./a/b/c",
                "mode": 0o755
            },
            {
                "name": "./a/b/c/f"
            },
            {
                "name": "./x",
                "mode": 0o755
            },
            {
                "name": "./x/y",
                "mode": 0o755
            },
            {
                "name": "./x/y/f"
            },
        ]
        self.assertTarFileContent(self.tempfile, content)
Beispiel #7
0
 def assertSimpleFileContent(self, names):
     with archive.TarFileWriter(self.tempfile) as f:
         for n in names:
             f.add_file(n, content=n)
     content = ([{
         "name": "."
     }] + [{
         "name": n,
         "size": len(n.encode("utf-8")),
         "data": n.encode("utf-8")
     } for n in names])
     self.assertTarFileContent(self.tempfile, content)
Beispiel #8
0
    def testAddingDirectoriesForFileSeparately(self):
        d_dir = os.path.join(os.environ["TEST_TMPDIR"], "d_dir")
        os.makedirs(d_dir)
        with open(os.path.join(d_dir, "dir_file"), "w"):
            pass
        a_dir = os.path.join(os.environ["TEST_TMPDIR"], "a_dir")
        os.makedirs(a_dir)
        with open(os.path.join(a_dir, "dir_file"), "w"):
            pass

        with archive.TarFileWriter(self.tempfile) as f:
            f.add_dir("d", d_dir)
            f.add_file("d/f")

            f.add_dir("a", a_dir)
            f.add_file("a/b/f")
        content = [
            {
                "name": ".",
                "mode": 0o755
            },
            {
                "name": "./d",
                "mode": 0o755
            },
            {
                "name": "./d/dir_file"
            },
            {
                "name": "./d/f"
            },
            {
                "name": "./a",
                "mode": 0o755
            },
            {
                "name": "./a/dir_file"
            },
            {
                "name": "./a/b",
                "mode": 0o755
            },
            {
                "name": "./a/b/f"
            },
        ]
        self.assertTarFileContent(self.tempfile, content)
Beispiel #9
0
 def testAddingDirectoriesForFile(self):
     with archive.TarFileWriter(self.tempfile) as f:
         f.add_file("d/f")
     content = [
         {
             "name": ".",
             "mode": 0o755
         },
         {
             "name": "./d",
             "mode": 0o755
         },
         {
             "name": "./d/f"
         },
     ]
     self.assertTarFileContent(self.tempfile, content)
Beispiel #10
0
 def testMergeTar(self):
     content = [
         {
             "name": "./a",
             "data": b"a"
         },
         {
             "name": "./ab",
             "data": b"ab"
         },
     ]
     for ext in ["", ".gz", ".bz2", ".xz"]:
         with archive.TarFileWriter(self.tempfile) as f:
             datafile = self.data_files.Rlocation(
                 os.path.join("rules_pkg", "tests", "testdata",
                              "tar_test.tar" + ext))
             f.add_tar(datafile, name_filter=lambda n: n != "./b")
         self.assertTarFileContent(self.tempfile, content)
Beispiel #11
0
 def testMergeTar(self):
     content = [
         {
             "name": "./a",
             "data": b"a"
         },
         {
             "name": "./ab",
             "data": b"ab"
         },
     ]
     for ext in [("." + comp if comp else "")
                 for comp in archive.COMPRESSIONS]:
         with archive.TarFileWriter(self.tempfile) as f:
             datafile = self.data_files.Rlocation(
                 "rules_pkg/tests/testdata/tar_test.tar" + ext)
             f.add_tar(datafile, name_filter=lambda n: n != "./b")
         self.assertTarFileContent(self.tempfile, content)
Beispiel #12
0
 def testAddDir(self):
     # For some strange reason, ending slash is stripped by the test
     content = [
         {
             "name": ".",
             "mode": 0o755
         },
         {
             "name": "./a",
             "mode": 0o755
         },
         {
             "name": "./a/b",
             "data": b"ab",
             "mode": 0o644
         },
         {
             "name": "./a/c",
             "mode": 0o755
         },
         {
             "name": "./a/c/d",
             "data": b"acd",
             "mode": 0o644
         },
     ]
     tempdir = os.path.join(os.environ["TEST_TMPDIR"], "test_dir")
     # Iterate over the `content` array to create the directory
     # structure it describes.
     for c in content:
         if "data" in c:
             # Create files is happening locally, so use native path sep.
             path_parts = c["name"][2:].split('/')
             p = os.path.join(tempdir, *path_parts)
             os.makedirs(os.path.dirname(p))
             with open(p, "wb") as f:
                 f.write(c["data"])
     with archive.TarFileWriter(self.tempfile) as f:
         f.add_dir("./", tempdir, mode=0o644)
     self.assertTarFileContent(self.tempfile, content)
Beispiel #13
0
 def testMergeTarRelocated(self):
     content = [
         {
             "name": ".",
             "mode": 0o755
         },
         {
             "name": "./foo",
             "mode": 0o755
         },
         {
             "name": "./foo/a",
             "data": b"a"
         },
         {
             "name": "./foo/ab",
             "data": b"ab"
         },
     ]
     with archive.TarFileWriter(self.tempfile) as f:
         datafile = self.data_files.Rlocation(
             os.path.join("rules_pkg", "tests", "testdata", "tar_test.tar"))
         f.add_tar(datafile, name_filter=lambda n: n != "./b", root="/foo")
     self.assertTarFileContent(self.tempfile, content)
Beispiel #14
0
 def testDottedFiles(self):
     with archive.TarFileWriter(self.tempfile) as f:
         f.add_file("a")
         f.add_file("/b")
         f.add_file("./c")
         f.add_file("./.d")
         f.add_file("..e")
         f.add_file(".f")
     content = [{
         "name": "."
     }, {
         "name": "./a"
     }, {
         "name": "/b"
     }, {
         "name": "./c"
     }, {
         "name": "./.d"
     }, {
         "name": "./..e"
     }, {
         "name": "./.f"
     }]
     self.assertTarFileContent(self.tempfile, content)
Beispiel #15
0
 def testDefaultMtimeNotProvided(self):
     with archive.TarFileWriter(self.tempfile) as f:
         self.assertEqual(f.default_mtime, 0)
Beispiel #16
0
 def testPortableMtime(self):
     with archive.TarFileWriter(self.tempfile,
                                default_mtime="portable") as f:
         self.assertEqual(f.default_mtime, 946684800)
Beispiel #17
0
 def testDefaultMtimeProvided(self):
     with archive.TarFileWriter(self.tempfile, default_mtime=1234) as f:
         self.assertEqual(f.default_mtime, 1234)
Beispiel #18
0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A simple cross-platform helper to create a timestamped tar file."""
import datetime
import sys
import tarfile

from rules_pkg import archive

if __name__ == '__main__':
    mtime = int(datetime.datetime.now().strftime('%s'))
    with archive.TarFileWriter(sys.argv[1]) as f:
        f.add_file('./',
                   tarfile.DIRTYPE,
                   uname='root',
                   gname='root',
                   mtime=mtime)
        f.add_file('./usr/',
                   tarfile.DIRTYPE,
                   uname='root',
                   gname='root',
                   mtime=mtime)
        f.add_file('./usr/bin/',
                   tarfile.DIRTYPE,
                   uname='root',
                   gname='root',
                   mtime=mtime)
Beispiel #19
0
 def testEmptyTarFile(self):
     with archive.TarFileWriter(self.tempfile):
         pass
     self.assertTarFileContent(self.tempfile, [])