def main(): check_min_supported_python_version() configure_console_logger() # Default logs: to console # Handle args: args = sys.argv[1:] ArgsReader.read_args(args) # Init Configuration.initialize_app(main_script_dir, main_script_path) # Check if need to log to file as well if Configuration.log_file and len(logging.getLogger( ).handlers) < 2: # Only two handlers are supported - Console & Log File add_log_file_logging() # Main dish if Configuration.export_packages: PackagesExporter.export_packages() if Configuration.import_packages: PackagesImporter.import_packages() # Print nice summary log_summary() # Finish successfully MyGlobals.terminate_program(0)
def setup_new_crontab_job(): if not MyCrontab.setup_script_as_crontab_job(): MyGlobals.terminate_program(2) MyGlobals.terminate_program( 0, 'Success setting-up script to run as a crontab job\nIt will run automatically by the specified timing' ) return
def index_packages_dir(dir_path): log_info("Indexing offline packages dir: {}".format(dir_path)) try: exit_code = commands.dir2pi(["dir2pi", dir_path, "--no-symlink"]) if exit_code != 0: log_error("Error while indexing dir: {}\nExit with: {}".format( dir_path, exit_code)) MyGlobals.terminate_program(1) except BaseException as error_msg: log_error("Error while indexing dir: {}\n{}".format( dir_path, error_msg)) MyGlobals.terminate_program(1)
def add_log_file_logging(): try: log_debug("Logging to file: {}".format(Configuration.log_file)) format_str = Configuration.log_fmt_str formatter = logging.Formatter(format_str, datefmt=Configuration.log_file_time_fmt) f_handler = logging.FileHandler(Configuration.log_file, mode='w') f_handler.setLevel(logging.INFO) f_handler.setFormatter(formatter) logging.getLogger().addHandler(f_handler) except BaseException as error_msg: log_error("Failed logging to file at: {}\n{}\nAborting..".format( Configuration.log_file, error_msg)) MyGlobals.terminate_program(1)
def export_packages(): export_list = Configuration.export_packages.split(',') for packages_src in export_list: collect_packages_to_export(packages_src.strip()) print_packages_to_export_dict() # Export the packages to: Configuration.export_to export_collected_packages() if not MyGlobals.is_dir(Configuration.export_to): log_error( "Finish exporting but missing exported packages dir: {}\nSomething went wrong. Check log above." .format(Configuration.export_to)) MyGlobals.terminate_program(1) MyDir2Pi.index_packages_dir(Configuration.export_to)
def collect_packages_to_export(packages_src): if not packages_src: log_warning("Skipping export of null or empty string value") return if MyGlobals.is_file(packages_src): log_debug("Reading packages from file: {}".format(packages_src)) action_dict = MyGlobals.read_file_lines_as_list(packages_src) if not action_dict["Result"]: MyGlobals.terminate_program(1) packages_list = action_dict["MoreInfo"] add_packages_list_to_packages_to_export_dict(packages_list) elif packages_src == "*": add_all_pip_freeze_packages_to_packages_to_export_dict() else: add_package_to_packages_to_export_dict( packages_src ) # Normal package request: 'pkg_name' or 'pkg_name==pkg_version'
def add_all_pip_freeze_packages_to_packages_to_export_dict(): pip = Configuration.pip_executable opts = "freeze" cmnd = "{} {}".format(pip, opts) action_dict = MyGlobals.execute_command(cmnd) if not action_dict["Result"]: log_error( "Failed to retrieve all installed pip packages using 'pip freeze' command" ) MyGlobals.terminate_program(1) pip_freeze_output = action_dict["MoreInfo"].decode( Configuration.decode_commands_output_fmt).split("\n") for pkg_str in pip_freeze_output: if pkg_str and "==" not in pkg_str: log_warning( "pip freeze command invalid output: {}. Not in format of: pkg_name==pkg_version. Skipping this line" .format(pkg_str)) continue add_package_to_packages_to_export_dict(pkg_str)
def main(): received_args = MyGlobals.read_command_line_args(sys.argv[1:]) print('FTP Puller - Started') print('Command Line: {} {}\n'.format(sys.argv[0], received_args)) if not MyGlobals.check_params(): # Check that params are ok MyGlobals.terminate_program(1) if MyGlobals.isPrintCronJobs: print_crontab_jobs_with_comment() if MyGlobals.isRemoveCronJobs: # If wants to remove old/running crontab jobs remove_old_crontab_jobs() if MyGlobals.isSetupAsCronjob and MyGlobals.receivedDownloadArgs: # Check if wants to setup this script as a crontab job setup_new_crontab_job() elif MyGlobals.receivedDownloadArgs: # Check if wants to run this script once pull_files_dirs_from_ftp() # Start downloading MyGlobals.terminate_program(0, 'FTP Puller - Finished')
def read_args(args_list): log_info("Reading received args: {}".format(args_list)) global received_args if len(args_list) == 0: parser.print_help() MyGlobals.terminate_program(0) try: received_args = parser.parse_args() except SystemExit as errorCode: MyGlobals.terminate_program(errorCode) except BaseException as errorMsg: log_error("Error - Failed to parse received args: {}\n{}".format(args_list, errorMsg)) MyGlobals.terminate_program(1) Configuration.pip_executable = received_args.pip_executable Configuration.extra_pip_args = received_args.extra_pip_args Configuration.is_verbose = received_args.verbose Configuration.export_packages = received_args.export_packages if received_args.export_to: Configuration.export_to = received_args.export_to Configuration.exported_files_json_file = path.join(received_args.export_to, "simple", "exported_files.json") Configuration.import_packages = received_args.import_packages Configuration.import_from = received_args.import_from Configuration.log_file = received_args.log_file log_debug("Finished reading args successfully")
def pull_files_dirs_from_ftp(): # Get connection to FTP server ftp_con = MyFtpLib.get_ftp_connection(MyGlobals.ftpAddr, MyGlobals.ftpPort, MyGlobals.ftpActionsTimeoutSec) if ftp_con is None: MyGlobals.terminate_program( 2, msg="Failed getting ftp connection to: {}:{}".format( MyGlobals.ftpAddr, MyGlobals.ftpPort)) ftp_password = MyGlobals.ftpPassword # If password is hashed - attempt to decipher it if MyGlobals.isHashed: ftp_password = MyPasswordDecipher.decipher_password_hash(ftp_password) if ftp_password is None: MyGlobals.terminate_program(2) # Attempt to login if not MyFtpLib.login_to_ftp_server(ftp_con, MyGlobals.ftpUser, ftp_password): MyGlobals.terminate_program(2) # Download src path: download_result = MyFtpLib.download_path(ftp_con, MyGlobals.ftpSourcePath, MyGlobals.destPath) # Check if successful if not download_result: MyGlobals.terminate_program( 2, 'FAILED - Downloading: {} to: {}'.format(MyGlobals.ftpSourcePath, MyGlobals.destPath)) print('SUCCESS - Downloading: {} to: {}'.format(MyGlobals.ftpSourcePath, MyGlobals.destPath)) return
def print_crontab_jobs_with_comment(): print_result = MyCrontab.print_current_ftp_puller_crontab_jobs() if print_result is False: MyGlobals.terminate_program(2) return
def remove_old_crontab_jobs(): removed_result = MyCrontab.remove_current_ftp_puller_crontab_jobs() if removed_result is False: MyGlobals.terminate_program(2) return
def check_min_supported_python_version(): if sys.version_info[0] < 3: print("Python 3 or a more recent version is required.") MyGlobals.terminate_program(1)