Balasankar C

Balasankar C

Geek. Freedom. Privacy.

Home | Blog | Talks | Setup | Feed

A Gemfile parser in Python

Heyo,
There were times when I thought I should go with both Python and Ruby and thus get a firm grip in both languages. But, it seems, I am no longer motivated to code in Ruby. So, I decided to stick with Python for all my coding purposes. But, there were some stuff that I use, which is already written in Ruby by someone else. So, what to do except porting it to Python?

One such project came with my Debian activities, where I maintain progressbars for the packaging status of Diaspora and GitLab. It involves finding out the dependencies mentioned in Gemfile (or gemspec file), finding out the dependency chains of these level-one dependencies using Rubygems API. Cedric of Debian-Ruby team have already written a program to accomplish these tasks in Ruby, which can be found here. It used a gem called gemnasium-parser which can be used to parse Gemfiles. I decided to write such an implementation in Python.

A Gemfile lists the dependencies of a Ruby application in a specific format. Example of a gemfile can be seen below

source "https://rubygems.org"

gem "rails", "4.2.4"
gem "rspec", group: test

group :development
gem "mini_portile"
gem "fog-atmos"
end

Gems may be under different groups, can have version requirements etc. These all has to be parsed correctly by a parser.

Reading the code of Gemnasium/Parser, I saw that they were using regular expressions to do the task. So, I decided to mimic it and use regular expressions. I wrote regexes to handle different tokens in the Gemfile - gem name, version, autorequire, group etc.

I have been following the PEP8 standards for so long, but this was the first project I decided to try Pylint also. Pylint + PEP8 sure is an awesome combination to make your code clean and professional.

So, after I wrote the parser and did several modifications, I decided to release it to PyPi, which I was doing for the first time. I created an account in PyPi and pushed the library to it.

I was (and still am) to see that the number of downloads gets increased at a fascinating rate (~1000 within few days for a library that has a very specific usecase). Maybe some bug in PyPi.

The code is available in my gitlab repo and the package can be found at the PyPi page .