Remove more unneeded features from @bazel_tools/build_defs/pkg/pkg.bzl
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: https://github.com/bazelbuild/bazel/issues/11183
RELNOTES: None
PiperOrigin-RevId: 377908363
diff --git a/tools/build_defs/pkg/archive.py b/tools/build_defs/pkg/archive.py
index 8d6e569..d510948 100644
--- a/tools/build_defs/pkg/archive.py
+++ b/tools/build_defs/pkg/archive.py
@@ -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");
@@ -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
@@ -58,7 +56,7 @@
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
@@ -113,7 +111,7 @@
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:
@@ -126,7 +124,7 @@
# 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,
@@ -158,8 +156,7 @@
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:
@@ -172,7 +169,6 @@
def add_file(self,
name,
kind=tarfile.REGTYPE,
- content=None,
link=None,
file_content=None,
uid=0,
@@ -186,7 +182,6 @@
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.
@@ -235,11 +230,7 @@
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)
@@ -274,7 +265,7 @@
"""
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'
@@ -286,9 +277,9 @@
# 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):
diff --git a/tools/build_defs/pkg/archive_test.py b/tools/build_defs/pkg/archive_test.py
index a300f1d..4cda0b7 100644
--- a/tools/build_defs/pkg/archive_test.py
+++ b/tools/build_defs/pkg/archive_test.py
@@ -77,19 +77,6 @@
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)
@@ -118,14 +105,6 @@
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")