Added new script to set an absolute mtime to a specific file
This commit is contained in:
parent
c5cc433e2f
commit
3e5bc7721d
118
set_mtime.py
Executable file
118
set_mtime.py
Executable file
@ -0,0 +1,118 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
"""
|
||||
This helper script will set the modify date (mtime) of a selected file.
|
||||
Copyright (C) 2024 Benjamin Burkhardt
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
Useful links:
|
||||
- https://docs.python.org/3/library/argparse.html#argparse-type
|
||||
- https://docs.python.org/3/library/datetime.html
|
||||
- https://www.tutorialspoint.com/How-to-get-file-creation-and-modification-date-times-in-Python
|
||||
- https://docs.python.org/3/library/os.html#os.utime
|
||||
- https://de.wikipedia.org/wiki/Modification,_Access,_Change
|
||||
- https://en.wikipedia.org/wiki/Unit_prefix
|
||||
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import datetime
|
||||
import os
|
||||
|
||||
|
||||
class COLORS:
|
||||
HEADER = '\033[95m'
|
||||
OKBLUE = '\033[94m'
|
||||
OKCYAN = '\033[96m'
|
||||
OKGREEN = '\033[92m'
|
||||
WARNING = '\033[93m'
|
||||
FAIL = '\033[91m'
|
||||
ENDC = '\033[0m'
|
||||
BOLD = '\033[1m'
|
||||
UNDERLINE = '\033[4m'
|
||||
|
||||
class LOGGER:
|
||||
def __init__(self, verbose: bool = False):
|
||||
self.verbose = verbose
|
||||
|
||||
def log(self, to_log, verbose=False, end="\n"):
|
||||
to_log = to_log + COLORS.ENDC
|
||||
if (self.verbose and verbose) or not verbose: # only print if it's not verbose or verbose is enabled
|
||||
print(to_log, end=end)
|
||||
|
||||
# set up the ArgumentParser for the CLI
|
||||
parser = argparse.ArgumentParser(
|
||||
description='modify_mtime.py Copyright (C) 2024 Benjamin Burkhardt\r\n'
|
||||
'This program comes with ABSOLUTELY NO WARRANTY and is licensed under the '
|
||||
'GNU General Public License 3.\r\n'
|
||||
'You should have received a copy of the GNU General Public License\r\n'
|
||||
'along with this program. If not, see <https://www.gnu.org/licenses/>.\r\n'
|
||||
'\r\n'
|
||||
'This helper script will set the modify date (mtime) of a selected file.\r\n'
|
||||
'The time arguments shall be given as positive or negative integers.\r\n'
|
||||
'BE AWARE THAT ALL CHANGES TO THE FILES CANNOT BE UNDONE! USE AT YOUR OWN RISK!',
|
||||
epilog='by Benjamin Burkhardt, 2024',
|
||||
add_help=False,
|
||||
formatter_class=argparse.RawTextHelpFormatter)
|
||||
|
||||
parser.add_argument('-f', '--file', type=str, default='')
|
||||
parser.add_argument('-mus', '--microsecond', type=int, default=0)
|
||||
parser.add_argument('-ms', '--millisecond', type=int, default=0)
|
||||
parser.add_argument('-s', '--second', type=int, default=0)
|
||||
parser.add_argument('-m', '--minute', type=int, default=0)
|
||||
parser.add_argument('-h', '--hour', type=int, default=0)
|
||||
parser.add_argument('-d', '--day', type=int, default=0, help='REQUIRED')
|
||||
parser.add_argument('-mo', '--month', type=int, default=0, help='REQUIRED')
|
||||
parser.add_argument('-y', '--year', type=int, default=0, help='REQUIRED')
|
||||
parser.add_argument('-v', '--verbose', action='store_true', help='enable verbose output') # on/off flag
|
||||
parser.add_argument('--help', action='store_true', help='display this help message and exit') # on/off flag
|
||||
|
||||
# parse given arguments
|
||||
args = parser.parse_args()
|
||||
|
||||
# set up logger
|
||||
logger = LOGGER(args.verbose)
|
||||
|
||||
# print help and exit if prompted to
|
||||
if args.help:
|
||||
parser.print_help()
|
||||
exit(0)
|
||||
|
||||
# check for an empty filename argument
|
||||
if args.file == '':
|
||||
logger.log(COLORS.FAIL + 'You need to specify a filename to set the modification time of it.')
|
||||
parser.print_usage()
|
||||
exit(1)
|
||||
|
||||
# check for missing year, month or day args
|
||||
if args.year == 0 or args.month == 0 or args.day == 0:
|
||||
logger.log(COLORS.FAIL + 'You need to give at least the --year, --month and --day arguments!')
|
||||
parser.print_usage()
|
||||
exit(1)
|
||||
|
||||
# create the datetime object with the given date
|
||||
new_absolute_time = datetime.datetime(year=args.year, month=args.month, day=args.day, hour=args.hour, minute=args.minute, second=args.second, microsecond=args.millisecond*1000+args.microsecond)
|
||||
|
||||
# log start of the manipulation process in verbose mode
|
||||
if args.verbose:
|
||||
print(f'[DEBUG] Setting the new mtime')
|
||||
|
||||
# Now set it!
|
||||
atime_timestamp = os.stat(args.file).st_atime # get atime timestamp (as os.utime requires this as an argument - we leave it unchanged)
|
||||
mtime_timestamp = new_absolute_time.timestamp() # get the mtime timestamp from the respective datetime object
|
||||
os.utime(args.file, (atime_timestamp, mtime_timestamp)) # set the new mtime (atime stays unchanged)
|
Loading…
Reference in New Issue
Block a user