OneRaynyDay OneRaynyDay - 2 months ago 7
Python Question

Using Makefile bash to save the contents of a python file

For those who are curious as to why I'm doing this: I need specific files in a tar ball - no more, no less. I have to write unit tests for

make check
, but since I'm constrained to having "no more" files, I have to write the check within the
make check
. In this way, I have to write bash(but I don't want to).

I dislike using bash for unit testing(sorry to all those who like bash. I just dislike it so much that I would rather go with an extremely hacky approach than to write many lines of bash code), so I wrote a python file. I later learned that I have to use bash because of some unknown strict rule. I figured that there was a way to cache the entire content of the python file into a single string in the bash file, so I could take the string literal in bash and write to a python file and then execute it.

I tried the following attempt (in the following script and result, I used another python file that's not unit_test.py, so don't worry if it doesn't actually look like a unit test):

toStr.py:



import re

with open("unit_test.py", 'r+') as f:
s = f.read()

s = s.replace("\n", "\\n")
print(s)




And then I piped the results out using:

python toStr.py > temp.txt


It looked something like:

#!/usr/bin/env python\n\nimport os\nimport sys\n\n#create number of bytes as specified in the args:\nif len(sys.argv) != 3:\n print("We need a correct number of args : 2 [NUM_BYTES][FILE_NAME].")\n exit(1)\nn = -1\ntry:\n n = int(sys.argv[1])\nexcept:\n print("Error casting number : " + sys.argv[1])\n exit(1)\n\nrand_string = os.urandom(n)\n\nwith open(sys.argv[2], 'wb+') as f:\n f.write(rand_string)\n f.flush()\n f.close()\n\n


I tried taking this as a string literal and echoing it into a new file and see whether I could run it as a python file but it failed.

echo '{insert that giant string above here}' > new_unit_test.py


I wanted to take this statement above and copy it into my "bash unit test" file so I can just execute the python file within the bash script.

The resulting file looked exactly like {insert giant string here}. What am I doing wrong in my attempt? Are there other, much easier ways where I can hold a python file as a string literal in a bash script?

Answer

the easiest way is to only use double-quotes in your python code, then, in your bash script, wrap all of your python code in one pair of single-quotes, e.g.,

#!/bin/bash

python -c 'import os
import sys

#create number of bytes as specified in the args:
if len(sys.argv) != 3:
    print("We need a correct number of args : 2 [NUM_BYTES][FILE_NAME].")
    exit(1)

n = -1
try:
    n = int(sys.argv[1])
except:
    print("Error casting number : " + sys.argv[1])
    exit(1)

rand_string = os.urandom(n)

# i changed ""s to ''s below -webb
with open(sys.argv[2], "wb+") as f:
    f.write(rand_string)
    f.flush()
    f.close()'
Comments