Ejemplo n.º 1
0
    def run(self, filename):
        """Run processing on a given file, with results
        being written back to same filename."""

        # make a tempdir so I can use a file-like object
        # instead of an OS-level handle (like with mkstemp)		
        tdir = make_tempdir()
        tname = os.path.join(tdir,'process.out')

        #print "TEMPDIR = ",tdir
        #print "TEMPFILE = ",tname
        
        f_out = open(tname,'wb')
        f_in = open(filename,'rb')

        # process in->out
        self.process(f_out, f_in)

        del f_out
        del f_in

        # copy tempfile -> filename

        #print "COPY %s -> %s" % (tname,filename)
        
        # I think this is secure ... since caller owns filename
        # there isn't a race, right? (unlike writing into a tempdir
        # which could have malicious symlinks in it)
        copy2( tname, filename )

        #print "RMDIR %s" % tname
        
        # clean up tempdir
        unlink(tname)
        os.rmdir(tdir)
Ejemplo n.º 2
0
def get_pyver_from_exe( exename ):
    """
    Given a python executable, find out its version.
    Returns version as 3-item list:

        (os.name, sys.platform, version)

    Where version is a 3-element list, e.g. [2,1,3]

    Returns None if can't get version.
    """

    # hack: when running a win32-native Python from a cygwin shell,
    # with cygwin Python installed, you'll see cygwin symlinks to the
    # real cygwin Python, but NTVDM.exe will crash when trying to run
    # the symlinks. Of course, to win32, they aren't links, so I can't
    # just filter them with islink(). Instead, I check if the binary
    # looks too small to be real.
    if os.stat(exename)[ST_SIZE] < 1000:
        return None
        
    # this is required to work on Python 1.5.2
    # note that splitting sys.version doesn't work on .0 releases, so
    # I try sys.version_info first, but it isn't available on 1.5.2.

    # Don't insert any lefthand spaces!
    pycmd = """
import sys, string, os
try: v = sys.version_info[0],sys.version_info[1],sys.version_info[2]
except: v = map(int,string.split(string.split(sys.version)[0],'.'))
open('lineout','w').write('%s %s %s %s %s\\n' % (os.name,sys.platform,v[0],v[1],v[2]))
"""

    # the most portable thing to do is write pycmd to a file, and
    # have pycmd write its results to a file as well. so make
    # a temp directory to run from.
    
    savedir = os.getcwd()

    tempdir = make_tempdir()
    os.chdir(tempdir)

    f = open('test.py','w')
    f.write( pycmd )
    del f # explicit close seems to be needed under win32 (i.e. open().write() fails)

    os.system('%s test.py' % exename)
    if not os.path.isfile('lineout'):
        return None # failed to run
    
    f = open('lineout','r')
    line = f.readline()
    del f # explicitly, for win32

    unlink('lineout')
    unlink('test.py')

    os.chdir(savedir)
    os.rmdir(tempdir)

    p = line.split()
    
    return (p[0], p[1], list(map( int, p[2:] )))
    # create dist (which *includes* MANIFEST just created)
    # (yes, its gross, but required as far as I can tell)
    rmtree('dist')

    os.system('%s setup.py master' % sys.executable)

    sys.exit(0)

if 'master' in sys.argv:
    import gnosis.version  # only as needed!

    myver = gnosis.version.VSTRING

    # -- create archives, multiple formats for user convenience --

    tempdir = make_tempdir()

    excludes = [re.compile('.+\.pyc$')]

    # .zip file
    zipname = os.path.join(tempdir, 'Gnosis_Utils-%s.zip' % myver)
    print "Creating %s ..." % os.path.basename(zipname)
    zip_current_dir(zipname, 'Gnosis_Utils-%s' % myver, excludes)

    # .tar.gz
    targz_name = os.path.join(tempdir, 'Gnosis_Utils-%s.tar.gz' % myver)
    print "Creating %s ..." % os.path.basename(targz_name)
    tar_gz_current_dir(targz_name, 'Gnosis_Utils-%s' % myver, excludes)

    # .tar.bz2
    tarbz2_name = os.path.join(tempdir, 'Gnosis_Utils-%s.tar.bz2' % myver)