Python

Control Spotify on Linux like a hacker

2017-03-05 DSP2017, Linux, Open source, Python No comments

Recently, I created a tiny script called spotify-cli, which allows you to control Spotify on Linux from terminal. It’s inspired by shpotify, which is a shell script doing similar things, but on macOS. My script is written in Python and uses dbus under the hood, which allows to communicate with bus daemon to pass messages between applications. I used pactl for controlling the system sound.

You can install spotify-cli as follows via wget:

sh -c "$(wget https://raw.githubusercontent.com/pwittchen/spotify-cli-linux/master/install.sh -O -)"

or via curl:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/pwittchen/spotify-cli-linux/master/install.sh)"

After that, you can just type spotify-cli in your terminal.
You can use spotify-cli with the following parameters:

--help, -h          shows help
--status            shows status (currently played song name and artist)
--play              plays the song
--pause             pauses the song
--playpause         plays or pauses the song (toggles a state)
--next              plays the next song
--previous, --prev  plays the previous song
--volumeup          increases sound volume
--volumedown        decreases sound volume

That’s it! Happy listening!

Source code of the project can be found at https://github.com/pwittchen/spotify-cli-linux.

Automate boring stuff

2017-01-01 Bash, Git, Python No comments

Introduction

In my current company all the people who perform creative work (mostly programmers) need to prepare so-called PKUP report. PKUP stands for Podwyższone Koszty Uzyskania Przychodu in the Polish language. It’s legal regulation in Poland, which allows paying a lower income tax due to the particular type of work. For the regular employee, it means that he or she will simply get a bit higher salary per month.

How the report looks in practice?

As a programmer, I simply create software as a source code. Added, removed and modified lines of code in the existing codebase are treated as my creative work. Luckily, we use Git so I can generate *.diff files from the Git repositories I’m contributing to. Besides that, I need to prepare document as a *.docx file with a short description of my work. My tasks look different every month, but report actually looks almost the same every month. Preparing this report is boring and repeatable stuff.

Let’s automate it!

Generating *.diff files from Git repos

I simply created a shell script, which goes through predefined project directories and saves *.diff files with names the same as project directory with changes performed by me from the 20th day of the last month until now.

Generating *.docx document

Next, I created a python script, which is parametrized and used by shell script. It uses python-docx library for generating *.docx report. I’ve chosen such option, because it’s one of the simplest solutions I’ve found and it’s lightweight. Moreover it can be easily used on Unix systems and integrated with shell scripts.

Personalization

I wanted to make a script available and usable for everyone, so I created .pkup.conf file, which is responsible for personalization and configuration of the script. I think, it looks pretty straightforward.

yDEV_PROJECTS_DIR=$yHYBRIS_SRC
yDEV_PROJECTS_LIST=(backoffice platform-backoffice cockpitng backofficesearch pcm pcmbackoffice cockpit cockpit-core)
yDEV_REPORT_DIR=~/Documents/hybris/pkup/raporty/doc/
yDEV_NAME="Your name"
yDEV_SURNAME="Your surname"
yDEV_ROLE="Software Developer"
yDEV_DEPARTMENT="P&I"
yDEV_MANAGER="Your manager name and surname"

Installation and uninstallation

I also created installation script, which allows to start using the scripts faster. Installation script install dependencies for python script, copies shell script and python script into /usr/local/bin/ directory and .pkup.conf file into home directory. Configuration file needs to be adjusted by the user manually after installtion. Of course, there’s another script, which can be used for uninstallation.

Tests

There are python tests for this solution in pkup_doc_test.py file, but they’re quite poor right now due to the limited amount of time. They can be a subject of improvements in the future. Note that such scripting solutions rarely have tests because they’re small and created ad-hoc. Nevertheless, I wanted to follow the philosophy from my last blog article and create tests for any kind of software I make.

Summary

I’ve spent some time for preparing this stuff, but it was fun and I think it should save me and hopefully my co-workers some amount of time during creating reports every month. In the future, it can be improved by automatic generation of report messages and sending data to the server.

