Thursday, June 22, 2023

FastAPI and Swagger2

 I recently started working on a web project with Python backend. The hosting was on Google Cloud. 

 We built the POC version of the Python backend using the most popular Python3 framework FastAPI. We deployed it on Cloud Run (public) with Docker image and everything worked as expected. 

 We then moved on to making the Cloud Run private and adding an API gateway infront of it. We saved the http://localhost:8000/openapi.json and ran the gcloud cli to create an API gateway and it failed!

 Looking at the documentation https://cloud.google.com/api-gateway/docs/openapi-overview we realized, Google, for some reason, still only supports OpenAPI 2.0 (Swagger 2.0). Working on AWS for quite some time, I had assumed OpenAPI 3 would be supported.

 We tried figuring out a way to configure FastAPI to generate Swagger 2 spec. But FastAPI only supports OpenAPI 3.x. Many have suggested downloading the openapi.json, passing it through some convertor and then some manual intervention to convert it to Swagger 2. 

 Since I wanted to use CI/CD, I didn't want to deal with manual conversion every time developers made some change. So, I referred the FastAPI OpenAPI related code and came up with Package version

Requirements

Python 3.8+
FastAPI 0.79.0+

Installation

$ pip install fastapi_swagger2

Example

from typing import Union
from fastapi import FastAPI
from fastapi_swagger2 import FastAPISwagger2

app = FastAPI()
FastAPISwagger2(app)


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}


This adds following endpoints:
http://localhost:8000/swagger2.json
http://localhost:8000/swagger2/docs
http://localhost:8000/swagger2/redoc


Generate spec for CI/CD

import os

import yaml

from app.main import app

URL = os.environ["CLOUD_RUN_URL"]

app.servers.append(URL)

spec = app.swagger2()
spec['x-google-backend'] = {'address': URL}

print(yaml.dump(spec))


Monday, April 12, 2021

Run GUI applications inside Multipass (Ubuntu) on macOS

Multipass is a nice docker like vm platform from Ubuntu. It can configure the vm with cloud-init so you can prototype your cloud launches locally for free. 

E.g. Launch an instance (by default you get the current Ubuntu LTS)

$ multipass launch --name foo

You can read more about multipass in the docs.

Primarly it does seem like just another alternative to docker. But then I found one nice little difference, not a resource hog unlike docker and you don't run as root.

Currently to run a GUI application which you just want to try without having to clean up the system, most common approach is create a vm with VirtualBox, VMware Fusion or Parallels. But it can be a time consuming task as it installs everything from scratch.

Getting GUI applications to work on docker is possible configuration. But then, atleast on Mac, Docker is a resource hog.

With multipass I was able to get IntelliJ IDEA with following:

Prerequisites:

  • Install Multipass
  • Install XQuartz. Also configure XAuthLocation.
    echo "XAuthLocation /opt/X11/bin/xauth" >> ~/.ssh/config

Launch a vm with current LTS ubuntu with 4 cores CPU and 4 GB memory

$ multipass launch -n idea -c 4 -m 4G

$ multipass shell idea

$ sudo apt update

$ sudo apt install -y libxrender-dev libxtst6 libxi6 fontconfig

$ cd /tmp

$ curl -L -O https://download.jetbrains.com/idea/ideaIC-2021.1.tar.gz

$ sudo tar -xzf ideaIC-2021.1.tar.gz -C /opt

$ echo 'PATH=/opt/idea-IC-211.6693.111/bin:$PATH' >> ~/.bashrc

$ sudo apt install -y openjdk-11-jdk

You could automate the above with cloud-init. (Currently as of this writing multipass only supports yaml configs)

Now time for the magic part (on the host):

multipass shell internally is just doing a SSH

$ sudo cp '/var/root/Library/Application Support/multipassd/ssh-keys/id_rsa' ~/.ssh/multipass_id_rsa

Save this script to your PATH

$ cat ~/bin/multipass_x_ssh

 

#!/bin/bash

IP=$(multipass info $1 | grep IPv4 | awk '{ print $2 }')

ssh -X -i ~/.ssh/multipass_id_rsa ubuntu@$IP

Now when I want to run idea, all I have to do is:

$ multipass_x_ssh idea

$ idea.sh

Saturday, February 27, 2021

pyenv install error on MacOS Big Sur - sendfile invalid

There seems to be some issue going on with pyenv and MacOS Big Sur.


