rm.py 937 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python
  2. # Copyright (c) 2016 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Delete a file.
  6. This module works much like the rm posix command.
  7. """
  8. from __future__ import print_function
  9. import argparse
  10. import os
  11. import sys
  12. def Main():
  13. parser = argparse.ArgumentParser()
  14. parser.add_argument('files', nargs='+')
  15. parser.add_argument('-f', '--force', action='store_true',
  16. help="don't err on missing")
  17. parser.add_argument('--stamp', required=True, help='touch this file')
  18. args = parser.parse_args()
  19. for f in args.files:
  20. try:
  21. os.remove(f)
  22. except OSError:
  23. if not args.force:
  24. print("'%s' does not exist" % f, file=sys.stderr)
  25. return 1
  26. with open(args.stamp, 'w'):
  27. os.utime(args.stamp, None)
  28. return 0
  29. if __name__ == '__main__':
  30. sys.exit(Main())