Skip to content

Commit 93d9472

Browse files
author
Mike Taylor
authored
Merge pull request #1725 from karlcow/1586/1
Fixes #1586 - Adds a data/ path logic to the code.
2 parents f901410 + 24dcc1d commit 93d9472

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

.gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ uploads/
1313
config/secrets.py
1414
package-lock.json
1515

16-
# backup folder contains the issues.db backup file with issues dump
17-
# for the previous versions
18-
backups/*.db
16+
# The data folder contains information that shouldn't live in version control.
17+
data/*
1918

2019
# these are our 'dist' files
2120
# checking them into the repo is silly

config/environment.py

+5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
# This Source Code Form is subject to the terms of the Mozilla Public
44
# License, v. 2.0. If a copy of the MPL was not distributed with this
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
"""Generic configuration for the project."""
67

78
import os
89

910
# Define the application base directory
1011
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
12+
DATA_PATH = os.path.join(BASE_DIR, 'data')
13+
# Add the data/ directory if it doesn't exist.
14+
if not os.path.exists(DATA_PATH):
15+
os.makedirs(DATA_PATH)
1116

1217
# the PRODUCTION and DEVELOPMENT environment variables are set in uwsgi.conf
1318
# on the webcompat server. If they're not set, they will default to None

tools/topsites.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This Source Code Form is subject to the terms of the Mozilla Public
44
# License, v. 2.0. If a copy of the MPL was not distributed with this
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
"""GitHub Webhook module for assigning priority to sites."""
67

78
import base64
89
import datetime
@@ -16,12 +17,12 @@
1617

1718
import requests
1819
from requests.exceptions import ConnectionError
19-
from sqlalchemy import create_engine
2020
from sqlalchemy import Column
21-
from sqlalchemy import Integer
22-
from sqlalchemy import String
21+
from sqlalchemy import create_engine
2322
from sqlalchemy.ext.declarative import declarative_base
23+
from sqlalchemy import Integer
2424
from sqlalchemy.orm import sessionmaker
25+
from sqlalchemy import String
2526

2627
# Add webcompat module to import path
2728
sys.path.append(os.path.realpath(os.pardir))
@@ -36,6 +37,9 @@
3637
ATS_HASH_ALGORITHM = 'HmacSHA256'
3738
ATS_COUNT = 100
3839

40+
# Location of the DB and its backup.
41+
DB_PATH = app.config['DATA_PATH']
42+
3943
# Regions to dump to topsites.db
4044
REGIONS = ['GLOBAL', 'US', 'FR', 'IN', 'DE', 'TW', 'ID', 'HK', 'SG', 'PL',
4145
'GB', 'RU']
@@ -48,15 +52,15 @@
4852
# Cache parsed sites, change priority if raised
4953
topsites = {}
5054

51-
engine = create_engine('sqlite:///' + os.path.join(
52-
app.config['BASE_DIR'], 'topsites-new.db'))
55+
engine = create_engine('sqlite:///' + os.path.join(DB_PATH, 'topsites-new.db'))
5356
Base = declarative_base()
5457
Session = sessionmaker(bind=engine)
5558
session = Session()
5659

5760

5861
class Site(Base):
5962
"""SQLAchemy base object for an Alexa top site."""
63+
6064
__tablename__ = "topsites"
6165

6266
url = Column(String, primary_key=True)
@@ -65,6 +69,7 @@ class Site(Base):
6569
ranking = Column(Integer)
6670

6771
def __init__(self, url, priority, country_code, ranking):
72+
"""Initialize parameters of the Alexa top site DB."""
6873
self.url = url
6974
self.priority = priority
7075
self.country_code = country_code
@@ -168,7 +173,7 @@ def build_query_string(country_code, start_ranking):
168173

169174

170175
def gen_sign(data):
171-
"""Computes RFC 2104-compliant HMAC signature."""
176+
"""Compute RFC 2104-compliant HMAC signature."""
172177
dig = hmac.new(ats_secret_key, data, hashlib.sha256).digest()
173178
return base64.b64encode(dig)
174179

@@ -199,8 +204,8 @@ def node_text(tree, tag_name):
199204
# Archive topsites.db and rename topsites-new.db to topsites.db
200205
session.close()
201206
archive_date = time.strftime("%Y%m%d", time.localtime())
202-
os.rename(os.path.join(app.config['BASE_DIR'], 'topsites.db'),
203-
os.path.join(app.config['BASE_DIR'],
207+
os.rename(os.path.join(DB_PATH, 'topsites.db'),
208+
os.path.join(DB_PATH,
204209
'topsites-archive-{}.db'.format(archive_date)))
205-
os.rename(os.path.join(app.config['BASE_DIR'], 'topsites-new.db'),
206-
os.path.join(app.config['BASE_DIR'], 'topsites.db'))
210+
os.rename(os.path.join(DB_PATH, 'topsites-new.db'),
211+
os.path.join(DB_PATH, 'topsites.db'))

webcompat/db/__init__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# License, v. 2.0. If a copy of the MPL was not distributed with this
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
66

7-
"""Databases initialization."""
7+
"""Database initialization."""
88

99
from hashlib import sha512
1010
import os
@@ -20,7 +20,7 @@
2020
from webcompat import app
2121

2222
session_engine = create_engine('sqlite:///' + os.path.join(
23-
app.config['BASE_DIR'], 'session.db'))
23+
app.config['DATA_PATH'], 'session.db'))
2424
session_db = scoped_session(sessionmaker(autocommit=False,
2525
autoflush=False,
2626
bind=session_engine))
@@ -29,12 +29,15 @@
2929

3030

3131
class User(UsersBase):
32+
"""Define the user DB holding the sessions."""
33+
3234
__tablename__ = 'users'
3335

3436
user_id = Column(String(128), unique=True, primary_key=True)
3537
access_token = Column(String(128), unique=True)
3638

3739
def __init__(self, access_token):
40+
"""Initialize the user db parameters."""
3841
self.access_token = access_token
3942
# We use the user_id in the session cookie to identify auth'd users.
4043
# Here we salt and hash the GitHub access token so you can't get

0 commit comments

Comments
 (0)