Access TeamCity Environment variables with python script - python

I am using TeamCity to test my code. I am using command line from TC and I am running a python test suite that tests the code.
I defined an environment variable (named "Server") and gave it a value ("production") and I want to pass it in a way that my python script will be able to access it and save it's value in the code.
I tried looking in TC documentation as well in Python's documentation and I couldn't find it.
I will appreciate your help.

Probably you should use os.environ
assert os.environ['Server'] == 'production'

Related

Modify a python script that runs on terminal with command line input to work in an IDE

I am playing around with a python software-like code that I downloaded. I need to extract some parts of the code and so I am currently tracing it to understand it better. It would make my life much easier to trace the code in an IDE (I use spyder) so that I can put breakpoints and visually inspect the variables. I therefore want to modify the code but I don't know how to proceed.
This is the directory structure of the code. The main directory is "advance" and 2 of its subdirectories are "advance" and "examples". The code was installed using pip and to run the code, one must go to the examples directory, enter a sub-directory which represents a test case, and simply type "advance ." in the terminal (the "." represents pwd).
After some tracing, I found out that doing this calls the file driver.py with address advance/advance/driver.py. The driver.py code has the structure below:
# import stuff here
def main():
parser = argparse.ArgumentParser(description="Read working directory")
# some stuff here
if __name__ == "__main__":
try:
main()
except:
print(traceback.format_exc())
print("Execution failed")
As you can see, the main function takes an input which is the working directory.
My question now is: How can I modify this so that I can run this code in my IDE? Or better yet, can I write a script that calls the main function in driver.py and give it the path of the directory for the example test case I want to run? If so, how can I do it?
Thanks and apologies if it sounds too simple. I am coming from Matlab and transitioning to python.
I found in this reddit post that spyder offers to pass command line options, when running a script. See the following setting:
Run -> Configure -> Command line options
Unfortunately, I am not familiar with Spyder, so that's what I can tell so far. I guess it works similar to PyCharm (which I use). There it is like this: You adapt your run configuration and can set the parameters you want to pass a script. Afterwards, when running that run configuration PyCharm will always pass those particular parameters to the script. This allows to use the IDE's debugger to examine the code. I would bet that Spyder behaves almost the same way. Good luck trying the suggested settings :)

Better python unittest integration?

I'm using GNU Emacs 24.5.1 to work on Python code. I often want to run just a single unit test. I can do this, for example, by running:
test=spi.test_views.IndexViewTest.generate_select2_data_with_embedded_spaces make test
with M-X compile. My life would be simpler if I could give some command like "Run the test where point is", and have emacs figure out the full name of the test for me. Is possible?
Update: with the folowing buffer, I'd like some command which runs M-X compile with:
test=spi.test_views.IndexViewTest.test_unknown_button make test
where spi is the name of the directory test_views.py is in. Well, technically, I need to construct the python path to my test function, but in practice, it'll be <directory.file.class.function>.
This seems like the kind of thing somebody would have already invented, but I don't see anything in the python mode docs.
I believe you use the "default" python mode, while the so-called elpy mode (that I strongly recommend giving a try when doing Python developments within Emacs) seems to provide what you are looking for:
C-c C-t (elpy-test)
Start a test run. This uses the currently configured test runner to discover
and run tests. If point is inside a test case, the test runner will run exactly
that test case. Otherwise, or if a prefix argument is given, it will run all tests.
Extra details
The elpy-test function internally relies on the function (elpy-test-at-point), which appears to be very close to the feature you mentioned in the question.
See e.g. the code/help excerpt in the following screenshot:

How to pass argument to pytest test in eclipse

I'm using Eclipse IDE for a project which uses pytest to write test cases. I have configured the pyunit environment to use "py.test runner" instead of "PyDev test runner", however, I'm not able to pass any arguments for pytest.
So this is the sample command I run in terminal -
pytest --server-version=5.0.0-3516 --sync-gateway-version=1.5.0-557 --sync-gateway-config-file resources/sync_gateway_configs/sync_gateway_default_cc.json --mode=cc testsuites/syncgateway/functional/tests/
Thanks for the help
So I figure out the way to pass the argument and select the test case one wants to run. So here is the snapshot of how to pass the argument.
To run a specific case use ctl + f9 and then select the case of that file. Now to debug it select the case using shift key.
Hope this helps others

Execute python script on Jenkins with variables

I have created a job in Jenkins, for which user provides the value of a specific parameter, let's say PYTHON_PARM, as an input. On this job I execute a python script (using Python Plugin). The problem is that I want to use as a variable the user input $PYTHON_PARM parameter. This is not considered as an environment variable, so when trying to use os.environ['PYTHON_PARM'], this doesn't work.
Any idea?
Thanks,
Solution:
from os import environ
Type = environ['Type']

MongoDB: how to get db.stats() from API

I'm trying to get results of db.stats() mongo shell command in my python code (for monitoring purposes).
But unlike for example serverStatus I can't do db.command('stats'). I was not able to find any API equivalent in mongodb docs. I've also tried variations with db.$cmd but none of that worked.
So,
Small question: how can I get results of db.stats() (number of connections/objects, size of data & indexes, etc) in my python code?
Bigger question: can anyone explain why some of shell commands are easily accessible from API, while others are not? It's very annoying: some admin-related tools are accessible via db.$cmd.sys, some via db.command, some via ...? Is there some standard or explanation of this situation?
PS: mongodb 2.0.2, pymongo 2.1.0, python 2.7
The Javascript shell's stats command helper actually invokes a command named dbstats, which you can run from PyMongo using the Database.command method. The easiest way to find out what command a shell helper will run is to invoke the shell helper without parentheses -- this will print out the Javascript code it runs:
> db.stats
function (scale) {
return this.runCommand({dbstats:1, scale:scale});
}
As for why some commands have helpers and others do not, it's largely a question of preference, time, and perceived frequency of use by the driver authors. You can run any command by name with Database.command, which is just a convenience wrapper around db.$cmd.find_one. You can find a full list of commands at List of Database Commands. You can also submit a patch against PyMongo to add a helper method for commands you find that you need to invoke frequently but aren't supported by PyMongo yet.

Categories