Skip to content
This repository was archived by the owner on Dec 5, 2022. It is now read-only.

Commit 237a471

Browse files
committed
Merge pull request #1 from cnelson/python2_compatibility
Updated to support python2.6 and python2.7; added a simple test
2 parents 0e83c55 + 60bdf4f commit 237a471

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
build
22
dist
33
*.egg*
4+
*.pyc
5+
.tox

nsenter/__init__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import os
1010
import logging
1111
from pathlib import Path
12-
from contextlib import ExitStack
12+
try:
13+
from contextlib import ExitStack
14+
except ImportError:
15+
from contextlib2 import ExitStack
1316

1417
NAMESPACE_NAMES = frozenset('mnt ipc net pid user uts'.split())
1518

@@ -18,22 +21,24 @@
1821
libc = ctypes.CDLL('libc.so.6')
1922

2023

21-
def nsfd(process: str, ns_type: str) -> Path:
24+
def nsfd(process, ns_type):
2225
"""
2326
Returns the namespace file descriptor for process (self or PID) and namespace type
2427
"""
2528
return Path('/proc') / str(process) / 'ns' / ns_type
29+
nsfd.__annotations__ = {'process': str, 'ns_type': str, 'return': Path}
2630

27-
28-
class Namespace:
29-
def __init__(self, pid: str, ns_type: str):
31+
class Namespace(object):
32+
def __init__(self, pid, ns_type):
3033
self.pid = pid
3134
self.ns_type = ns_type
3235
self.parent_fd = nsfd('self', ns_type).open()
3336
self.parent_fileno = self.parent_fd.fileno()
3437
self.target_fd = nsfd(pid, ns_type).open()
3538
self.target_fileno = self.target_fd.fileno()
3639

40+
__init__.__annotations__ = {'pid': str, 'ns_type': str}
41+
3742
def __enter__(self):
3843
log.debug('Entering %s namespace %s', self.ns_type, self.pid)
3944
libc.setns(self.target_fileno, 0)
@@ -53,7 +58,7 @@ def main():
5358
parser.add_argument('--target', '-t', required=True, metavar='PID',
5459
help='Specify a target process to get contexts from.')
5560
for ns in NAMESPACE_NAMES:
56-
parser.add_argument('--{}'.format(ns), action='store_true', help='Enter the {} namespace'.format(ns))
61+
parser.add_argument('--{0}'.format(ns), action='store_true', help='Enter the {0} namespace'.format(ns))
5762
parser.add_argument('--all', action='store_true', help='Enter all namespaces')
5863
parser.add_argument('command', nargs='*', default=['/bin/sh'])
5964

@@ -68,6 +73,5 @@ def main():
6873
stack.enter_context(ns)
6974
os.execl(args.command[0], *args.command)
7075

71-
7276
if __name__ == '__main__':
7377
main()

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ def setup_package():
2525
classifiers=[
2626
'Development Status :: 4 - Beta',
2727
'Programming Language :: Python',
28+
'Programming Language :: Python :: 2',
29+
'Programming Language :: Python :: 2.6',
30+
'Programming Language :: Python :: 2.7',
31+
'Programming Language :: Python :: 3',
2832
'Programming Language :: Python :: 3.4',
2933
'Programming Language :: Python :: Implementation :: CPython',
3034
'Operating System :: POSIX :: Linux',
3135
'License :: OSI Approved :: Apache Software License'],
3236
test_suite='tests',
33-
setup_requires=['flake8'],
37+
setup_requires=['flake8', 'pathlib', 'contextlib2', 'argparse'],
3438
packages=setuptools.find_packages(exclude=['tests', 'tests.*']),
3539
entry_points={'console_scripts': ['nsenter = nsenter:main']}
3640
)

tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
3+
import subprocess
4+
5+
from nsenter import Namespace, NAMESPACE_NAMES
6+
7+
class TestNamespaces(unittest.TestCase):
8+
9+
def setUp(self):
10+
self._child = subprocess.Popen(['/bin/cat'])
11+
12+
def tearDown(self):
13+
self._child.terminate()
14+
self._child.wait()
15+
16+
def test_namespace(self):
17+
for name in NAMESPACE_NAMES:
18+
with Namespace(self._child.pid, name):
19+
assert True
20+
21+
pass
22+
23+
if __name__ == '__main__':
24+
unittest.main()
25+

tox.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
[tox]
2+
envlist = py26,py27,py31,py32,py33,py34
3+
[testenv]
4+
commands=python setup.py test
15
[flake8]
26
max-line-length=120

0 commit comments

Comments
 (0)