./Modules/posixmodule.c:8320:15: error: implicit declaration of function 'sendfile' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

        ret = sendfile(in, out, offset, &sbytes, &sf, flags);


After referring multiple posts, this is what worked for me.

brew install zlib bzip2

CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install 3.5.3 --patch < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

Wednesday, February 10, 2021

Vmware Fusion 12 - Mac OS Big Sur - Could not connect Network

Was unable to get the network to work after a restart.

| vmx| I005: VNET: 'ethernet0' enable link state propagation, lsp.state = 5

| vmx| I005: DictionaryLoad: Cannot open file "/Library/Preferences/VMware Fusion/config": No such file or directory.

| vmx| I005: VNET: MACVNetMacosGetRealAdapterType: network type for adapter 0: 8

| vmx| I005: VNET: MACVNetMacosGetVnetProperties: vnet properties: vnet=vmnet8, nat=yes, dhcp=yes (ignored), subnet=172.16.82.0, mask=255.255.255.0, firstAddr=172.16.82.1, lastAddr=172.16.82.127, isIPv6=no, IPv6Prefix=fd15:4ba5:5a2b:1008::, IPv6PrefixLen=64

| vmx| I005: VNET: MACVNetPortVirtApiStartInterface: Waiting on semaphore for adapter: 0

| host-1216256| I005: VNET: MACVNetPortVirtApiStartHandler: starting interface for adapter: 0, status: 1009

| host-1216256| W003: VNET: MACVNetPortVirtApiStartHandler: unable to create virtual intrface for device: 0, status: 1009

| vmx| I005: VNET: Semaphore signalled for adapter: 0, timeoutMs=5000, waitMs=3

| vmx| W003: VNET: MACVNetPortVirtApiStartInterface: Failed to create interface for adapter: 0, handlerStatus: 1009

| vmx| I005: VNET: MACVNetPort_Connect: Ethernet0: can't start virtual interface

| vmx| I005: Msg_Post: Error

| vmx| I005: [msg.vnet.connectvnet] Could not connect 'Ethernet0' to virtual network '/dev/vmnet8'. More information can be found in the vmware.log file.

| vmx| I005: [msg.device.badconnect] Failed to connect virtual device 'Ethernet0'.

| vmx| I005: ----------------------------------------


Here's what worked for me:

  • Click on "Apple" logo in menu bar
  • Click "System Preferences"
  • Click "Sharing"
  • Disable "Internet Sharing"
  • Restart the VM.

 


Saturday, June 20, 2020

MacOS pyenv python 3.8 tkinter install


brew install pyenv

brew install tcl-tk

echo 'export PATH="$(brew --prefix tcl-tk)/bin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L$(brew --prefix tcl-tk)/lib"
export CPPFLAGS="-I$(brew --prefix tcl-tk)/include"
export PKG_CONFIG_PATH="$(brew --prefix tcl-tk)/lib/pkgconfig"


PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I$(brew --prefix tcl-tk)/include' --with-tcltk-libs='-L$(brew --prefix tcl-tk)/lib -ltcl8.6 -ltk8.6'" CFLAGS="-I$(brew --prefix tcl-tk)/include" pyenv install 3.8.3

Monday, September 10, 2018

Duply backup on MacOS

Download duplicity from http://duplicity.nongnu.org/

untar

cd duplicity-0.7.18

mkvirtualenv duply_backup

brew install librsync
brew install gpg
pip install fasteners

python setup install

Download duply from https://sourceforge.net/projects/ftplicity/files/duply%20%28simple%20duplicity%29/

untar

cd duply_2.1

workon duply_backup

