def main(argv=None): """Initialize the database.""" print( 'Deprecation warning: This script is going to be removed. ' 'Please use cnx-db init instead.', file=sys.stderr) parser = create_parser('initdb', description=__doc__) parser.add_argument('--with-example-data', action='store_true', help="Initializes the database with example data.") parser.add_argument('--superuser', action='store', help="NOT IMPLEMENTED") parser.add_argument('--super-password', action='store', help="NOT IMPLEMENTED") args = parser.parse_args(argv) settings = get_app_settings_from_arguments(args) try: init_db(settings[config.CONNECTION_STRING], as_venv_importable=True) except DBSchemaInitialized: print("Error: Database is already initialized.", file=sys.stderr) return 1 if args.with_example_data: connection_string = settings[config.CONNECTION_STRING] with psycopg2.connect(connection_string) as db_connection: with db_connection.cursor() as cursor: for filepath in EXAMPLE_DATA_FILEPATHS: with open(filepath, 'r') as fb: cursor.execute(fb.read()) return 0
def main(argv=None): """Initialize the database.""" parser = create_parser('initdb', description=__doc__) parser.add_argument('--with-example-data', action='store_true', help="Initializes the database with example data.") parser.add_argument('--superuser', action='store', help="NOT IMPLEMENTED") parser.add_argument('--super-password', action='store', help="NOT IMPLEMENTED") args = parser.parse_args(argv) settings = get_app_settings_from_arguments(args) try: initdb(settings) except psycopg2.InternalError as exc: print("Error: {}".format(exc.args[0]), file=sys.stderr) return 1 if args.with_example_data: connection_string = settings[config.CONNECTION_STRING] with psycopg2.connect(connection_string) as db_connection: with db_connection.cursor() as cursor: for filepath in EXAMPLE_DATA_FILEPATHS: with open(filepath, 'r') as fb: cursor.execute(fb.read()) return 0
def main(argv=None): """Count the hits from logfile.""" parser = create_parser('hits_counter', description=__doc__) parser.add_argument('--hostname', default='cnx.org', help="hostname of the site (default: cnx.org)") parser.add_argument('--log-format', default=LOG_FORMAT_GZ, choices=LOG_FORMATS, help="(default: {})".format(LOG_FORMAT_GZ)) parser.add_argument('log_file', help="path to the logfile.") args = parser.parse_args(argv) opener = LOG_FORMAT_OPENERS_MAPPING[args.log_format] # Build the URL pattern. hostname = args.hostname.replace('.', '\.') url_pattern = URL_PATTERN_TMPLT.format(hostname) url_pattern = re.compile(url_pattern) # Parse the log to structured data. with opener(args.log_file) as log: hits, start_timestamp, end_timestamp = parse_log(log, url_pattern) # Parse the configuration file for the postgres connection string. settings = get_app_settings_from_arguments(args) # Insert the hits into the database. connection_string = settings[config.CONNECTION_STRING] db_connection = psycopg2.connect(connection_string) with db_connection: with db_connection.cursor() as cursor: for ident_hash, hit_count in hits.items(): cursor.execute("""\ INSERT INTO document_hits (documentid, start_timestamp, end_timestamp, hits) SELECT module_ident, %s, %s, %s FROM modules WHERE ident_hash(uuid, major_version, minor_version) = %s""", (start_timestamp, end_timestamp, hit_count, ident_hash)) cursor.execute("SELECT update_hit_ranks();") db_connection.close() return 0
def main(argv=None): """Count the hits from logfile.""" parser = create_parser('hits_counter', description=__doc__) parser.add_argument('--hostname', default='cnx.org', help="hostname of the site (default: cnx.org)") parser.add_argument('--log-format', default=LOG_FORMAT_GZ, choices=LOG_FORMATS, help="(default: {})".format(LOG_FORMAT_GZ)) parser.add_argument('log_file', help="path to the logfile.") args = parser.parse_args(argv) opener = LOG_FORMAT_OPENERS_MAPPING[args.log_format] # Build the URL pattern. hostname = args.hostname.replace('.', '\.') url_pattern = URL_PATTERN_TMPLT.format(hostname) url_pattern = re.compile(url_pattern) # Parse the log to structured data. with opener(args.log_file) as log: hits, start_timestamp, end_timestamp = parse_log(log, url_pattern) # Parse the configuration file for the postgres connection string. settings = get_app_settings_from_arguments(args) # Insert the hits into the database. connection_string = settings[config.CONNECTION_STRING] with psycopg2.connect(connection_string) as db_connection: with db_connection.cursor() as cursor: for ident_hash, hit_count in hits.items(): id, version = split_ident_hash(ident_hash) cursor.execute(SQL_GET_MODULE_IDENT_BY_UUID_N_VERSION, (id, version)) module_ident = cursor.fetchone() payload = (module_ident, start_timestamp, end_timestamp, hit_count,) cursor.execute("INSERT INTO document_hits " " VALUES (%s, %s, %s, %s);", payload) cursor.execute("SELECT update_hit_ranks();") return 0
def call_target(self, *args, **kwargs): from cnxarchive.scripts._utils import create_parser from cnxarchive.scripts._utils import get_app_settings_from_arguments parser = create_parser('settings-test', 'just a test') arguments = parser.parse_args(*args, **kwargs) return get_app_settings_from_arguments(arguments)