[Distutils] Parsing setup()

Phillip J. Eby pje at telecommunity.com
Tue Jun 5 01:22:11 CEST 2007


At 03:58 PM 6/4/2007 -0700, Rob wrote:
>Ian Bicking wrote:
>
> > If you have a built egg (which you can do with python setup.py 
> egg_info, without actually installing
> > anything) you can read the requires.txt file with pkg_resources.
>
>I had tried that but I was having problems with un-installed packages.
>
>I have this so far, which works with *nux, but I'll have to figure out
>how to find the correct Python
>executable in Windows (I think using different Python versions could
>yield different requirements?).
>
>
>pkg_name = "pycopia-net"
>os.chdir(setup_dir)
>os.system("/usr/bin/env python %s egg_info" % setup_file)

If you use "-e sometmpdir" as an option, you can have the .egg-info 
put in a temporary directory with a known location.  You need this 
because some package layouts don't have the .egg-info in the package 
root, so the rest of your stuff won't work.

To be honest, though, I think you're going to way too much trouble 
here.  Just what exactly is the use case here?  Couldn't you just 
easy_install the package to a temporary directory with 
--no-deps?  Why do you think you need to inspect the setup.py in the 
first place?

By the way, your code here is broken even on *nux systems if the user 
runs your tool with a different Python than the system 
one.  sys.executable gives you the full path to the currently-running 
version of Python, irrespective of platform.

(You might also want to consider using the 
setuptools.sandbox.run_setup() function instead of os.system.)


>ws = WorkingSet(setup_dir)
>env = Environment()
>dist = env.best_match(Requirement.parse(pkg_name), ws)
>print dist.get_metadata("requires.txt").split()
>
>['pycopia-process>=0.9', 'pycopia-utils>=0.9']

Just use dist.requires() here, and then you won't be dependent on 
internal details of the format.

In general, I would suggest that trying to work your way around 
setuptools' APIs is a very bad idea.  More often than not, you will 
miss one of the nine jillion supported configurations and thus will 
end up with a tool that only works on your system...  *sometimes*.

Case in point: your code above depends on the .egg-info being in the 
setup directory, which is often not the case.  Zope packages, for 
example, usually have a src/ or lib/ subdirectory that will be where 
the egg-info is.  Other packages may have it in other places, perhaps 
even more deeply nested.

Pretty much *any* place where you are reading a file format directly 
or looking at directory contents yourself, you're probably breaking 
something that setuptools has encapsulated for a Very Good 
Reason.  When in doubt, ask.  :)



More information about the Distutils-SIG mailing list