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

Subscribers

People subscribed via source and target branches