./duply -V
  duply version 2.1
  (http://duply.net)

  Using installed duplicity version 0.7.18, python 2.7.14 (/Users/vkanwade/.virtualenvs/duply_backup/bin/python2), gpg 2.2.10 (Home: /Users/vkanwade/.gnupg), awk 'awk version 20070501', grep 'grep (BSD grep) 2.5.1-FreeBSD', bash '3.2.57(1)-release (x86_64-apple-darwin17)'.


export DUPL_PYTHON_BIN=~/.virtualenvs/duply_backup/bin/python
export PATH="$PATH:/opt/duply_2.1/"

duply myprofile create
vim ~/.duply/myprofile/conf

#GPG_KEY='_KEY_ID_'
GPG_PW='<some random pwd generated with pwgen. store it safe, required for restore>'

TARGET='s3://< s3_prefix>< s3_region >.amazonaws.com/< s3_bucket >/duply/myprofile'
# s3_prefix: eu-central-1 's3.', else 's3-'

export AWS_ACCESS_KEY_ID='< aws_s3_backup_id >'
export AWS_SECRET_ACCESS_KEY='< aws_s3_backup_secret >'
DUPL_PARAMS="$DUPL_PARAMS --allow-source-mismatch --s3-use-new-style --s3-european-buckets "

SOURCE='/'

MAX_AGE=1M

MAX_FULLBKP_AGE=1M
DUPL_PARAMS="$DUPL_PARAMS --full-if-older-than $MAX_FULLBKP_AGE "

ARCH_DIR=~/duply-cache

NOTE: just wanted to dump the info. formatting will come.

Tuesday, October 25, 2016

MacOS - Operation not permitted - Can't upgrade package 'six'

For most of the packages, python-dateutil is the one which causes all the issue. So just upgrade it an all other installations should be fine.

sudo -H pip install --upgrade python-dateutil --ignore-installed six

Tuesday, September 22, 2015

Bash - Get output after matching line

$ cat file.txt
line 1
line 2
this is the line
line 4
line 5

If you want lines starting from "this is the line"

$ cat file.txt | awk '/this is the line/{ f=1; } f'
this is the line
line 4
line 5

When the line is found, set f to 1.
The last 'f' is shortcut to 'f == 1 { print }'

If you want output after the matching line

$ cat file.txt | awk '/this is the line/{ f=1; next; } f'
line 4
line 5

Saturday, August 01, 2015

Get celery init.d scripts

Downloads init.d scripts (celeryd and celerybeat) for your installed version.
Also accepts APP_NAME as an argument. Saves the files as celeryd-<app_name> and celerybeat-<app_name>


Install Redis inside virtualenv

Just run this script.
Puts the redis.conf in ${VIRTUAL_ENV}/etc/redis.conf.
Adds a wrapper redis-server-ve to use this conf by default.

Defaults to stable release. Else pass the version as an argument.


Monday, October 27, 2014

Packer vmware-iso on Ubuntu 14.04 with VMware Player 6

VMware build can be done with packer on Ubuntu using VMware Player 6.

Download VMware Player 6 from:
https://my.vmware.com/web/vmware/free#desktop_end_user_computing/vmware_player/6_0|PLAYER-603|product_downloads

Also, click on Driver & Tools and download the correct version of VIX.

e.g.
$ wget https://download3.vmware.com/software/player/file/VMware-Player-6.0.3-1895310.x86_64.bundle
$ chmod +x VMware-Player-6.0.3-1895310.x86_64.bundle
$ sudo ./VMware-Player-6.0.3-1895310.x86_64.bundle

$ wget https://download3.vmware.com/software/player/file/VMware-VIX-1.13.3-1895310.x86_64.bundle
$ chmod +x VMware-VIX-1.13.3-1895310.x86_64.bundle
$ sudo ./VMware-VIX-1.13.3-1895310.x86_64.bundle

$ sudo apt-get install qemu-utils

$ packer build vmware.json

If you see errors like vmware or vmrun not in $PATH, VIX was not installed properly.

If you see errors like "specified version not found", incorrect version of VIX was installed.

If you see qemu related errors, qemu-utils was not installed.

vmware-iso only outputs the vmware instance as .vmx along with other files.


To export to ovf/ova format
$ ovftool appliance.vmx appliance.ovf
$ ovftool appliance.vmx appliance.ova

Thursday, July 18, 2013

VMware Fusion - Permissions on Shared Folders on Ubuntu

VMware Fusion provides a nice functionality of sharing folder from host (Mac OSX) to guest (Ubuntu). But then, it seems they are more focused on windows guests, because they screwup on file permissions of shared folders.

"ls -al /mnt/hgfs" will show 501 dialout as user and group, which is sure to cause permission issues on linux guest.

Update: Found a more persistent alternative
1. sudo vim /etc/vmware-tools/services.sh
2. Search for 'vmhgfs_mnt="/mnt/hgfs"'. After this line add: 'vmuser=${VMWARE_MNT_USER:-root}'
3. Then search for 'vmware_exec_selinux "mount -t vmhgfs .host:/ $vmhgfs_mnt"' and replace it with following section:
   uid=`id --user $vmuser`
   gid=`id --group $vmuser`
   vmware_exec_selinux "mount -t vmhgfs .host:/ $vmhgfs_mnt -o uid=$uid,gid=$gid"
4. sudo vim /etc/init/vmware-tools.conf
Before the "pre-start" and "post-stop" lines add:
env VMWARE_MNT_USER=[The guest user you want]
5. sudo reboot

NOTE: This will have to be redone when you update/ reinstall vmware-tools

To fix this:
ssh to your ubuntu.
run command "id". Make note of uid and gid.
"sudo vim /etc/mtab"
look for line ".host:/ /mnt/hgfs vmhgfs rw,ttl=1 0 0"
Duplicate the line and change it to ".host:/ /mnt/hgfs vmhgfs rw,ttl=1,uid=1000,gid=1000 0 0"
"sudo umount /mnt/hgfs"
"sudo mount /mnt/hgfs"

Now do a "ls -al /mnt/hgfs" to verify if correct username and group are shown.

Ideally, vmware fusion should be making this as transparent as possible. But then again, they don't care because they still don't have a real competition. Once, we do some real competitors, maybe then might start worrying about such minor but key items.

Currently, I spend more time trying to get it working than actually working on a VM. I am desperately looking for a better alternative.

This seems like a pretty neat fix. On the sharing screen, VMWare could add a simple textbox for user name and next time vmware tools are installed, use this value while generating the vmware-tools.conf file.

Not sure when VMWare will take note of such minor issues.

Tuesday, May 07, 2013

error in setup.cfg: command 'nosetests' has no such option 'with_pylons'

When running nosetests, if you see error in setup.cfg: command 'nosetests' has no such option 'with_pylons' or Error reading config file 'setup.cfg': no such option 'with-pylons', it could be because you have Pylons==1.0* installed. Pylons==1.0* does not register itself as a nose plugin (https://github.com/Pylons/pylons/issues/13). Use following work arounds:
  1. For "python setup.py nosetests":
    Add the following to your projects "setup.py -> entry_points" and ".egg-info/entry_points.txt"
        [nose.plugins]
        pylons = pylons.test:PylonsPlugin
    
  2. For "nosetests --version":
    sudo vim `which nosetests`
    
    Make it look something like:
    #!/usr/bin/python
    # EASY-INSTALL-ENTRY-SCRIPT: 'nose==1.3.0','console_scripts','nosetests'
    __requires__ = 'nose==1.3.0'
    import sys
    from pkg_resources import load_entry_point
    
    addplugins = []
    
    try:
        import pylons
        if pylons.__version__[0] == '1':
            from pylons.test import PylonsPlugin
            addplugins.append(PylonsPlugin())
    except ImportError:
        pass
    
    if __name__ == '__main__':
        sys.exit(
            load_entry_point('nose==1.3.0', 'console_scripts', 'nosetests')(addplugins=addplugins)
        )
    
    now you should be able to run it as nosetests --version

Friday, April 19, 2013

Working with multiple Python projects

While working with multiple Python projects, you might soon realize you need different versions of the same Py package or packages are conflicting between multiple projects.
VirtualEnv to your rescue.
But once you go through the docs, install and start using virtualenv, you might soon realize it is a little cumbersome.
  1. You need to provide the complete path where you want to save your virtualenv environment folder. virtualenv proj1 will just create the env in your current directory. If you are using a long path to store your envs, it might be a pain to put it in every-time.
  2. To activate an env, source <path-to-envs>/proj1/bin/activate
  3. To find all envs, you have to ls <path-to-envs>. But this works only if you have all yours envs in a single place.
Don't worry, VirtualEnvWrapper is here to help you.
Now you have a wrapper which provides you some shortcuts to the virtualenv commands. But it doesn't end there. VirtualEnvWrapper provides you with the an extension system.
ls $WORKON_HOME and you will find files starting with "post" and "pre". These are simple shell scripts. You can add your commands to these scripts and they will be everytime you run one of the commands.
It doesn't end there. You can also have separate extensions for each of your env. ls $WORKON_HOME/proj1/bin/ should show some similar files starting with "post" and "pre". For eg. To go to your source folder when you activate your env, just add "cd to "postactivate"
All said and done, one thing that might trouble you is figure out what packages are available in your current env?
Yolk will prove useful here. Remember, you can write extensions in wrapper. Just add pip install yolk to "$WORKON_HOME/postmkvirtualenv" and yolk will be installed on all new virtual envs created automatically. You can then activate your env and just use yolk -l to list packages available in that env.
Not much time for formatting. Will do it later.

Wednesday, April 03, 2013

Install CyanogenMod 10.1 (JellyBean Android 4.2.2) on Samsung S2 Skyrocket

Before you begin:

  • You NEED a windows machine (atleast as of now) for odin.
  • Download using Chrome/ Firefox to avoid issues with tar/ md5 downloads.
  • Read all steps before you start anything.

5 simple steps:

  1. Download CyanogenMod 10.1 and gapps, copy it on the internal external SD card
  2. Download and run odin on Windows machine
  3. Download and install TWRP using Odin
  4. Boot in recovery and install the ROM using TWRP
  5. Install gapps using TWRP

Lets get going:

  1. CyanogenMod 10.1:
    1. Go to http://get.cm/?device=skyrocket
    2. Select the type of build
      • Stable - Stable. Well tested. But will not have latest features.
      • Nightly - Nightly builds. Latest features added but unstable.
      • M snapshot (Experimental) - Similar to nightly. You could call them monthly builds. Somewhere between stable and nightly.
    3. Copy the file onto the phone using USB transfer.
  2. GApps
    1. Go to http://goo.im/gapps
    2. Download the gapps which match your CyanogenMod version.
    3. Copy the file onto the phone using USB transfer.
  3. TWRP (Similar to clockworkmod [CWM])
    1. Download TWRP from http://d-h.st/Xo2
  4. Odin
    1. Download odin from http://forum.xda-developers.com/attachment.php?attachmentid=925569&d=1330549534.
    2. Extract the zip.
    3. Run the exe.
    4. Check "auto-boot". Uncheck all other options
    5. Click on "PDA" button. Select TWRP tar file.
    6. HOLD YOUR HORSES HERE
  5. Process - Install TWRP
    1. Put the phone into download mode. (Download mode is different from recovery mode.)
    2. Connect USB cable to computer but not the phone.
    3. Shutdown and remove the battery of the phone
    4. Wait for sometime and reinsert the battery.
      • AT&T - Hold both volume buttons.
      • Rogers - Hold only volume down button.
      (Do not release the buttons until instructed to do so. Do not touch the power button.)
    5. Insert the usb cable into the phone. The phone will boot up. When you see a download mode page with a little yellow triangle at bottom, release the volume button(s).
    6. Now press the volume up button as per the instructions on the screen to continue to download mode.
    7. Odin should have recognized your phone now. You should see a green box with com port listed.
    8. Click start in Odin.
    9. When odin is finished flashing, you should see a green box with Pass written right above the com port green box.
  6. Process - Install CyanogenMod 10.1
    1. Put the phone in recovery mode:
    2. Turn off phone and unplug USB.
    3. Hold down both volume keys.
    4. Hold power button till you feel the phone vibrate. (Release only power button, not the volume keys when the phone vibrates.)
    5. When the screen goes black, release volume keys.
    6. You should now be in recovery mode.
    7. It is a good idea to backup here.
    8. Click on install button.
    9. Select the cyanogenmod zip file from internal/ external sd card and follow on screen instructions to complete the install.
    10. Click Home.
    11. Click Wipe.
    12. Click on Cache, Dalvik Cache and Factory Reset.
    13. Click on back button.
    14. Click on Reboot -> System.

Since we reset cache, the first boot could take 5-10 mins. But if it is taking longer, go to recovery mode and try other options in Wipe menu of TWRP. Then reboot.

On first boot, you might panic to see none of the Google related apps available. Not even Google Play. Thats where the gapps we downloaded come into picture.

  1. Install GApps
    1. Go to recovery mode.
    2. Click Install and select the gapps file. Complete the install.
    3. Reboot.

You should be good to go.

References

Need some more time to work on the post formatting. But instructions are pretty much covered.

Tuesday, December 25, 2012

MimeTypes for Google Docs and Drive

Google Docs: application/vnd.google-apps.document
Google Presentations: application/vnd.google-apps.presentation
Google Spreadsheets: application/vnd.google-apps.spreadsheet
Google Drawing: application/vnd.google-apps.drawing

Few more of Google Docs and Google Drive MIME types:
application/vnd.google-apps.audio
application/vnd.google-apps.document
application/vnd.google-apps.drawing
application/vnd.google-apps.file
application/vnd.google-apps.folder
application/vnd.google-apps.form
application/vnd.google-apps.fusiontable
application/vnd.google-apps.kix
application/vnd.google-apps.photo
application/vnd.google-apps.presentation
application/vnd.google-apps.script
application/vnd.google-apps.sites
application/vnd.google-apps.spreadsheet
application/vnd.google-apps.unknown
application/vnd.google-apps.video

Wednesday, October 03, 2012

Extract compressed files easily on Linux

Tired of remembering the unzip, tar command line options? Just add this to your ~/.bashrc.

extract () {
   if [ -f $1 ] ; then
       case $1 in
 *.tar.bz2) tar xvjf $1 && cd $(basename "$1" .tar.bz2) ;;
 *.tar.gz) tar xvzf $1 && cd $(basename "$1" .tar.gz) ;;
 *.tar.xz) tar Jxvf $1 && cd $(basename "$1" .tar.xz) ;;
 *.bz2)  bunzip2 $1 && cd $(basename "$1" /bz2) ;;
 *.rar)  unrar x $1 && cd $(basename "$1" .rar) ;;
 *.gz)  gunzip $1 && cd $(basename "$1" .gz) ;;
 *.tar)  tar xvf $1 && cd $(basename "$1" .tar) ;;
 *.tbz2)  tar xvjf $1 && cd $(basename "$1" .tbz2) ;;
 *.tgz)  tar xvzf $1 && cd $(basename "$1" .tgz) ;;
 *.zip)  unzip $1 && cd $(basename "$1" .zip) ;;
 *.Z)  uncompress $1 && cd $(basename "$1" .Z) ;;
 *.7z)  7z x $1 && cd $(basename "$1" .7z) ;;
 *)  echo "don't know how to extract '$1'..." ;;
       esac
   else
       echo "'$1' is not a valid file!"
   fi
}
And then all you do is $ extract filename.tar.bz2

