Skip to content

Commit 6273019

Browse files
committed
modules: make session as func param not global
1 parent 7e645dc commit 6273019

File tree

9 files changed

+63
-42
lines changed

9 files changed

+63
-42
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ import pprint
1818
import winrm
1919

2020
from winremote import winremote
21+
from winremote.modules import services
2122

2223
session = winrm.Session(target='10.0.0.1', auth=('Administrator', '******'))
23-
win = winremote.Windows(session, winremote.WMI(session))
24-
pprint.pprint(win.services.get('WinRM'))
24+
pprint.pprint(
25+
services.get(
26+
winremote.Windows(session, winremote.WMI(session)),
27+
'WinRM'
28+
)
29+
)
2530
```
2631

2732
This package uses [pywinrm](https://pypi.python.org/pypi/pywinrm/),

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bdist_rpm]
2-
release = 1.0.7
2+
release = 1.1.0
33
packager = Ondra Machacek <[email protected]>
44
doc_files = README.md
55
description = 'Tool to manage your windows machines remotely'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name='winremote',
13-
version='1.0.7',
13+
version='1.1.0',
1414
description='Tool to manage your windows machines remotely',
1515
long_description=long_description,
1616
url='https://github.com/machacekondra/winremote',

winremote/__main__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,18 @@ def main():
7979

8080
pprint.pprint(
8181
getattr(
82-
getattr(win, args.args[0]),
82+
getattr(
83+
__import__(
84+
'modules',
85+
globals(),
86+
locals(),
87+
[args.args[0]],
88+
-1
89+
),
90+
args.args[0],
91+
),
8392
args.args[1],
84-
)(*args.args[2:], **kw)
93+
)(win, *args.args[2:], **kw)
8594
)
8695
ret = 0
8796
except Exception as e:

winremote/modules/devices.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
This module implements work with devices via WMI.
33
"""
44

5-
# session is dynamically loaded
6-
session = None
75

8-
9-
def list(attributes='Name,ConfigManagerErrorCode'):
6+
def list(session, attributes='Name,ConfigManagerErrorCode'):
107
"""
118
Description: return list of all devices on windows machine
129
10+
:param session: instance of Windows, which hold session to win machine
11+
:type session: winremote.Windows
1312
:param attributes: comma delimited name of attributes to be returned
1413
:type attributes: str
1514
:returns: list of devices info
@@ -18,10 +17,12 @@ def list(attributes='Name,ConfigManagerErrorCode'):
1817
return session._wmi.query('select %s from Win32_PnPEntity' % attributes)
1918

2019

21-
def status(name):
20+
def status(session, name):
2221
"""
2322
Description: check status of device
2423
24+
:param session: instance of Windows, which hold session to win machine
25+
:type session: winremote.Windows
2526
:param name: name of the device to fetch info
2627
:type name: str
2728
:returns: True or False, True if device is OK, False otherwise
@@ -36,10 +37,12 @@ def status(name):
3637
return False
3738

3839

39-
def get(name, attributes='Name'):
40+
def get(session, name, attributes='Name'):
4041
"""
4142
Description: get basic info about windows device @name
4243
44+
:param session: instance of Windows, which hold session to win machine
45+
:type session: winremote.Windows
4346
:param attributes: comma delimited name of attributes to be returned
4447
:type attributes: str
4548
:param name: name of the device to fetch info

winremote/modules/files.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
This module implements work with files and basic cli commands.
33
"""
44

5-
# session is dynamically loaded
6-
session = None
75

8-
9-
def __transfer_text(src_path, dest_path):
6+
def __transfer_text(session, src_path, dest_path):
107
content = ''
118
with open(src_path) as f:
129
content = f.read()
@@ -18,10 +15,12 @@ def __transfer_text(src_path, dest_path):
1815
return not r.status_code, r.std_out, r.std_err
1916

2017

21-
def copy_remote(src_path, dest_path):
18+
def copy_remote(session, src_path, dest_path):
2219
"""
2320
Copy file from local machine to windows machine
2421
22+
:param session: instance of Windows, which hold session to win machine
23+
:type session: winremote.Windows
2524
:param src_path: source path of file/directory on local machine
2625
:type src_path: str
2726
:param dest_path: destination path of file/directory on windows machine
@@ -33,10 +32,12 @@ def copy_remote(src_path, dest_path):
3332
return __transfer_text(src_path, dest_path)
3433

3534

36-
def copy_local(src_path, dest_path):
35+
def copy_local(session, src_path, dest_path):
3736
"""
3837
Copy file/directory
3938
39+
:param session: instance of Windows, which hold session to win machine
40+
:type session: winremote.Windows
4041
:param src_path: source path of file/directory
4142
:type src_path: str
4243
:param dest_path: destination path of file/directory
@@ -47,10 +48,12 @@ def copy_local(src_path, dest_path):
4748
return session.run_cmd('copy /Y "%s" "%s"' % (src_path, dest_path))
4849

4950

50-
def make_dir(path):
51+
def make_dir(session, path):
5152
"""
5253
Create directory
5354
55+
:param session: instance of Windows, which hold session to win machine
56+
:type session: winremote.Windows
5457
:param path: path to directory
5558
:type path: str
5659
:returns: result of command, status_code, std_out, std_err
@@ -59,10 +62,12 @@ def make_dir(path):
5962
return session.run_cmd('mkdir "%s"' % path)
6063

6164

62-
def remove_dir(path):
65+
def remove_dir(session, path):
6366
"""
6467
Remove directory
6568
69+
:param session: instance of Windows, which hold session to win machine
70+
:type session: winremote.Windows
6671
:param path: path to directory
6772
:type path: str
6873
:returns: result of command, status_code, std_out, std_err
@@ -71,10 +76,12 @@ def remove_dir(path):
7176
return session.run_cmd('rmdir "%s"' % path)
7277

7378

74-
def cat_file(path):
79+
def cat_file(session, path):
7580
"""
7681
Get file content
7782
83+
:param session: instance of Windows, which hold session to win machine
84+
:type session: winremote.Windows
7885
:param path: path to file
7986
:type path: str
8087
:returns: content of file
@@ -83,10 +90,12 @@ def cat_file(path):
8390
return session.run_cmd('type "%s"' % path)[1]
8491

8592

86-
def remove_file(path):
93+
def remove_file(session, path):
8794
"""
8895
Remove file
8996
97+
:param session: instance of Windows, which hold session to win machine
98+
:type session: winremote.Windows
9099
:param path: path to file
91100
:type path: str
92101
:returns: result of command, status_code, std_out, std_err
@@ -95,10 +104,12 @@ def remove_file(path):
95104
return session.run_cmd('del "%s"' % path)
96105

97106

98-
def exists(path):
107+
def exists(session, path):
99108
"""
100109
Check if file/directory exists
101110
111+
:param session: instance of Windows, which hold session to win machine
112+
:type session: winremote.Windows
102113
:param path: path to file
103114
:type path: str
104115
:returns: True or False, True if file/dir exists False otherwise

winremote/modules/products.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
This module implements work with products via WMI.
33
"""
44

5-
# session is dynamically loaded
6-
session = None
75

8-
9-
def list(attributes='Name'):
6+
def list(session, attributes='Name'):
107
"""
118
Description: return list of all products installed on windows machine
129
10+
:param session: instance of Windows, which hold session to win machine
11+
:type session: winremote.Windows
1312
:param attributes: comma delimited name of attributes to be returned
1413
:type attributes: str
1514
:returns: list of installed products
@@ -18,10 +17,12 @@ def list(attributes='Name'):
1817
return session._wmi.query('select %s from win32_product' % attributes)
1918

2019

21-
def get(name, attributes='Name'):
20+
def get(session, name, attributes='Name'):
2221
"""
2322
Description: return information about product @name
2423
24+
:param session: instance of Windows, which hold session to win machine
25+
:type session: winremote.Windows
2526
:param name: name of product to be searched
2627
:type name: str
2728
:param attributes: comma delimited name of attributes to be returned

winremote/modules/services.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
This module implements work with services via WMI.
33
"""
44

5-
# session is dynamically loaded
6-
session = None
75

8-
9-
def list(attributes='Name,State,StartMode'):
6+
def list(session, attributes='Name,State,StartMode'):
107
"""
118
Description: return list of all services on windows machine
129
10+
:param session: instance of Windows, which hold session to win machine
11+
:type session: winremote.Windows
1312
:param attributes: comma delimited name of attributes to be returned
1413
:type attributes: str
1514
:returns: list of services info
@@ -18,10 +17,12 @@ def list(attributes='Name,State,StartMode'):
1817
return session._wmi.query('select %s from Win32_service' % attributes)
1918

2019

21-
def get(name, attributes='Name,State,StartMode'):
20+
def get(session, name, attributes='Name,State,StartMode'):
2221
"""
2322
Description: return information about service @name
2423
24+
:param session: instance of Windows, which hold session to win machine
25+
:type session: winremote.Windows
2526
:param name: name of service to be searched
2627
:type name: str
2728
:param attributes: comma delimited name of attributes to be returned

winremote/winremote.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@ def __init__(self, session, wmi):
2626
self._session = session
2727
self._wmi = wmi
2828

29-
def __getattr__(self, name):
30-
mod = getattr(
31-
__import__('modules', globals(), locals(), [name], -1),
32-
name,
33-
)
34-
setattr(mod, 'session', self)
35-
setattr(self, name, mod)
36-
return mod
37-
3829
def run_cmd(self, cmd, params=[]):
3930
"""
4031
Run command on windows.

0 commit comments

Comments
 (0)