How to use machine-generated variables in cookiecutter - cookiecutter

Is there a way to machine-generate some values, after the user has supplied some their values for the variables in cookiecutter.json?
The reason I ask is that:
one of the values I need to prompt for is rather hard for users to work out
but it's really easy for me to write some Python code to generate the right value
So I'd really like to be able to remove the user prompt, and calculate the value instead.
Things I've tried:
Searched online for an example pre_gen_project.py file to show how to do it
Read the cookiecutter Advanced Usage page
I'm using cookiecutter on the command line:
cookiecutter path_to_template
Am I missing any tricks?

I needed this exact capability just a few days ago. The solution I came up with was to write a wrapper script for cookiecutter, similar to what is mentioned in:
http://cookiecutter.readthedocs.io/en/latest/advanced_usage.html#calling-cookiecutter-functions-from-python
My script generates a random string for use in a Django project. I called my script cut-cut:
#! /usr/bin/env python
from cookiecutter.main import cookiecutter
import os
rstring = ''.join([c for c in os.urandom(1024)
if c.isalnum()])[:64]
cookiecutter(
'django-template', # path/url to cookiecutter template
extra_context={'secret': rstring},
)
So now I simply run cut-cut and step through the process as normal. The only difference is that the entry named secret in my cookiecutter.json file is prepopulated with the generated value in rstring from the script, provided via the extra_context passed.
You could modify the script to accept the template via the command line, but in my usage I always use the same template, thus I simply pass a hard coded value "django-template" as noted in the code above.

Related

Changing cisco ios description with python - excel input

What I'm going to do is writing an script with python to take an excel file as an input and then read the number and description of interfaces of a switch which is written in there , and then ssh to a cisco switch and change the description with the values added before in excel .
could any body give me a hint?
Try checking netmiko module. I was able to do something close to what you require using netmiko but now I use ansible ios_command which is a lot more easier for a non programmer network engineer.
Start with Paramiko or Netmiko , Netmiko is a bit better version. I would also just rethink about the actual project where instead of thinking about one switch think about all of them and see if you have some universal thing which you need to do in all of your switches instead of one.
For this project you could do below.
1 . save date in CSV
2 . Open CSV file
3. Create a dictionary and Save interface name as key , and description as values
4. Create a list where you can save all your keys --> l = d.keys()
4. SSH to the sw via paramiko/Netmiko .
5. Run a loop in the list l
on each iteration send below commands
interface l[i]
description d[l[i]]
this will translate to below
interface eth1/1
description d['eth1/1'] ( d['eth1/1'] will be value/description of whatever you are gonna get from CSV)
If you really try to learn python then its a good start however if you are on a time crunch Ansible is easier option

Attempting to append all content into file, last iteration is the only one filling text document

I'm trying to Create a file and append all the content being calculated into that file, but when I run the script the very last iteration is written inside the file and nothing else.
My code is on pastebin, it's too long, and I feel like you would have to see exactly how the iteration is happening.
Try to summarize it, Go through an array of model numbers, if the model number matches call the function that calculates that MAC_ADDRESS, when done calculating store all the content inside a the file.
I have tried two possible routes and both have failed, giving the same result. There is no error in the code (it runs) but it just doesn't store the content into the file properly there should be 97 different APs and it's storing only 1.
The difference between the first and second attempt,
1 attempt) I open/create file in the beginning of the script and close at the very end.
2 attempt) I open/create file and close per-iteration.
First Attempt:
https://pastebin.com/jCpLGMCK
#Beginning of code
File = open("All_Possibilities.txt", "a+")
#End of code
File.close()
Second Attempt:
https://pastebin.com/cVrXQaAT
#Per function
File = open("All_Possibilities.txt", "a+")
#per function
File.close()
If I'm not suppose to reference other websites, please let me know and I'll just paste the code in his post.
Rather than close(), please use with:
with open('All_Possibilities.txt', 'a') as file_out:
file_out.write('some text\n')
The documentation explains that you don't need + to append writes to a file.
You may want to add some debugging console print() statements, or use a debugger like pdb, to verify that the write() statement actually ran, and that the variable you were writing actually contained the text you thought it did.
You have several loops that could be a one-liner using readlines().
Please do this:
$ pip install flake8
$ flake8 *.py
That is, please run the flake8 lint utility against your source code,
and follow the advice that it offers you.
In particular, it would be much better to name your identifier file than to name it File.
The initial capital letter means something to humans reading your code -- it is
used when naming classes, rather than local variables. Good luck!

How to run a string from an input file as python code?

