function to find the modification date of the project

J

Joe Strout

This isn't a question, but something I thought others may find useful
(and if somebody can spot any errors with it, I'll be grateful).

We had a case recently where the client was running an older version of
our app, and didn't realize it. In other languages I've avoided this by
displaying the compile date in the About box, but of course Python
doesn't really have a meaningful compile date. So, instead we're now
displaying the latest modification date of any .py file in the project.
Here's the function that finds that:


def lastModDate():
"Get the latest modification date (as a string) of any .py file in
this project."
import os, time
latest = 0
dir = os.path.dirname(__file__)
if dir == '': dir = '.' # HACK, but appears necessary
for fname in os.listdir(dir):
if fname.endswith('.py'):
modtime = os.stat(os.path.join(dir, fname)).st_mtime
if modtime > latest: latest = modtime
out = time.strftime('%Y-%m-%d', time.localtime(latest))
return out


The intent of this code is to find any .py files in the same directory
as the module containing the above code, and return (as a date string in
SQL/ISO format) the latest modification date thereof. Then, this is
displayed to the user in the about box like so:

dlg = wx.MessageDialog(self,
"etown unified database system\nRevision: %s" \
% lastModDate(),
"About etown Central", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()

(That's wxPython, of course.)

I haven't yet tested this in a packaged app or on Windows, but it seems
to work in our OS X test environment.

One odd thing was the need to employ the HACK identified above, where if
__file__ happens to already be in the current directory, then
os.path.dirname of it returns the empty-string -- yet the empty-string
is not a valid argument to os.listdir(). Is there a better way to a
list of files in the same directory as a given file?

Cheers,
- Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top