To sum up, preparing reports manually is boring. Generating reports automatically is exciting!

Complete solution described in this article with documentation is available on GitHub: https://github.com/pwittchen/pkup.

3 questions about your Git repository

2015-12-28 Git, Linux, Open source, Python 3 comments

Introduction

Can you answer the following questions about your Git repository?

  1. Does development branch has all changes from master branch?
  2. Is your gitlog a crap?
  3. How old are your branches?

If not, but you want to know answers, you’re lucky, because I prepared 3 simple scripts for you, which can help to find it out.

Does development branch has all changes from master branch?

git-branch-comparator is a python script, which checks if development branch has all changes from master branch in Git repository.

Another, easier way to accomplish the same task suggested in comments by Mike (thanks!) is to call simply:

$ git pull
$ git branch --contains master --no-merged development

When we are working in a Git Flow and critical bug occurs on production, sometimes there is a necessity to create so called hot-fix. We can create separate branch from master branch for this hot-fix and then merge it into master branch or we can commit a change on master branch. Second option is not recommended. After that, we have to remember to merge master branch into a development branch to have our hot-fix in a development version as well and avoid merge conflicts in the future.

This python script checks, if all changes made on master branch were also merged into development branch to keep those two branches consistent. We can add it as a job into Jenkins CI server and monitor branches consistency. In addition, release jobs can depend on that job and we can avoid merge conflicts or project unstability before release.

source code & documentation: https://github.com/pwittchen/git-branch-comparator

Is your gitlog a crap?

craplog is a python script, which checks if git git log of the given project is crappy or not. Right now, script is very simple. It just checks if more than half of the commit messages are good. Commit message is considered as good, when it contains more than two words. Of course, this is not the only condition determining the quality of the commit message, but this is early beta version of the script and can be improved later.

I’ve read a discussion in one of the pull requests to Linux kernel. It made me think about quality of Git commit messages. Of course, Linux kernel is a specific project and has its own standards. Maybe not all of these standards will be valid for a simpler or less complicated projects. Nevertheless, a lot of people don’t pay attention to git commit messages. They put crappy stuff inside them like random letters and numbers or stupid expressions, which has no specific meaning, aren’t related to the project or aren’t informative enough. In my opinion, good git log is one of the factors determining good quality of the project. Sometimes, we need to browse log to find some changes or analyze project history in order to fix a bug or find important information. It’s easier to do it, when git log is good. I’ve made some of the mentioned mistakes in the past, but I try to avoid them now.

source code & documentation: https://github.com/pwittchen/craplog

How old are your branches?

git-aged-branches is a shell script showing git branches of defined repository with age of their last commit. It works on Mac OS X, Linux and can be helpful while investigating old Git branches to delete. This script does not delete anything! It’s just for informational purposes.

source code & documentation: https://github.com/pwittchen/git-aged-branches

Summary

I hope, some of you will find these tools useful and maybe they’ll solve your current problems or help to improve quality of your projects.
If you would like to know more details about mentioned projects, check instructions how to use them and their source code, visit linked repositories on GitHub.

Note: Any feedback, new issues or pull requests are appreciated!

Learning Python

2015-09-01 Python No comments

Some time ago, I’ve decided to learn Python programming language. I thought it may be useful during performing some basic tasks in the system and Linux or Windows scripting. Moreover, it’s good language for developing web applications with frameworks like Django, Flask or Bottle. I’ve decided to go through all of the tasks in the Learn Python – The Hard Way course. Basic tasks weren’t surprising. They were similar to other object oriented languages. In the further tasks we can learn basic features available in Python language, which are not so common for e.g. Java programmers. I’ve placed all tasks in learn-python-the-hard-way repository on GitHub.