I am creating something along the likes of a text adventure game. I have a .yaml file that is my input. This file looks something like this
node_type:
action
title:
Do some stuff
info:
This does some stuff and things
script:
'print("hello world")
print(ret_val)
foo.bar(True)
ret_val = (foo.bar() == True)
if (thing):
print(thing)
print(ret_val)
'
My end goal is to have my python program run the script portion of the yaml file exactly as if it had been copy pasted into the main code. (I know there are about ten bazillion security reasons I should not be running user input like this, but I am the only one writing these nodes, and the only one using this program so I'm mostly just ignoring this fact...)
Currently my attempt goes like this: I load my yaml file as a dict using pyyaml
node = yaml.safe_load(file.yaml)
Then I'm trying to use exec to run my code and hitting a lot of problems, I can't run if statements, I simply get a syntax error, and I can't get any sort of return value from my code. I've tried this as a work around:
def main()
ret_val = "test";
thing = exec(node['script'], globals(),locals())
print(ret_val)
which when run with the above .yaml file prints
>> hello world
>> test
>> True
>> test
for some reason not actually modifying any of my main variables even though I fed them to exec.
Is there any way for me to work around these issues or is there an all together better way to be doing this?
One way of doing this would be to parse the code out and save it to a .py file, from which it can be imported dynamically, for example by importlib.
You might want to encapsulate parsed code into a function, which you can then easily call to invoke your action. Also, it would make sense to specify some default imports there.

Using nohup to help run a loop of python code while disconnecting from ssh

I'm looking for help running a python script that takes some time to run.
It is a long running process that takes about 2hours per test observation. For example, these observations could be the 50 states of the usa.
I dont want to baby sit this process all day - I'd like to kick it off then drive home from work - or have it run while I'm sleeping.
Since this a loop - I would need to call one python script that loops through my code going over each of the 50 states - and a 2nd that runs my actual code that does things.
I've heard of NOHUP, but I have very limited knowledge. I saw nohup ipython mypython.py but then when I google I get alot of other people chiming in with other methods and so I don't know what is the ideal approach for someone like me. Additionally, I am essentially looking to run this as a loop - so don't know how that complicates things.
Please give me something simple and easier to understand. I don't know linux all that well or I wouldn't be asking as this seems like a common sort of command/activity...
Basic example of my code:
Two files: code_file.py and loop_file.py
Code_file.py does all the work. Loop file just passes in the list of things to run the stuff for.
code_file.py
output = each_state + ' needs some help!'
print output
loop_file.py
states = ['AL','CO','CA','NY','VA','TX']
for each_state in states:
code_file.py
Regarding the loop - I have also heard that I can't pass in parameters or something via nohup? I can fix this part within my python code....for example reading from a CSV in my code and deleting the current record from that CSV file and then re-writing it out...that way I can always select the top record in the CSV file for my loop (the list of states)
May be you could modify your loop_file.py like this:
import os
states = ['AL','CO','CA','NY','VA','TX']
for each_state in states:
os.system("python /dir_of_your_code/code_file.py")
Then in a shell, you could run the loop_file.py with:
nohup python loop_file.py & # & is not necessary, it just redirect all output of the file to a file named nohup.out instead of printing it on screen.

Throws error when passing argument with space in JAVA_OPTS in Linux

I am passing command line parameters to gatling script.
This works and executes my test in Windows operating system:
set JAVA_OPTS="-DuserCount=2 -DflowRepeatCount=3 -DdefinitionId=102168 -DtestServerUrl=https://someURL -DenvAuthenticationHeaderFromPostman="Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE="
It works and takes input which is passed
**********************INPUT*************************************
User Count ====>> 2
Repeat Count ====>> 3
Definition ID ====>> 102168
Environment URL ====>> https://someURL
Authentication Header ====>> Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE=
***********************************************************
I want to do this same thing on Linux System.
While if I use this command in Linux then it throws error or takes Null or Binary values as input
(Passing arguments with ./gatling.sh)
JAVA_OPTS="-DuserCount=2 -DflowRepeatCount=3 -DdefinitionId=102168 -DtestServerUrl='https://someURL' -DenvAuthenticationHeaderFromPostman='Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE='" ./gatling.sh
Gives this error,
GATLING_HOME is set to /opt/gatling-charts-highcharts-2.0.3 Error:
Could not find or load main class
UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE='
Here the problem is the space given in argument of -DenvAuthenticationHeaderFromPostman='Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbm='.
What is the solution?
The problem is that the $JAVA_OPTS variable is probably not surrounded by quotes. See this question: Passing a space-separated System Property via a shell script doesn't work
The gatling guys clearly forgot to do that.
I would file a bug and/or just edit gatling.sh.
Ideally though you might just want to consider seeing if Gatling takes a properties file or some other way to configure.

Resources