-2

There are several commands. Each of these commands performs certain actions. I want to combine all these commands into a single script file. After running this script, I want to get a .zip file at the output in the specified directory. What is the best way to create this script to work on Linux and macOS operating systems? Which is better to use .sh, Makefile, or something else?

For example, the pip install cassandra-driver command installs a third-party package in my virtual environment? I can install this third-party package in a specific directory on my operating system using the --target command. Is it possible to immediately wrap this downloadable package in a .zip file? Plus you need to add a file to this .zip file config.py file.

macOS:

mkdir /src/dependencies/cassandra-driver
pip install cassandra-driver --targer /src/dependencies/cassandra-driver
zip -r cassandra-driver.zip /src/dependencies/cassandra-driver /src/config.py
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193
  • It's not clear... what are the expected contents of this zip file? For pip, instead of install, you can just [download the package without installing](https://stackoverflow.com/q/7300321/2745495) or use `wget` (or similar) to download it direct from PyPi. As for "best way", that would lead to opinion-based answers, which are off-topic here. Are you asking for a cross-platform shell script? Something that will run on both Linux and mac? What's wrong with those 3 lines you have now? It seems straightforward to put those 3 lines in a script, but then you mentioned Makefile, and it got confusing. – Gino Mempin Oct 06 '20 at 10:14
  • ...or are you asking for a script that will work for any Python package? How about if the environment has no `pip` available? – Gino Mempin Oct 06 '20 at 10:15
  • In short, I need this to archive packages for `AWS Lambda Layers`. I want to understand how to optimize commands. To be more precise, combine the second and third commands. As I asked in the post, is it possible to immediately wrap this downloadable package in a `.zip` file? I don't want to store this third-party package. Also, my third command raises such error: `zip error: Nothing to do! (try: zip -r cassandra-driver.zip . -i /src/config.py)`. I tried to use this command in the error message but it returns an empty `.zip` file. Do you have any ideas? `. – Nurzhan Nogerbek Oct 06 '20 at 11:05

1 Answers1

1

Just create a bash script and have it executed on *nix.

Use arguments to specify what package should be zipped to what location and what config should be added.

#!/bin/bash

packageName=$1
destinationPath=$2
configLocation=$3

mkdir /tmp/$packageName
pip download $packageName -d /tmp/$packageName
zip -r $destinationPath/$packageName.zip /tmp/$packageName/* $configLocation
rm -rf /tmp/$packageName

Use with:

bash /path/scriptName.sh cassandra-driver /src/dependencies/ /src/config.py

with scriptName.sh being this script,

cassandra-driver the name of the package,

/src/dependencies/ the path where you want to store the .zip,

/src/config.py full path of file you want to append to the archive.

EDIT: It now downloads the package as .zip without installing it.

x3l51
  • 693
  • 4
  • 19
  • Is it possible to combine the second and third lines of your script? After archiving, I don't need a third-party package. You will have to write an additional command to delete it. Isn't it? – Nurzhan Nogerbek Oct 06 '20 at 10:21
  • You want to delete the downloaded package, because you now have the zip? – x3l51 Oct 06 '20 at 10:30
  • Yes, you are right. As I asked in the post, is it possible to immediately wrap this downloadable package in a `.zip` file? I don't want to store this third-party package. – Nurzhan Nogerbek Oct 06 '20 at 10:32
  • Can I specify the output directory where to put the zip file in the third line of the script? – Nurzhan Nogerbek Oct 06 '20 at 10:39
  • See my edit. It now doesn't install the package but downloads a .zip right away. – x3l51 Oct 06 '20 at 10:40
  • Thank you for your answer. I tried to test your new code but it raises an error: `no such option: --target`. Also, the script raises such error: `zip error: Nothing to do! (try: zip -r cassandra-driver.zip . -i /src/config.py)`. I tried to use this command in the error message but it returns an empty `.zip` file. Do you have any ideas? – Nurzhan Nogerbek Oct 06 '20 at 10:52
  • Yes, `--target` doesn't work for `download`. I edited the answer again. – x3l51 Oct 06 '20 at 10:57
  • Have you tested your script? It downloads a third-party package to the specified directory but does not archive it or delete it after downloading. What could be the reason for the error that I described to you in the previous comment? The third command doesn't work if you run it through a script. Very strange. – Nurzhan Nogerbek Oct 06 '20 at 11:13
  • Try again. If it doesn't work for you we can discuss it via chat. – x3l51 Oct 06 '20 at 11:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222589/discussion-between-nurzhan-nogerbek-and-anonjnr). – Nurzhan Nogerbek Oct 06 '20 at 11:43