Skip to content

Commit

Permalink
modules: make session as func param not global
Browse files Browse the repository at this point in the history
  • Loading branch information
machacekondra committed Oct 9, 2015
1 parent 7e645dc commit 6273019
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 42 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ import pprint
import winrm

from winremote import winremote
from winremote.modules import services

session = winrm.Session(target='10.0.0.1', auth=('Administrator', '******'))
win = winremote.Windows(session, winremote.WMI(session))
pprint.pprint(win.services.get('WinRM'))
pprint.pprint(
services.get(
winremote.Windows(session, winremote.WMI(session)),
'WinRM'
)
)
```

This package uses [pywinrm](https://pypi.python.org/pypi/pywinrm/),
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bdist_rpm]
release = 1.0.7
release = 1.1.0
packager = Ondra Machacek <[email protected]>
doc_files = README.md
description = 'Tool to manage your windows machines remotely'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='winremote',
version='1.0.7',
version='1.1.0',
description='Tool to manage your windows machines remotely',
long_description=long_description,
url='https://github.com/machacekondra/winremote',
Expand Down
13 changes: 11 additions & 2 deletions winremote/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,18 @@ def main():

pprint.pprint(
getattr(
getattr(win, args.args[0]),
getattr(
__import__(
'modules',
globals(),
locals(),
[args.args[0]],
-1
),
args.args[0],
),
args.args[1],
)(*args.args[2:], **kw)
)(win, *args.args[2:], **kw)
)
ret = 0
except Exception as e:
Expand Down
15 changes: 9 additions & 6 deletions winremote/modules/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
This module implements work with devices via WMI.
"""

# session is dynamically loaded
session = None


def list(attributes='Name,ConfigManagerErrorCode'):
def list(session, attributes='Name,ConfigManagerErrorCode'):
"""
Description: return list of all devices on windows machine
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param attributes: comma delimited name of attributes to be returned
:type attributes: str
:returns: list of devices info
Expand All @@ -18,10 +17,12 @@ def list(attributes='Name,ConfigManagerErrorCode'):
return session._wmi.query('select %s from Win32_PnPEntity' % attributes)


def status(name):
def status(session, name):
"""
Description: check status of device
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param name: name of the device to fetch info
:type name: str
:returns: True or False, True if device is OK, False otherwise
Expand All @@ -36,10 +37,12 @@ def status(name):
return False


def get(name, attributes='Name'):
def get(session, name, attributes='Name'):
"""
Description: get basic info about windows device @name
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param attributes: comma delimited name of attributes to be returned
:type attributes: str
:param name: name of the device to fetch info
Expand Down
33 changes: 22 additions & 11 deletions winremote/modules/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
This module implements work with files and basic cli commands.
"""

# session is dynamically loaded
session = None


def __transfer_text(src_path, dest_path):
def __transfer_text(session, src_path, dest_path):
content = ''
with open(src_path) as f:
content = f.read()
Expand All @@ -18,10 +15,12 @@ def __transfer_text(src_path, dest_path):
return not r.status_code, r.std_out, r.std_err


def copy_remote(src_path, dest_path):
def copy_remote(session, src_path, dest_path):
"""
Copy file from local machine to windows machine
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param src_path: source path of file/directory on local machine
:type src_path: str
:param dest_path: destination path of file/directory on windows machine
Expand All @@ -33,10 +32,12 @@ def copy_remote(src_path, dest_path):
return __transfer_text(src_path, dest_path)


def copy_local(src_path, dest_path):
def copy_local(session, src_path, dest_path):
"""
Copy file/directory
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param src_path: source path of file/directory
:type src_path: str
:param dest_path: destination path of file/directory
Expand All @@ -47,10 +48,12 @@ def copy_local(src_path, dest_path):
return session.run_cmd('copy /Y "%s" "%s"' % (src_path, dest_path))


def make_dir(path):
def make_dir(session, path):
"""
Create directory
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param path: path to directory
:type path: str
:returns: result of command, status_code, std_out, std_err
Expand All @@ -59,10 +62,12 @@ def make_dir(path):
return session.run_cmd('mkdir "%s"' % path)


def remove_dir(path):
def remove_dir(session, path):
"""
Remove directory
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param path: path to directory
:type path: str
:returns: result of command, status_code, std_out, std_err
Expand All @@ -71,10 +76,12 @@ def remove_dir(path):
return session.run_cmd('rmdir "%s"' % path)


def cat_file(path):
def cat_file(session, path):
"""
Get file content
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param path: path to file
:type path: str
:returns: content of file
Expand All @@ -83,10 +90,12 @@ def cat_file(path):
return session.run_cmd('type "%s"' % path)[1]


def remove_file(path):
def remove_file(session, path):
"""
Remove file
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param path: path to file
:type path: str
:returns: result of command, status_code, std_out, std_err
Expand All @@ -95,10 +104,12 @@ def remove_file(path):
return session.run_cmd('del "%s"' % path)


def exists(path):
def exists(session, path):
"""
Check if file/directory exists
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param path: path to file
:type path: str
:returns: True or False, True if file/dir exists False otherwise
Expand Down
11 changes: 6 additions & 5 deletions winremote/modules/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
This module implements work with products via WMI.
"""

# session is dynamically loaded
session = None


def list(attributes='Name'):
def list(session, attributes='Name'):
"""
Description: return list of all products installed on windows machine
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param attributes: comma delimited name of attributes to be returned
:type attributes: str
:returns: list of installed products
Expand All @@ -18,10 +17,12 @@ def list(attributes='Name'):
return session._wmi.query('select %s from win32_product' % attributes)


def get(name, attributes='Name'):
def get(session, name, attributes='Name'):
"""
Description: return information about product @name
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param name: name of product to be searched
:type name: str
:param attributes: comma delimited name of attributes to be returned
Expand Down
11 changes: 6 additions & 5 deletions winremote/modules/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
This module implements work with services via WMI.
"""

# session is dynamically loaded
session = None


def list(attributes='Name,State,StartMode'):
def list(session, attributes='Name,State,StartMode'):
"""
Description: return list of all services on windows machine
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param attributes: comma delimited name of attributes to be returned
:type attributes: str
:returns: list of services info
Expand All @@ -18,10 +17,12 @@ def list(attributes='Name,State,StartMode'):
return session._wmi.query('select %s from Win32_service' % attributes)


def get(name, attributes='Name,State,StartMode'):
def get(session, name, attributes='Name,State,StartMode'):
"""
Description: return information about service @name
:param session: instance of Windows, which hold session to win machine
:type session: winremote.Windows
:param name: name of service to be searched
:type name: str
:param attributes: comma delimited name of attributes to be returned
Expand Down
9 changes: 0 additions & 9 deletions winremote/winremote.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ def __init__(self, session, wmi):
self._session = session
self._wmi = wmi

def __getattr__(self, name):
mod = getattr(
__import__('modules', globals(), locals(), [name], -1),
name,
)
setattr(mod, 'session', self)
setattr(self, name, mod)
return mod

def run_cmd(self, cmd, params=[]):
"""
Run command on windows.
Expand Down

0 comments on commit 6273019

Please sign in to comment.