I'm pretty used to referencing variables inside print blocks in Perl. I'm not at all comfortable with Python. I have a block of code that I want to change the 'onhg' to come out of a config file. So I set up a scratch directory and make a bare bones implementation:
[th199096@jhereg etc]> ls -laiR ~/scratch/
/home/th199096/scratch/:
total 42
749 drwxr-xr-x 3 th199096 staff 4 Oct 6 16:36 .
3 drwxr-xr-x 39 th199096 staff 55 Oct 6 16:35 ..
752 drwxr-xr-x 2 th199096 staff 6 Oct 6 16:42 etc
750 -rwxr-xr-x 1 th199096 staff 1297 Oct 6 16:42 updateoso.py
/home/th199096/scratch/etc:
total 25
752 drwxr-xr-x 2 th199096 staff 6 Oct 6 16:42 .
749 drwxr-xr-x 3 th199096 staff 4 Oct 6 16:36 ..
753 -rw-r--r-- 1 th199096 staff 1052 Oct 6 16:40 __init__.py
754 -rw-r--r-- 1 th199096 staff 243 Oct 6 16:41 __init__.pyc
751 -rwxr-xr-x 1 th199096 staff 94 Oct 6 16:42 config.py
756 -rw-r--r-- 1 th199096 staff 257 Oct 6 16:42 config.pyc
Where the config file simply has:
GATE_USER = "onhg" GATE_GROUP = "gk" OSOREPO = "ssh://hg.opensolaris.org/hg/onnv/onnv-gate"
And the updateoso has:
import os, pwd, subprocess, sys
from mercurial import hg, repo
from mercurial.node import hex
sys.path.insert(1,
os.path.realpath(os.path.join(os.path.dirname(__file__), "..")))
from etc import config
__USAGE = """
updateoso.py [-n] <-R repo root>
-n: dry run, no email sent (displayed on stdout)
-R: root dir of repo (where .hg is)
Attempt to send changes to
%s
This script must be run as user "onhg".
You should set up RBAC and use pfexec(1).
""" % (config.OSOREPO)
__USAGE = __USAGE.strip()
print >> sys.stderr, __USAGE
Well, in isolation, I can already see what I am going to have to do. All I need to do is replace the 'ohng' with a %s and add a second argument:
[th199096@jhereg ~/scratch]> diff updateoso.py updateoso.py.first 39c39 < This script must be run as user "%s". --- > This script must be run as user "ohng". 41c41 < """ % (config.OSOREPO, config.GATE_USER) --- > """ % (config.OSOREPO)
And we get:
[th199096@jhereg ~/scratch]> ./updateoso.py
updateoso.py [-n] <-R repo root>
-n: dry run, no email sent (displayed on stdout)
-R: root dir of repo (where .hg is)
Attempt to send changes to
ssh://hg.opensolaris.org/hg/onnv/onnv-gate
This script must be run as user "onhg".
You should set up RBAC and use pfexec(1).
I ought to be able to test this inside the interactive shell:
[th199096@jhereg ~/scratch]> python
Python 2.4.4 (#1, Aug 25 2008, 03:30:42) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import updateoso
updateoso.py [-n] <-R repo root>
-n: dry run, no email sent (displayed on stdout)
-R: root dir of repo (where .hg is)
Attempt to send changes to
ssh://hg.opensolaris.org/hg/onnv/onnv-gate
This script must be run as user "onhg".
You should set up RBAC and use pfexec(1).
>>> config.GATE_USER = "duke"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'config' is not defined
Okay, I should have known that wasn't going to work. It would probably work in the code (we'll see later), but for now this will work:
>>> updateoso.config.GATE_USER = "duke"
>>> reload(updateoso)
updateoso.py [-n] <-R repo root>
-n: dry run, no email sent (displayed on stdout)
-R: root dir of repo (where .hg is)
Attempt to send changes to
ssh://hg.opensolaris.org/hg/onnv/onnv-gate
This script must be run as user "duke".
You should set up RBAC and use pfexec(1).
<module 'updateoso' from 'updateoso.pyc'>
To be honest, I knew the reference would work, but I expected it to be reset. In retrospect, I can see that I reloaded updateoso and etc/config. Just something to get used to. I could force it to 'reset' via:
>>> reload(etc/config)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'etc' is not defined
>>> from etc reload(config)
File "<stdin>", line 1
from etc reload(config)
^
SyntaxError: invalid syntax
>>> reload(updateoso.config)
<module 'etc.config' from 'etc/config.pyc'>
>>> reload(updateoso)
updateoso.py [-n] <-R repo root>
-n: dry run, no email sent (displayed on stdout)
-R: root dir of repo (where .hg is)
Attempt to send changes to
ssh://hg.opensolaris.org/hg/onnv/onnv-gate
This script must be run as user "onhg".
You should set up RBAC and use pfexec(1).
<module 'updateoso' from 'updateoso.pyc'>
Took me a bit to figure out the syntax.
Okay, can I see the change from the script:
[th199096@jhereg ~/scratch]> diff updateoso.py updateoso.py.second 45,48d44 < < config.GATE_USER = "gark" < print >> sys.stderr, __USAGE <
I don't expect this to work. And it doesn't.
This script must be run as user "onhg". ... This script must be run as user "onhg".
How about a test driver script?
[th199096@jhereg ~/scratch]> more test.py import updateoso print "Now change the user" updateoso.config.GATE_USER = "nark" reload(updateoso)
And that works:
This script must be run as user "onhg". ... Now change the user ... This script must be run as user "nark".