Advertisement
Guest User

Untitled

a guest
May 24th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. exemple usage: ./mirrorlist.py -o mirrorlist.txt uncomment $(./mirrorlist.py sections | dmenu -i -l 3)
  6. """
  7.  
  8. import sys
  9. import argparse
  10. import os
  11. import contextlib
  12.  
  13.  
  14. @contextlib.contextmanager
  15. def output_context(output):
  16. if output == '-':
  17. yield sys.stdout
  18. else:
  19. yield open(output, 'w')
  20.  
  21.  
  22. def split_head(lines):
  23. head = []
  24. body = []
  25.  
  26. target = head
  27.  
  28. for line in lines:
  29. if target is head:
  30. if not line.startswith('##'):
  31. target = body
  32.  
  33. target.append(line)
  34.  
  35. return head, body
  36.  
  37.  
  38. def list_sections(args):
  39. with open(args.mirrorlist_path, 'r') as mirrorlist:
  40. lines = mirrorlist.readlines()
  41.  
  42. head, body = split_head(lines)
  43.  
  44. with output_context(args.output) as output:
  45. for line in body:
  46. if line.startswith('##'):
  47. section = line.replace('#', '').strip()
  48. output.write('{}\n'.format(section))
  49.  
  50.  
  51. def uncomment_sections(args):
  52. with open(args.mirrorlist_path, 'r') as mirrorlist:
  53. lines = mirrorlist.readlines()
  54.  
  55. head, body = split_head(lines)
  56.  
  57. with output_context(args.output) as output:
  58. for line in head:
  59. output.write(line)
  60.  
  61. uncomment = False
  62. for line in body:
  63. if line.startswith('##'):
  64. section = line.replace('#', '').strip()
  65.  
  66. if section in args.sections:
  67. uncomment = True
  68.  
  69. output.write(line)
  70. elif line.startswith('#'):
  71. if uncomment:
  72. output.write(line.replace('#', ''))
  73. else:
  74. output.write(line)
  75. elif not line.strip():
  76. uncomment = False
  77. output.write(line)
  78. else:
  79. output.write(line)
  80.  
  81.  
  82. def parse_args(argv=sys.argv):
  83. parser = argparse.ArgumentParser(description='Manage archlinux mirrorlist')
  84. parser.add_argument('-p', '--path',
  85. dest='mirrorlist_path',
  86. default='/etc/pacman.d/mirrorlist',
  87. help='mirrorlist path')
  88. parser.add_argument('-o', '--output',
  89. dest='output',
  90. default='-',
  91. help='result output (default: stdout)')
  92.  
  93. subparsers = parser.add_subparsers(help='sub-command help')
  94.  
  95. sections_parser = subparsers.add_parser('sections', help='sections help')
  96. sections_parser.set_defaults(action=list_sections)
  97.  
  98. uncomment_parser = subparsers.add_parser('uncomment', help='uncomment help')
  99. uncomment_parser.set_defaults(action=uncomment_sections)
  100. uncomment_parser.add_argument('sections', nargs='+',
  101. help='sections to uncomment')
  102.  
  103. return parser.parse_args(argv[1:])
  104.  
  105.  
  106. if __name__ == "__main__":
  107. args = parse_args()
  108. args.action(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement