history_reset.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. '''
  2. Created on 24-12-2012
  3. @author: marcin
  4. '''
  5. import os
  6. import re
  7. rootpath = "."
  8. specfile = "pspec.xml"
  9. newcomment = "First release"
  10. newrelease = "1"
  11. relative_backup_path = "../backup"
  12. def read_file(path):
  13. with open(path) as f:
  14. return f.read().strip()
  15. def reset_history(spec):
  16. release = re.compile("(.*?release\s*=\s*)[\"\']\d+[\"\'](.*?)")
  17. newspec = ''
  18. history = False
  19. update1 = False
  20. skipline = False
  21. for line in spec.split("\n"):
  22. if "</History>" in line: history = False
  23. if "<History>" in line: history = True
  24. if history:
  25. if "</Update>" in line:
  26. if not update1: newspec = "\n".join((newspec, line))
  27. update1 = True
  28. if update1: continue
  29. else:
  30. if "<Update" in line: line = re.sub(release, r'\1"%s"\2' % newrelease, line)
  31. elif "<Comment" in line:
  32. if not "</Comment" in line:
  33. skipline = True
  34. if "</Comment" in line:
  35. skipline = False
  36. line = " " * 12 + "<Comment>%s</Comment>" % newcomment
  37. if not skipline: newspec = "\n".join((newspec, line))
  38. else: newspec = "\n".join((newspec, line))
  39. return newspec
  40. if __name__ == '__main__':
  41. backup_path = os.path.normpath(os.path.join(rootpath, relative_backup_path))
  42. if not os.path.isdir(backup_path):
  43. os.makedirs(backup_path)
  44. else:
  45. suffix = 0
  46. parent_backup = os.path.dirname(backup_path)
  47. base_name = os.path.basename(relative_backup_path)
  48. for d in os.listdir(parent_backup):
  49. if d.startswith(base_name):
  50. ext = d.replace(base_name, '')
  51. if os.path.isdir("%s/%s" % (parent_backup, d)) and ext.isdigit():
  52. if int(ext) > suffix:
  53. suffix = int(ext)
  54. suffix +=1
  55. suffix = "0%d" % suffix if suffix < 10 else str(suffix)
  56. os.rename(backup_path, backup_path + suffix)
  57. os.makedirs(backup_path)
  58. for root, dirs, files in os.walk(rootpath):
  59. if specfile in files:
  60. f = "%s/%s" % (root, specfile)
  61. print(f)
  62. spec = read_file(f)
  63. bf = "%s/%s" % (os.path.normpath(root.replace(rootpath, relative_backup_path)), specfile)
  64. os.makedirs(os.path.dirname(bf))
  65. open(bf, "w").write(spec)
  66. open(bf, "a").write("\n")
  67. open(f, "w").write(reset_history(spec))
  68. open(f, "a").write("\n")