<commit_before>#!/usr/bin/env python3
#
# Copyright 2021 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.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.
# pylint: disable=missing-function-docstring
# pylint: disable=too-many-instance-attributes
# pylint: disable=unspecified-encoding
# pylint: disable=invalid-name
"""Tool classes to handle a Bazel registry"""

import base64
import difflib
import hashlib
import json
import netrc
import pathlib
import shutil
import urllib.parse
import urllib.request
import yaml

GREEN = "\x1b[32m"
RESET = "\x1b[0m"


def log(msg):
  print(f"{GREEN}INFO: {RESET}{msg}")


def download(url):
  parts = urllib.parse.urlparse(url)
  authenticators = netrc.netrc().authenticators(parts.netloc)
  if authenticators != None:
    (login, _, password) = authenticators
    req = urllib.request.Request(url)
    creds = base64.b64encode(str.encode('%s:%s' % (login, password))).decode()
    req.add_header("Authorization", "Basic %s" % creds)
  else:
    req = url

  with urllib.request.urlopen(req) as response:
    return response.read()


def read(path):
  with open(path, "rb") as file:
    return file.read()


def integrity(data):
  hash_value = hashlib.sha256(data)
  return "sha256-" + base64.b64encode(hash_value.digest()).decode()<commit_msg>Correctly handle missing .netrc for #227 (#228)<commit_after>#!/usr/bin/env python3
#
# Copyright 2021 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.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.
# pylint: disable=missing-function-docstring
# pylint: disable=too-many-instance-attributes
# pylint: disable=unspecified-encoding
# pylint: disable=invalid-name
"""Tool classes to handle a Bazel registry"""

import base64
import difflib
import hashlib
import json
import netrc
import pathlib
import shutil
import urllib.parse
import urllib.request
import yaml

GREEN = "\x1b[32m"
RESET = "\x1b[0m"


def log(msg):
  print(f"{GREEN}INFO: {RESET}{msg}")


def download(url):
  parts = urllib.parse.urlparse(url)
  try:
    authenticators = netrc.netrc().authenticators(parts.netloc)
  except FileNotFoundError:
    authenticators = None
  if authenticators != None:
    (login, _, password) = authenticators
    req = urllib.request.Request(url)
    creds = base64.b64encode(str.encode('%s:%s' % (login, password))).decode()
    req.add_header("Authorization", "Basic %s" % creds)
  else:
    req = url

  with urllib.request.urlopen(req) as response:
    return response.read()


def read(path):
  with open(path, "rb") as file:
    return file.read()


def integrity(data):
  hash_value = hashlib.sha256(data)
  return "sha256-" + base64.b64encode(hash_value.digest()).decode()
{'fraction_non_alphanumeric': 0.11169188445667125, 'fraction_numerical': 0.018156808803301238, 'mean_word_length': 4.525835866261398, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2083154', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1120, 'n_words': 366}