Tag Archives: python

Finding the Versions of All Java Jars in a Directory

Tasked with converting a project to Maven, I needed to find out the version of the dependencies, as not all of them had the version in the name.

jars-without-versions-edited

Looking Up a Single Jar

If it’s a single jar you want to look up, it’s quite easy.  Simply take the sha1 hash of the file, head over to the Maven Central Advanced Search, and put the hash in the “SHA-1 Checksum” field.

maven-central-search-results

Looking Up Many Jars

The above becomes very tedious very quick if you have dozens of jars to lookup.  A whole directory of mysterious dependencies.

Maven Central has a search API that I quickly took advantage of to automate the above process in Python.  I created a script that will, for every file in a given path, hash the file and search for its version.

You simply run it as >jar_version.py <path>

jar-versions-found

The Script

Feel free to use the script.

import sys
import urllib2
import json
import os


def sha1_of_file(file_path):
    import hashlib
    with open(file_path, 'rb') as f:
        return hashlib.sha1(f.read()).hexdigest()


def search_maven_central(sha1_hash):
    maven_hash_url = "http://search.maven.org/solrsearch/select?q=1:%22" + sha1_hash + "%22&rows=20&wt=json"
    response = urllib2.urlopen(maven_hash_url)
    return response.read()


def pull_result_version_from_results(maven_central_response):
    results_parsed = json.loads(maven_central_response)
    if results_parsed["response"]["numFound"] == 0:
        return "Not Found"
    else:
        specific_results = results_parsed["response"]["docs"][0]
        return specific_results["g"] + " " + specific_results["a"] + " v" + specific_results["v"]


def process_artifact(artifact_path):
    sha1_hash = sha1_of_file(artifact_path)
    html = search_maven_central(sha1_hash)
    result_value = pull_result_version_from_results(html)
    print artifact_path + " -> " + result_value


def main():
    path = sys.argv[1]
    if os.path.isfile(path):
        process_artifact(path)
    else:
        for root, _, files in os.walk(path):
            for f in files:
                full_path = os.path.join(root, f)
                process_artifact(full_path)


main()

 

Monitoring an Outlook Inbox with Python

I get a lot of email at work.  Mostly by robots that are triggering on some event or another.  But after a few years of more and more distribution lists having me on it, the amount of email I get in a day is actually unmanageable.  I’m talking thousands.  The vast majority can be safely ignored, such as “Job ABC completely successfully”.

So I’ve gotten pretty aggressive with Outlook rules in order to filter my incoming email into different folders.  But the rules have always felt limited to me.  For example, I cannot say “If the subject contains this or this, do that“.  I’m limited to a logical and.

Looking into whether or not you can use Python in your Outlook rules, I came across an example where you can actively monitor your Inbox and have access to the messages that come in, all from Python.

C:meDevelopmentpythonoutlook>outlook-monitor.py
Subj: Test Email
Body: Hey!

Test

-Matthew Urch | Programmer Extraordinaire
========

This opens up a whole world of email automation.

The basic script I made that achieves the above is this:

import win32com.client
import pythoncom

class Handler_Class(object):
    def OnNewMailEx(self, receivedItemsIDs):
        for ID in receivedItemsIDs.split(","):
            # Microsoft.Office.Interop.Outlook _MailItem properties:
            # https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._mailitem_properties.aspx
            mailItem = outlook.Session.GetItemFromID(ID)
            print "Subj: " + mailItem.Subject
            print "Body: " + mailItem.Body.encode( 'ascii', 'ignore' )
            print "========"
        
outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class)
pythoncom.PumpMessages()

And then from here, you can do whatever you like with your email messages that are coming in.