Skip to content

Commit

Permalink
Merge pull request vmware-archive#17 from hartsock/package_refactor
Browse files Browse the repository at this point in the history
Package refactor
  • Loading branch information
hartsock committed Sep 17, 2014
2 parents e2657d4 + 3388f64 commit 08ce094
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 64 deletions.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: python
python:
- "2.6"
- "2.7"
- "pypy"
- "3.3"
- "3.4"

before_install:
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi
- pip install -r requirements.txt
- pip install -r test-requirements.txt

install:
- python setup.py install

script: python setup.py test
File renamed without changes.
2 changes: 1 addition & 1 deletion README.md → README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ utilities for themselves. This project represents a place for everyone to
share those tools with each other.

Tool Incubator
===============
==============

Utilities started here may be graduated to free-standing libraries distributed
independently of this library. A graduated library would be expressed as a
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyvmomi
six>=1.7.3
25 changes: 13 additions & 12 deletions samples/renamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# 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 print_function

"""
A Python script for changing the name of an object. Demonstrates the use
Expand All @@ -24,7 +25,7 @@
import sys

from pyVim import connect
from pyvmomi_tools import extensions


def get_args():
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -81,7 +82,7 @@ def get_args():
entity = si.content.rootFolder.find_by_name(args.name)

if entity is None:
print "A object named %s could not be found" % args.name
print("A object named {0} could not be found", args.name)
exit()

if args.new_name:
Expand All @@ -90,24 +91,24 @@ def get_args():
# just because we want the script to do *something*
new_name = args.name + "0"

print
print "name : %s" % entity.name
print
print " renaming from %s to %s" % (args.name, new_name)
print
print()
print("name : {0}", entity.name)
print()
print(" renaming from {0} to {1}", args.name, new_name)
print()

# rename creates a task...
task = entity.Rename(new_name)

print "task status: "
print("task status: ")

# demonstrate callbacks firing only on task state-transition
task.wait(queued=lambda t: sys.stdout.write("in queue\n"),
running=lambda t: sys.stdout.write("is running\n"),
success=lambda t: sys.stdout.write("success!\n"),
error=lambda t: sys.stdout.write('error!\n'))
print
print()

print
print "rename finished"
print
print()
print("rename finished")
print()
19 changes: 7 additions & 12 deletions samples/renamer_with_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# 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 print_function

"""
A Python script for changing the name of an object. Demonstrates the use
Expand All @@ -24,7 +25,7 @@

from pyVim import connect
from pyvmomi_tools.cli import cursor
from pyvmomi_tools import extensions


def get_args():
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -81,7 +82,7 @@ def get_args():
entity = si.content.rootFolder.find_by_name(args.name)

if entity is None:
print "A object named %s could not be found" % args.name
print("A object named {0} could not be found", args.name)
exit()

if args.new_name:
Expand All @@ -90,22 +91,16 @@ def get_args():
# just because we want the script to do *something*
new_name = args.name + "0"

print
print "name : %s" % entity.name
print
print " renaming from %s to %s" % (args.name, new_name)
print
print("\nname : {0}\n", entity.name)
print("\n renaming from {0} to {1}\n", args.name, new_name)

# rename creates a task...
task = entity.Rename(new_name)

print "task status: "
print("task status: \n")

# demonstrate task state polling
while task.is_alive:
cursor.spinner('renaming')
print

print
print "rename finished"
print
print("\n\n\nrename finished\n")
12 changes: 6 additions & 6 deletions samples/tasks_different_styles.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import print_function

import atexit
import argparse
import sys

from pyVim import connect

from pyVmomi import vim

from pyvmomi_tools import cli
Expand Down Expand Up @@ -39,9 +40,9 @@ def match_name(obj, name):

root_folder = si.content.rootFolder

print "using wait_for_updates"
print("using wait_for_updates")
for vm in root_folder.find_by(match_name, args.name):
print "Found VirtualMachine: %s Name: %s" % (vm, vm.name)
print("Found VirtualMachine: {0} Name: {1}", vm, vm.name)

if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
task = vm.PowerOff()
Expand All @@ -54,9 +55,9 @@ def match_name(obj, name):
task = vm.PowerOff()
task.wait(success=lambda t: sys.stdout.write("\rpower off\n"))

print "using task callback extensions"
print("using task callback extensions")
for vm in root_folder.find_by(match_name, args.name):
print "Found VirtualMachine: %s Name: %s" % (vm, vm.name)
print("Found VirtualMachine: {0} Name: {1}", vm, vm.name)

if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
vm.PowerOff().poll(
Expand All @@ -68,4 +69,3 @@ def match_name(obj, name):
periodic=lambda t: sys.stdout.write('\ron\n'),
success=lambda t: vm.PowerOff().poll(
periodic=lambda t: sys.stdout.write("\roff\n")))

41 changes: 23 additions & 18 deletions samples/virtual_machine_power_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# 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 print_function

