Dec-01-2016, 10:24 PM
I want my Python programs to display a timestamp when they run. It doesn't have to be precise, but it should reflect the date (at least) when the program was declared to be ready for use. It must be set automatically, because if it has to be done manually, a user can never be sure whether it's reliable (whether the developer remembered to set it) or not.
This is easy with a compiled language like C, at least in principle: insert the time into the object code when the program is linked. In Python it's more elusive. The time when when the program is compiled to .pyc files is meaningless, because every user who installs the program "compiles" it the first time they run it.
An obvious solution would be to use the .py files' timestamps; the latest one among the program's source files is the program's timestamp. But this won't work because our source files are distributed through a source control system, and when a user fetches a new version of a file, the local copy is timestamped with the fetch time. (The s.c.s. can be configured to preserve the original timestamps, but for most other types of files the fetch time is right, so we're not likely to change it.)
A secondary problem is that a Python "program" really doesn't exist as a distinct entity. There's no one file we can point to and say, "this file's timestamp is the program's timestamp." The best we can do is look at every script the program executes and take the latest of their dates. But we want to know the program's timestamp at the start of execution, and we can't easily tell what scripts it executes until it's done. To do that we'd have to perform a static analysis of the program's call tree, which sounds far more technically challenging than anything I can justify doing.
Does anyone have thoughts on a good way to solve this problem?
This is easy with a compiled language like C, at least in principle: insert the time into the object code when the program is linked. In Python it's more elusive. The time when when the program is compiled to .pyc files is meaningless, because every user who installs the program "compiles" it the first time they run it.
An obvious solution would be to use the .py files' timestamps; the latest one among the program's source files is the program's timestamp. But this won't work because our source files are distributed through a source control system, and when a user fetches a new version of a file, the local copy is timestamped with the fetch time. (The s.c.s. can be configured to preserve the original timestamps, but for most other types of files the fetch time is right, so we're not likely to change it.)
A secondary problem is that a Python "program" really doesn't exist as a distinct entity. There's no one file we can point to and say, "this file's timestamp is the program's timestamp." The best we can do is look at every script the program executes and take the latest of their dates. But we want to know the program's timestamp at the start of execution, and we can't easily tell what scripts it executes until it's done. To do that we'd have to perform a static analysis of the program's call tree, which sounds far more technically challenging than anything I can justify doing.
Does anyone have thoughts on a good way to solve this problem?