Moreover, in README.md file I’ve added information about:

  • Installing Python on Windows and Linux
  • Executing Python scripts from terminal
  • Pip package manager and installing it on Windows and Linux
  • Using Pip
  • Unit Testing
  • Virtualenv
  • Scripts on Linux
  • Style Guide for Python Code
  • Development Environments
  • Popular Python web frameworks
  • Useful Python libraries
  • Additional Resources & links

In addition, to learn this language better, I’ve decided to create a few simple projects:

  • android-resource-converter – scripts converting Android xml resources with translations to csv file and backwards
  • git-branch-comparator – checks if development branch has all changes from master branch in Git repository
  • craplog – verifies whether your git log is a crap or not

What’s interesting first two of them were used in production and second project is running regularly on a CI server.
Other developers also create similar tools and scripts. For example Pidcat is pretty handy and useful tool for Android developers and it’s just a single script written in Python.

To sum up, I can tell that Python is great object oriented language with a clean and simple syntax. With this language we can achieve our goals really fast with a few lines of code and readable solution. I recommend to learn this language to broaden our horizon even if we are programming in totally different technologies on daily basis.

Glances – light, but powerful system monitoring

2015-07-11 Devops, Linux, Python No comments

Recently, during reading information in The Art of Command Line repository, I’ve found Glances, which is a cross-platform curses-based system monitoring tool written in Python. It’s light, but very powerful tool, which allows to monitor your system. Moreover, it’s open-source.

glances

Before you install Glances, you need Python, PyPI and Psutil. If you want to run Glaces as a web server, you also need to install Bottle.
To monitor temperature of your CPU, install Pysensors.

You can install the most important packages with the following command:

$ sudo pip install psutil bottle pysensors

and then install Glances

$ sudo pip install glances

After that, just run glances

$ glances

If you want to run Glances as a web server, type:

$ glances -w

open the following address in your favorite web browser: http://<your_ip>:61208/ and replace <your_ip> with appropriate value.
If you just want to check it locally, you can use this address: http://127.0.0.1:61208/.

Checking consistency of Git branches

2015-05-14 Git, Jenkins, Python No comments

Recently I’ve created a simple Python script, which checks whether ‘development’ branch has all changes from ‘master’ branch in a Git repository. It’s important when we work in a Git Flow. Branch inconsistency may occur when change with hot-fix will be committed to ‘master’ branch and we forget to merge ‘master’ branch back to ‘development’ branch to have our hot-fix in a ‘development’ version of the project as well. We should keep branch consistency to avoid merge conflicts and problems with release of the project in the future. Mentioned script is able to perform necessary validation helping to detect potential problems.

Moreover, script can be integrated with the Jenkins CI server and we can execute it from a command line via Jenkins job. When, changes from ‘master’ won’t be merged into ‘development’, job will fail. In opposite case, job should finish with a success. In a Jenkins job we need to remember to add the following Additional Behaviours in Source Code Management (Git) section:

  • Clean before checkout
  • Wipe out repository and force clone

Usage of this script is quite simple:

$ python compare-branches.py <path_to_your_git_repository>

Script is open-source and can be found at: https://github.com/pwittchen/git-branch-comparator

Android Resource Converter

2015-01-17 Android, Python No comments

In international projects sometimes there’s a need to send resource files to the client in order to have translated strings. Client doesn’t have to understand XML notation and editing two or more files at the same time is inconvenient. It’s easier to send file which can be edited in MS Excel or Libre Office Calc.

I’ve created Python scripts, which are able to convert Android xml resources with translations to a single *.csv file ready to open in software for common users. In addition, there’s another script, which can generate xml resource files with translations from *.csv file.

Generating *.csv file from resources is easy:
$ python xml2csv.py directory_with_resources

Exemplary output looks as follows:

key;strings_english.xml;strings_polish.xml
hello_world;Hello World!;Witaj Świecie!
app_name;My application;Moja aplikacja

Generating resources from file with translations is easy as well:
$ python csv2xml.py translations.csv

As an output we will get *.xml files containing resources for translations in Android application.

Check out repository here: https://github.com/pwittchen/android-resource-converter