{"action":"created","comment":{"url":"https://api.github.com/repos/seomoz/roger-mesos/pulls/comments/56994740","id":56994740,"diff_hunk":"@@ -0,0 +1,117 @@\n+#--------------------------------------------------------------------------------\n+# Run this script as follow, it can only read from Google Docs\n+#\n+# Requires packages:\n+#                   1. gspread\n+#                   2. oauth2client\n+#\n+# python generate_user_permissions.py -f  \"oath_credentials.json\" \n+#                                     -ss \"google_user_spreadsheet_permissions\"\n+#                                     -ss \"google_bot_spreadsheet_permissions\"\n+#--------------------------------------------------------------------------------\n+import json\n+import sys \n+import os \n+import argparse\n+import gspread\n+from oauth2client.service_account import ServiceAccountCredentials\n+\n+\n+def main():\n+    parser = argparse.ArgumentParser()\n+\n+    parser.add_argument(\"-f\", \"--oauth_credentials_file\", help=\"Path of the file where oauth credetials to \" +\n+                    \"access google drive API is stored, Make sure the email in this file \" + \n+                    \"has edit access on the spreadsheets being read in this script\")\n+    parser.add_argument('-ss', \"--spreadsheet_title\", action='append')\n+    parser.add_argument('-p', '--password_column_name', help=\"Name of the column in the spreadsheet which has password,\"+\n+                        \"if not specified password is same as username\", nargs='?')\n+    args = parser.parse_args()\n+    sheet_titles = args.spreadsheet_title\n+    password_column = args.password_column_name\n+    oauth_credentials_file = args.oauth_credentials_file\n+    user_permission_rows = getUserPermissionRows(sheet_titles, oauth_credentials_file)\n+    users_json = generateUserPermissionsJson(user_permission_rows, password_column)\n+    filename = 'user-permissions.json'    \n+    writeJsonToFile(users_json, filename)\n+\n+    #-------------------------------------------------------------------------------\n+    # This code reads user permissions from a spread sheet as of now, the source can\n+    # changed easily as long as it returns rows in following format.\n+    # \n+    #  {\"username\":\"jon\", \"team\":\"watch\", \"path\":\"/winterfell/\", \n+    #   \"create\":\"allowed\", \"update\":\"allowed\", \"delete\":\"allowed\", \n+    #   \"view\":\"allowed\", \"killTask\":\"allowed\"\n+    #\n+    #-------------------------------------------------------------------------------    \n+\n+def getUserPermissionRows(sheet_titles, oauth_credentials_file):\n+    scope = ['https://spreadsheets.google.com/feeds']\n+    # Currently, the email defined under this file has to have edit permissions on the file\n+    # todo : check if we can get rid of that and use our email instead of the generated\n+    credentials = ServiceAccountCredentials.from_json_keyfile_name(oauth_credentials_file, scope)\n+    gc = gspread.authorize(credentials)\n+\n+    # open by title - titles can change and so is fragile, keys don't\n+    # this works as well\n+    # gc.open_by_key(\"1Izpq1eZI1NuMBddpvvxsIt3usWm11j56eJwPdfxCw6E\").sheet1\n+    rows = []\n+    for title in sheet_titles:\n+        sheet = gc.open(title).sheet1\n+        rows = rows + sheet.get_all_records() \n+    return rows\n+    #-------------------------------------------------------------------------------\n+\n+\n+def generateUserPermissionsJson(rows, password_column):\n+    multiple_user_permissions_per_user = {}\n+    user_permissions_json = []\n+    actions = [\"create\", \"update\", \"delete\", \"view\", \"killTask\"]\n+    permissions_json = {}\n+    credentials = {}\n+\n+    for row in rows:\n+        # todo : \"username\" and \"path\" are required keys, bailout if they are not defined\n+        # dictionary with keys username, team, path, create, update, delete, view, killTask\n+        actions_allowed = []\n+        user = row[\"username\"]\n+        if (row[\"path\"] == None or row[\"path\"] == \"\"):\n+            continue\n+        # remove spaces from key and values \n+        row = {k.translate(None, ' '): v.translate(None, ' ') for k, v in row.iteritems()}\n+        if any ( action in row.keys() for action in actions):\n+            # user custom permissions        \n+            if (\"create\" in row.keys() and row[\"create\"] == \"allowed\"): actions_allowed.append(\"create\")\n+            if (\"update\" in row.keys() and row[\"update\"] == \"allowed\"): actions_allowed.append(\"update\")\n+            if (\"delete\" in row.keys() and row[\"delete\"] == \"allowed\"): actions_allowed.append(\"delete\")\n+            if (\"view\" in row.keys() and row[\"view\"] == \"allowed\") : actions_allowed.append(\"view\")\n+            if (\"killTask\" in row.keys() and row[\"killTask\"] == \"allowed\"): actions_allowed.append(\"killTask\")\n+        else:\n+            actions_allowed = actions\n+\n+        for action in actions_allowed:    \n+            permission = {}\n+            permission[\"allowed\"] = action\n+            permission[\"on\"] = row[\"path\"]\n+            if (user not in permissions_json.keys()):\n+                permissions_json[user] = []\n+            permissions_json[user].append(permission) \n+\n+        if (credentials.get(user) == None or credentials.get(user) == \"\"):\n+            if (password_column in row.keys()):\n+                credentials[user] = row[password_column]\n+            else:\n+                credentials[user] = user\n+                \n+    for user, password in credentials.iteritems():\n+        user_permissions_json.append ({\"user\": user.lower(), \"password\": password.lower(), \"permissions\": permissions_json[user]})","path":"scripts/generate_user_permissions.py","position":107,"original_position":107,"commit_id":"a4d71ec29c05fab530288f1b4e3a84eedeaa2f75","original_commit_id":"a4d71ec29c05fab530288f1b4e3a84eedeaa2f75","user":{"login":"vmahedia","id":8220481,"avatar_url":"https://avatars.githubusercontent.com/u/8220481?v=3","gravatar_id":"","url":"https://api.github.com/users/vmahedia","html_url":"https://github.com/vmahedia","followers_url":"https://api.github.com/users/vmahedia/followers","following_url":"https://api.github.com/users/vmahedia/following{/other_user}","gists_url":"https://api.github.com/users/vmahedia/gists{/gist_id}","starred_url":"https://api.github.com/users/vmahedia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vmahedia/subscriptions","organizations_url":"https://api.github.com/users/vmahedia/orgs","repos_url":"https://api.github.com/users/vmahedia/repos","events_url":"https://api.github.com/users/vmahedia/events{/privacy}","received_events_url":"https://api.github.com/users/vmahedia/received_events","type":"User","site_admin":false},"body":":+1:  very good catch. ","created_at":"2016-03-22T14:27:42Z","updated_at":"2016-03-22T14:27:42Z","html_url":"https://github.com/seomoz/roger-mesos/pull/38#discussion_r56994740","pull_request_url":"https://api.github.com/repos/seomoz/roger-mesos/pulls/38","_links":{"self":{"href":"https://api.github.com/repos/seomoz/roger-mesos/pulls/comments/56994740"},"html":{"href":"https://github.com/seomoz/roger-mesos/pull/38#discussion_r56994740"},"pull_request":{"href":"https://api.github.com/repos/seomoz/roger-mesos/pulls/38"}}},"pull_request":{"url":"https://api.github.com/repos/seomoz/roger-mesos/pulls/38","id":63642873,"html_url":"https://github.com/seomoz/roger-mesos/pull/38","diff_url":"https://github.com/seomoz/roger-mesos/pull/38.diff","patch_url":"https://github.com/seomoz/roger-mesos/pull/38.patch","issue_url":"https://api.github.com/repos/seomoz/roger-mesos/issues/38","number":38,"state":"open","locked":false,"title":"Add script to generate user permissions file from google spreadsheets","user":{"login":"vmahedia","id":8220481,"avatar_url":"https://avatars.githubusercontent.com/u/8220481?v=3","gravatar_id":"","url":"https://api.github.com/users/vmahedia","html_url":"https://github.com/vmahedia","followers_url":"https://api.github.com/users/vmahedia/followers","following_url":"https://api.github.com/users/vmahedia/following{/other_user}","gists_url":"https://api.github.com/users/vmahedia/gists{/gist_id}","starred_url":"https://api.github.com/users/vmahedia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vmahedia/subscriptions","organizations_url":"https://api.github.com/users/vmahedia/orgs","repos_url":"https://api.github.com/users/vmahedia/repos","events_url":"https://api.github.com/users/vmahedia/events{/privacy}","received_events_url":"https://api.github.com/users/vmahedia/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-03-21T22:28:55Z","updated_at":"2016-03-22T14:27:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d9200c7af5ac3762c12213113a887391b72c0568","assignee":{"login":"ankanm","id":558066,"avatar_url":"https://avatars.githubusercontent.com/u/558066?v=3","gravatar_id":"","url":"https://api.github.com/users/ankanm","html_url":"https://github.com/ankanm","followers_url":"https://api.github.com/users/ankanm/followers","following_url":"https://api.github.com/users/ankanm/following{/other_user}","gists_url":"https://api.github.com/users/ankanm/gists{/gist_id}","starred_url":"https://api.github.com/users/ankanm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ankanm/subscriptions","organizations_url":"https://api.github.com/users/ankanm/orgs","repos_url":"https://api.github.com/users/ankanm/repos","events_url":"https://api.github.com/users/ankanm/events{/privacy}","received_events_url":"https://api.github.com/users/ankanm/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/seomoz/roger-mesos/pulls/38/commits","review_comments_url":"https://api.github.com/repos/seomoz/roger-mesos/pulls/38/comments","review_comment_url":"https://api.github.com/repos/seomoz/roger-mesos/pulls/comments{/number}","comments_url":"https://api.github.com/repos/seomoz/roger-mesos/issues/38/comments","statuses_url":"https://api.github.com/repos/seomoz/roger-mesos/statuses/a4d71ec29c05fab530288f1b4e3a84eedeaa2f75","head":{"label":"vmahedia:generate_user_permissions","ref":"generate_user_permissions","sha":"a4d71ec29c05fab530288f1b4e3a84eedeaa2f75","user":{"login":"vmahedia","id":8220481,"avatar_url":"https://avatars.githubusercontent.com/u/8220481?v=3","gravatar_id":"","url":"https://api.github.com/users/vmahedia","html_url":"https://github.com/vmahedia","followers_url":"https://api.github.com/users/vmahedia/followers","following_url":"https://api.github.com/users/vmahedia/following{/other_user}","gists_url":"https://api.github.com/users/vmahedia/gists{/gist_id}","starred_url":"https://api.github.com/users/vmahedia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vmahedia/subscriptions","organizations_url":"https://api.github.com/users/vmahedia/orgs","repos_url":"https://api.github.com/users/vmahedia/repos","events_url":"https://api.github.com/users/vmahedia/events{/privacy}","received_events_url":"https://api.github.com/users/vmahedia/received_events","type":"User","site_admin":false},"repo":{"id":48134050,"name":"roger-mesos","full_name":"vmahedia/roger-mesos","owner":{"login":"vmahedia","id":8220481,"avatar_url":"https://avatars.githubusercontent.com/u/8220481?v=3","gravatar_id":"","url":"https://api.github.com/users/vmahedia","html_url":"https://github.com/vmahedia","followers_url":"https://api.github.com/users/vmahedia/followers","following_url":"https://api.github.com/users/vmahedia/following{/other_user}","gists_url":"https://api.github.com/users/vmahedia/gists{/gist_id}","starred_url":"https://api.github.com/users/vmahedia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vmahedia/subscriptions","organizations_url":"https://api.github.com/users/vmahedia/orgs","repos_url":"https://api.github.com/users/vmahedia/repos","events_url":"https://api.github.com/users/vmahedia/events{/privacy}","received_events_url":"https://api.github.com/users/vmahedia/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/vmahedia/roger-mesos","description":"A complete mesos cluster setup with automatic load balancing","fork":true,"url":"https://api.github.com/repos/vmahedia/roger-mesos","forks_url":"https://api.github.com/repos/vmahedia/roger-mesos/forks","keys_url":"https://api.github.com/repos/vmahedia/roger-mesos/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vmahedia/roger-mesos/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vmahedia/roger-mesos/teams","hooks_url":"https://api.github.com/repos/vmahedia/roger-mesos/hooks","issue_events_url":"https://api.github.com/repos/vmahedia/roger-mesos/issues/events{/number}","events_url":"https://api.github.com/repos/vmahedia/roger-mesos/events","assignees_url":"https://api.github.com/repos/vmahedia/roger-mesos/assignees{/user}","branches_url":"https://api.github.com/repos/vmahedia/roger-mesos/branches{/branch}","tags_url":"https://api.github.com/repos/vmahedia/roger-mesos/tags","blobs_url":"https://api.github.com/repos/vmahedia/roger-mesos/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vmahedia/roger-mesos/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vmahedia/roger-mesos/git/refs{/sha}","trees_url":"https://api.github.com/repos/vmahedia/roger-mesos/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vmahedia/roger-mesos/statuses/{sha}","languages_url":"https://api.github.com/repos/vmahedia/roger-mesos/languages","stargazers_url":"https://api.github.com/repos/vmahedia/roger-mesos/stargazers","contributors_url":"https://api.github.com/repos/vmahedia/roger-mesos/contributors","subscribers_url":"https://api.github.com/repos/vmahedia/roger-mesos/subscribers","subscription_url":"https://api.github.com/repos/vmahedia/roger-mesos/subscription","commits_url":"https://api.github.com/repos/vmahedia/roger-mesos/commits{/sha}","git_commits_url":"https://api.github.com/repos/vmahedia/roger-mesos/git/commits{/sha}","comments_url":"https://api.github.com/repos/vmahedia/roger-mesos/comments{/number}","issue_comment_url":"https://api.github.com/repos/vmahedia/roger-mesos/issues/comments{/number}","contents_url":"https://api.github.com/repos/vmahedia/roger-mesos/contents/{+path}","compare_url":"https://api.github.com/repos/vmahedia/roger-mesos/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vmahedia/roger-mesos/merges","archive_url":"https://api.github.com/repos/vmahedia/roger-mesos/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vmahedia/roger-mesos/downloads","issues_url":"https://api.github.com/repos/vmahedia/roger-mesos/issues{/number}","pulls_url":"https://api.github.com/repos/vmahedia/roger-mesos/pulls{/number}","milestones_url":"https://api.github.com/repos/vmahedia/roger-mesos/milestones{/number}","notifications_url":"https://api.github.com/repos/vmahedia/roger-mesos/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vmahedia/roger-mesos/labels{/name}","releases_url":"https://api.github.com/repos/vmahedia/roger-mesos/releases{/id}","deployments_url":"https://api.github.com/repos/vmahedia/roger-mesos/deployments","created_at":"2015-12-16T20:41:41Z","updated_at":"2016-01-11T22:09:06Z","pushed_at":"2016-03-21T22:25:06Z","git_url":"git://github.com/vmahedia/roger-mesos.git","ssh_url":"git@github.com:vmahedia/roger-mesos.git","clone_url":"https://github.com/vmahedia/roger-mesos.git","svn_url":"https://github.com/vmahedia/roger-mesos","homepage":"","size":9040,"stargazers_count":0,"watchers_count":0,"language":"Shell","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"seomoz:master","ref":"master","sha":"270a8bd012d9c10fe888eaefea3953b93be33147","user":{"login":"seomoz","id":104895,"avatar_url":"https://avatars.githubusercontent.com/u/104895?v=3","gravatar_id":"","url":"https://api.github.com/users/seomoz","html_url":"https://github.com/seomoz","followers_url":"https://api.github.com/users/seomoz/followers","following_url":"https://api.github.com/users/seomoz/following{/other_user}","gists_url":"https://api.github.com/users/seomoz/gists{/gist_id}","starred_url":"https://api.github.com/users/seomoz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seomoz/subscriptions","organizations_url":"https://api.github.com/users/seomoz/orgs","repos_url":"https://api.github.com/users/seomoz/repos","events_url":"https://api.github.com/users/seomoz/events{/privacy}","received_events_url":"https://api.github.com/users/seomoz/received_events","type":"Organization","site_admin":false},"repo":{"id":47104887,"name":"roger-mesos","full_name":"seomoz/roger-mesos","owner":{"login":"seomoz","id":104895,"avatar_url":"https://avatars.githubusercontent.com/u/104895?v=3","gravatar_id":"","url":"https://api.github.com/users/seomoz","html_url":"https://github.com/seomoz","followers_url":"https://api.github.com/users/seomoz/followers","following_url":"https://api.github.com/users/seomoz/following{/other_user}","gists_url":"https://api.github.com/users/seomoz/gists{/gist_id}","starred_url":"https://api.github.com/users/seomoz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seomoz/subscriptions","organizations_url":"https://api.github.com/users/seomoz/orgs","repos_url":"https://api.github.com/users/seomoz/repos","events_url":"https://api.github.com/users/seomoz/events{/privacy}","received_events_url":"https://api.github.com/users/seomoz/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/seomoz/roger-mesos","description":"A complete mesos cluster setup with automatic load balancing","fork":false,"url":"https://api.github.com/repos/seomoz/roger-mesos","forks_url":"https://api.github.com/repos/seomoz/roger-mesos/forks","keys_url":"https://api.github.com/repos/seomoz/roger-mesos/keys{/key_id}","collaborators_url":"https://api.github.com/repos/seomoz/roger-mesos/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/seomoz/roger-mesos/teams","hooks_url":"https://api.github.com/repos/seomoz/roger-mesos/hooks","issue_events_url":"https://api.github.com/repos/seomoz/roger-mesos/issues/events{/number}","events_url":"https://api.github.com/repos/seomoz/roger-mesos/events","assignees_url":"https://api.github.com/repos/seomoz/roger-mesos/assignees{/user}","branches_url":"https://api.github.com/repos/seomoz/roger-mesos/branches{/branch}","tags_url":"https://api.github.com/repos/seomoz/roger-mesos/tags","blobs_url":"https://api.github.com/repos/seomoz/roger-mesos/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/seomoz/roger-mesos/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/seomoz/roger-mesos/git/refs{/sha}","trees_url":"https://api.github.com/repos/seomoz/roger-mesos/git/trees{/sha}","statuses_url":"https://api.github.com/repos/seomoz/roger-mesos/statuses/{sha}","languages_url":"https://api.github.com/repos/seomoz/roger-mesos/languages","stargazers_url":"https://api.github.com/repos/seomoz/roger-mesos/stargazers","contributors_url":"https://api.github.com/repos/seomoz/roger-mesos/contributors","subscribers_url":"https://api.github.com/repos/seomoz/roger-mesos/subscribers","subscription_url":"https://api.github.com/repos/seomoz/roger-mesos/subscription","commits_url":"https://api.github.com/repos/seomoz/roger-mesos/commits{/sha}","git_commits_url":"https://api.github.com/repos/seomoz/roger-mesos/git/commits{/sha}","comments_url":"https://api.github.com/repos/seomoz/roger-mesos/comments{/number}","issue_comment_url":"https://api.github.com/repos/seomoz/roger-mesos/issues/comments{/number}","contents_url":"https://api.github.com/repos/seomoz/roger-mesos/contents/{+path}","compare_url":"https://api.github.com/repos/seomoz/roger-mesos/compare/{base}...{head}","merges_url":"https://api.github.com/repos/seomoz/roger-mesos/merges","archive_url":"https://api.github.com/repos/seomoz/roger-mesos/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/seomoz/roger-mesos/downloads","issues_url":"https://api.github.com/repos/seomoz/roger-mesos/issues{/number}","pulls_url":"https://api.github.com/repos/seomoz/roger-mesos/pulls{/number}","milestones_url":"https://api.github.com/repos/seomoz/roger-mesos/milestones{/number}","notifications_url":"https://api.github.com/repos/seomoz/roger-mesos/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/seomoz/roger-mesos/labels{/name}","releases_url":"https://api.github.com/repos/seomoz/roger-mesos/releases{/id}","deployments_url":"https://api.github.com/repos/seomoz/roger-mesos/deployments","created_at":"2015-11-30T08:24:43Z","updated_at":"2016-03-15T07:53:26Z","pushed_at":"2016-03-21T22:28:56Z","git_url":"git://github.com/seomoz/roger-mesos.git","ssh_url":"git@github.com:seomoz/roger-mesos.git","clone_url":"https://github.com/seomoz/roger-mesos.git","svn_url":"https://github.com/seomoz/roger-mesos","homepage":"","size":12149,"stargazers_count":7,"watchers_count":7,"language":"Shell","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":8,"forks":3,"open_issues":8,"watchers":7,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/seomoz/roger-mesos/pulls/38"},"html":{"href":"https://github.com/seomoz/roger-mesos/pull/38"},"issue":{"href":"https://api.github.com/repos/seomoz/roger-mesos/issues/38"},"comments":{"href":"https://api.github.com/repos/seomoz/roger-mesos/issues/38/comments"},"review_comments":{"href":"https://api.github.com/repos/seomoz/roger-mesos/pulls/38/comments"},"review_comment":{"href":"https://api.github.com/repos/seomoz/roger-mesos/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/seomoz/roger-mesos/pulls/38/commits"},"statuses":{"href":"https://api.github.com/repos/seomoz/roger-mesos/statuses/a4d71ec29c05fab530288f1b4e3a84eedeaa2f75"}}}}