PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/get-shit-done.py

https://github.com/sergray/get-shit-done
Python | 104 lines | 98 code | 5 blank | 1 comment | 8 complexity | bcdf04e7c0965206c9032ce35fb20967 MD5 | raw file
  1. #!/usr/bin/env python
  2. import argparse
  3. import getpass
  4. SITE_LIST=[
  5. 'bash.org.ru',
  6. 'bash.org.ua',
  7. 'bebo.com',
  8. 'blip.com',
  9. 'break.com',
  10. 'delicious.com',
  11. 'digg.com',
  12. 'facebook.com',
  13. 'fb.com'
  14. 'flickr.com',
  15. 'forums.somethingawful.com',
  16. 'friendster.com',
  17. 'habrahabr.ru',
  18. 'hi5.com',
  19. 'infoq.com',
  20. 'korrespondent.net',
  21. 'lenta.ru',
  22. 'linkedin.com',
  23. 'livejournal.com',
  24. 'meetup.com',
  25. 'myspace.com',
  26. 'news.ycombinator.com',
  27. 'newsru.com',
  28. 'plurk.com',
  29. 'reddit.com',
  30. 'slashdot.com',
  31. 'somethingawful.com',
  32. 'stickam.com',
  33. 'stumbleupon.com',
  34. 'twitter.com',
  35. 'vimeo.com',
  36. 'vk.com', 'vkontakte.ru',
  37. 'yelp.com',
  38. 'youtube.com',
  39. ]
  40. HOST_FILE = '/etc/hosts';
  41. START_TOKEN = '## start-gsd';
  42. END_TOKEN = '## end-gsd';
  43. class ModeAction(argparse.Action):
  44. def __call__(self, parser, namespace, values=None, option_string=None):
  45. if getpass.getuser() != 'root':
  46. raise Exception('Script should be executed as the root')
  47. if values == 'work':
  48. f = open(HOST_FILE, 'r')
  49. content = f.read()
  50. f.close()
  51. if START_TOKEN in content and END_TOKEN in content:
  52. raise Exception('Work mode already set')
  53. elif START_TOKEN in content or END_TOKEN in content:
  54. raise Exception('Smth goes wrong - 1 token is in file, but second - not')
  55. else:
  56. f = open(HOST_FILE, 'w')
  57. new_content = []
  58. new_content.append(START_TOKEN+'\n')
  59. for site in SITE_LIST:
  60. new_content.append("127.0.0.1\t%s\n" % site)
  61. new_content.append("127.0.0.1\twww.%s\n" % site)
  62. new_content.append(END_TOKEN+'\n')
  63. f.write(content+"".join(new_content))
  64. elif values == 'play':
  65. f = open(HOST_FILE, 'r')
  66. content = f.read()
  67. f.close()
  68. if not START_TOKEN in content and not END_TOKEN in content:
  69. raise Exception('Play mode already set')
  70. elif START_TOKEN in content and END_TOKEN in content:
  71. f = open(HOST_FILE, 'r')
  72. lines = f.readlines()
  73. f.close()
  74. f = open(HOST_FILE, 'w')
  75. inside_token = False
  76. new_content = []
  77. for line in lines:
  78. if START_TOKEN in line:
  79. inside_token = True
  80. if not inside_token:
  81. new_content.append(line)
  82. if END_TOKEN in line:
  83. inside_token = False
  84. f.write("".join(new_content))
  85. elif START_TOKEN in content or END_TOKEN in content:
  86. raise Exception('Smth goes wrong - 1 token is in file, but second - not')
  87. def main():
  88. parser = argparse.ArgumentParser(description='Get shit done.')
  89. parser.add_argument('-m, --mode', action=ModeAction,
  90. choices=['work', 'play'],
  91. help='switch to mode')
  92. args = parser.parse_args()
  93. if __name__ == "__main__":
  94. main()