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): """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): parser = create_parser('export_epub', description=__doc__) parser.add_argument('ident_hash', help="ident-hash of the content ") parser.add_argument('file', type=argparse.FileType('wb'), help="output file") args = parser.parse_args(argv) if args.file is sys.stdout: raise RuntimeError("Can't stream a zipfile to stdout " "because it will have issues closing") env = bootstrap(args.config_uri) create_epub(args.ident_hash, args.file) 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 main(argv=None): parser = create_parser('inject_resource', description=__doc__) parser.add_argument('--media-type', help="explicitly set the media-type") parser.add_argument('--resource-filename', help="filename of the resource, overrides the input") parser.add_argument('ident_hash', help="ident-hash of the content " "to associate the resource with") parser.add_argument('resource', help="filename to the resource") args = parser.parse_args(argv) env = bootstrap(args.config_uri) media_type = args.media_type ident_hash = args.ident_hash resource = args.resource filename = args.resource_filename # Discover the filename from the given resource. if filename is None: filename = os.path.basename(resource) # Guess the media-type from the given resource. if media_type is None: media_type = guess_media_type(resource) # Print some feedback incase of strange inputs print("Using:", file=sys.stderr) print(" filename: {}".format(filename), file=sys.stderr) print(" media_type: {}".format(media_type), file=sys.stderr) with open(resource, 'r') as file: rhash = inject_resource(ident_hash, file, filename, media_type) # Print the path to the resource. uri = env['request'].route_path('resource', hash=rhash) print(uri, file=sys.stdout) 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)