Untitled

 avatar
unknown
python
2 years ago
2.4 kB
4
Indexable
# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
import os
import sys
import csv


def save_to_time_file(rootdir, time_file):

  main_files = []
  for subdir, dirs, files in os.walk(rootdir):
    for file in files:
      if file.endswith(('.h', '.c')):
        #print(file, subdir[len(rootdir):])
        main_files.append(os.path.join(subdir, file))

  main_relpaths = [os.path.relpath(path, rootdir) for path in main_files]

  path_times = []
  for file in main_relpaths:
    t1 = os.path.getmtime(os.path.join(rootdir, file))
    path_times.append([file, t1])

  with open(time_file, 'w') as csvfile:

    # creating a csv writer object
    csvwriter = csv.writer(csvfile)

    # writing the fields
    csvwriter.writerow(['path', 'time'])

    # writing the data rows
    csvwriter.writerows(path_times)


def main(rootdir, time_file):

  #print("workingo on main\n")

  main_files = []
  for subdir, dirs, files in os.walk(rootdir):
    for file in files:
      if file.endswith(('.h', '.c')):
        #print(file, subdir[len(rootdir):])
        main_files.append(os.path.join(subdir, file))

  #print("working on backup\n")

  main_relpaths = [os.path.relpath(path, rootdir) for path in main_files]
  #print("\n\n")

  rel_paths = []
  times = []
  with open(time_file, mode='r') as file:
    csvFile = csv.reader(file)
    i = 0
    for lines in csvFile:
      if i == 0:
        i += 1
        continue
      rel_paths.append(lines[0])
      times.append(lines[1])

  for file in main_relpaths:
    if file in rel_paths:
      t1 = os.path.getmtime(os.path.join(rootdir, file))

      t2 = times[rel_paths.index(file)]

      if float(t1) != float(t2):
        print("changed", file)
      else:
        pass
        #print("not changed", file)
    else:
      print("new file created", file)


if __name__ == '__main__':
  print("searching")
  #print("Argument List:", str(sys.argv))
  args = sys.argv
  if len(args) == 3:
    if not os.path.isdir(args[1]):
      print("directory does not exist")
      exit()
    main(args[1], args[2])
  elif len(args) == 4 and args[3] == 'save':
    if not os.path.isdir(args[1]):
      print("directory does not exist")

    save_to_time_file(args[1], args[2])
  else:
    print("enter correct args [full main folder path ,backup time file path]")
Editor is loading...