<commit_before>#****************************************************************************
#  Copyright (C) 2005-2010 <NAME> <<EMAIL>> and
#                          <NAME> <<EMAIL>> and
#                          <NAME> <<EMAIL>
#  Distributed under the terms of the BSD License.  
#****************************************************************************

# PyXG-0.3.0

"""PyXG provides a python interface to Apple's Xgrid.  

Xgrid is Apple's software for building and managing clusters of 
Macintosh computers for use in high performance computation.  
See http://www.apple.com/server/macosx/technology/xgrid.html for more details.

This module wraps the xgrid command line in Mac OS X. It will not work with
the Technonogy Previews of Xgrid. The command line is wrapped in this module 
as the goal is to provide an interface to Xgrid that can be used from an
interactive python prompt. The Cocoa API for Xgrid (XgridFoundation) is 
based on an event-loop paradigm and is less well suited for interactive work.
If you want to use Xgrid and python from within a Cocoa application, you should
use XgridFoundation and PyObjC.

Features
========

    1.  Use Xgrid from within python scripts as well as in interactive python
        sessions.
    2.  Create, submit and manage simple (one task) and batch (many task) Xgrid
        jobs using python's elegant syntax.
    3.  Work with multiple Xgrid controllers simultaneouly.
    4.  List available grids for each controller and query their status.

Quick Start
===========

Import xg, create a Connection and Controller object:

>>> from xg import *
>>> conn = Connection(hostname='xgrid.work.com',password='<PASSWORD>')
>>> cont = Controller(conn)

List the grids managed by the controller:

>>> cont.gridIDs()
(0, 3)
>>> cont.grids()
[<Grid with gridID = 0>, <Grid with gridID = 3>]

Work with the default grid, listing active jobs:

>>> g = cont.grid(0)
>>> g.jobIDs()
(229, 230, 231, 232)
>>> g.printJobs()
##################################################
id   Date Started             Status     CPU Power 
##################################################
229  2005-12-22 11:18:47 -0800 Finished   0         
230  2005-12-22 11:18:50 -0800 Finished   0         
231  2005-12-22 11:18:52 -0800 Finished   0         
232  2005-12-22 11:18:55 -0800 Finished   0         

Get a job from the default grid and work with it:

>>> j = g.job(229)      
>>> j.printInfo()
{
    name:  /usr/bin/cal
    jobStatus:  Finished
    taskCount:  1
    undoneTaskCount:  0
    percentDone:  100
}
>>> j.printInfo(verbose=False)
229  2005-12-22 11:18:47 -0800 Finished   0   
>>> j.printSpecification()
{
    applicationIdentifier :  com.apple.xgrid.cli
    taskSpecifications :  {0 = {arguments = (); command = "/usr/bin/cal"; }; }
    name :  /usr/bin/cal
    inputFiles :  {}
    submissionIdentifier :  abc
}

Get job results:

>>> j.results(stdout="job229.out",stderr="job229.err")
Job stdout saved in file: job229.out 

Use a Grid object to submit a single task job:

>>> j = g.submit(cmd='/usr/bin/cal')
Job submitted with id:  234
>>> j.printInfo(verbose=False)
234  2005-12-22 13:09:52 -0800 Finished   0      
"""

#####################################################################
#   Imports                                                         #
##################################################################### 

#import string
#import os, sys
import time
import commands
import re
import itertools
import tempfile
import os.path
from functools import wraps

try:
    import Foundation
    import objc
except ImportError, e:
    print "This module requires PyObjC."
    raise e
    
#####################################################################
#   Exceptions                                                      #
#####################################################################  

class XgridError:
    """Xgrid exception class."""
    def __init__(self, err):
        self.err = err
        
    def __repr__(self):
        return "Xgrid Error: %s" % (self.err)
    
class InvalidIdentifier(XgridError):
    """Xgrid exception for invalid job or grid identifiers."""
    def __init__(self, id):
        self.id = id
    def __repr__(self):
        return "Invalid Xgrid Identifier: " + str(self.id)

