john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

subprocess vmrun double quote os command directory list sort filter set git pull listdir

    import os
        # workaround No PID file found
        try:
            pid_path = '/usr/local/var/run/myprogram/'
            onlyfiles = [ f for f in os.listdir(pid_path) if os.path.isfile(os.path.join(pid_path,f)) ]
            for f in onlyfiles:
                os.system("kill -15 $(cat " + f + ")")
        except Exception:
            pass




import sys
import os
import subprocess
import tempfile

SCRIPTNAME = sys.argv[0]
if len( sys.argv ) < 2:
  print "ERROR: wrong number of arguments, correct usage: python " + SCRIPTNAME + " vmname"
  sys.exit( 1 )

VM = sys.argv[1]
STAGEFILE = '/tmp/' + str( os.getpid() ) + '.myip'
HOST = 'vc0.example.com'
DATACENTER = 'datacenter-2'
DATASTORE = 'localtb0'
VMPATH = '[' + DATACENTER + '/' + DATASTORE + '] ' + VM + '/' + VM + '.vmx'   # list2cmdline forces a " if there's a space
COMMAND_BASE = 'vmrun -T vc -h http://' + HOST + '/sdk -u root -p mypassword'


params = COMMAND_BASE + ' list'
cmd = params.split()
print subprocess.list2cmdline( cmd )  # preview the command
p = subprocess.Popen( cmd, shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
out, err = p.communicate()
errcode = p.returncode
print out


params = COMMAND_BASE + ' start'
cmd = params.split()
cmd.append( VMPATH )
print subprocess.list2cmdline( cmd )  # preview the command
p = subprocess.Popen( cmd, shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
out, err = p.communicate()
errcode = p.returncode
print out


params= COMMAND_BASE + ' -gu admin -gp mypassword runScriptInGuest '
cmd = params.split()
cmd.append( VMPATH )
cmd.append( '/bin/sh' )
cmd.append( 'echo 1 > /tmp/1.txt' )
print subprocess.list2cmdline( cmd )  # preview the command
result = subprocess.check_call( cmd )  # if non zero return code raise CalledProcessError
print result


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# usage: python git_pull_all.py , assumes you are in the ~/workspace (repo) root directory

import os

def getDirectoryListing( directory ):
    directory_list = list()
    for element in os.listdir(  directory ):
        if os.path.isdir( element ):
            if( element[0] != '.' ):    # do not add hidden directories
                directory_list.append( element )
    return directory_list


def executeCommand( command , argument , directory_list ):
    search_root = os.getcwd()
    for element in directory_list:
        target = os.path.join( search_root , element )
        print target
        os.chdir( target )
        os.system( command + " " + argument )
        os.chdir( search_root )
        print ""


# main #######
exclusion_set = set( ['notarepo' , 'not-a-repo' ] )

print ""
search_root = os.getcwd()
directory_list = getDirectoryListing( search_root )     # intermediate variables for readability, not performance =]
directory_set = set( directory_list )
directory_set_filtered = directory_set - exclusion_set
directory_list_sorted_filtered = sorted( directory_set_filtered  )

executeCommand( "git" , "pull" , directory_list_sorted_filtered )

  • « configparser postfix config main stringio workaround
  • elasticsearch »

Published

Mar 5, 2014

Category

python

~280 words

Tags

  • command 29
  • directory 13
  • double 2
  • filter 6
  • git 8
  • list 23
  • listdir 1
  • os 7
  • pull 1
  • python 180
  • quote 1
  • set 5
  • sort 11
  • subprocess 2
  • vmrun 3