Monday, November 28, 2011

No module named PIL - after buildout

If you are using buildout to install PIL and if you get this error, check once in the eggs directory. If there is a PIL egg directory, check its contents. If there are files directly, that is the reason for the error. You can manually move all those contents to PIL subdirectory or add the following buildout part to your buildout script

# ============================================================================================== #
# PIL Hack #
# When buildout install PIL egg, it keeps all files under the PIL-1.1.7-py2.6-X-X-universal.egg. #
# We need to put all the files under a subdirectory PIL inside this folder to make it work. #
# ============================================================================================== #
[PIL-hack]
recipe=iw.recipe.cmd:py
on_install=true
on_update=true
cmds=
>>> import os
>>> while True:
>>> lst = []
>>> for p in os.listdir('${buildout:eggs-directory}'):
>>> if p.startswith('PIL-${versions:PIL}') and p.endswith('.egg'):
>>> lst.append(p)
>>> if len(lst) > 1:
>>> print '\033[91m' + 'I am confused. Multiple PIL eggs found. %s.' % lst + '\033[0m'
>>> raw_input('Delete the one which is not required and then press Enter to continue...')
>>> elif len(lst) == 0:
>>> print '\033[91m' + 'No PIL egg found in eggs directory' + '\033[0m'
>>> raw_input('Press Enter to continue...')
>>> break
>>> elif len(lst) == 1:
>>> path = os.path.join('${buildout:eggs-directory}', lst[0])
>>> path2 = os.path.join(path, 'PIL')
>>> if not os.path.exists(path2):
>>> print '\033[92m' + ('Using PIL egg: %s' % lst[0]) + '\033[0m'
>>> lstdir = os.listdir(path)
>>> os.mkdir(path2)
>>> for p in lstdir:
>>> os.rename(os.path.join(path, p), os.path.join(path2, p))
>>> break

Python colors on command line print

While writing a command line script, adding colors might ease understanding the output. To print a line in color just follow this simple technique
print '\033[91m' + 'my error string' + '\033[0m'
You might have guessed it by now. '\033[' is a control char and '0m'/ '91m' define the color to be displayed.
Few examples: 0m is white 91m red 92m green 93m orange 94m blue Hope this helps

Reusing the same Next.js Docker image with runtime CDN assetPrefix

Recently, while investigating a production issue, I needed to deploy the production Docker image locally for debugging purposes. However, I ...