class InvalidGridIdentifier(InvalidIdentifier):
    """Invalid grid identifier exception."""
    def __repr__(self):
        return "Invalid Grid Identifier: " + str(self.id)
        
class InvalidJobIdentifier(InvalidIdentifier):
    """Invalid job identifier exception."""
    def __repr__(self):
        return "Invalid Job Identifier: " + str(self.id)
        
class InvalidAction(XgridError):
    """Invalid action exception."""
    def __init__(self, action):
        self.action = action
    def __repr__(self):
        return "Invalid Xgrid Action: " + str(self.action)

class InvalidIdentifierType:
    """Invalid job or grid identifier type."""
    def __init__(self, bad_var):
        self.bad_var = bad_var
    def __repr__(self):
        return "Invalid Xgrid Identifier Type: " + str(self.bad_var)

# Setting this flag causes printing of every Xgrid command that is executed
PYXGRID_DEBUG = False
VERSION = '0.3.0'

#####################################################################
#   See if there is an Xgrid cluster defined by environment vars    #
#####################################################################  

defaultXgridHostname = os.environ.get('XGRID_CONTROLLER_HOSTNAME')
defaultXgridPassword = os.environ.get('XGRID_CONTROLLER_PASSWORD')

if not defaultXgridPassword:
    defaultXgridPassword = ''

#####################################################################
#       Some utilities for running and parsing Xgrid commands       #
##################################################################### 

def autorelease(func):
    """A decorator to properly release ObjC object instances.
    
    Anytime an ObjC object instance is created, an NSAutoReleasePool needs to
    be available. PyObjC will create one but it won't get drained very often.
    
    @param func: The function to be decorated.
    """
    
    @wraps(func)
    def wrapped(*args, **kw):
        pool = Foundation.NSAutoreleasePool.alloc().init()<commit_msg>fix imports...<commit_after>#****************************************************************************
#  Copyright (C) 2005-2010 <NAME> <<EMAIL>> and
#                          <NAME> <<EMAIL>> and
#                          <NAME> <<EMAIL>
#  Distributed under the terms of the BSD License.  
#****************************************************************************

# PyXG-0.3.0

"""PyXG provides a python interface to Apple's Xgrid.  

Xgrid is Apple's software for building and managing clusters of 
Macintosh computers for use in high performance computation.  
See http://www.apple.com/server/macosx/technology/xgrid.html for more details.

This module wraps the xgrid command line in Mac OS X. It will not work with
the Technonogy Previews of Xgrid. The command line is wrapped in this module 
as the goal is to provide an interface to Xgrid that can be used from an
interactive python prompt. The Cocoa API for Xgrid (XgridFoundation) is 
based on an event-loop paradigm and is less well suited for interactive work.
If you want to use Xgrid and python from within a Cocoa application, you should
use XgridFoundation and PyObjC.

Features
========

    1.  Use Xgrid from within python scripts as well as in interactive python
        sessions.
    2.  Create, submit and manage simple (one task) and batch (many task) Xgrid
        jobs using python's elegant syntax.
    3.  Work with multiple Xgrid controllers simultaneouly.
    4.  List available grids for each controller and query their status.

Quick Start
===========

Import xg, create a Connection and Controller object:

>>> from xg import *
>>> conn = Connection(hostname='xgrid.work.com',password='<PASSWORD>')
>>> cont = Controller(conn)

List the grids managed by the controller:

>>> cont.gridIDs()
(0, 3)
>>> cont.grids()
[<Grid with gridID = 0>, <Grid with gridID = 3>]

Work with the default grid, listing active jobs:

>>> g = cont.grid(0)
>>> g.jobIDs()
(229, 230, 231, 232)
>>> g.printJobs()
##################################################
id   Date Started             Status     CPU Power 
##################################################
229  2005-12-22 11:18:47 -0800 Finished   0         
230  2005-12-22 11:18:50 -0800 Finished   0         
231  2005-12-22 11:18:52 -0800 Finished   0         
232  2005-12-22 11:18:55 -0800 Finished   0         

Get a job from the default grid and work with it:

>>> j = g.job(229)      
>>> j.printInfo()
{
    name:  /usr/bin/cal
    jobStatus:  Finished
    taskCount:  1
    undoneTaskCount:  0
    percentDone:  100
}
>>> j.printInfo(verbose=False)
229  2005-12-22 11:18:47 -0800 Finished   0   
>>> j.printSpecification()
{
    applicationIdentifier :  com.apple.xgrid.cli
    taskSpecifications :  {0 = {arguments = (); command = "/usr/bin/cal"; }; }
    name :  /usr/bin/cal
    inputFiles :  {}
    submissionIdentifier :  abc
}

Get job results:

>>> j.results(stdout="job229.out",stderr="job229.err")
Job stdout saved in file: job229.out 

Use a Grid object to submit a single task job:

>>> j = g.submit(cmd='/usr/bin/cal')
Job submitted with id:  234
>>> j.printInfo(verbose=False)
234  2005-12-22 13:09:52 -0800 Finished   0      
"""

