<commit_before># -*- coding: utf-8 -*-
# Copyright 2013 Google Inc. 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.

from __future__ import absolute_import

from contextlib import contextmanager
from cStringIO import StringIO
import functools
import os
import pkgutil
import posixpath
import re
import sys
import tempfile
import unittest
import urlparse

import boto
import crcmod
from gslib.cloud_api import ResumableDownloadException
from gslib.cloud_api import ResumableUploadException
from gslib.encryption_helper import Base64Sha256FromBase64EncryptionKey
from gslib.posix_util import GetDefaultMode
import gslib.tests as gslib_tests
from gslib.util import IS_WINDOWS
from gslib.util import LazyWrapper
from gslib.util import MakeHumanReadable
from gslib.util import UsingCrcmodExtension

# pylint: disable=g-import-not-at-top, g-long-lambda
if not IS_WINDOWS:
  import grp
  import pwd

  USER_ID = os.getuid()
  USER_NAME = LazyWrapper(lambda: (pwd.getpwuid(USER_ID).pw_name))
  PRIMARY_GID = LazyWrapper(lambda: (pwd.getpwuid(USER_ID).pw_gid))

  # Get a list of all groups on the system where the current username is listed
  # as a member of the group in the gr_mem group attribute. Make this a list of
  # all group IDs and cast as a set for more efficient lookup times.
  USER_GROUPS = LazyWrapper(
      lambda: set([PRIMARY_GID] +
                  [g.gr_gid for g in grp.getgrall()
                   if USER_NAME in g.gr_mem]))
  # Select a group for the current user that is not the user's primary group. If
  # the length of the user's groups is 1, then we must use the primary group.
  # Otherwise put all of the user's groups (except the primary group) in a list,
  # and use the first element. This guarantees us a group that is not the user's
  # primary group (unless the user is only a member of one group).
  NON_PRIMARY_GID = LazyWrapper(lambda: (PRIMARY_GID if len(USER_GROUPS) == 1
                                         else [g for g in list(USER_GROUPS)
                                               if g != PRIMARY_GID][0]))

  DEFAULT_MODE = int(GetDefaultMode(), 8)
<commit_msg>Use os.getgid over pwd.getpwuid to avoid stale GID<commit_after># -*- coding: utf-8 -*-
# Copyright 2013 Google Inc. 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.

from __future__ import absolute_import

from contextlib import contextmanager
from cStringIO import StringIO
import functools
import os
import pkgutil
import posixpath
import re
import sys
import tempfile
import unittest
import urlparse

import boto
import crcmod
from gslib.cloud_api import ResumableDownloadException
from gslib.cloud_api import ResumableUploadException
from gslib.encryption_helper import Base64Sha256FromBase64EncryptionKey
from gslib.posix_util import GetDefaultMode
import gslib.tests as gslib_tests
from gslib.util import IS_WINDOWS
from gslib.util import LazyWrapper
from gslib.util import MakeHumanReadable
from gslib.util import UsingCrcmodExtension

# pylint: disable=g-import-not-at-top, g-long-lambda
if not IS_WINDOWS:
  import grp
  import pwd

  USER_ID = os.getuid()
  USER_NAME = LazyWrapper(lambda: pwd.getpwuid(USER_ID).pw_name)
  PRIMARY_GID = LazyWrapper(lambda: os.getgid())

  # Get a list of all groups on the system where the current username is listed
  # as a member of the group in the gr_mem group attribute. Make this a list of
  # all group IDs and cast as a set for more efficient lookup times.
  USER_GROUPS = LazyWrapper(
      lambda: set([PRIMARY_GID] +
                  [g.gr_gid for g in grp.getgrall()
                   if USER_NAME in g.gr_mem]))
  # Select a group for the current user that is not the user's primary group. If
  # the length of the user's groups is 1, then we must use the primary group.
  # Otherwise put all of the user's groups (except the primary group) in a list,
  # and use the first element. This guarantees us a group that is not the user's
  # primary group (unless the user is only a member of one group).
  NON_PRIMARY_GID = LazyWrapper(lambda: (PRIMARY_GID if len(USER_GROUPS) == 1
                                         else [g for g in list(USER_GROUPS)
                                               if g != PRIMARY_GID][0]))

  DEFAULT_MODE = int(GetDefaultMode(), 8)

{'fraction_non_alphanumeric': 0.05963650132525559, 'fraction_numerical': 0.007572889057175312, 'mean_word_length': 3.5425623387790197, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5282372', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1512, 'n_words': 651}