-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-definitions.py
executable file
·166 lines (144 loc) · 6.57 KB
/
add-definitions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
# # -*- coding: utf-8 -*-
""" Smogdance
An open-source collection of scripts to collect, store and graph air quality data
from publicly available sources.
This adds sensors according to their definitions into the database.
gnd, 2017 - 2018
"""
import os
import sys
import shlex
import MySQLdb
import subprocess
import ConfigParser
### load config
settings_file = os.path.join(sys.path[0], 'settings_python')
config = ConfigParser.ConfigParser()
config.readfp(open(settings_file))
SCRAPY_BIN = config.get('globals', 'SCRAPY_BIN')
DATA_DIR = config.get('globals', 'DATA_DIR')
TEMP_DIR = config.get('globals', 'TEMP_DIR')
SPIDER_DIR = config.get('globals', 'SPIDER_DIR')
### connect to the db
DB_HOST = config.get('database', 'DB_HOST')
DB_USER = config.get('database', 'DB_USER')
DB_PASS = config.get('database', 'DB_PASS')
DB_NAME = config.get('database', 'DB_NAME')
DB_TABLE = config.get('database', 'DB_TABLE')
DATA_TABLE = config.get('database', 'DATA_TABLE')
db = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASS, db=DB_NAME, use_unicode=True, charset="utf8")
cur = db.cursor()
### process input
if (len(sys.argv) > 1):
task = sys.argv[1]
else:
sys.exit("Usage: ./add-definitions.py <custom file> <normal | special> [at | cz | hu | pl | sk]")
### import custom sensors
if (task == 'custom'):
if (len(sys.argv) > 2):
custom = sys.argv[2]
f = file(custom, 'r')
definitions = f.readlines()
f.close()
print "Importing definitions from %s" % (custom)
for line in definitions:
import_cmd = "%s/add-sensor.py %s" % (DATA_DIR, line)
import_args = shlex.split(import_cmd)
### run the import
try:
print "Trying to run %s/add-sensor.py %s" % (DATA_DIR, line)
process = subprocess.Popen(import_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = process.communicate()[1]
if (process.returncode != 0):
print "Import failed:\n%s" % (out)
except:
print "Couldnt run %s/add-sensor.py %s" % (DATA_DIR, line)
else:
sys.exit("Usage: ./add-definitions.py <custom file> <normal | special> [at | cz | hu | pl | sk]")
### import normal sensors
elif (task == 'normal'):
if (len(sys.argv) > 2):
country = sys.argv[2]
files = [f for f in os.listdir(DATA_DIR+"/definitions") if os.path.isfile(os.path.join(DATA_DIR+"/definitions", f))]
for def_file in files:
if (def_file.split("-")[0] == country):
f = file(DATA_DIR+"/definitions/"+def_file, 'r')
definitions = f.readlines()
f.close()
print "Importing definitions from %s" % (def_file)
for line in definitions:
line = line.replace('TEMP_DIR', TEMP_DIR)
import_cmd = "%s/add-sensor.py %s" % (DATA_DIR, line)
import_args = shlex.split(import_cmd)
### run the import
try:
print "Trying to run %s/add-sensor.py %s" % (DATA_DIR, line)
process = subprocess.Popen(import_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = process.communicate()[1]
if (process.returncode != 0):
print "Import failed:\n%s" % (out)
except:
print "Couldnt run %s/add-sensor.py %s" % (DATA_DIR, line)
else:
files = [f for f in os.listdir(DATA_DIR+"/definitions") if os.path.isfile(os.path.join(DATA_DIR+"/definitions", f))]
for def_file in files:
if (def_file != "special"):
f = file(DATA_DIR+"/definitions/"+def_file, 'r')
definitions = f.readlines()
f.close()
print "Importing definitions from %s" % (def_file)
for line in definitions:
line = line.replace('TEMP_DIR', TEMP_DIR)
import_cmd = "%s/add-sensor.py %s" % (DATA_DIR, line)
import_args = shlex.split(import_cmd)
### run the import
try:
print "Trying to run %s/add-sensor.py %s" % (DATA_DIR, line)
process = subprocess.Popen(import_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = process.communicate()[1]
if (process.returncode != 0):
print "Import failed:\n%s" % (out)
except:
print "Couldnt run %s/add-sensor.py %s" % (DATA_DIR, line)
### import special sensors
elif (task == 'special'):
if (len(sys.argv) > 2):
country = sys.argv[2]
f = file(DATA_DIR+"/definitions/special", 'r')
definitions = f.readlines()
f.close()
print "Importing definitions from special"
for line in definitions:
if (line.split(' ')[4] == country):
import_cmd = "%s/add-special-sensor.py %s" % (DATA_DIR, line)
import_args = shlex.split(import_cmd)
### run the import
try:
print "Trying to run %s/add-special-sensor.py %s" % (DATA_DIR, line)
process = subprocess.Popen(import_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = process.communicate()[1]
if (process.returncode != 0):
print "Import failed:\n%s" % (out)
except:
print "Couldnt run %s/add-sensor.py %s" % (DATA_DIR, line)
else:
f = file(DATA_DIR+"/definitions/special", 'r')
definitions = f.readlines()
f.close()
print "Importing definitions from special"
for line in definitions:
import_cmd = "%s/add-special-sensor.py %s" % (DATA_DIR, line)
import_args = shlex.split(import_cmd)
### run the import
try:
print "Trying to run %s/add-special-sensor.py %s" % (DATA_DIR, line)
process = subprocess.Popen(import_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = process.communicate()[1]
if (process.returncode != 0):
print "Import failed:\n%s" % (out)
except:
print "Couldnt run %s/add-sensor.py %s" % (DATA_DIR, line)
### just show the usage
else:
sys.exit("Usage: ./add-definitions.py <custom file> <normal | special> [at | cz | hu | pl | sk]")