#####################################################################
#   Imports                                                         #
##################################################################### 

#import string
#import os, sys
import time
import commands
import re
import itertools
import tempfile
import os.path
from functools import wraps

try:
    import Foundation
    import objc
except ImportError, e:
    print "This module requires PyObjC."
    raise e
    
#####################################################################
#   Exceptions                                                      #
#####################################################################  

class XgridError:
    """Xgrid exception class."""
    def __init__(self, err):
        self.err = err
        
    def __repr__(self):
        return "Xgrid Error: %s" % (self.err)
    
class InvalidIdentifier(XgridError):
    """Xgrid exception for invalid job or grid identifiers."""
    def __init__(self, id):
        self.id = id
    def __repr__(self):
        return "Invalid Xgrid Identifier: " + str(self.id)

class InvalidGridIdentifier(InvalidIdentifier):
    """Invalid grid identifier exception."""
    def __repr__(self):
        return "Invalid Grid Identifier: " + str(self.id)
        
class InvalidJobIdentifier(InvalidIdentifier):
    """Invalid job identifier exception."""
    def __repr__(self):
        return "Invalid Job Identifier: " + str(self.id)
        
class InvalidAction(XgridError):
    """Invalid action exception."""
    def __init__(self, action):
        self.action = action
    def __repr__(self):
        return "Invalid Xgrid Action: " + str(self.action)

class InvalidIdentifierType:
    """Invalid job or grid identifier type."""
    def __init__(self, bad_var):
        self.bad_var = bad_var
    def __repr__(self):
        return "Invalid Xgrid Identifier Type: " + str(self.bad_var)

# Setting this flag causes printing of every Xgrid command that is executed
PYXGRID_DEBUG = False
VERSION = '0.3.0'

#####################################################################
#   See if there is an Xgrid cluster defined by environment vars    #
#####################################################################  

defaultXgridHostname = os.environ.get('XGRID_CONTROLLER_HOSTNAME')
defaultXgridPassword = os.environ.get('XGRID_CONTROLLER_PASSWORD')

if not defaultXgridPassword:
    defaultXgridPassword = ''

#####################################################################
#       Some utilities for running and parsing Xgrid commands       #
##################################################################### 

def autorelease(func):
    """A decorator to properly release ObjC object instances.
    
    Anytime an ObjC object instance is created, an NSAutoReleasePool needs to
    be available. PyObjC will create one but it won't get drained very often.
    
    @param func: The function to be decorated.
    """
    
    @wraps(func)
    def wrapped(*args, **kw):
        pool = objc.NSAutoreleasePool.alloc().init()
{'fraction_non_alphanumeric': 0.21398355814510336, 'fraction_numerical': 0.030010375927847395, 'mean_word_length': 3.349184311003124, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '837478', 'n_tokens_mistral': 3636, 'n_tokens_neox': 3321, 'n_words': 1162}