Changeset 17:323bb4ed1810 in stamper for bin/stamp2json


Ignore:
Timestamp:
Jul 19, 2014, 9:51:02 AM (10 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Prevent default overwrite of the destination file on conversion.
Now the script asks you for confirmation before overwriting the
destination if it is a file, exists with an error message & code
if it is a directory.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • bin/stamp2json

    r16 r17  
    22
    33import json
     4import sys
    45from argparse import ArgumentParser
    5 from os.path import expanduser
     6from os.path import expanduser, isfile, isdir
    67
    78
     
    5960                'action': action,
    6061            })
     62
     63    if isfile(args.output):
     64        print('[warning] %(out)s already exist'
     65              % {'out': args.output})
     66        confirm = raw_input('overwrite? (y/n) ')
     67        while confirm not in ['y','n','Y','N']:
     68            confirm = raw_input('overwrite? (y/n) ')
     69        if confirm in ['n', 'N']:
     70            print('[warning] exiting without converting')
     71            sys.exit()
     72
     73    elif isdir(args.output):
     74        print('[error] %(out)s is a directory, remove it first'
     75              % {'out': args.output})
     76        sys.exit(1)
     77
    6178    with open(args.output, 'w') as stamps_file:
    6279        json.dump(stamps, stamps_file, indent=4)
Note: See TracChangeset for help on using the changeset viewer.