Skip to content

Commit

Permalink
Remove more unneeded features from @bazel_tools/build_defs/pkg/pkg.bzl
Browse files Browse the repository at this point in the history
Specifically:
- Remove the dependency on six
- Switch to pure python3
- Remove a capability to push raw data from a python program into a tar file. The capability was in an internal library, which no one should be using.

See: #11183

RELNOTES: None
PiperOrigin-RevId: 377908363
  • Loading branch information
aiuto authored and copybara-github committed Jun 7, 2021
1 parent 5ff09e0 commit 0954af6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
27 changes: 9 additions & 18 deletions tools/build_defs/pkg/archive.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Lint as: python2, python3
# Lint as: python3
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,10 +19,8 @@
from __future__ import division
from __future__ import print_function
import gzip
import io
import os
import tarfile
import six

# Use a deterministic mtime that doesn't confuse other programs.
# See: https://github.com/bazelbuild/bazel/issues/1299
Expand Down Expand Up @@ -58,7 +56,7 @@ def __init__(self,
mode = 'w:'
self.gz = compression in ['tgz', 'gz']
self.name = name
self.root_directory = six.ensure_str(root_directory).rstrip('/')
self.root_directory = root_directory.rstrip('/')

self.preserve_mtime = preserve_tar_mtimes

Expand Down Expand Up @@ -113,7 +111,7 @@ def add_dir(self,
TarFileWriter.Error: when the recursion depth has exceeded the
`depth` argument.
"""
if not (name == self.root_directory or six.ensure_str(name).startswith('/')
if not (name == self.root_directory or name.startswith('/')
or name.startswith(self.root_directory + '/')):
name = os.path.join(self.root_directory, name)
if mtime is None:
Expand All @@ -126,7 +124,7 @@ def add_dir(self,
# The x bit is set only to if the read bit is set.
dirmode = (mode | ((0o444 & mode) >> 2)) if mode else mode
self.add_file(
six.ensure_str(name) + '/',
name + '/',
tarfile.DIRTYPE,
uid=uid,
gid=gid,
Expand Down Expand Up @@ -158,8 +156,7 @@ def add_dir(self,

def _addfile(self, info, fileobj=None):
"""Add a file in the tar file if there is no conflict."""
if not six.ensure_str(
info.name).endswith('/') and info.type == tarfile.DIRTYPE:
if not info.name.endswith('/') and info.type == tarfile.DIRTYPE:
# Enforce the ending / for directories so we correctly deduplicate.
info.name += '/'
if info.name not in self.members:
Expand All @@ -172,7 +169,6 @@ def _addfile(self, info, fileobj=None):
def add_file(self,
name,
kind=tarfile.REGTYPE,
content=None,
link=None,
file_content=None,
uid=0,
Expand All @@ -186,7 +182,6 @@ def add_file(self,
Args:
name: the name of the file to add.
kind: the type of the file to add, see tarfile.*TYPE.
content: a textual content to put in the file.
link: if the file is a link, the destination of the link.
file_content: file to read the content from. Provide either this
one or `content` to specifies a content for the file.
Expand Down Expand Up @@ -235,11 +230,7 @@ def add_file(self,
tarinfo.mode = mode
if link:
tarinfo.linkname = link
if content:
content_bytes = six.ensure_binary(content, 'utf-8')
tarinfo.size = len(content_bytes)
self._addfile(tarinfo, io.BytesIO(content_bytes))
elif file_content:
if file_content:
with open(file_content, 'rb') as f:
tarinfo.size = os.fstat(f.fileno()).st_size
self._addfile(tarinfo, f)
Expand Down Expand Up @@ -274,7 +265,7 @@ def add_tar(self,
"""
if root and root[0] not in ['/', '.']:
# Root prefix should start with a '/', adds it if missing
root = '/' + six.ensure_str(root)
root = '/' + root
compression = os.path.splitext(tar)[-1][1:]
if compression == 'tgz':
compression = 'gz'
Expand All @@ -286,9 +277,9 @@ def add_tar(self,
# prevent performance issues due to accidentally-introduced seeks
# during intar traversal by opening in "streaming" mode. gz, bz2
# are supported natively by python 2.7 and 3.x
inmode = 'r|' + six.ensure_str(compression)
inmode = 'r|' + compression
else:
inmode = 'r:' + six.ensure_str(compression)
inmode = 'r:' + compression
intar = tarfile.open(name=tar, mode=inmode)
for tarinfo in intar:
if name_filter is None or name_filter(tarinfo.name):
Expand Down
21 changes: 0 additions & 21 deletions tools/build_defs/pkg/archive_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,6 @@ def testEmptyTarFile(self):
pass
self.assertTarFileContent(self.tempfile, [])

def assertSimpleFileContent(self, names):
with archive.TarFileWriter(self.tempfile) as f:
for n in names:
f.add_file(n, content=n.encode("utf-8"))
content = ([{
"name": "."
}] + [{
"name": n,
"size": len(n.encode("utf-8")),
"data": n.encode("utf-8")
} for n in names])
self.assertTarFileContent(self.tempfile, content)

def testDefaultMtimeNotProvided(self):
with archive.TarFileWriter(self.tempfile) as f:
self.assertEqual(f.default_mtime, 0)
Expand Down Expand Up @@ -118,14 +105,6 @@ def testPreserveTarMtimesFalse(self):
for output_file in f.tar:
self.assertEqual(output_file.mtime, 0)

def testAddFile(self):
self.assertSimpleFileContent(["./a"])
self.assertSimpleFileContent(["./b"])
self.assertSimpleFileContent(["./ab"])
self.assertSimpleFileContent(["./a", "./b"])
self.assertSimpleFileContent(["./a", "./ab"])
self.assertSimpleFileContent(["./a", "./b", "./ab"])

def testDottedFiles(self):
with archive.TarFileWriter(self.tempfile) as f:
f.add_file("a")
Expand Down

0 comments on commit 0954af6

Please sign in to comment.