Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #163 from pragmattica/Issue162
Browse files Browse the repository at this point in the history
Wraps use of the Python 'pwd' module in a utility procedure for Windows compatibility
  • Loading branch information
Wouter de Bie committed Jul 28, 2015
2 parents 2cea49e + 4e614d6 commit f5237e1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions snakebite/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
# Standard library imports
import socket
import os
import pwd
import math

# Third party imports
Expand All @@ -53,6 +52,7 @@
from snakebite.protobuf.ProtobufRpcEngine_pb2 import RequestHeaderProto
from snakebite.protobuf.datatransfer_pb2 import OpReadBlockProto, BlockOpResponseProto, PacketHeaderProto, ClientReadStatusProto

from snakebite.platformutils import get_current_username
from snakebite.formatter import format_bytes
from snakebite.errors import RequestError
from snakebite.crc32c import crc
Expand Down Expand Up @@ -181,7 +181,7 @@ def __init__(self, host, port, version, effective_user=None, use_sasl=False):
kerberos = Kerberos()
self.effective_user = effective_user or kerberos.user_principal().name
else:
self.effective_user = effective_user or pwd.getpwuid(os.getuid())[0]
self.effective_user = effective_user or get_current_username()

def validate_request(self, request):
'''Validate the client request against the protocol file.'''
Expand Down
8 changes: 4 additions & 4 deletions snakebite/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import snakebite.protobuf.ClientNamenodeProtocol_pb2 as client_proto
import snakebite.glob as glob
from snakebite.platformutils import get_current_username
from snakebite.channel import DataXceiverChannel
from snakebite.config import HDFSConfig
from snakebite.errors import (
Expand All @@ -36,7 +37,6 @@
import logging
import os
import os.path
import pwd
import fnmatch
import inspect
import socket
Expand Down Expand Up @@ -1176,7 +1176,7 @@ def _find_items(self, paths, processor, include_toplevel=False, include_children
'''

if not paths:
paths = [os.path.join("/user", pwd.getpwuid(os.getuid())[0])]
paths = [os.path.join("/user", get_current_username())]

# Expand paths if necessary (/foo/{bar,baz} --> ['/foo/bar', '/foo/baz'])
paths = glob.expand_paths(paths)
Expand Down Expand Up @@ -1326,10 +1326,10 @@ def _get_file_info(self, path):
return self.service.getFileInfo(request)

def _join_user_path(self, path):
return os.path.join("/user", pwd.getpwuid(os.getuid())[0], path)
return os.path.join("/user", get_current_username(), path)

def _remove_user_path(self, path):
dir_to_remove = os.path.join("/user", pwd.getpwuid(os.getuid())[0])
dir_to_remove = os.path.join("/user", get_current_username())
return path.replace(dir_to_remove+'/', "", 1)

def _normalize_path(self, path):
Expand Down
4 changes: 2 additions & 2 deletions snakebite/commandlineparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import errno
import sys
import os
import pwd
import json
from urlparse import urlparse

Expand All @@ -35,6 +34,7 @@
from snakebite.config import HDFSConfig
from snakebite.version import version
from snakebite.namenode import Namenode
from snakebite.platformutils import get_current_username


def print_error_exit(msg, fd=sys.stderr):
Expand Down Expand Up @@ -194,7 +194,7 @@ def _build_parent_parser(self):
self.parser.add_argument(opt_data['short'], opt_data['long'], help=opt_data['help'], type=opt_data['type'])

def _add_subparsers(self):
default_dir = os.path.join("/user", pwd.getpwuid(os.getuid())[0])
default_dir = os.path.join("/user", get_current_username())

#sub-options
arg_parsers = {}
Expand Down
13 changes: 13 additions & 0 deletions snakebite/platformutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
import platform

if platform.system() != "Windows":
import pwd
else:
import getpass

def get_current_username():
if platform.system() != "Windows":
return pwd.getpwuid(os.getuid())[0]
else:
return getpass.getuser()
6 changes: 3 additions & 3 deletions test/commandlineparser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# the License.
import unittest2
import os
import pwd
import json
import sys
import traceback
Expand All @@ -24,14 +23,15 @@
from snakebite.config import HDFSConfig
from snakebite.commandlineparser import Commands, CommandLineParser
from snakebite.namenode import Namenode
from snakebite.platformutils import get_current_username

from config_test import ConfigTest

class CommandLineParserTest(unittest2.TestCase):

def setUp(self):
self.parser = CommandLineParser()
self.default_dir = os.path.join("/user", pwd.getpwuid(os.getuid())[0])
self.default_dir = os.path.join("/user", get_current_username())

def test_general_options(self):
parser = self.parser
Expand Down Expand Up @@ -660,7 +660,7 @@ def __contains__(self, b):
class CommandLineParserInternalConfigTest(unittest2.TestCase):
def setUp(self):
self.parser = CommandLineParser()
self.default_dir = os.path.join("/user", pwd.getpwuid(os.getuid())[0])
self.default_dir = os.path.join("/user", get_current_username())


def assert_namenode_spec(self, host, port, version=None):
Expand Down
4 changes: 2 additions & 2 deletions test/delete_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
# the License.
from snakebite.errors import FileNotFoundException
from snakebite.errors import InvalidInputException
from snakebite.platformutils import get_current_username
from minicluster_testbase import MiniClusterTestBase

import pwd
import os
import re

Expand Down Expand Up @@ -59,7 +59,7 @@ class DeleteWithTrashTest(MiniClusterTestBase):
def setUp(self):
super(DeleteWithTrashTest, self).setUp()
self.client.use_trash = True
self.username = pwd.getpwuid(os.getuid())[0]
self.username = get_current_username()
self.trash_location = "/user/%s/.Trash/Current" % self.username

def assertNotExists(self, location_under_test):
Expand Down

0 comments on commit f5237e1

Please sign in to comment.