fork download
  1. #!/usr/bin/env python
  2. import sys
  3. from datetime import datetime
  4. from itertools import groupby
  5.  
  6. # find all 'name: value' pairs
  7. file = sys.stdin
  8. pairs = ([s.strip() for s in line.partition(':')[::2]]
  9. for line in file if ':' in line)
  10.  
  11. # group records
  12. def record_start(pair, count=[False]):
  13. """Mark start of a record."""
  14. if pair[0] == 'Build':
  15. count[0] = not count[0]
  16. return count[0]
  17. records = (dict(record) for _, record in groupby(pairs, record_start))
  18.  
  19. approved = (r for r in records if r.get('Status') == 'Approved' and
  20. all(r.get(name) for name in "BuildDate Location".split()))
  21.  
  22. # find latest record
  23. def get_date(record):
  24. try:
  25. return datetime.strptime(record['BuildDate'], '%m/%d/%Y %H:%M:%S')
  26. except ValueError:
  27. return datetime.min # handle invalid date strings
  28.  
  29. latest = max(approved, key=get_date)
  30. assert get_date(latest) != datetime.min
  31. print(latest['Location'])
Success #stdin #stdout 0.03s 6048KB
stdin
INPUT:-
Build:          M1234BAAAANAAW9321.1
Location:       \\dreyers\builds468\INTEGRATION\M1234BAAAANAA9321.1
Comments:       Build completed, labeled, and marked for retention.
Status:         Approved
BuildDate:      10/25/2012 12:51:25


Build:          M1234BAAAANAAW9321.2
Location:       \\crmbld01\Builds\FAILED\M1234BAAAANAA9321.2
Comments:       The build is currently in a failed status.
Status:         Failed
BuildDate:      10/25/2012 19:37:17


Build:          M1234BAAAANAAW9321.3
Location:       \\freeze\builds427\INTEGRATION\M1234BAAAANAA9321.3
Comments:       Build completed, labeled, and marked for retention.
Status:         Approved
BuildDate:      10/25/2012 19:43:28

OUTPUT:-\\freeze\builds427\INTEGRATION\M1234BAAAANAA9321.3

stdout
\\freeze\builds427\INTEGRATION\M1234BAAAANAA9321.3