"""
A Python script for power cycling a virtual machine. Demonstrates the use
Expand All @@ -21,6 +22,7 @@

import atexit
import argparse
from six import PY2
import sys
import textwrap

Expand All @@ -29,6 +31,9 @@

from pyvmomi_tools import cli

if PY2:
input = raw_input


def get_args():
parser = argparse.ArgumentParser()
Expand All @@ -55,38 +60,38 @@ def get_args():
# search the whole inventory tree recursively... a brutish but effective tactic
vm = si.content.rootFolder.find_by_name(args.name)
if not isinstance(vm, vim.VirtualMachine):
print "could not find a virtual machine with the name %s" % args.name
print("could not find a virtual machine with the name {0}", args.name)
sys.exit(-1)

print "Found VirtualMachine: %s Name: %s" % (vm, vm.name)
print("Found VirtualMachine: {0} Name: {1}", vm, vm.name)

if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
# using a dynamic class extension for power_off
# this is a blocking method call and the script will pause
# while the machine powers off.
print "powering off..."
print("powering off...")
vm.power_off()
print "power is off."
print("power is off.")


def answer_question(vm):
print "\n"
print("\n")
choices = vm.runtime.question.choice.choiceInfo
default_option = None
if vm.runtime.question.choice.defaultIndex is not None:
ii = vm.runtime.question.choice.defaultIndex
default_option = choices[ii]
choice = None
while choice not in [o.key for o in choices]:
print "VM power on is paused by this question:\n\n"
print "\n".join(textwrap.wrap(vm.runtime.question.text, 60))
print("VM power on is paused by this question:\n\n")
print("\n".join(textwrap.wrap(vm.runtime.question.text, 60)))
for option in choices:
print "\t %s: %s " % (option.key, option.label)
print("\t {0}: {1} ", option.key, option.label)
if default_option is not None:
print "default (%s): %s\n" % (default_option.label,
default_option.key)
choice = raw_input("\nchoice number: ").strip()
print "..."
print("default ({0}): {1}\n", default_option.label,
default_option.key)
choice = input("\nchoice number: ").strip()
print("...")
return choice


Expand All @@ -95,7 +100,7 @@ def answer_question(vm):
# poll our task repeatedly and also check for any run-time issues. This
# code deals with a common problem, what to do if a VM question pops up
# and how do you handle it in the API?
print "powering on VM %s" % vm.name
print("powering on VM {0}", vm.name)
if vm.runtime.powerState != vim.VirtualMachinePowerState.poweredOn:

# now we get to work... calling the vSphere API generates a task...
Expand Down Expand Up @@ -123,12 +128,12 @@ def handle_question(current_task, virtual_machine):

if task.info.state == vim.TaskInfo.State.error:
# some vSphere errors only come with their class and no other message
print "error type: %s" % task.info.error.__class__.__name__
print "found cause: %s" % task.info.error.faultCause
print("error type: {0}", task.info.error.__class__.__name__)
print("found cause: {0}", task.info.error.faultCause)
for fault_msg in task.info.error.faultMessage:
print fault_msg.key
print fault_msg.message
print(fault_msg.key)
print(fault_msg.message)
sys.exit(-1)

print
print(".")
sys.exit(0)
9 changes: 5 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[nosetests]
verbosity=0
detailed-errors=1
with-doctest=1
[bdist_wheel]
universal=1

[bdist_rpm]
requires=python-six,python-requests
43 changes: 32 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,45 @@ def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()


with open('requirements.txt') as f:
required = f.read().splitlines()

with open('test-requirements.txt') as f:
required_for_tests = f.read().splitlines()

setup(name='pyvmomi-tools',
version='5.5.0-2014.dev',
# NOTE: Versions will be 0.1.0, 1.0.0, 1.1.0 and so on
version='DEVELOPMENT',
description='VMware vSphere Python SDK Community Tools',
author='VMware, Inc.',
author_email='[email protected]',
url='https://github.com/vmware/pyvmomi-tools',
packages=['pyvmomi_tools'],
install_requires=['pyvmomi'],
dependency_links=['https://github.com/vmware/pyvmomi'],
install_requires=required,
dependency_links=['https://github.com/vmware/pyvmomi',
'https://github.com/kevin1024/vcrpy'],
license='Apache',
long_description=read('README.md'),
long_description=read('README.rst'),
classifiers=[
"License :: OSI Approved :: Apache Software License",
"Development Status :: 1 - Planning",
"Environment :: No Input/Output (Daemon)",
"Intended Audience :: Information Technology",
"Intended Audience :: System Administrators",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: System :: Distributed Computing"
'Development Status :: 2 - Pre-Alpha',
'License :: OSI Approved :: Apache Software License',
'Intended Audience :: Information Technology',
'Intended Audience :: System Administrators',
'Intended Audience :: Developers',
'Environment :: No Input/Output (Daemon)',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: System :: Distributed Computing',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Operating System :: Unix',
'Operating System :: MacOS',
],
test_suite='tests',
tests_require=required_for_tests,
zip_safe=True)
2 changes: 2 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
testtools>=0.9.34
vcrpy
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added tests/test_task.py
Empty file.
File renamed without changes.

0 comments on commit 08ce094

Please sign in to comment.