Last active
September 24, 2018 19:58
-
-
Save mikecharles/07a61518cbe1003cc125 to your computer and use it in GitHub Desktop.
Find missing files, given a range of dates and a file template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from datetime import date, datetime, timedelta | |
import os | |
import sys | |
import jinja2 | |
script_name = os.path.basename(__file__) | |
def usage(): | |
print('{} [START-DATE] [END-DATE] [FILE-TEMPLATE]'.format(script_name)) | |
print('') | |
print(' where:') | |
print('') | |
print(' START-DATE start date (YYYYMMDD)') | |
print(' END-DATE end date (YYYYMMDD)') | |
print(' FILE-TEMPLATE file template (use {yyyy}, {mm}, and {dd})') | |
print('') | |
print(' For example:') | |
print('') | |
print(' {} 20140101 20141231 /data/{{{{yyyy}}}}/{{{{mm}}}}/{{{{dd}}}}/data-file-{{{{yyyy}}}}{{{{mm}}}}{{{{dd}}}}.txt'.format(script_name)) | |
print('') | |
def main(): | |
try: | |
start_date = sys.argv[1] | |
end_date = sys.argv[2] | |
file_template = sys.argv[3] | |
except IndexError: | |
usage() | |
sys.exit(2) | |
start_date_obj = datetime.strptime(start_date, '%Y%m%d') | |
end_date_obj = datetime.strptime(end_date, '%Y%m%d') | |
date_list = [(start_date_obj + timedelta(days=i)).strftime('%Y%m%d') for i in range((end_date_obj - start_date_obj).days + 1)] | |
none_missing = True | |
for date in date_list: | |
kwargs = {'yyyy': date[0:4], 'mm': date[4:6], 'dd': date[6:8]} | |
file = jinja2.Template(os.path.expandvars(file_template)).render(**kwargs) | |
if not os.path.isfile(file): | |
none_missing = False | |
print('Missing file: {}'.format(file)) | |
if none_missing: | |
print('No missing files...') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment