Merge lp:~dooferlad/linaro-license-protection/add_do_if_old into lp:~linaro-automation/linaro-license-protection/trunk

Proposed by James Tunnicliffe
Status: Merged
Merged at revision: 35
Proposed branch: lp:~dooferlad/linaro-license-protection/add_do_if_old
Merge into: lp:~linaro-automation/linaro-license-protection/trunk
Diff against target: 66 lines (+62/-0)
1 file modified
scripts/do_if_old.py (+62/-0)
To merge this branch: bzr merge lp:~dooferlad/linaro-license-protection/add_do_if_old
Reviewer Review Type Date Requested Status
Linaro Infrastructure Pending
Review via email: mp+88241@code.launchpad.net

Description of the change

Adding script that runs a command on a directory if it is older than the age specified.

To post a comment you must log in.
Revision history for this message
Deepti B. Kalakeri (deeptik) wrote :

This looks good to me.
I had one suggestion, why don't we provide the option to specify the time trigger_age_string in days as well. It would be little inconvenient to convert the days into hrs if we intend to find information which are slightly very old.

otherwise +1

Thanks!!!
Deepti.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'scripts/do_if_old.py'
2--- scripts/do_if_old.py 1970-01-01 00:00:00 +0000
3+++ scripts/do_if_old.py 2012-01-11 17:43:27 +0000
4@@ -0,0 +1,62 @@
5+#!/usr/bin/env python
6+import argparse
7+import os
8+import re
9+import time
10+import sys
11+
12+def search_and_run(root_dir, command, trigger_age_string):
13+ """Run command on old directories
14+
15+ Search under root_dir. If a directory is found that is older than the
16+ age specified by trigger_age_string, run command on it.
17+
18+ Does not recurse down into subdirectories.
19+ """
20+ trigger_age_seconds = None
21+
22+ age_search = re.search("^(\d+)(\w+)$", trigger_age_string)
23+
24+ if age_search:
25+ if age_search.group(2) == "m":
26+ trigger_age_seconds = int(age_search.group(1)) * 60
27+ elif age_search.group(2) == "h":
28+ trigger_age_seconds = int(age_search.group(1)) * 60 * 60
29+ elif age_search.group(2) == "s":
30+ trigger_age_seconds = int(age_search.group(1))
31+
32+ if not trigger_age_seconds:
33+ print >> sys.stderr, "Time string format unrecognised."
34+ print >> sys.stderr, "Format should be <number><unit>, for example:"
35+ print >> sys.stderr, " 2h = two hours."
36+ print >> sys.stderr, " 30m = 30 minutes."
37+ print >> sys.stderr, " 45s = 45 seconds."
38+ sys.exit(1)
39+
40+ root = os.path.abspath(root_dir)
41+
42+ for thing in os.listdir(root):
43+ if os.path.isdir(os.path.join(root, thing)):
44+ folder = os.path.join(root, thing)
45+ path = os.path.join(root, folder)
46+ mod_time = os.path.getmtime(path)
47+
48+ if (time.time() - mod_time) > trigger_age_seconds:
49+ os.system(command + " " + path)
50+
51+if __name__ == '__main__':
52+ """Run command on directories that are older than trigger-age."""
53+
54+ parser = argparse.ArgumentParser(description="Run command on directories"
55+ "that are older than trigger-age.")
56+
57+ parser.add_argument('--root-dir', type=str, nargs=1, required=True,
58+ help="Root directory to search")
59+ parser.add_argument('--trigger-age', type=str, nargs=1, required=True,
60+ help="Age of directory (or files in it) to trigger command")
61+ parser.add_argument('--command', type=str, nargs=1, required=True,
62+ help="Command to run on directory")
63+
64+ args = parser.parse_args()
65+
66+ search_and_run(args.root_dir[0], args.command[0], args.trigger_age[0])

Subscribers

People subscribed via source and target branches