annotate modules/web_view/collect_issues.py @ 16:f89ad628f831

adding the renamed files
author sean
date Wed, 05 Aug 2015 13:33:15 +0200
parents
children
rev   line source
16
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
1 #!/usr/bin/env python3
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
2
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
3 """ Fetch issues from a roundup-tracker and save them in a databse.
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
4
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
5 author: Sascha L. Teichmann <sascha.teichmann@intevation.de>
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
6 author: Bernhard Reiter <bernhard@intevation.de>
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
7 author: Sean Engelhardt <sean.engelhardt@intevation.de>
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
8
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
9 (c) 2010,2015 by Intevation GmbH
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
10
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
11 This is Free Software unter the terms of the
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
12 GNU GENERAL PUBLIC LICENSE Version 3 or later.
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
13 See http://www.gnu.org/licenses/gpl-3.0.txt for details
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
14
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
15
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
16 ##USAGE EXAMPLE: ##
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
17
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
18 BASE_URL_DEMO = "http://localhost:8917/demo/"
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
19 SEARCH_URL_DEMO = "issue?@action=export_csv&@columns=title,priority&@filter=status&@pagesize=50&@startwith=0&status=-1,1,2,3,4,5,6,7"
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
20
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
21 LOGIN_PARAMETERS_DEMO = (
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
22 ("__login_name", "demo"),
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
23 ("__login_password", "demo"),
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
24 ("@action", "Login"),
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
25 )
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
26
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
27 save_stats_in_db(LOGIN_PARAMETERS_DEMO, BASE_URL_DEMO, rcd.DATABASE_DEMO, rcd.COLUMNS, rcd.CREATE_DB, rcd.INSERT_NEW, SEARCH_URL_DEMO)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
28 """
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
29
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
30 import http.cookiejar
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
31 import urllib.parse
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
32 import urllib.request
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
33 import csv
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
34 import io
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
35 import sqlite3 as db
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
36 import os
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
37 import web_view.roundup_content_data as rcd
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
38
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
39
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
40 CHECK_ROUNDUP_ORDER = "priority?@action=export_csv&@columns=id,order"
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
41 CHECK_ROUNDUP_SEARCH_VALUES = "status?@action=export_csv&@columns=id&@filter=open&open=1"
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
42 SEARCH_ROUNDUP = "issue?@action=export_csv&@columns=priority&@filter=status&@pagesize=500&@startwith=0&status=-1,{search_values}"
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
43
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
44
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
45 def connect_to_server(params, baseurl):
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
46 enc_data = urllib.parse.urlencode(params).encode()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
47 cj = http.cookiejar.CookieJar()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
48 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
49 req = urllib.request.Request(url=baseurl, data=enc_data)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
50 opener.open(req)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
51 return opener
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
52
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
53
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
54 def get_csv_from_server(opener, roundup_url, sub_url):
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
55 csv_req = urllib.request.Request(url=roundup_url+sub_url)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
56 f = opener.open(csv_req)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
57 csv_reader = csv.DictReader(io.TextIOWrapper(f))
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
58 return csv_reader
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
59
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
60
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
61 def set_search_paramters_on_URL(url, search_param_csv):
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
62
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
63 id_list = []
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
64
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
65 for row in search_param_csv:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
66 id_list.append(row["id"])
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
67
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
68 new_url = url.format(search_values = ",".join(id_list))
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
69
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
70 return new_url
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
71
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
72
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
73 def check_create_database(database_file, sql_create_db):
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
74 if not os.path.isfile(database_file):
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
75 con = None
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
76 cur = None
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
77 try:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
78 con = db.connect(database_file)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
79 cur = con.cursor()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
80 try:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
81 cur.execute(sql_create_db)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
82 con.commit()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
83 os.chmod(database_file, 0o644)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
84 except:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
85 con.rollback()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
86 raise
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
87 finally:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
88 if cur:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
89 cur.close()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
90 if con:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
91 con.close()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
92
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
93
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
94 def issues_to_quantities(issue_csv, columns, orders_csv):
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
95
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
96 quantities = [0] * len(columns)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
97 order_dict = {}
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
98
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
99 #convert the csv-dict reader to real dict
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
100 for row in orders_csv:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
101 order_dict[row["id"]] = int(float(row["order"])) # int(float()) because the order-value is indeed "1.0, 2.0" etc
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
102
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
103 for issue in issue_csv:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
104 priority = issue["priority"]
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
105
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
106 if priority.isdigit() == True :
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
107 quantities[order_dict[priority] -1 ] += 1
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
108
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
109 # print("quantities : " + str(quantities))
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
110
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
111 return quantities
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
112
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
113
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
114 def save_issues_to_db(quantities, database_file, sql_create_db, sql_insert_in_db):
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
115 check_create_database(database_file, sql_create_db)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
116
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
117 cur = None
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
118 con = None
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
119
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
120 try:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
121 con = db.connect(database_file)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
122 cur = con.cursor()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
123 try:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
124 cur.execute(sql_insert_in_db, quantities)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
125 con.commit()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
126 except:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
127 con.rollback()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
128 raise
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
129 finally:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
130 if cur:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
131 cur.close()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
132 if con:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
133 con.close()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
134
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
135
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
136 def save_stats_in_db(login_parmeters, baseurl, db_file, columns, sql_create_db, sql_insert_in_db, searchurl=False):
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
137 try:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
138
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
139 opener = connect_to_server(login_parmeters, baseurl)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
140
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
141 search_operators_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_SEARCH_VALUES)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
142 order_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_ORDER)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
143
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
144 if searchurl == False:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
145 formated_search_url = set_search_paramters_on_URL(SEARCH_ROUNDUP, search_operators_csv)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
146 else:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
147 formated_search_url = searchurl
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
148
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
149 current_issues_csv = get_csv_from_server(opener, baseurl, formated_search_url)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
150
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
151 opener.close()
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
152
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
153 quantities = issues_to_quantities(current_issues_csv, columns, order_csv)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
154
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
155 save_issues_to_db(quantities, db_file, sql_create_db, sql_insert_in_db)
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
156
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
157 except urllib.error.URLError as e:
f89ad628f831 adding the renamed files
sean
parents:
diff changeset
158 print("No Valid Connection to server : " + baseurl + "\nerror: " + str(e))
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)